sortabletable.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'pear/HTML/Table.php';
  4. require_once 'pear/Pager/Pager.php';
  5. require_once 'tablesort.lib.php';
  6. /**
  7. * This class allows you to display a sortable data-table. It is possible to
  8. * split the data in several pages.
  9. * Using this class you can:
  10. * - automatically create checkboxes of the first table column
  11. * - a "select all" and "deselect all" link is added
  12. * - only if you provide a list of actions for the selected items
  13. * - click on the table header to sort the data
  14. * - choose how many items you see per page
  15. * - navigate through all data-pages
  16. */
  17. class SortableTable extends HTML_Table {
  18. /**
  19. * A name for this table
  20. */
  21. public $table_name;
  22. /**
  23. * The page to display
  24. */
  25. public $page_nr;
  26. /**
  27. * The column to sort the data
  28. */
  29. public $column;
  30. /**
  31. * The sorting direction (ASC or DESC)
  32. */
  33. public $direction;
  34. /**
  35. * Number of items to display per page
  36. */
  37. public $per_page;
  38. /**
  39. * The default number of items to display per page
  40. */
  41. public $default_items_per_page;
  42. /**
  43. * A prefix for the URL-parameters, can be used on pages with multiple
  44. * SortableTables
  45. */
  46. public $param_prefix;
  47. /**
  48. * The pager object to split the data in several pages
  49. */
  50. public $pager;
  51. /**
  52. * The total number of items in the table
  53. */
  54. public $total_number_of_items;
  55. /**
  56. * The function to get the total number of items
  57. */
  58. public $get_total_number_function;
  59. /**
  60. * The function to the the data to display
  61. */
  62. public $get_data_function;
  63. /**
  64. * An array with defined column-filters
  65. */
  66. public $column_filters;
  67. /**
  68. * A list of actions which will be available through a select list
  69. */
  70. public $form_actions;
  71. /**
  72. * Additional parameters to pass in the URL
  73. */
  74. public $additional_parameters;
  75. /**
  76. * Additional attributes for the th-tags
  77. */
  78. public $th_attributes;
  79. /**
  80. * Additional attributes for the td-tags
  81. */
  82. public $td_attributes;
  83. /**
  84. * Array with names of the other tables defined on the same page of this
  85. * table
  86. */
  87. public $other_tables;
  88. /**
  89. * Create a new SortableTable
  90. * @param string $table_name A name for the table (default = 'table')
  91. * @param string $get_total_number_function A user defined function to get
  92. * the total number of items in the table
  93. * @param string $get_data_function A function to get the data to display on
  94. * the current page
  95. * @param int $default_column The default column on which the data should be
  96. * sorted
  97. * @param int $default_items_per_page The default number of items to show
  98. * on one page
  99. * @param string $default_order_direction The default order direction;
  100. * either the constant 'ASC' or 'DESC'
  101. */
  102. 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') {
  103. parent :: __construct (array ('class' => 'data_table'));
  104. $this->table_name = $table_name;
  105. $this->additional_parameters = array ();
  106. $this->param_prefix = $table_name.'_';
  107. $this->page_nr = isset ($_SESSION[$this->param_prefix.'page_nr']) ? intval($_SESSION[$this->param_prefix.'page_nr']) : 1;
  108. $this->page_nr = isset ($_GET[$this->param_prefix.'page_nr']) ? intval($_GET[$this->param_prefix.'page_nr']) : $this->page_nr;
  109. $this->column = isset ($_SESSION[$this->param_prefix.'column']) ? intval($_SESSION[$this->param_prefix.'column']) : $default_column;
  110. $this->column = isset ($_GET[$this->param_prefix.'column']) ? intval($_GET[$this->param_prefix.'column']) : $this->column;
  111. //$this->direction = isset ($_SESSION[$this->param_prefix.'direction']) ? $_SESSION[$this->param_prefix.'direction'] : $default_order_direction;
  112. if (isset($_SESSION[$this->param_prefix.'direction'])) {
  113. $my_session_direction = $_SESSION[$this->param_prefix.'direction'];
  114. if (!in_array($my_session_direction, array('ASC', 'DESC'))) {
  115. $this->direction = 'ASC';
  116. } else {
  117. if ($my_session_direction == 'ASC') {
  118. $this->direction = 'ASC';
  119. } elseif ($my_session_direction == 'DESC') {
  120. $this->direction = 'DESC';
  121. }
  122. }
  123. }
  124. if (isset($_GET[$this->param_prefix.'direction'])) {
  125. $my_get_direction = $_GET[$this->param_prefix.'direction'];
  126. if (!in_array($my_get_direction, array('ASC', 'DESC'))){
  127. $this->direction = 'ASC';
  128. } else {
  129. if ($my_get_direction == 'ASC') {
  130. $this->direction = 'ASC';
  131. } elseif ($my_get_direction == 'DESC') {
  132. $this->direction = 'DESC';
  133. }
  134. }
  135. }
  136. // Allow to change paginate in multiples tabs
  137. unset($_SESSION[$this->param_prefix.'per_page']);
  138. $this->per_page = isset ($_SESSION[$this->param_prefix.'per_page']) ? intval($_SESSION[$this->param_prefix.'per_page']) : $default_items_per_page;
  139. $this->per_page = isset ($_GET[$this->param_prefix.'per_page']) ? intval($_GET[$this->param_prefix.'per_page']) : $this->per_page;
  140. $_SESSION[$this->param_prefix.'per_page'] = $this->per_page;
  141. $_SESSION[$this->param_prefix.'direction'] = $this->direction ;
  142. $_SESSION[$this->param_prefix.'page_nr'] = $this->page_nr;
  143. $_SESSION[$this->param_prefix.'column'] = $this->column;
  144. $this->pager = null;
  145. $this->default_items_per_page = $default_items_per_page;
  146. $this->total_number_of_items = -1;
  147. $this->get_total_number_function = $get_total_number_function;
  148. $this->get_data_function = $get_data_function;
  149. $this->column_filters = array ();
  150. $this->form_actions = array ();
  151. $this->checkbox_name = null;
  152. $this->td_attributes = array ();
  153. $this->th_attributes = array ();
  154. $this->other_tables = array();
  155. }
  156. /**
  157. * Get the Pager object to split the showed data in several pages
  158. */
  159. public function get_pager () {
  160. if (is_null($this->pager)) {
  161. $total_number_of_items = $this->get_total_number_of_items();
  162. $params['mode'] = 'Sliding';
  163. $params['perPage'] = $this->per_page;
  164. $params['totalItems'] = $total_number_of_items;
  165. $params['urlVar'] = $this->param_prefix.'page_nr';
  166. $params['currentPage'] = $this->page_nr;
  167. $icon_attributes = array('style' => 'vertical-align: middle;');
  168. $params['prevImg'] = Display :: return_icon('action_prev.png', get_lang('PreviousPage'), $icon_attributes);
  169. $params['nextImg'] = Display :: return_icon('action_next.png', get_lang('NextPage'), $icon_attributes);
  170. $params['firstPageText'] = Display :: return_icon('action_first.png', get_lang('FirstPage'), $icon_attributes);
  171. $params['lastPageText'] = Display :: return_icon('action_last.png', get_lang('LastPage'), $icon_attributes);
  172. $params['firstPagePre'] = '';
  173. $params['lastPagePre'] = '';
  174. $params['firstPagePost'] = '';
  175. $params['lastPagePost'] = '';
  176. $params['spacesBeforeSeparator'] = '';
  177. $params['spacesAfterSeparator'] = '';
  178. $query_vars = array_keys($_GET);
  179. $query_vars_needed = array ($this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page');
  180. if (count($this->additional_parameters) > 0) {
  181. $query_vars_needed = array_merge($query_vars_needed, array_keys($this->additional_parameters));
  182. }
  183. $query_vars_exclude = array_diff($query_vars, $query_vars_needed);
  184. $params['excludeVars'] = $query_vars_exclude;
  185. $this->pager = & Pager :: factory($params);
  186. }
  187. return $this->pager;
  188. }
  189. /**
  190. * Displays the table, complete with navigation buttons to browse through
  191. * the data-pages.
  192. */
  193. public function display () {
  194. $empty_table = false;
  195. if ($this->get_total_number_of_items() == 0) {
  196. $cols = $this->getColCount();
  197. $this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  198. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  199. $this->setCellContents(1, 0, $message_empty);
  200. $empty_table = true;
  201. }
  202. $html = '';
  203. if (!$empty_table) {
  204. $form = $this->get_page_select_form();
  205. $nav = $this->get_navigation_html();
  206. $html = '<table style="width:100%;">';
  207. $html .= '<tr>';
  208. $html .= '<td style="width:25%;">';
  209. $html .= $form;
  210. $html .= '</td>';
  211. $html .= '<td style="text-align:center;">';
  212. $html .= $this->get_table_title();
  213. $html .= '</td>';
  214. $html .= '<td style="text-align:right;width:25%;">';
  215. $html .= $nav;
  216. $html .= '</td>';
  217. $html .= '</tr>';
  218. $html .= '</table>';
  219. if (count($this->form_actions) > 0) {
  220. $html .= '<script language="JavaScript" type="text/javascript">
  221. /*<![CDATA[*/
  222. function setCheckbox(value) {
  223. d = document.form_'.$this->table_name.';
  224. for (i = 0; i < d.elements.length; i++) {
  225. if (d.elements[i].type == "checkbox") {
  226. d.elements[i].checked = value;
  227. }
  228. }
  229. }
  230. /*]]>*/
  231. </script>';
  232. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  233. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  234. }
  235. }
  236. $html .= $this->get_table_html();
  237. if (!$empty_table) {
  238. $html .= '<table style="width:100%;">';
  239. $html .= '<tr>';
  240. $html .= '<td colspan="2">';
  241. if (count($this->form_actions) > 0) {
  242. $html .= '<br />';
  243. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript: setCheckbox(true); return false;">'.get_lang('SelectAll').'</a> - ';
  244. $html .= '<a href="?'.$params.'" onclick="javascript: setCheckbox(false); return false;">'.get_lang('UnSelectAll').'</a> ';
  245. $html .= '<select name="action">';
  246. foreach ($this->form_actions as $action => & $label) {
  247. $html .= '<option value="'.$action.'">'.$label.'</option>';
  248. }
  249. $html .= '</select>';
  250. $html .= '&nbsp;&nbsp;<button type="submit" class="save" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."'".')) return false;">'.get_lang('Select').'</button>';
  251. } else {
  252. $html .= $form;
  253. }
  254. $html .= '</td>';
  255. $html .= '<td style="text-align:right;">';
  256. $html .= $nav;
  257. $html .= '</td>';
  258. $html .= '</tr>';
  259. $html .= '</table>';
  260. if (count($this->form_actions) > 0) {
  261. $html .= '</form>';
  262. }
  263. }
  264. echo $html;
  265. }
  266. /**
  267. * This function shows the content of a table in a grid.
  268. * Should not be use to edit information (edit/delete rows) only.
  269. * */
  270. public function display_grid () {
  271. $empty_table = false;
  272. if ($this->get_total_number_of_items() == 0) {
  273. $cols = $this->getColCount();
  274. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  275. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  276. $this->setCellContents(1, 0, $message_empty);
  277. $empty_table = true;
  278. }
  279. $html = '';
  280. if (!$empty_table) {
  281. $form = $this->get_page_select_form();
  282. $nav = $this->get_navigation_html();
  283. // @todo This style css must be moved to default.css only for dev
  284. echo '<style>
  285. .main-grid { width:100%;}
  286. .sub-header { width:100%; padding-top: 10px; padding-right: 10px; padding-left: 10px; height:30px;}
  287. .grid_container { width:100%;}
  288. .grid_item { height: 120px; width:98px; float:left; padding:5px; margin:8px;}
  289. .grid_element_0 { width:100px; height: 100px; float:left; text-align:center; margin-bottom:5px;}
  290. .grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
  291. .grid_element_2 { width:150px; float:left;}
  292. .grid_selectbox { width:30%; float:left;}
  293. .grid_title { width:30%; float:left;}
  294. .grid_nav { }
  295. </style>';
  296. // @todo This also must be moved
  297. // Show only navigations if there are more than 1 page
  298. $my_pager = $this->get_pager();
  299. $html .= '<div class="main-grid">';
  300. if ($my_pager->numPages() > 1) {
  301. $html .= '<div class="sub-header">';
  302. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  303. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  304. $html .= '<div class="grid_nav">'.$nav.'</div>';
  305. $html .= '</div>';
  306. }
  307. $html .= '<div class="clear"></div>';
  308. if (count($this->form_actions) > 0) {
  309. $script= '<script language="JavaScript" type="text/javascript">
  310. /*<![CDATA[*/
  311. function setCheckbox(value) {
  312. d = document.form_'.$this->table_name.';
  313. for (i = 0; i < d.elements.length; i++) {
  314. if (d.elements[i].type == "checkbox") {
  315. d.elements[i].checked = value;
  316. }
  317. }
  318. }
  319. /*]]>*/
  320. </script>';
  321. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  322. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  323. }
  324. }
  325. // Getting the items of the table
  326. $items = $this->get_clean_html(false); //no sort
  327. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  328. // i.e: .whoisonline_table_grid_container instead of .grid_container
  329. // where whoisonline is the table's name like drupal's template engine
  330. $html .= '<div class="grid_container">';
  331. if (is_array($items) && count($items) > 0) {
  332. foreach ($items as & $row) {
  333. $html .= '<div class="grid_item">';
  334. $i = 0;
  335. foreach ($row as & $element) {
  336. $html .= '<div class="grid_element_'.$i.'">'.$element.'</div>';
  337. $i++;
  338. }
  339. $html .= '</div>';
  340. }
  341. }
  342. $html .= '</div>'; //close grid_container
  343. $html .= '</div>'; //close main grid
  344. $html .= '<div class="clear"></div>';
  345. /*
  346. if (!$empty_table) {
  347. $html .= '<table style="width:100%;">';
  348. $html .= '<tr>';
  349. $html .= '<td colspan="2">';
  350. if (count($this->form_actions) > 0) {
  351. $html .= '<br />';
  352. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
  353. $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
  354. $html .= '<select name="action">';
  355. foreach ($this->form_actions as $action => $label) {
  356. $html .= '<option value="'.$action.'">'.$label.'</option>';
  357. }
  358. $html .= '</select>';
  359. $html .= '&nbsp;&nbsp;<button type="submit" class="save" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."'".')) return false;">'.get_lang('Select').'</button>';
  360. } else {
  361. $html .= $form;
  362. }
  363. $html .= '</td>';
  364. $html .= '<td style="text-align:right;">';
  365. $html .= $nav;
  366. $html .= '</td>';
  367. $html .= '</tr>';
  368. $html .= '</div>';
  369. if (count($this->form_actions) > 0) {
  370. $html .= '</form>';
  371. }
  372. }
  373. */
  374. echo $html;
  375. }
  376. /**
  377. * This function returns the content of a table in a grid
  378. * Should not be use to edit information (edit/delete rows) only.
  379. * @param array options of visibility
  380. * @param bool hide navigation optionally
  381. * @param int content per page when show navigation (optional)
  382. * @param bool sort data optionally
  383. * @return string grid html
  384. */
  385. public function display_simple_grid($visibility_options, $hide_navigation = true, $per_page = 0, $sort_data = true) {
  386. $empty_table = false;
  387. if ($this->get_total_number_of_items() == 0) {
  388. $cols = $this->getColCount();
  389. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  390. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  391. $this->setCellContents(1, 0, $message_empty);
  392. $empty_table = true;
  393. }
  394. $html = '';
  395. if (!$empty_table) {
  396. // If we show the pagination
  397. if (!$hide_navigation) {
  398. $form = '&nbsp;';
  399. if ($per_page > 10) {
  400. $form = $this->get_page_select_form();
  401. }
  402. $nav = $this->get_navigation_html();
  403. // This also must be moved
  404. $html = '<div class="sub-header" >';
  405. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  406. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  407. $html .= '<div class="grid_nav">'.$nav.'</div>';
  408. $html .= '</div>';
  409. }
  410. $html .= '<div class="clear"></div>';
  411. if (count($this->form_actions) > 0) {
  412. $script= '<script language="javaScript" type="text/javascript">
  413. /*<![CDATA[*/
  414. function setCheckbox(value) {
  415. d = document.form_'.$this->table_name.';
  416. for (i = 0; i < d.elements.length; i++) {
  417. if (d.elements[i].type == "checkbox") {
  418. d.elements[i].checked = value;
  419. }
  420. }
  421. }
  422. /*]]>*/
  423. </script>';
  424. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  425. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  426. }
  427. }
  428. if ($hide_navigation) {
  429. $items = $this->table_data; // This is a faster way to get what we want
  430. } else {
  431. // The normal way
  432. $items = $this->get_clean_html($sort_data); // Getting the items of the table
  433. }
  434. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  435. // i.e: .whoisonline_table_grid_container instead of .grid_container
  436. // where whoisonline is the table's name like drupal's template engine
  437. if (is_array($visibility_options)) {
  438. $filter = false; // The 2nd condition of the if will be loaded
  439. } else {
  440. $filter = $visibility_options !== false;
  441. }
  442. $html .= '<div class="'.$this->table_name.'_grid_container">';
  443. if (is_array($items) && count($items) >0) {
  444. foreach ($items as & $row) {
  445. $html .= '<div class="'.$this->table_name.'_grid_item">';
  446. $i = 0;
  447. foreach ($row as & $element) {
  448. if ($filter || $visibility_options[$i]) {
  449. $html.='<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
  450. }
  451. $i++;
  452. }
  453. $html .= '</div>';
  454. }
  455. }
  456. $html .= '</div>';
  457. $html .= '<div class="clear"></div>';
  458. return $html;
  459. }
  460. /**
  461. * Get the HTML-code with the navigational buttons to browse through the
  462. * data-pages.
  463. */
  464. public function get_navigation_html () {
  465. $pager = $this->get_pager();
  466. $pager_links = $pager->getLinks();
  467. $showed_items = $pager->getOffsetByPageId();
  468. $nav = $pager_links['first'].' '.$pager_links['back'];
  469. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  470. $nav .= $pager_links['next'].' '.$pager_links['last'];
  471. return $nav;
  472. }
  473. /**
  474. * Get the HTML-code with the data-table.
  475. */
  476. public function get_table_html () {
  477. $pager = $this->get_pager();
  478. $val = $pager->getOffsetByPageId();
  479. $offset = $pager->getOffsetByPageId();
  480. $from = $offset[0] - 1;
  481. $table_data = $this->get_table_data($from);
  482. if (is_array($table_data)) {
  483. foreach ($table_data as $index => & $row) {
  484. $row = $this->filter_data($row);
  485. $this->addRow($row);
  486. }
  487. }
  488. $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
  489. foreach ($this->th_attributes as $column => $attributes) {
  490. $this->setCellAttributes(0, $column, $attributes);
  491. }
  492. foreach ($this->td_attributes as $column => $attributes) {
  493. $this->setColAttributes($column, $attributes);
  494. }
  495. return $this->toHTML();
  496. }
  497. /**
  498. * This function return the items of the table
  499. * @param bool true for sorting table data or false otherwise
  500. * @return array table row items
  501. */
  502. public function get_clean_html ($sort = true) {
  503. $pager = $this->get_pager();
  504. $val = $pager->getOffsetByPageId();
  505. $offset = $pager->getOffsetByPageId();
  506. $from = $offset[0] - 1;
  507. $table_data = $this->get_table_data($from, $sort);
  508. $new_table_data = array();
  509. if (is_array($table_data)) {
  510. foreach ($table_data as $index => & $row) {
  511. $row = $this->filter_data($row);
  512. $new_table_data[] = $row;
  513. }
  514. }
  515. return $new_table_data;
  516. }
  517. /**
  518. * Get the HTML-code wich represents a form to select how many items a page
  519. * should contain.
  520. */
  521. public function get_page_select_form () {
  522. $total_number_of_items = $this->get_total_number_of_items();
  523. if ($total_number_of_items <= $this->default_items_per_page) {
  524. return '';
  525. }
  526. $result[] = '<form method="get" action="'.api_get_self().'" style="display:inline;">';
  527. $param[$this->param_prefix.'direction'] = $this->direction;
  528. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  529. $param[$this->param_prefix.'column'] = $this->column;
  530. if (is_array($this->additional_parameters)) {
  531. $param = array_merge($param, $this->additional_parameters);
  532. }
  533. foreach ($param as $key => & $value) {
  534. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  535. }
  536. $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript: this.form.submit();">';
  537. for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) {
  538. $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  539. }
  540. // @todo no limits
  541. //if ($total_number_of_items < 500) {
  542. $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
  543. //}
  544. $result[] = '</select>';
  545. $result[] = '<noscript>';
  546. $result[] = '<button class="save" type="submit">'.get_lang('Save').'</button>';
  547. $result[] = '</noscript>';
  548. $result[] = '</form>';
  549. $result = implode("\n", $result);
  550. return $result;
  551. }
  552. /**
  553. * Get the table title.
  554. */
  555. public function get_table_title () {
  556. $pager = $this->get_pager();
  557. $showed_items = $pager->getOffsetByPageId();
  558. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  559. }
  560. /**
  561. * Set the header-label
  562. * @param int $column The column number
  563. * @param string $label The label
  564. * @param boolean $sortable Is the table sortable by this column? (defatult
  565. * = true)
  566. * @param string $th_attributes Additional attributes for the th-tag of the
  567. * table header
  568. * @param string $td_attributes Additional attributes for the td-tags of the
  569. * column
  570. */
  571. public function set_header ($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null) {
  572. $param['direction'] = 'ASC';
  573. if ($this->column == $column && $this->direction == 'ASC') {
  574. $param['direction'] = 'DESC';
  575. }
  576. $param['page_nr'] = $this->page_nr;
  577. $param['per_page'] = $this->per_page;
  578. $param['column'] = $column;
  579. if ($sortable) {
  580. $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
  581. foreach ($param as $key => & $value) {
  582. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  583. }
  584. $link .= $this->get_additional_url_paramstring();
  585. $link .= '">'.$label.'</a>';
  586. if ($this->column == $column) {
  587. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  588. }
  589. } else {
  590. $link = $label;
  591. }
  592. $this->setHeaderContents(0, $column, $link);
  593. if (!is_null($td_attributes)) {
  594. $this->td_attributes[$column] = $td_attributes;
  595. }
  596. if (!is_null($th_attributes)) {
  597. $this->th_attributes[$column] = $th_attributes;
  598. }
  599. }
  600. /**
  601. * Get the parameter-string with additional parameters to use in the URLs
  602. * generated by this SortableTable
  603. */
  604. public function get_additional_url_paramstring () {
  605. $param_string_parts = array ();
  606. if (is_array($this->additional_parameters) && count($this->additional_parameters) > 0) {
  607. foreach ($this->additional_parameters as $key => & $value) {
  608. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  609. }
  610. }
  611. $result = implode('&amp;', $param_string_parts);
  612. foreach ($this->other_tables as $index => & $tablename) {
  613. $param = array();
  614. if (isset($_GET[$tablename.'_direction'])) {
  615. //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  616. $my_get_direction = $_GET[$tablename.'_direction'];
  617. if (!in_array($my_get_direction, array('ASC', 'DESC'))) {
  618. $param[$tablename.'_direction'] = 'ASC';
  619. } else {
  620. $param[$tablename.'_direction'] = $my_get_direction;
  621. }
  622. }
  623. if (isset($_GET[$tablename.'_page_nr'])) {
  624. $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
  625. }
  626. if (isset($_GET[$tablename.'_per_page'])) {
  627. $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
  628. }
  629. if (isset($_GET[$tablename.'_column'])) {
  630. $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
  631. }
  632. $param_string_parts = array ();
  633. foreach ($param as $key => & $value) {
  634. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  635. }
  636. if (count($param_string_parts) > 0)
  637. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  638. }
  639. return $result;
  640. }
  641. /**
  642. * Get the parameter-string with the SortableTable-related parameters to use
  643. * in URLs
  644. */
  645. public function get_sortable_table_param_string () {
  646. $param[$this->param_prefix.'direction'] = $this->direction;
  647. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  648. $param[$this->param_prefix.'per_page'] = $this->per_page;
  649. $param[$this->param_prefix.'column'] = $this->column;
  650. $param_string_parts = array ();
  651. foreach ($param as $key => & $value) {
  652. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  653. }
  654. $res = implode('&amp;', $param_string_parts);
  655. return $res;
  656. }
  657. /**
  658. * Add a filter to a column. If another filter was allready defined for the
  659. * given column, it will be overwritten.
  660. * @param int $column The number of the column
  661. * @param string $function The name of the filter-function. This should be a
  662. * function wich requires 1 parameter and returns the filtered value.
  663. */
  664. public function set_column_filter ($column, $function) {
  665. $this->column_filters[$column] = $function;
  666. }
  667. /**
  668. * Define a list of actions which can be performed on the table-date.
  669. * If you define a list of actions, the first column of the table will be
  670. * converted into checkboxes.
  671. * @param array $actions A list of actions. The key is the name of the
  672. * action. The value is the label to show in the select-box
  673. * @param string $checkbox_name The name of the generated checkboxes. The
  674. * value of the checkbox will be the value of the first column.
  675. */
  676. public function set_form_actions ($actions, $checkbox_name = 'id') {
  677. $this->form_actions = $actions;
  678. $this->checkbox_name = $checkbox_name;
  679. }
  680. /**
  681. * Define a list of additional parameters to use in the generated URLs
  682. * @example $parameters['action'] = 'test'; will be convert in <input type="hidden" name="action" value="test">
  683. * @param array $parameters
  684. */
  685. public function set_additional_parameters ($parameters) {
  686. $this->additional_parameters = $parameters;
  687. }
  688. /**
  689. * Set other tables on the same page.
  690. * If you have other sortable tables on the page displaying this sortable
  691. * tables, you can define those other tables with this function. If you
  692. * don't define the other tables, there sorting and pagination will return
  693. * to their default state when sorting this table.
  694. * @param array $tablenames An array of table names.
  695. */
  696. public function set_other_tables ($tablenames) {
  697. $this->other_tables = $tablenames;
  698. }
  699. /**
  700. * Transform all data in a table-row, using the filters defined by the
  701. * function set_column_filter(...) defined elsewhere in this class.
  702. * If you've defined actions, the first element of the given row will be
  703. * converted into a checkbox
  704. * @param array $row A row from the table.
  705. */
  706. public function filter_data ($row) {
  707. $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  708. foreach ($this->column_filters as $column => & $function) {
  709. $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
  710. }
  711. if (count($this->form_actions) > 0) {
  712. if (strlen($row[0]) > 0) {
  713. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  714. if (isset($_GET[$this->param_prefix.'selectall'])) {
  715. $row[0] .= ' checked="checked"';
  716. }
  717. $row[0] .= '/>';
  718. }
  719. }
  720. foreach ($row as $index => & $value) {
  721. if (empty($value)) {
  722. $value = '-';
  723. }
  724. }
  725. return $row;
  726. }
  727. /**
  728. * Get the total number of items. This function calls the function given as
  729. * 2nd argument in the constructor of a SortableTable. Make sure your
  730. * function has the same parameters as defined here.
  731. */
  732. public function get_total_number_of_items () {
  733. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
  734. $this->total_number_of_items = call_user_func($this->get_total_number_function);
  735. }
  736. return $this->total_number_of_items;
  737. }
  738. /**
  739. * Get the data to display. This function calls the function given as
  740. * 2nd argument in the constructor of a SortableTable. Make sure your
  741. * function has the same parameters as defined here.
  742. * @param int $from Index of the first item to return.
  743. * @param int $per_page The number of items to return
  744. * @param int $column The number of the column on which the data should be
  745. * sorted
  746. * @param string $direction In which order should the data be sorted (ASC
  747. * or DESC)
  748. */
  749. public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null) {
  750. if (!is_null($this->get_data_function)) {
  751. return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
  752. }
  753. return array ();
  754. }
  755. }
  756. /**
  757. * Sortable table which can be used for data available in an array
  758. */
  759. class SortableTableFromArray extends SortableTable {
  760. /**
  761. * The array containing all data for this table
  762. */
  763. public $table_data;
  764. /**
  765. * Constructor
  766. * @param array $table_data
  767. * @param int $default_column
  768. * @param int $default_items_per_page
  769. */
  770. public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename') {
  771. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
  772. $this->table_data = $table_data;
  773. }
  774. /**
  775. * Get table data to show on current page
  776. * @see SortableTable#get_table_data
  777. */
  778. public function get_table_data ($from = 1, $sort = true) {
  779. if ($sort) {
  780. $content = TableSort :: sort_table($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC);
  781. } else {
  782. $content = $this->table_data;
  783. }
  784. return array_slice($content, $from, $this->per_page);
  785. }
  786. /**
  787. * Get total number of items
  788. * @see SortableTable#get_total_number_of_items
  789. */
  790. public function get_total_number_of_items() {
  791. return count($this->table_data);
  792. }
  793. }
  794. /**
  795. * Sortable table which can be used for data available in an array
  796. *
  797. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  798. * $column_show is an array that lets us decide which are going to be the columns to show
  799. * $column_order is an array that lets us decide the ordering of the columns
  800. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  801. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  802. */
  803. class SortableTableFromArrayConfig extends SortableTable {
  804. /**
  805. * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
  806. */
  807. private $column_show;
  808. /**
  809. *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  810. */
  811. private $column_order;
  812. /**
  813. * The array containing all data for this table
  814. */
  815. private $table_data;
  816. /**
  817. * Constructor
  818. * @param array $table_data All the information of the table
  819. * @param int $default_column Default column that will be use in the sorts functions
  820. * @param int $default_items_per_page quantity of pages that we are going to see
  821. * @param int $tablename Name of the table
  822. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  823. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  824. */
  825. public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename', $column_show = null, $column_order = null, $direction = 'ASC') {
  826. $this->column_show = $column_show;
  827. $this->column_order = $column_order;
  828. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page, $direction);
  829. $this->table_data = $table_data;
  830. }
  831. /**
  832. * Get table data to show on current page
  833. * @see SortableTable#get_table_data
  834. */
  835. public function get_table_data($from = 1) {
  836. $content = TableSort :: sort_table_config($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC, $this->column_show, $this->column_order);
  837. return array_slice($content, $from, $this->per_page);
  838. }
  839. /**
  840. * Get total number of items
  841. * @see SortableTable#get_total_number_of_items
  842. */
  843. public function get_total_number_of_items () {
  844. return count($this->table_data);
  845. }
  846. }