sortabletable.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'pear/HTML/Table.php';
  4. require_once 'pear/Pager/Pager.php';
  5. require_once 'tablesort.lib.php';
  6. /**
  7. * This class allows you to display a sortable data-table. It is possible to
  8. * split the data in several pages.
  9. * Using this class you can:
  10. * - automatically create checkboxes of the first table column
  11. * - a "select all" and "deselect all" link is added
  12. * - only if you provide a list of actions for the selected items
  13. * - click on the table header to sort the data
  14. * - choose how many items you see per page
  15. * - navigate through all data-pages
  16. */
  17. class SortableTable extends HTML_Table {
  18. /**
  19. * A name for this table
  20. */
  21. public $table_name;
  22. /**
  23. * The page to display
  24. */
  25. public $page_nr;
  26. /**
  27. * The column to sort the data
  28. */
  29. public $column;
  30. /**
  31. * The sorting direction (ASC or DESC)
  32. */
  33. public $direction;
  34. /**
  35. * Number of items to display per page
  36. */
  37. public $per_page;
  38. /**
  39. * The default number of items to display per page
  40. */
  41. public $default_items_per_page;
  42. /**
  43. * A prefix for the URL-parameters, can be used on pages with multiple
  44. * SortableTables
  45. */
  46. public $param_prefix;
  47. /**
  48. * The pager object to split the data in several pages
  49. */
  50. public $pager;
  51. /**
  52. * The total number of items in the table
  53. */
  54. public $total_number_of_items;
  55. /**
  56. * The function to get the total number of items
  57. */
  58. public $get_total_number_function;
  59. /**
  60. * The function to the the data to display
  61. */
  62. public $get_data_function;
  63. /**
  64. * An array with defined column-filters
  65. */
  66. public $column_filters;
  67. /**
  68. * A list of actions which will be available through a select list
  69. */
  70. public $form_actions;
  71. /**
  72. * Additional parameters to pass in the URL
  73. */
  74. public $additional_parameters;
  75. /**
  76. * Additional attributes for the th-tags
  77. */
  78. public $th_attributes;
  79. /**
  80. * Additional attributes for the td-tags
  81. */
  82. public $td_attributes;
  83. /**
  84. * Array with names of the other tables defined on the same page of this
  85. * table
  86. */
  87. public $other_tables;
  88. /**
  89. * Create a new SortableTable
  90. * @param string $table_name A name for the table (default = 'table')
  91. * @param string $get_total_number_function A user defined function to get
  92. * the total number of items in the table
  93. * @param string $get_data_function A function to get the data to display on
  94. * the current page
  95. * @param int $default_column The default column on which the data should be
  96. * sorted
  97. * @param int $default_items_per_page The default number of items to show
  98. * on one page
  99. * @param string $default_order_direction The default order direction;
  100. * either the constant 'ASC' or 'DESC'
  101. */
  102. public function __construct($table_name = 'table', $get_total_number_function = null, $get_data_function = null, $default_column = 1, $default_items_per_page = 20, $default_order_direction = 'ASC') {
  103. parent :: __construct (array ('class' => 'data_table'));
  104. $this->table_name = $table_name;
  105. $this->additional_parameters = array ();
  106. $this->param_prefix = $table_name.'_';
  107. $this->page_nr = isset ($_SESSION[$this->param_prefix.'page_nr']) ? intval($_SESSION[$this->param_prefix.'page_nr']) : 1;
  108. $this->page_nr = isset ($_GET[$this->param_prefix.'page_nr']) ? intval($_GET[$this->param_prefix.'page_nr']) : $this->page_nr;
  109. $this->column = isset ($_SESSION[$this->param_prefix.'column']) ? intval($_SESSION[$this->param_prefix.'column']) : $default_column;
  110. $this->column = isset ($_GET[$this->param_prefix.'column']) ? intval($_GET[$this->param_prefix.'column']) : $this->column;
  111. //$this->direction = isset ($_SESSION[$this->param_prefix.'direction']) ? $_SESSION[$this->param_prefix.'direction'] : $default_order_direction;
  112. if (isset($_SESSION[$this->param_prefix.'direction'])) {
  113. $my_session_direction = $_SESSION[$this->param_prefix.'direction'];
  114. if (!in_array($my_session_direction, array('ASC', 'DESC'))) {
  115. $this->direction = 'ASC';
  116. } else {
  117. if ($my_session_direction == 'ASC') {
  118. $this->direction = 'ASC';
  119. } elseif ($my_session_direction == 'DESC') {
  120. $this->direction = 'DESC';
  121. }
  122. }
  123. }
  124. if (isset($_GET[$this->param_prefix.'direction'])) {
  125. $my_get_direction = $_GET[$this->param_prefix.'direction'];
  126. if (!in_array($my_get_direction, array('ASC', 'DESC'))){
  127. $this->direction = 'ASC';
  128. } else {
  129. if ($my_get_direction == 'ASC') {
  130. $this->direction = 'ASC';
  131. } elseif ($my_get_direction == 'DESC') {
  132. $this->direction = 'DESC';
  133. }
  134. }
  135. }
  136. // Allow to change paginate in multiples tabs
  137. unset($_SESSION[$this->param_prefix.'per_page']);
  138. $this->per_page = isset ($_SESSION[$this->param_prefix.'per_page']) ? intval($_SESSION[$this->param_prefix.'per_page']) : $default_items_per_page;
  139. $this->per_page = isset ($_GET[$this->param_prefix.'per_page']) ? intval($_GET[$this->param_prefix.'per_page']) : $this->per_page;
  140. $_SESSION[$this->param_prefix.'per_page'] = $this->per_page;
  141. $_SESSION[$this->param_prefix.'direction'] = $this->direction ;
  142. $_SESSION[$this->param_prefix.'page_nr'] = $this->page_nr;
  143. $_SESSION[$this->param_prefix.'column'] = $this->column;
  144. $this->pager = null;
  145. $this->default_items_per_page = $default_items_per_page;
  146. $this->total_number_of_items = -1;
  147. $this->get_total_number_function = $get_total_number_function;
  148. $this->get_data_function = $get_data_function;
  149. $this->column_filters = array();
  150. $this->form_actions = array();
  151. $this->checkbox_name = null;
  152. $this->td_attributes = array ();
  153. $this->th_attributes = array ();
  154. $this->other_tables = array();
  155. }
  156. /**
  157. * Get the Pager object to split the showed data in several pages
  158. */
  159. public function get_pager() {
  160. if (is_null($this->pager)) {
  161. $total_number_of_items = $this->get_total_number_of_items();
  162. $params['mode'] = 'Sliding';
  163. $params['perPage'] = $this->per_page;
  164. $params['totalItems'] = $total_number_of_items;
  165. $params['urlVar'] = $this->param_prefix.'page_nr';
  166. $params['currentPage'] = $this->page_nr;
  167. $icon_attributes = array('style' => 'vertical-align: middle;');
  168. $params['prevImg'] = Display :: return_icon('action_prev.png', get_lang('PreviousPage'), $icon_attributes);
  169. $params['nextImg'] = Display :: return_icon('action_next.png', get_lang('NextPage'), $icon_attributes);
  170. $params['firstPageText'] = Display :: return_icon('action_first.png', get_lang('FirstPage'), $icon_attributes);
  171. $params['lastPageText'] = Display :: return_icon('action_last.png', get_lang('LastPage'), $icon_attributes);
  172. $params['firstPagePre'] = '';
  173. $params['lastPagePre'] = '';
  174. $params['firstPagePost'] = '';
  175. $params['lastPagePost'] = '';
  176. $params['spacesBeforeSeparator'] = '';
  177. $params['spacesAfterSeparator'] = '';
  178. $query_vars = array_keys($_GET);
  179. $query_vars_needed = array ($this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page');
  180. if (count($this->additional_parameters) > 0) {
  181. $query_vars_needed = array_merge($query_vars_needed, array_keys($this->additional_parameters));
  182. }
  183. $query_vars_exclude = array_diff($query_vars, $query_vars_needed);
  184. $params['excludeVars'] = $query_vars_exclude;
  185. $this->pager = & Pager::factory($params);
  186. }
  187. return $this->pager;
  188. }
  189. /**
  190. * Displays the table, complete with navigation buttons to browse through
  191. * the data-pages.
  192. */
  193. public function display() {
  194. $empty_table = false;
  195. $content = $this->get_table_html();
  196. if ($this->get_total_number_of_items() == 0) {
  197. $cols = $this->getColCount();
  198. $this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  199. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  200. $this->setCellContents(1, 0, $message_empty);
  201. $empty_table = true;
  202. }
  203. $html = '';
  204. if (!$empty_table) {
  205. $form = $this->get_page_select_form();
  206. $nav = $this->get_navigation_html();
  207. //Only show pagination info when there are items to paginate
  208. if ($this->get_total_number_of_items() > $this->default_items_per_page) {
  209. $html = '<table class="data_table_pagination">';
  210. $html .= '<tr>';
  211. $html .= '<td style="width:25%;">';
  212. $html .= $form;
  213. $html .= '</td>';
  214. $html .= '<td style="text-align:center;">';
  215. $html .= $this->get_table_title();
  216. $html .= '</td>';
  217. $html .= '<td style="text-align:right;width:25%;">';
  218. $html .= $nav;
  219. $html .= '</td>';
  220. $html .= '</tr>';
  221. $html .= '</table>';
  222. }
  223. if (count($this->form_actions) > 0) {
  224. $html .= '<script language="JavaScript" type="text/javascript">
  225. /*<![CDATA[*/
  226. function setCheckbox(value) {
  227. d = document.form_'.$this->table_name.';
  228. for (i = 0; i < d.elements.length; i++) {
  229. if (d.elements[i].type == "checkbox") {
  230. d.elements[i].checked = value;
  231. }
  232. }
  233. }
  234. /*]]>*/
  235. </script>';
  236. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  237. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  238. }
  239. }
  240. $html .= $content;
  241. if (!$empty_table) {
  242. $html .= '<table style="width:100%;">';
  243. $html .= '<tr>';
  244. $html .= '<td colspan="2">';
  245. if (count($this->form_actions) > 0) {
  246. $html .= '<br />';
  247. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript: setCheckbox(true); return false;">'.get_lang('SelectAll').'</a> - ';
  248. $html .= '<a href="?'.$params.'" onclick="javascript: setCheckbox(false); return false;">'.get_lang('UnSelectAll').'</a> ';
  249. $html .= '<select name="action">';
  250. foreach ($this->form_actions as $action => & $label) {
  251. $html .= '<option value="'.$action.'">'.$label.'</option>';
  252. }
  253. $html .= '</select>';
  254. $html .= '&nbsp;&nbsp;<button type="submit" class="save" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."'".')) return false;">'.get_lang('Select').'</button>';
  255. } else {
  256. $html .= $form;
  257. }
  258. $html .= '</td>';
  259. if ($this->get_total_number_of_items() > $this->default_items_per_page) {
  260. $html .= '<td style="text-align:right;">';
  261. $html .= $nav;
  262. $html .= '</td>';
  263. }
  264. $html .= '</tr>';
  265. $html .= '</table>';
  266. if (count($this->form_actions) > 0) {
  267. $html .= '</form>';
  268. }
  269. }
  270. echo $html;
  271. }
  272. /**
  273. * This function shows the content of a table in a grid.
  274. * Should not be use to edit information (edit/delete rows) only.
  275. * */
  276. public function display_grid() {
  277. $empty_table = false;
  278. if ($this->get_total_number_of_items() == 0) {
  279. $cols = $this->getColCount();
  280. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  281. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  282. $this->setCellContents(1, 0, $message_empty);
  283. $empty_table = true;
  284. }
  285. $html = '';
  286. if (!$empty_table) {
  287. $form = $this->get_page_select_form();
  288. $nav = $this->get_navigation_html();
  289. // @todo This style css must be moved to default.css only for dev
  290. echo '<style>
  291. .main-grid { width:100%;}
  292. .sub-header { width:100%; padding-top: 10px; padding-right: 10px; padding-left: 10px; height:30px;}
  293. .grid_container { width:100%;}
  294. .grid_item { height: 120px; width:98px; float:left; padding:5px; margin:8px;}
  295. .grid_element_0 { width:100px; height: 100px; float:left; text-align:center; margin-bottom:5px;}
  296. .grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
  297. .grid_element_2 { width:150px; float:left;}
  298. .grid_selectbox { width:30%; float:left;}
  299. .grid_title { width:30%; float:left;}
  300. .grid_nav { }
  301. </style>';
  302. // @todo This also must be moved
  303. // Show only navigations if there are more than 1 page
  304. $my_pager = $this->get_pager();
  305. $html .= '<div class="main-grid">';
  306. if ($my_pager->numPages() > 1) {
  307. $html .= '<div class="sub-header">';
  308. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  309. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  310. $html .= '<div class="grid_nav">'.$nav.'</div>';
  311. $html .= '</div>';
  312. }
  313. $html .= '<div class="clear"></div>';
  314. if (count($this->form_actions) > 0) {
  315. $script= '<script language="JavaScript" type="text/javascript">
  316. /*<![CDATA[*/
  317. function setCheckbox(value) {
  318. d = document.form_'.$this->table_name.';
  319. for (i = 0; i < d.elements.length; i++) {
  320. if (d.elements[i].type == "checkbox") {
  321. d.elements[i].checked = value;
  322. }
  323. }
  324. }
  325. /*]]>*/
  326. </script>';
  327. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  328. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  329. }
  330. }
  331. // Getting the items of the table
  332. $items = $this->get_clean_html(false); //no sort
  333. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  334. // i.e: .whoisonline_table_grid_container instead of .grid_container
  335. // where whoisonline is the table's name like drupal's template engine
  336. $html .= '<div class="grid_container">';
  337. if (is_array($items) && count($items) > 0) {
  338. foreach ($items as & $row) {
  339. $html .= '<div class="grid_item">';
  340. $i = 0;
  341. foreach ($row as & $element) {
  342. $html .= '<div class="grid_element_'.$i.'">'.$element.'</div>';
  343. $i++;
  344. }
  345. $html .= '</div>';
  346. }
  347. }
  348. $html .= '</div>'; //close grid_container
  349. $html .= '</div>'; //close main grid
  350. $html .= '<div class="clear"></div>';
  351. /*
  352. if (!$empty_table) {
  353. $html .= '<table style="width:100%;">';
  354. $html .= '<tr>';
  355. $html .= '<td colspan="2">';
  356. if (count($this->form_actions) > 0) {
  357. $html .= '<br />';
  358. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
  359. $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
  360. $html .= '<select name="action">';
  361. foreach ($this->form_actions as $action => $label) {
  362. $html .= '<option value="'.$action.'">'.$label.'</option>';
  363. }
  364. $html .= '</select>';
  365. $html .= '&nbsp;&nbsp;<button type="submit" class="save" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."'".')) return false;">'.get_lang('Select').'</button>';
  366. } else {
  367. $html .= $form;
  368. }
  369. $html .= '</td>';
  370. $html .= '<td style="text-align:right;">';
  371. $html .= $nav;
  372. $html .= '</td>';
  373. $html .= '</tr>';
  374. $html .= '</div>';
  375. if (count($this->form_actions) > 0) {
  376. $html .= '</form>';
  377. }
  378. }
  379. */
  380. echo $html;
  381. }
  382. /**
  383. * This function returns the content of a table in a grid
  384. * Should not be use to edit information (edit/delete rows) only.
  385. * @param array options of visibility
  386. * @param bool hide navigation optionally
  387. * @param int content per page when show navigation (optional)
  388. * @param bool sort data optionally
  389. * @return string grid html
  390. */
  391. public function display_simple_grid($visibility_options, $hide_navigation = true, $per_page = 20, $sort_data = true) {
  392. $empty_table = false;
  393. if ($this->get_total_number_of_items() == 0) {
  394. $cols = $this->getColCount();
  395. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  396. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  397. $this->setCellContents(1, 0, $message_empty);
  398. $empty_table = true;
  399. }
  400. $html = '';
  401. if (!$empty_table) {
  402. // If we show the pagination
  403. if (!$hide_navigation) {
  404. $form = '&nbsp;';
  405. if ($this->get_total_number_of_items() > $per_page) {
  406. if ($per_page > 10) {
  407. $form = $this->get_page_select_form();
  408. }
  409. $nav = $this->get_navigation_html();
  410. // This also must be moved
  411. $html = '<div class="sub-header" >';
  412. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  413. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  414. $html .= '<div class="grid_nav">'.$nav.'</div>';
  415. $html .= '</div>';
  416. }
  417. }
  418. $html .= '<div class="clear"></div>';
  419. if (count($this->form_actions) > 0) {
  420. $script= '<script language="javaScript" type="text/javascript">
  421. /*<![CDATA[*/
  422. function setCheckbox(value) {
  423. d = document.form_'.$this->table_name.';
  424. for (i = 0; i < d.elements.length; i++) {
  425. if (d.elements[i].type == "checkbox") {
  426. d.elements[i].checked = value;
  427. }
  428. }
  429. }
  430. /*]]>*/
  431. </script>';
  432. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  433. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  434. }
  435. }
  436. if ($hide_navigation) {
  437. $items = $this->table_data; // This is a faster way to get what we want
  438. } else {
  439. // The normal way
  440. $items = $this->get_clean_html($sort_data); // Getting the items of the table
  441. }
  442. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  443. // i.e: .whoisonline_table_grid_container instead of .grid_container
  444. // where whoisonline is the table's name like drupal's template engine
  445. if (is_array($visibility_options)) {
  446. $filter = false; // The 2nd condition of the if will be loaded
  447. } else {
  448. $filter = $visibility_options !== false;
  449. }
  450. $html .= '<div class="'.$this->table_name.'_grid_container">';
  451. if (is_array($items) && count($items) > 0) {
  452. foreach ($items as & $row) {
  453. $html .= '<div class="'.$this->table_name.'_grid_item">';
  454. $i = 0;
  455. foreach ($row as & $element) {
  456. if ($filter || $visibility_options[$i]) {
  457. $html.='<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
  458. }
  459. $i++;
  460. }
  461. $html .= '</div>';
  462. }
  463. }
  464. $html .= '</div>';
  465. $html .= '<div class="clear"></div>';
  466. return $html;
  467. }
  468. /**
  469. * Get the HTML-code with the navigational buttons to browse through the
  470. * data-pages.
  471. */
  472. public function get_navigation_html () {
  473. $pager = $this->get_pager();
  474. $pager_links = $pager->getLinks();
  475. $showed_items = $pager->getOffsetByPageId();
  476. $nav = $pager_links['first'].' '.$pager_links['back'];
  477. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  478. $nav .= $pager_links['next'].' '.$pager_links['last'];
  479. return $nav;
  480. }
  481. /**
  482. * Get the HTML-code with the data-table.
  483. */
  484. public function get_table_html () {
  485. $pager = $this->get_pager();
  486. $val = $pager->getOffsetByPageId();
  487. $offset = $pager->getOffsetByPageId();
  488. $from = $offset[0] - 1;
  489. $table_data = $this->get_table_data($from);
  490. if (is_array($table_data)) {
  491. foreach ($table_data as $index => & $row) {
  492. $row = $this->filter_data($row);
  493. $this->addRow($row);
  494. }
  495. }
  496. $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
  497. foreach ($this->th_attributes as $column => $attributes) {
  498. $this->setCellAttributes(0, $column, $attributes);
  499. }
  500. foreach ($this->td_attributes as $column => $attributes) {
  501. $this->setColAttributes($column, $attributes);
  502. }
  503. return $this->toHTML();
  504. }
  505. /**
  506. * This function return the items of the table
  507. * @param bool true for sorting table data or false otherwise
  508. * @return array table row items
  509. */
  510. public function get_clean_html($sort = true) {
  511. $pager = $this->get_pager();
  512. $val = $pager->getOffsetByPageId();
  513. $offset = $pager->getOffsetByPageId();
  514. $from = $offset[0] - 1;
  515. $table_data = $this->get_table_data($from, $sort);
  516. $new_table_data = array();
  517. if (is_array($table_data)) {
  518. foreach ($table_data as $index => & $row) {
  519. $row = $this->filter_data($row);
  520. $new_table_data[] = $row;
  521. }
  522. }
  523. return $new_table_data;
  524. }
  525. /**
  526. * Get the HTML-code wich represents a form to select how many items a page
  527. * should contain.
  528. */
  529. public function get_page_select_form () {
  530. $total_number_of_items = $this->get_total_number_of_items();
  531. if ($total_number_of_items <= $this->default_items_per_page) {
  532. return '';
  533. }
  534. $result[] = '<form method="get" action="'.api_get_self().'" style="display:inline;">';
  535. $param[$this->param_prefix.'direction'] = $this->direction;
  536. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  537. $param[$this->param_prefix.'column'] = $this->column;
  538. if (is_array($this->additional_parameters)) {
  539. $param = array_merge($param, $this->additional_parameters);
  540. }
  541. foreach ($param as $key => & $value) {
  542. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  543. }
  544. $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript: this.form.submit();">';
  545. for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) {
  546. $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  547. }
  548. // @todo no limits
  549. //if ($total_number_of_items < 500) {
  550. $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
  551. //}
  552. $result[] = '</select>';
  553. $result[] = '<noscript>';
  554. $result[] = '<button class="save" type="submit">'.get_lang('Save').'</button>';
  555. $result[] = '</noscript>';
  556. $result[] = '</form>';
  557. $result = implode("\n", $result);
  558. return $result;
  559. }
  560. /**
  561. * Get the table title.
  562. */
  563. public function get_table_title () {
  564. $pager = $this->get_pager();
  565. $showed_items = $pager->getOffsetByPageId();
  566. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  567. }
  568. /**
  569. * Set the header-label
  570. * @param int $column The column number
  571. * @param string $label The label
  572. * @param boolean $sortable Is the table sortable by this column? (defatult
  573. * = true)
  574. * @param string $th_attributes Additional attributes for the th-tag of the
  575. * table header
  576. * @param string $td_attributes Additional attributes for the td-tags of the
  577. * column
  578. */
  579. public function set_header ($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null) {
  580. $param['direction'] = 'ASC';
  581. if ($this->column == $column && $this->direction == 'ASC') {
  582. $param['direction'] = 'DESC';
  583. }
  584. $param['page_nr'] = $this->page_nr;
  585. $param['per_page'] = $this->per_page;
  586. $param['column'] = $column;
  587. if ($sortable) {
  588. $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
  589. foreach ($param as $key => & $value) {
  590. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  591. }
  592. $link .= $this->get_additional_url_paramstring();
  593. $link .= '">'.$label.'</a>';
  594. if ($this->column == $column) {
  595. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  596. }
  597. } else {
  598. $link = $label;
  599. }
  600. $this->setHeaderContents(0, $column, $link);
  601. if (!is_null($td_attributes)) {
  602. $this->td_attributes[$column] = $td_attributes;
  603. }
  604. if (!is_null($th_attributes)) {
  605. $this->th_attributes[$column] = $th_attributes;
  606. }
  607. }
  608. /**
  609. * Get the parameter-string with additional parameters to use in the URLs
  610. * generated by this SortableTable
  611. */
  612. public function get_additional_url_paramstring () {
  613. $param_string_parts = array ();
  614. if (is_array($this->additional_parameters) && count($this->additional_parameters) > 0) {
  615. foreach ($this->additional_parameters as $key => & $value) {
  616. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  617. }
  618. }
  619. $result = implode('&amp;', $param_string_parts);
  620. foreach ($this->other_tables as $index => & $tablename) {
  621. $param = array();
  622. if (isset($_GET[$tablename.'_direction'])) {
  623. //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  624. $my_get_direction = $_GET[$tablename.'_direction'];
  625. if (!in_array($my_get_direction, array('ASC', 'DESC'))) {
  626. $param[$tablename.'_direction'] = 'ASC';
  627. } else {
  628. $param[$tablename.'_direction'] = $my_get_direction;
  629. }
  630. }
  631. if (isset($_GET[$tablename.'_page_nr'])) {
  632. $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
  633. }
  634. if (isset($_GET[$tablename.'_per_page'])) {
  635. $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
  636. }
  637. if (isset($_GET[$tablename.'_column'])) {
  638. $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
  639. }
  640. $param_string_parts = array ();
  641. foreach ($param as $key => & $value) {
  642. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  643. }
  644. if (count($param_string_parts) > 0)
  645. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  646. }
  647. return $result;
  648. }
  649. /**
  650. * Get the parameter-string with the SortableTable-related parameters to use
  651. * in URLs
  652. */
  653. public function get_sortable_table_param_string () {
  654. $param[$this->param_prefix.'direction'] = $this->direction;
  655. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  656. $param[$this->param_prefix.'per_page'] = $this->per_page;
  657. $param[$this->param_prefix.'column'] = $this->column;
  658. $param_string_parts = array ();
  659. foreach ($param as $key => & $value) {
  660. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  661. }
  662. $res = implode('&amp;', $param_string_parts);
  663. return $res;
  664. }
  665. /**
  666. * Add a filter to a column. If another filter was allready defined for the
  667. * given column, it will be overwritten.
  668. * @param int $column The number of the column
  669. * @param string $function The name of the filter-function. This should be a
  670. * function wich requires 1 parameter and returns the filtered value.
  671. */
  672. public function set_column_filter ($column, $function) {
  673. $this->column_filters[$column] = $function;
  674. }
  675. /**
  676. * Define a list of actions which can be performed on the table-date.
  677. * If you define a list of actions, the first column of the table will be
  678. * converted into checkboxes.
  679. * @param array $actions A list of actions. The key is the name of the
  680. * action. The value is the label to show in the select-box
  681. * @param string $checkbox_name The name of the generated checkboxes. The
  682. * value of the checkbox will be the value of the first column.
  683. */
  684. public function set_form_actions ($actions, $checkbox_name = 'id') {
  685. $this->form_actions = $actions;
  686. $this->checkbox_name = $checkbox_name;
  687. }
  688. /**
  689. * Define a list of additional parameters to use in the generated URLs
  690. * @example $parameters['action'] = 'test'; will be convert in <input type="hidden" name="action" value="test">
  691. * @param array $parameters
  692. */
  693. public function set_additional_parameters ($parameters) {
  694. $this->additional_parameters = $parameters;
  695. }
  696. /**
  697. * Set other tables on the same page.
  698. * If you have other sortable tables on the page displaying this sortable
  699. * tables, you can define those other tables with this function. If you
  700. * don't define the other tables, there sorting and pagination will return
  701. * to their default state when sorting this table.
  702. * @param array $tablenames An array of table names.
  703. */
  704. public function set_other_tables ($tablenames) {
  705. $this->other_tables = $tablenames;
  706. }
  707. /**
  708. * Transform all data in a table-row, using the filters defined by the
  709. * function set_column_filter(...) defined elsewhere in this class.
  710. * If you've defined actions, the first element of the given row will be
  711. * converted into a checkbox
  712. * @param array $row A row from the table.
  713. */
  714. public function filter_data ($row) {
  715. $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  716. foreach ($this->column_filters as $column => & $function) {
  717. $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
  718. }
  719. if (count($this->form_actions) > 0) {
  720. if (strlen($row[0]) > 0) {
  721. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  722. if (isset($_GET[$this->param_prefix.'selectall'])) {
  723. $row[0] .= ' checked="checked"';
  724. }
  725. $row[0] .= '/>';
  726. }
  727. }
  728. foreach ($row as $index => & $value) {
  729. if (empty($value)) {
  730. $value = '-';
  731. }
  732. }
  733. return $row;
  734. }
  735. /**
  736. * Get the total number of items. This function calls the function given as
  737. * 2nd argument in the constructor of a SortableTable. Make sure your
  738. * function has the same parameters as defined here.
  739. */
  740. public function get_total_number_of_items () {
  741. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
  742. $this->total_number_of_items = call_user_func($this->get_total_number_function);
  743. }
  744. return $this->total_number_of_items;
  745. }
  746. /**
  747. * Get the data to display. This function calls the function given as
  748. * 2nd argument in the constructor of a SortableTable. Make sure your
  749. * function has the same parameters as defined here.
  750. * @param int $from Index of the first item to return.
  751. * @param int $per_page The number of items to return
  752. * @param int $column The number of the column on which the data should be
  753. * sorted
  754. * @param string $direction In which order should the data be sorted (ASC
  755. * or DESC)
  756. */
  757. public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null) {
  758. if (!is_null($this->get_data_function)) {
  759. return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
  760. }
  761. return array ();
  762. }
  763. }
  764. /**
  765. * Sortable table which can be used for data available in an array
  766. */
  767. class SortableTableFromArray extends SortableTable {
  768. /**
  769. * The array containing all data for this table
  770. */
  771. public $table_data;
  772. /**
  773. * Constructor
  774. * @param array $table_data
  775. * @param int $default_column
  776. * @param int $default_items_per_page
  777. */
  778. public function __construct($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename') {
  779. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
  780. $this->table_data = $table_data;
  781. }
  782. /**
  783. * Get table data to show on current page
  784. * @see SortableTable#get_table_data
  785. */
  786. public function get_table_data($from = 1, $sort = true) {
  787. if ($sort) {
  788. $content = TableSort :: sort_table($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC);
  789. } else {
  790. $content = $this->table_data;
  791. }
  792. return array_slice($content, $from, $this->per_page);
  793. }
  794. /**
  795. * Get total number of items
  796. * @see SortableTable#get_total_number_of_items
  797. */
  798. public function get_total_number_of_items() {
  799. return count($this->table_data);
  800. }
  801. }
  802. /**
  803. * Sortable table which can be used for data available in an array
  804. *
  805. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  806. * $column_show is an array that lets us decide which are going to be the columns to show
  807. * $column_order is an array that lets us decide the ordering of the columns
  808. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  809. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  810. */
  811. class SortableTableFromArrayConfig extends SortableTable {
  812. /**
  813. * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
  814. */
  815. private $column_show;
  816. /**
  817. *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  818. */
  819. private $column_order;
  820. /**
  821. * The array containing all data for this table
  822. */
  823. private $table_data;
  824. /**
  825. * Constructor
  826. * @param array $table_data All the information of the table
  827. * @param int $default_column Default column that will be use in the sorts functions
  828. * @param int $default_items_per_page quantity of pages that we are going to see
  829. * @param int $tablename Name of the table
  830. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  831. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  832. */
  833. public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename', $column_show = null, $column_order = null, $direction = 'ASC') {
  834. $this->column_show = $column_show;
  835. $this->column_order = $column_order;
  836. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page, $direction);
  837. $this->table_data = $table_data;
  838. }
  839. /**
  840. * Get table data to show on current page
  841. * @see SortableTable#get_table_data
  842. */
  843. public function get_table_data($from = 1) {
  844. $content = TableSort :: sort_table_config($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC, $this->column_show, $this->column_order);
  845. return array_slice($content, $from, $this->per_page);
  846. }
  847. /**
  848. * Get total number of items
  849. * @see SortableTable#get_total_number_of_items
  850. */
  851. public function get_total_number_of_items () {
  852. return count($this->table_data);
  853. }
  854. }