sortable_table.class.php 41 KB

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