sortabletable.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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" alt="'.get_lang('PreviousPage').'" title="'.get_lang('PreviousPage').'" style="vertical-align: middle;"/>';
  185. $params['nextImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/next.png" alt="'.get_lang('NextPage').'" title="'.get_lang('NextPage').'" style="vertical-align: middle;"/>';
  186. $params['firstPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/first.png" alt="'.get_lang('FirstPage').'" title="'.get_lang('FirstPage').'" style="vertical-align: middle;"/>';
  187. $params['lastPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/last.png" alt="'.get_lang('LastPage').'" title="'.get_lang('LastPage').'" 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. * This function shows the content of a table in a grid.
  299. * Should not be use to edit information (edit/delete rows) only.
  300. * */
  301. public function display_grid () {
  302. global $charset;
  303. $empty_table = false;
  304. $html = '';
  305. if ($this->get_total_number_of_items() == 0) {
  306. $cols = $this->getColCount();
  307. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  308. if (api_is_xml_http_request()===true) {
  309. $message_empty=api_utf8_encode(get_lang('TheListIsEmpty'));
  310. } else {
  311. $message_empty=get_lang('TheListIsEmpty');
  312. }
  313. $this->setCellContents(1, 0,$message_empty);
  314. $empty_table = true;
  315. }
  316. $html='';
  317. if (!$empty_table) {
  318. $form = $this->get_page_select_form();
  319. $nav = $this->get_navigation_html();
  320. // @todo This style css must be moved to default.css only for dev
  321. echo '<style>
  322. .grid_container { width:100%;}
  323. .grid_item { height: 120px; width:98px; border:1px dotted #ccc; float:left; padding:5px; margin:8px;}
  324. .grid_element_0 { width:100px; float:left; text-align:center; margin-bottom:5px;}
  325. .grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
  326. .grid_element_2 { width:150px; float:left;}
  327. .grid_selectbox { width:50%; float:left;}
  328. .grid_title { width:30%; float:left;}
  329. .grid_nav { float:right;}
  330. </style>';
  331. //this also must be moved
  332. $html = '<div class="sub-header">';
  333. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  334. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  335. $html .= '<div class="grid_nav">'.$nav.'</div>';
  336. $html .= '</div>';
  337. $html .= '<div class="clear"></div>';
  338. if (count($this->form_actions) > 0) {
  339. $script= '<script language="JavaScript" type="text/javascript">
  340. /*<![CDATA[*/
  341. function setCheckbox(value) {
  342. d = document.form_'.$this->table_name.';
  343. for (i = 0; i < d.elements.length; i++) {
  344. if (d.elements[i].type == "checkbox") {
  345. d.elements[i].checked = value;
  346. }
  347. }
  348. }
  349. /*]]>*/
  350. </script>';
  351. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  352. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  353. }
  354. }
  355. $items = $this->get_clean_html(); // getting the items of the table
  356. // the generating of style classes must be improved. Maybe we need a a table name to create style on the fly:
  357. // i.e: .whoisonline_table_grid_container instead of .grid_container
  358. // where whoisonline is the table's name like drupal's template engine
  359. $html .= '<div class="grid_container">';
  360. if (is_array($items) && count($items)>0 ) {
  361. foreach($items as $row) {
  362. $html.= '<div class="grid_item">';
  363. $i=0;
  364. foreach($row as $element) {
  365. $html.='<div class="grid_element_'.$i.'">'.$element.'</div>';
  366. $i++;
  367. }
  368. $html.='</div>';
  369. }
  370. $html .= '</div>';
  371. }
  372. $html .= '<div class="clear"></div>';
  373. /*
  374. if (!$empty_table) {
  375. $html .= '<table style="width:100%;">';
  376. $html .= '<tr>';
  377. $html .= '<td colspan="2">';
  378. if (count($this->form_actions) > 0) {
  379. $html .= '<br/>';
  380. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
  381. $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
  382. $html .= '<select name="action">';
  383. foreach ($this->form_actions as $action => $label) {
  384. $html .= '<option value="'.$action.'">'.$label.'</option>';
  385. }
  386. $html .= '</select>';
  387. $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>';
  388. } else {
  389. $html .= $form;
  390. }
  391. $html .= '</td>';
  392. $html .= '<td style="text-align:right;">';
  393. $html .= $nav;
  394. $html .= '</td>';
  395. $html .= '</tr>';
  396. $html .= '</div>';
  397. if (count($this->form_actions) > 0) {
  398. $html .= '</form>';
  399. }
  400. }
  401. */
  402. echo $html;
  403. }
  404. /**
  405. * This function return the content of a table in a grid
  406. * Should not be use to edit information (edit/delete rows) only.
  407. * @param array options of visibility
  408. * @param bool hide navigation optionally
  409. * @param int content per page when show navigation (optional)
  410. * @param bool sort data optionally
  411. * @return string grid html
  412. */
  413. public function display_simple_grid($vibility_options, $hide_navigation = true, $per_page = 0, $sort_data = true) {
  414. global $charset;
  415. $empty_table = false;
  416. $html = '';
  417. if ($this->get_total_number_of_items() == 0) {
  418. $cols = $this->getColCount();
  419. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  420. if (api_is_xml_http_request()===true) {
  421. $message_empty=api_utf8_encode(get_lang('TheListIsEmpty'));
  422. } else {
  423. $message_empty=get_lang('TheListIsEmpty');
  424. }
  425. $this->setCellContents(1, 0,$message_empty);
  426. $empty_table = true;
  427. }
  428. $html='';
  429. if (!$empty_table) {
  430. //if we show the pagination
  431. if ($hide_navigation == false ) {
  432. $form = '&nbsp;';
  433. if ($per_page > 10) {
  434. $form = $this->get_page_select_form();
  435. }
  436. $nav = $this->get_navigation_html();
  437. //this also must be moved
  438. $html = '<div class="sub-header" >';
  439. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  440. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  441. $html .= '<div class="grid_nav">'.$nav.'</div>';
  442. $html .= '</div>';
  443. }
  444. $html .= '<div class="clear"></div>';
  445. if (count($this->form_actions) > 0) {
  446. $script= '<script language="javaScript" type="text/javascript">
  447. /*<![CDATA[*/
  448. function setCheckbox(value) {
  449. d = document.form_'.$this->table_name.';
  450. for (i = 0; i < d.elements.length; i++) {
  451. if (d.elements[i].type == "checkbox") {
  452. d.elements[i].checked = value;
  453. }
  454. }
  455. }
  456. /*]]>*/
  457. </script>';
  458. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  459. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  460. }
  461. }
  462. if ($hide_navigation == true ) {
  463. $items = $this->table_data; //this is a faster way to get what we want
  464. } else {
  465. //the normal way
  466. $items = $this->get_clean_html($sort_data); // getting the items of the table
  467. }
  468. // the generating of style classes must be improved. Maybe we need a a table name to create style on the fly:
  469. // i.e: .whoisonline_table_grid_container instead of .grid_container
  470. // where whoisonline is the table's name like drupal's template engine
  471. if (is_array($vibility_options)) {
  472. $filter = false; // the 2nd condition of the if will be loaded
  473. } else {
  474. if ($vibility_options === false) {
  475. $filter = false;
  476. } else {
  477. $filter = true;
  478. }
  479. }
  480. $html .= '<div class="'.$this->table_name.'_grid_container">';
  481. if (is_array($items) && count($items)>0 ) {
  482. foreach($items as $row) {
  483. $html.= '<div class="'.$this->table_name.'_grid_item">';
  484. $i=0;
  485. foreach($row as $element) {
  486. if ( $filter === true || $vibility_options[$i] == true) {
  487. $html.='<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
  488. }
  489. $i++;
  490. }
  491. $html.='</div>';
  492. }
  493. }
  494. $html.='</div>';
  495. $html .= '<div class="clear"></div>';
  496. return $html;
  497. }
  498. /**
  499. * Get the HTML-code with the navigational buttons to browse through the
  500. * data-pages.
  501. */
  502. public function get_navigation_html () {
  503. $pager = $this->get_pager();
  504. $pager_links = $pager->getLinks();
  505. $showed_items = $pager->getOffsetByPageId();
  506. $nav = $pager_links['first'].' '.$pager_links['back'];
  507. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  508. $nav .= $pager_links['next'].' '.$pager_links['last'];
  509. return $nav;
  510. }
  511. /**
  512. * Get the HTML-code with the data-table.
  513. */
  514. public function get_table_html () {
  515. $pager = $this->get_pager();
  516. $val = $pager->getOffsetByPageId();
  517. $offset = $pager->getOffsetByPageId();
  518. $from = $offset[0] - 1;
  519. $table_data = $this->get_table_data($from);
  520. if (is_array($table_data)) {
  521. foreach ($table_data as $index => $row) {
  522. $row = $this->filter_data($row);
  523. $this->addRow($row);
  524. }
  525. }
  526. $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
  527. foreach ($this->th_attributes as $column => $attributes) {
  528. $this->setCellAttributes(0, $column, $attributes);
  529. }
  530. foreach ($this->td_attributes as $column => $attributes) {
  531. $this->setColAttributes($column, $attributes);
  532. }
  533. return $this->toHTML();
  534. }
  535. /**
  536. * This function return the items of the table
  537. * @param bool true for sorting table data or false otherwise
  538. * @return array table row items
  539. */
  540. public function get_clean_html ($sort=true) {
  541. $pager = $this->get_pager();
  542. $val = $pager->getOffsetByPageId();
  543. $offset = $pager->getOffsetByPageId();
  544. $from = $offset[0] - 1;
  545. $table_data = $this->get_table_data($from,$sort);
  546. $new_table_data = array();
  547. if (is_array($table_data)) {
  548. foreach ($table_data as $index => $row) {
  549. $row = $this->filter_data($row);
  550. $new_table_data[] = $row;
  551. }
  552. }
  553. return $new_table_data;
  554. }
  555. /**
  556. * Get the HTML-code wich represents a form to select how many items a page
  557. * should contain.
  558. */
  559. public function get_page_select_form () {
  560. $total_number_of_items = $this->get_total_number_of_items();
  561. if ($total_number_of_items <= $this->default_items_per_page)
  562. {
  563. return '';
  564. }
  565. $result[] = '<form method="get" action="'.api_get_self().'" style="display:inline;">';
  566. $param[$this->param_prefix.'direction'] = $this->direction;
  567. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  568. $param[$this->param_prefix.'column'] = $this->column;
  569. $param = array_merge($param, $this->additional_parameters);
  570. foreach ($param as $key => $value) {
  571. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  572. }
  573. $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript:this.form.submit();">';
  574. for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) {
  575. $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  576. }
  577. if ($total_number_of_items < 500) {
  578. $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
  579. }
  580. $result[] = '</select>';
  581. $result[] = '<noscript>';
  582. $result[] = '<button class="save" type="submit">'.get_lang('Save').'</button>';
  583. $result[] = '</noscript>';
  584. $result[] = '</form>';
  585. $result = implode("\n", $result);
  586. return $result;
  587. }
  588. /**
  589. * Get the table title.
  590. */
  591. public function get_table_title () {
  592. $pager = $this->get_pager();
  593. $showed_items = $pager->getOffsetByPageId();
  594. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  595. }
  596. /**
  597. * Set the header-label
  598. * @param int $column The column number
  599. * @param string $label The label
  600. * @param boolean $sortable Is the table sortable by this column? (defatult
  601. * = true)
  602. * @param string $th_attributes Additional attributes for the th-tag of the
  603. * table header
  604. * @param string $td_attributes Additional attributes for the td-tags of the
  605. * column
  606. */
  607. public function set_header ($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null) {
  608. $param['direction'] = 'ASC';
  609. if ($this->column == $column && $this->direction == 'ASC') {
  610. $param['direction'] = 'DESC';
  611. }
  612. $param['page_nr'] = $this->page_nr;
  613. $param['per_page'] = $this->per_page;
  614. $param['column'] = $column;
  615. if ($sortable) {
  616. $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
  617. foreach ($param as $key => $value) {
  618. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  619. }
  620. $link .= $this->get_additional_url_paramstring();
  621. $link .= '">'.$label.'</a>';
  622. if ($this->column == $column)
  623. {
  624. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  625. }
  626. }
  627. else
  628. {
  629. $link = $label;
  630. }
  631. $this->setHeaderContents(0, $column, $link);
  632. if (!is_null($td_attributes))
  633. {
  634. $this->td_attributes[$column] = $td_attributes;
  635. }
  636. if (!is_null($th_attributes))
  637. {
  638. $this->th_attributes[$column] = $th_attributes;
  639. }
  640. }
  641. /**
  642. * Get the parameter-string with additional parameters to use in the URLs
  643. * generated by this SortableTable
  644. */
  645. public function get_additional_url_paramstring () {
  646. $param_string_parts = array ();
  647. if(is_array($this->additional_parameters) && count($this->additional_parameters)>0)
  648. {
  649. foreach ($this->additional_parameters as $key => $value)
  650. {
  651. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  652. }
  653. }
  654. $result = implode('&amp;', $param_string_parts);
  655. foreach($this->other_tables as $index => $tablename) {
  656. $param = array();
  657. if( isset($_GET[$tablename.'_direction'])) {
  658. //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  659. $my_get_direction = $_GET[$tablename.'_direction'];
  660. if(!in_array($my_get_direction ,array('ASC','DESC'))){
  661. $param[$tablename.'_direction'] = 'ASC';
  662. } else {
  663. $param[$tablename.'_direction'] = $my_get_direction;
  664. }
  665. }
  666. if( isset($_GET[$tablename.'_page_nr']))
  667. $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
  668. if( isset($_GET[$tablename.'_per_page']))
  669. $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
  670. if( isset($_GET[$tablename.'_column']))
  671. $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
  672. $param_string_parts = array ();
  673. foreach ($param as $key => $value) {
  674. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  675. }
  676. if(count($param_string_parts) > 0)
  677. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  678. }
  679. return $result;
  680. }
  681. /**
  682. * Get the parameter-string with the SortableTable-related parameters to use
  683. * in URLs
  684. */
  685. public function get_sortable_table_param_string () {
  686. $param[$this->param_prefix.'direction'] = $this->direction;
  687. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  688. $param[$this->param_prefix.'per_page'] = $this->per_page;
  689. $param[$this->param_prefix.'column'] = $this->column;
  690. $param_string_parts = array ();
  691. foreach ($param as $key => $value) {
  692. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  693. }
  694. $res = implode('&amp;', $param_string_parts);
  695. return $res;
  696. }
  697. /**
  698. * Add a filter to a column. If another filter was allready defined for the
  699. * given column, it will be overwritten.
  700. * @param int $column The number of the column
  701. * @param string $function The name of the filter-function. This should be a
  702. * function wich requires 1 parameter and returns the filtered value.
  703. */
  704. public function set_column_filter ($column, $function) {
  705. $this->column_filters[$column] = $function;
  706. }
  707. /**
  708. * Define a list of actions which can be performed on the table-date.
  709. * If you define a list of actions, the first column of the table will be
  710. * converted into checkboxes.
  711. * @param array $actions A list of actions. The key is the name of the
  712. * action. The value is the label to show in the select-box
  713. * @param string $checkbox_name The name of the generated checkboxes. The
  714. * value of the checkbox will be the value of the first column.
  715. */
  716. public function set_form_actions ($actions, $checkbox_name = 'id') {
  717. $this->form_actions = $actions;
  718. $this->checkbox_name = $checkbox_name;
  719. }
  720. /**
  721. * Define a list of additional parameters to use in the generated URLs
  722. * @param array $parameters
  723. */
  724. public function set_additional_parameters ($parameters) {
  725. $this->additional_parameters = $parameters;
  726. }
  727. /**
  728. * Set other tables on the same page.
  729. * If you have other sortable tables on the page displaying this sortable
  730. * tables, you can define those other tables with this function. If you
  731. * don't define the other tables, there sorting and pagination will return
  732. * to their default state when sorting this table.
  733. * @param array $tablenames An array of table names.
  734. */
  735. public function set_other_tables ($tablenames) {
  736. $this->other_tables = $tablenames;
  737. }
  738. /**
  739. * Transform all data in a table-row, using the filters defined by the
  740. * function set_column_filter(...) defined elsewhere in this class.
  741. * If you've defined actions, the first element of the given row will be
  742. * converted into a checkbox
  743. * @param array $row A row from the table.
  744. */
  745. public function filter_data ($row) {
  746. $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  747. foreach ($this->column_filters as $column => $function)
  748. {
  749. $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
  750. }
  751. if (count($this->form_actions) > 0) {
  752. if (strlen($row[0]) > 0) {
  753. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  754. if (isset ($_GET[$this->param_prefix.'selectall'])) {
  755. $row[0] .= ' checked="checked"';
  756. }
  757. $row[0] .= '/>';
  758. }
  759. }
  760. foreach ($row as $index => $value) {
  761. if (strlen($row[$index]) == 0)
  762. {
  763. $row[$index] = '-';
  764. }
  765. }
  766. return $row;
  767. }
  768. /**
  769. * Get the total number of items. This function calls the function given as
  770. * 2nd argument in the constructor of a SortableTable. Make sure your
  771. * function has the same parameters as defined here.
  772. */
  773. public function get_total_number_of_items () {
  774. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
  775. $this->total_number_of_items = call_user_func($this->get_total_number_function);
  776. }
  777. return $this->total_number_of_items;
  778. }
  779. /**
  780. * Get the data to display. This function calls the function given as
  781. * 2nd argument in the constructor of a SortableTable. Make sure your
  782. * function has the same parameters as defined here.
  783. * @param int $from Index of the first item to return.
  784. * @param int $per_page The number of items to return
  785. * @param int $column The number of the column on which the data should be
  786. * sorted
  787. * @param string $direction In which order should the data be sorted (ASC
  788. * or DESC)
  789. */
  790. public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null) {
  791. if (!is_null($this->get_data_function))
  792. {
  793. return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
  794. }
  795. return array ();
  796. }
  797. }
  798. /**
  799. * Sortable table which can be used for data available in an array
  800. */
  801. class SortableTableFromArray extends SortableTable {
  802. /**
  803. * The array containing all data for this table
  804. */
  805. public $table_data;
  806. /**
  807. * Constructor
  808. * @param array $table_data
  809. * @param int $default_column
  810. * @param int $default_items_per_page
  811. */
  812. public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename') {
  813. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
  814. $this->table_data = $table_data;
  815. }
  816. /**
  817. * Get table data to show on current page
  818. * @see SortableTable#get_table_data
  819. */
  820. public function get_table_data ($from = 1,$sort = true) {
  821. if ($sort) {
  822. $content = TableSort :: sort_table($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC);
  823. } else {
  824. $content = $this->table_data;
  825. }
  826. return array_slice($content, $from, $this->per_page);
  827. }
  828. /**
  829. * Get total number of items
  830. * @see SortableTable#get_total_number_of_items
  831. */
  832. public function get_total_number_of_items() {
  833. return count($this->table_data);
  834. }
  835. }
  836. /**
  837. * Sortable table which can be used for data available in an array
  838. *
  839. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  840. * $column_show is an array that lets us decide which are going to be the columns to show
  841. * $column_order is an array that lets us decide the ordering of the columns
  842. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  843. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  844. */
  845. class SortableTableFromArrayConfig extends SortableTable {
  846. /**
  847. * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
  848. */
  849. private $column_show;
  850. /**
  851. *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  852. */
  853. private $column_order;
  854. /**
  855. * The array containing all data for this table
  856. */
  857. private $table_data;
  858. /**
  859. * Constructor
  860. * @param array $table_data All the information of the table
  861. * @param int $default_column Default column that will be use in the sorts functions
  862. * @param int $default_items_per_page quantity of pages that we are going to see
  863. * @param int $tablename Name of the table
  864. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  865. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  866. */
  867. public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename',$column_show=null,$column_order=null,$direction='ASC') {
  868. $this->column_show=$column_show;
  869. $this->column_order=$column_order;
  870. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page,$direction);
  871. $this->table_data = $table_data;
  872. }
  873. /**
  874. * Get table data to show on current page
  875. * @see SortableTable#get_table_data
  876. */
  877. public function get_table_data($from = 1) {
  878. $content = TableSort :: sort_table_config($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC ,$this->column_show, $this->column_order);
  879. return array_slice($content, $from, $this->per_page);
  880. }
  881. /**
  882. * Get total number of items
  883. * @see SortableTable#get_total_number_of_items
  884. */
  885. public function get_total_number_of_items () {
  886. return count($this->table_data);
  887. }
  888. }
  889. ?>