sortabletable.class.php 24 KB

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