sortabletable.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2005-2008 Dokeos S.A.
  6. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. require_once "HTML/Table.php"; //See http://pear.php.net/package/HTML_Table
  19. require_once "Pager/Pager.php"; //See http://pear.php.net/package/Pager
  20. require_once 'tablesort.lib.php';
  21. /**
  22. * This class allows you to display a sortable data-table. It is possible to
  23. * split the data in several pages.
  24. * Using this class you can:
  25. * - automatically create checkboxes of the first table column
  26. * - a "select all" and "deselect all" link is added
  27. * - only if you provide a list of actions for the selected items
  28. * - click on the table header to sort the data
  29. * - choose how many items you see per page
  30. * - navigate through all data-pages
  31. */
  32. class SortableTable extends HTML_Table
  33. {
  34. /**
  35. * A name for this table
  36. */
  37. var $table_name;
  38. /**
  39. * The page to display
  40. */
  41. var $page_nr;
  42. /**
  43. * The column to sort the data
  44. */
  45. var $column;
  46. /**
  47. * The sorting direction (ASC or DESC)
  48. */
  49. var $direction;
  50. /**
  51. * Number of items to display per page
  52. */
  53. var $per_page;
  54. /**
  55. * The default number of items to display per page
  56. */
  57. var $default_items_per_page;
  58. /**
  59. * A prefix for the URL-parameters, can be used on pages with multiple
  60. * SortableTables
  61. */
  62. var $param_prefix;
  63. /**
  64. * The pager object to split the data in several pages
  65. */
  66. var $pager;
  67. /**
  68. * The total number of items in the table
  69. */
  70. var $total_number_of_items;
  71. /**
  72. * The function to get the total number of items
  73. */
  74. var $get_total_number_function;
  75. /**
  76. * The function to the the data to display
  77. */
  78. var $get_data_function;
  79. /**
  80. * An array with defined column-filters
  81. */
  82. var $column_filters;
  83. /**
  84. * A list of actions which will be available through a select list
  85. */
  86. var $form_actions;
  87. /**
  88. * Additional parameters to pass in the URL
  89. */
  90. var $additional_parameters;
  91. /**
  92. * Additional attributes for the th-tags
  93. */
  94. var $th_attributes;
  95. /**
  96. * Additional attributes for the td-tags
  97. */
  98. var $td_attributes;
  99. /**
  100. * Array with names of the other tables defined on the same page of this
  101. * table
  102. */
  103. var $other_tables;
  104. /**
  105. * Create a new SortableTable
  106. * @param string $table_name A name for the table (default = 'table')
  107. * @param string $get_total_number_function A user defined function to get
  108. * the total number of items in the table
  109. * @param string $get_data_function A function to get the data to display on
  110. * the current page
  111. * @param int $default_column The default column on which the data should be
  112. * sorted
  113. * @param int $default_items_per_page The default number of items to show
  114. * on one page
  115. * @param string $default_order_direction The default order direction;
  116. * either the constant 'ASC' or 'DESC'
  117. */
  118. function SortableTable($table_name = 'table', $get_total_number_function = null, $get_data_function = null, $default_column = 1, $default_items_per_page = 20, $default_order_direction = 'ASC')
  119. {
  120. parent :: HTML_Table(array ('class' => 'data_table'));
  121. $this->table_name = $table_name;
  122. $this->additional_parameters = array ();
  123. $this->param_prefix = $table_name.'_';
  124. $this->page_nr = isset ($_SESSION[$this->param_prefix.'page_nr']) ? $_SESSION[$this->param_prefix.'page_nr'] : 1;
  125. $this->page_nr = isset ($_GET[$this->param_prefix.'page_nr']) ? $_GET[$this->param_prefix.'page_nr'] : $this->page_nr;
  126. $this->column = isset ($_SESSION[$this->param_prefix.'column']) ? $_SESSION[$this->param_prefix.'column'] : $default_column;
  127. $this->column = isset ($_GET[$this->param_prefix.'column']) ? $_GET[$this->param_prefix.'column'] : $this->column;
  128. $this->direction = isset ($_SESSION[$this->param_prefix.'direction']) ? $_SESSION[$this->param_prefix.'direction'] : $default_order_direction;
  129. $this->direction = isset ($_GET[$this->param_prefix.'direction']) ? $_GET[$this->param_prefix.'direction'] : $this->direction;
  130. $this->per_page = isset ($_SESSION[$this->param_prefix.'per_page']) ? $_SESSION[$this->param_prefix.'per_page'] : $default_items_per_page;
  131. $this->per_page = isset ($_GET[$this->param_prefix.'per_page']) ? $_GET[$this->param_prefix.'per_page'] : $this->per_page;
  132. $_SESSION[$this->param_prefix.'per_page'] = $this->per_page;
  133. $_SESSION[$this->param_prefix.'direction'] = $this->direction ;
  134. $_SESSION[$this->param_prefix.'page_nr'] = $this->page_nr;
  135. $_SESSION[$this->param_prefix.'column'] = $this->column;
  136. $this->pager = null;
  137. $this->default_items_per_page = $default_items_per_page;
  138. $this->total_number_of_items = -1;
  139. $this->get_total_number_function = $get_total_number_function;
  140. $this->get_data_function = $get_data_function;
  141. $this->column_filters = array ();
  142. $this->form_actions = array ();
  143. $this->checkbox_name = null;
  144. $this->td_attributes = array ();
  145. $this->th_attributes = array ();
  146. $this->other_tables = array();
  147. }
  148. /**
  149. * Get the Pager object to split the showed data in several pages
  150. */
  151. function get_pager()
  152. {
  153. if (is_null($this->pager))
  154. {
  155. $total_number_of_items = $this->get_total_number_of_items();
  156. $params['mode'] = 'Sliding';
  157. $params['perPage'] = $this->per_page;
  158. $params['totalItems'] = $total_number_of_items;
  159. $params['urlVar'] = $this->param_prefix.'page_nr';
  160. $params['currentPage'] = $this->page_nr;
  161. $params['prevImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/prev.png" style="vertical-align: middle;"/>';
  162. $params['nextImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/next.png" style="vertical-align: middle;"/>';
  163. $params['firstPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/first.png" style="vertical-align: middle;"/>';
  164. $params['lastPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/last.png" style="vertical-align: middle;"/>';
  165. $params['firstPagePre'] = '';
  166. $params['lastPagePre'] = '';
  167. $params['firstPagePost'] = '';
  168. $params['lastPagePost'] = '';
  169. $params['spacesBeforeSeparator'] = '';
  170. $params['spacesAfterSeparator'] = '';
  171. $query_vars = array_keys($_GET);
  172. $query_vars_needed = array ($this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page');
  173. if (count($this->additional_parameters) > 0)
  174. {
  175. $query_vars_needed = array_merge($query_vars_needed, array_keys($this->additional_parameters));
  176. }
  177. $query_vars_exclude = array_diff($query_vars, $query_vars_needed);
  178. $params['excludeVars'] = $query_vars_exclude;
  179. $this->pager = & Pager :: factory($params);
  180. }
  181. return $this->pager;
  182. }
  183. /**
  184. * Displays the table, complete with navigation buttons to browse through
  185. * the data-pages.
  186. */
  187. function display()
  188. {
  189. global $charset;
  190. $empty_table = false;
  191. if ($this->get_total_number_of_items() == 0)
  192. {
  193. $cols = $this->getColCount();
  194. $this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  195. $this->setCellContents(1, 0, get_lang('TheListIsEmpty'));
  196. $empty_table = true;
  197. }
  198. if (!$empty_table)
  199. {
  200. $form = $this->get_page_select_form();
  201. $nav = $this->get_navigation_html();
  202. $html = '<table style="width:100%;">';
  203. $html .= '<tr>';
  204. $html .= '<td style="width:25%;">';
  205. $html .= $form;
  206. $html .= '</td>';
  207. $html .= '<td style="text-align:center;">';
  208. $html .= $this->get_table_title();
  209. $html .= '</td>';
  210. $html .= '<td style="text-align:right;width:25%;">';
  211. $html .= $nav;
  212. $html .= '</td>';
  213. $html .= '</tr>';
  214. $html .= '</table>';
  215. if (count($this->form_actions) > 0)
  216. {
  217. $html .= '<script language="JavaScript" type="text/javascript">
  218. /*<![CDATA[*/
  219. function setCheckbox(value) {
  220. d = document.form_'.$this->table_name.';
  221. for (i = 0; i < d.elements.length; i++) {
  222. if (d.elements[i].type == "checkbox") {
  223. d.elements[i].checked = value;
  224. }
  225. }
  226. }
  227. /*]]>*/
  228. </script>';
  229. $params = $this->get_sortable_table_param_string.'&amp;'.$this->get_additional_url_paramstring();
  230. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  231. }
  232. }
  233. $html .= $this->get_table_html();
  234. if (!$empty_table)
  235. {
  236. $html .= '<table style="width:100%;">';
  237. $html .= '<tr>';
  238. $html .= '<td colspan="2">';
  239. if (count($this->form_actions) > 0)
  240. {
  241. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
  242. $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
  243. $html .= '<select name="action">';
  244. foreach ($this->form_actions as $action => $label)
  245. {
  246. $html .= '<option value="'.$action.'">'.$label.'</option>';
  247. }
  248. $html .= '</select>';
  249. $html .= '<input type="submit" value="'.get_lang('Ok').'" onclick="javascript:if(!confirm('."'".addslashes(htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."'".')) return false;"/>';
  250. }
  251. else
  252. {
  253. $html .= $form;
  254. }
  255. $html .= '</td>';
  256. $html .= '<td style="text-align:right;">';
  257. $html .= $nav;
  258. $html .= '</td>';
  259. $html .= '</tr>';
  260. $html .= '</table>';
  261. if (count($this->form_actions) > 0)
  262. {
  263. $html .= '</form>';
  264. }
  265. }
  266. echo $html;
  267. }
  268. /**
  269. * Get the HTML-code with the navigational buttons to browse through the
  270. * data-pages.
  271. */
  272. function get_navigation_html()
  273. {
  274. $pager = $this->get_pager();
  275. $pager_links = $pager->getLinks();
  276. $showed_items = $pager->getOffsetByPageId();
  277. $nav = $pager_links['first'].' '.$pager_links['back'];
  278. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  279. $nav .= $pager_links['next'].' '.$pager_links['last'];
  280. return $nav;
  281. }
  282. /**
  283. * Get the HTML-code with the data-table.
  284. */
  285. function get_table_html()
  286. {
  287. $pager = $this->get_pager();
  288. $offset = $pager->getOffsetByPageId();
  289. $from = $offset[0] - 1;
  290. $table_data = $this->get_table_data($from);
  291. foreach ($table_data as $index => $row)
  292. {
  293. $row = $this->filter_data($row);
  294. $this->addRow($row);
  295. }
  296. $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
  297. foreach ($this->th_attributes as $column => $attributes)
  298. {
  299. $this->setCellAttributes(0, $column, $attributes);
  300. }
  301. foreach ($this->td_attributes as $column => $attributes)
  302. {
  303. $this->setColAttributes($column, $attributes);
  304. }
  305. return $this->toHTML();
  306. }
  307. /**
  308. * Get the HTML-code wich represents a form to select how many items a page
  309. * should contain.
  310. */
  311. function get_page_select_form()
  312. {
  313. $total_number_of_items = $this->get_total_number_of_items();
  314. if ($total_number_of_items <= $this->default_items_per_page)
  315. {
  316. return '';
  317. }
  318. $result[] = '<form method="get" action="'.api_get_self().'" style="display:inline;">';
  319. $param[$this->param_prefix.'direction'] = $this->direction;
  320. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  321. $param[$this->param_prefix.'column'] = $this->column;
  322. $param = array_merge($param, $this->additional_parameters);
  323. foreach ($param as $key => $value)
  324. {
  325. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  326. }
  327. $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript:this.form.submit();">';
  328. for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10)
  329. {
  330. $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  331. }
  332. if ($total_number_of_items < 500)
  333. {
  334. $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.ucfirst(get_lang('All')).'</option>';
  335. }
  336. $result[] = '</select>';
  337. $result[] = '<noscript>';
  338. $result[] = '<input type="submit" value="'.get_lang('Ok').'"/>';
  339. $result[] = '</noscript>';
  340. $result[] = '</form>';
  341. $result = implode("\n", $result);
  342. return $result;
  343. }
  344. /**
  345. * Get the table title.
  346. */
  347. function get_table_title()
  348. {
  349. $pager = $this->get_pager();
  350. $showed_items = $pager->getOffsetByPageId();
  351. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  352. }
  353. /**
  354. * Set the header-label
  355. * @param int $column The column number
  356. * @param string $label The label
  357. * @param boolean $sortable Is the table sortable by this column? (defatult
  358. * = true)
  359. * @param string $th_attributes Additional attributes for the th-tag of the
  360. * table header
  361. * @param string $td_attributes Additional attributes for the td-tags of the
  362. * column
  363. */
  364. function set_header($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null)
  365. {
  366. $param['direction'] = 'ASC';
  367. if ($this->column == $column && $this->direction == 'ASC')
  368. {
  369. $param['direction'] = 'DESC';
  370. }
  371. $param['page_nr'] = $this->page_nr;
  372. $param['per_page'] = $this->per_page;
  373. $param['column'] = $column;
  374. if ($sortable)
  375. {
  376. $link = '<a href="'.api_get_self().'?';
  377. foreach ($param as $key => $value)
  378. {
  379. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  380. }
  381. $link .= $this->get_additional_url_paramstring();
  382. $link .= '">'.$label.'</a>';
  383. if ($this->column == $column)
  384. {
  385. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  386. }
  387. }
  388. else
  389. {
  390. $link = $label;
  391. }
  392. $this->setHeaderContents(0, $column, $link);
  393. if (!is_null($td_attributes))
  394. {
  395. $this->td_attributes[$column] = $td_attributes;
  396. }
  397. if (!is_null($th_attributes))
  398. {
  399. $this->th_attributes[$column] = $th_attributes;
  400. }
  401. }
  402. /**
  403. * Get the parameter-string with additional parameters to use in the URLs
  404. * generated by this SortableTable
  405. */
  406. function get_additional_url_paramstring()
  407. {
  408. $param_string_parts = array ();
  409. if(is_array($this->additional_parameters) && count($this->additional_parameters)>0)
  410. {
  411. foreach ($this->additional_parameters as $key => $value)
  412. {
  413. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  414. }
  415. }
  416. $result = implode('&amp;', $param_string_parts);
  417. foreach($this->other_tables as $index => $tablename)
  418. {
  419. $param = array();
  420. if( isset($_GET[$tablename.'_direction']))
  421. $param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  422. if( isset($_GET[$tablename.'_page_nr']))
  423. $param[$tablename.'_page_nr'] = $_GET[$tablename.'_page_nr'];
  424. if( isset($_GET[$tablename.'_per_page']))
  425. $param[$tablename.'_per_page'] = $_GET[$tablename.'_per_page'];
  426. if( isset($_GET[$tablename.'_column']))
  427. $param[$tablename.'_column'] = $_GET[$tablename.'_column'];
  428. $param_string_parts = array ();
  429. foreach ($param as $key => $value)
  430. {
  431. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  432. }
  433. if(count($param_string_parts) > 0)
  434. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  435. }
  436. return $result;
  437. }
  438. /**
  439. * Get the parameter-string with the SortableTable-related parameters to use
  440. * in URLs
  441. */
  442. function get_sortable_table_param_string()
  443. {
  444. $param[$this->param_prefix.'direction'] = $this->direction;
  445. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  446. $param[$this->param_prefix.'per_page'] = $this->per_page;
  447. $param[$this->param_prefix.'column'] = $this->column;
  448. $param_string_parts = array ();
  449. foreach ($param as $key => $value)
  450. {
  451. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  452. }
  453. $res = implode('&amp;', $param_string_parts);
  454. return $res;
  455. }
  456. /**
  457. * Add a filter to a column. If another filter was allready defined for the
  458. * given column, it will be overwritten.
  459. * @param int $column The number of the column
  460. * @param string $function The name of the filter-function. This should be a
  461. * function wich requires 1 parameter and returns the filtered value.
  462. */
  463. function set_column_filter($column, $function)
  464. {
  465. $this->column_filters[$column] = $function;
  466. }
  467. /**
  468. * Define a list of actions which can be performed on the table-date.
  469. * If you define a list of actions, the first column of the table will be
  470. * converted into checkboxes.
  471. * @param array $actions A list of actions. The key is the name of the
  472. * action. The value is the label to show in the select-box
  473. * @param string $checkbox_name The name of the generated checkboxes. The
  474. * value of the checkbox will be the value of the first column.
  475. */
  476. function set_form_actions($actions, $checkbox_name = 'id')
  477. {
  478. $this->form_actions = $actions;
  479. $this->checkbox_name = $checkbox_name;
  480. }
  481. /**
  482. * Define a list of additional parameters to use in the generated URLs
  483. * @param array $parameters
  484. */
  485. function set_additional_parameters($parameters)
  486. {
  487. $this->additional_parameters = $parameters;
  488. }
  489. /**
  490. * Set other tables on the same page.
  491. * If you have other sortable tables on the page displaying this sortable
  492. * tables, you can define those other tables with this function. If you
  493. * don't define the other tables, there sorting and pagination will return
  494. * to their default state when sorting this table.
  495. * @param array $tablenames An array of table names.
  496. */
  497. function set_other_tables($tablenames)
  498. {
  499. $this->other_tables = $tablenames;
  500. }
  501. /**
  502. * Transform all data in a table-row, using the filters defined by the
  503. * function set_column_filter(...) defined elsewhere in this class.
  504. * If you've defined actions, the first element of the given row will be
  505. * converted into a checkbox
  506. * @param array $row A row from the table.
  507. */
  508. function filter_data($row)
  509. {
  510. $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  511. foreach ($this->column_filters as $column => $function)
  512. {
  513. $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
  514. }
  515. if (count($this->form_actions) > 0)
  516. {
  517. if (strlen($row[0]) > 0)
  518. {
  519. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  520. if (isset ($_GET[$this->param_prefix.'selectall']))
  521. {
  522. $row[0] .= ' checked="checked"';
  523. }
  524. $row[0] .= '/>';
  525. }
  526. }
  527. foreach ($row as $index => $value)
  528. {
  529. if (strlen($row[$index]) == 0)
  530. {
  531. $row[$index] = '-';
  532. }
  533. }
  534. return $row;
  535. }
  536. /**
  537. * Get the total number of items. This function calls the function given as
  538. * 2nd argument in the constructor of a SortableTable. Make sure your
  539. * function has the same parameters as defined here.
  540. */
  541. function get_total_number_of_items()
  542. {
  543. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function))
  544. {
  545. $this->total_number_of_items = call_user_func($this->get_total_number_function);
  546. }
  547. return $this->total_number_of_items;
  548. }
  549. /**
  550. * Get the data to display. This function calls the function given as
  551. * 2nd argument in the constructor of a SortableTable. Make sure your
  552. * function has the same parameters as defined here.
  553. * @param int $from Index of the first item to return.
  554. * @param int $per_page The number of items to return
  555. * @param int $column The number of the column on which the data should be
  556. * sorted
  557. * @param string $direction In which order should the data be sorted (ASC
  558. * or DESC)
  559. */
  560. function get_table_data($from = null, $per_page = null, $column = null, $direction = null)
  561. {
  562. if (!is_null($this->get_data_function))
  563. {
  564. return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
  565. }
  566. return array ();
  567. }
  568. }
  569. /**
  570. * Sortable table which can be used for data available in an array
  571. */
  572. class SortableTableFromArray extends SortableTable
  573. {
  574. /**
  575. * The array containing all data for this table
  576. */
  577. var $table_data;
  578. /**
  579. * Constructor
  580. * @param array $table_data
  581. * @param int $default_column
  582. * @param int $default_items_per_page
  583. */
  584. function SortableTableFromArray($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename')
  585. {
  586. parent :: SortableTable($tablename, null, null, $default_column, $default_items_per_page);
  587. $this->table_data = $table_data;
  588. }
  589. /**
  590. * Get table data to show on current page
  591. * @see SortableTable#get_table_data
  592. */
  593. function get_table_data($from = 1)
  594. {
  595. $content = TableSort :: sort_table($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC);
  596. return array_slice($content, $from, $this->per_page);
  597. }
  598. /**
  599. * Get total number of items
  600. * @see SortableTable#get_total_number_of_items
  601. */
  602. function get_total_number_of_items()
  603. {
  604. return count($this->table_data);
  605. }
  606. }
  607. /**
  608. * Sortable table which can be used for data available in an array
  609. *
  610. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  611. * $column_show is an array that lets us decide which are going to be the columns to show
  612. * $column_order is an array that lets us decide the ordering of the columns
  613. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  614. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  615. */
  616. class SortableTableFromArrayConfig extends SortableTable
  617. {
  618. /**
  619. * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
  620. */
  621. private $column_show;
  622. /**
  623. *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  624. */
  625. private $column_order;
  626. /**
  627. * The array containing all data for this table
  628. */
  629. private $table_data;
  630. /**
  631. * Constructor
  632. * @param array $table_data All the information of the table
  633. * @param int $default_column Default column that will be use in the sorts functions
  634. * @param int $default_items_per_page quantity of pages that we are going to see
  635. * @param int $tablename Name of the table
  636. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  637. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  638. */
  639. public function SortableTableFromArrayConfig($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename',$column_show=null,$column_order=null)
  640. {
  641. $this->column_show=$column_show;
  642. $this->column_order=$column_order;
  643. parent :: SortableTable($tablename, null, null, $default_column, $default_items_per_page,'ASC');
  644. $this->table_data = $table_data;
  645. }
  646. /**
  647. * Get table data to show on current page
  648. * @see SortableTable#get_table_data
  649. */
  650. public function get_table_data($from = 1)
  651. {
  652. $content = TableSort :: sort_table_config($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC ,$this->column_show, $this->column_order);
  653. return array_slice($content, $from, $this->per_page);
  654. }
  655. /**
  656. * Get total number of items
  657. * @see SortableTable#get_total_number_of_items
  658. */
  659. public function get_total_number_of_items()
  660. {
  661. return count($this->table_data);
  662. }
  663. }
  664. ?>