sortable_table.class.php 41 KB

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