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