sortable_table.class.php 42 KB

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