sortabletable.class.php 33 KB

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