sortabletable.class.php 34 KB

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