sortable_table.class.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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('PreviousPage'),
  268. $icon_attributes
  269. );
  270. $params['nextImg'] = Display:: return_icon(
  271. 'action_next.png',
  272. get_lang('NextPage'),
  273. $icon_attributes
  274. );
  275. $params['firstPageText'] = Display:: return_icon(
  276. 'action_first.png',
  277. get_lang('FirstPage'),
  278. $icon_attributes
  279. );
  280. $params['lastPageText'] = Display:: return_icon(
  281. 'action_last.png',
  282. get_lang('LastPage'),
  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('TheListIsEmpty'));
  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="table-well">';
  343. $html .= '<table class="data_table_pagination">';
  344. $html .= '<tr>';
  345. $html .= '<td style="width:25%;">';
  346. $html .= $form;
  347. $html .= '</td>';
  348. $html .= '<td style="text-align:center;">';
  349. $html .= $this->get_table_title();
  350. $html .= '</td>';
  351. $html .= '<td style="text-align:right;width:25%;">';
  352. $html .= $nav;
  353. $html .= '</td>';
  354. $html .= '</tr>';
  355. $html .= '</table>';
  356. $html .= '</div>';
  357. }
  358. if (count($this->form_actions) > 0) {
  359. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  360. $html .= '<form id ="'.$table_id.'" class="form-search" method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  361. }
  362. }
  363. $html .= '<div class="table-responsive">'.$content.'</div>';
  364. if (!$empty_table) {
  365. if (!empty($this->additional_parameters)) {
  366. foreach ($this->additional_parameters as $key => $value) {
  367. $html .= '<input type="hidden" name ="'.Security::remove_XSS($key).'" value ="'.Security::remove_XSS($value).'" />';
  368. }
  369. }
  370. $html .= '<input type="hidden" name="action">';
  371. $html .= '<table style="width:100%;">';
  372. $html .= '<tr>';
  373. $html .= '<td>';
  374. if (count($this->form_actions) > 0) {
  375. $html .= '<div class="btn-toolbar">';
  376. $html .= '<div class="btn-group">';
  377. $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>';
  378. $html .= '<a class="btn btn-default" href="?'.$params.'" onclick="javascript: setCheckbox(false, \''.$table_id.'\'); return false;">'.get_lang('UnSelectAll').'</a> ';
  379. $html .= '</div>';
  380. $html .= '<div class="btn-group">
  381. <button class="btn btn-default" onclick="javascript:return false;">'.get_lang('Actions').'</button>
  382. <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  383. <span class="caret"></span>
  384. </button>';
  385. $html .= '<ul class="dropdown-menu">';
  386. foreach ($this->form_actions as $action => &$label) {
  387. $html .= '<li><a data-action ="'.$action.'" href="#" onclick="javascript:action_click(this, \''.$table_id.'\');">'.$label.'</a></li>';
  388. }
  389. $html .= '</ul>';
  390. $html .= '</div>'; //btn-group
  391. $html .= '</div>'; //toolbar
  392. } else {
  393. $html .= $form;
  394. }
  395. $html .= '</td>';
  396. // Pagination
  397. if ($this->get_total_number_of_items() > $this->default_items_per_page) {
  398. $html .= '<td style="text-align:right;">';
  399. $html .= $nav;
  400. $html .= '</td>';
  401. } else {
  402. $html .= '<td> ';
  403. $html .= '</td>';
  404. }
  405. $html .= '</tr>';
  406. $html .= '</table>';
  407. if (count($this->form_actions) > 0) {
  408. $html .= '</form>';
  409. }
  410. }
  411. return $html;
  412. }
  413. /**
  414. * This function shows the content of a table in a grid.
  415. * Should not be use to edit information (edit/delete rows) only.
  416. */
  417. public function display_grid()
  418. {
  419. $empty_table = false;
  420. if ($this->get_total_number_of_items() == 0) {
  421. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  422. $this->setCellContents(1, 0, $message_empty);
  423. $empty_table = true;
  424. }
  425. $html = '';
  426. if (!$empty_table) {
  427. $form = $this->get_page_select_form();
  428. $nav = $this->get_navigation_html();
  429. // @todo This style css must be moved to default.css only for dev
  430. echo '<style>
  431. .main-grid { width:100%;}
  432. .sub-header { width:100%; padding-top: 10px; padding-right: 10px; padding-left: 10px; height:30px;}
  433. .grid_container { width:100%;}
  434. .grid_item { height: 120px; width:98px; float:left; padding:5px; margin:8px;}
  435. .grid_element_0 { width:100px; height: 100px; float:left; text-align:center; margin-bottom:5px;}
  436. .grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
  437. .grid_element_2 { width:150px; float:left;}
  438. .grid_selectbox { width:30%; float:left;}
  439. .grid_title { width:30%; float:left;}
  440. .grid_nav { }
  441. </style>';
  442. // @todo This also must be moved
  443. // Show only navigations if there are more than 1 page
  444. $my_pager = $this->get_pager();
  445. $html .= '<div class="main-grid">';
  446. if ($my_pager->numPages() > 1) {
  447. $html .= '<div class="sub-header">';
  448. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  449. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  450. $html .= '<div class="grid_nav">'.$nav.'</div>';
  451. $html .= '</div>';
  452. }
  453. $html .= '<div class="clear"></div>';
  454. if (count($this->form_actions) > 0) {
  455. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  456. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  457. }
  458. }
  459. // Getting the items of the table
  460. $items = $this->get_clean_html(false); //no sort
  461. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  462. // i.e: .whoisonline_table_grid_container instead of .grid_container
  463. // where whoisonline is the table's name like drupal's template engine
  464. $html .= '<div class="grid_container">';
  465. if (is_array($items) && count($items) > 0) {
  466. foreach ($items as &$row) {
  467. $html .= '<div class="grid_item">';
  468. $i = 0;
  469. foreach ($row as &$element) {
  470. $html .= '<div class="grid_element_'.$i.'">'.$element.'</div>';
  471. $i++;
  472. }
  473. $html .= '</div>';
  474. }
  475. }
  476. $html .= '</div>'; //close grid_container
  477. $html .= '</div>'; //close main grid
  478. $html .= '<div class="clear"></div>';
  479. echo $html;
  480. }
  481. /**
  482. * This function returns the content of a table in a grid
  483. * Should not be use to edit information (edit/delete rows) only.
  484. *
  485. * @param array options of visibility
  486. * @param bool hide navigation optionally
  487. * @param int content per page when show navigation (optional)
  488. * @param bool sort data optionally
  489. *
  490. * @return string grid html
  491. */
  492. public function display_simple_grid(
  493. $visibility_options,
  494. $hide_navigation = true,
  495. $per_page = 20,
  496. $sort_data = true,
  497. $grid_class = []
  498. ) {
  499. $empty_table = false;
  500. if ($this->get_total_number_of_items() == 0) {
  501. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  502. $this->setCellContents(1, 0, $message_empty);
  503. $empty_table = true;
  504. }
  505. $html = '';
  506. if (!$empty_table) {
  507. // If we show the pagination
  508. if (!$hide_navigation) {
  509. $form = '&nbsp;';
  510. if ($this->get_total_number_of_items() > $per_page) {
  511. if ($per_page > 10) {
  512. $form = $this->get_page_select_form();
  513. }
  514. $nav = $this->get_navigation_html();
  515. // This also must be moved
  516. $html = '<div class="sub-header">';
  517. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  518. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  519. $html .= '<div class="grid_nav">'.$nav.'</div>';
  520. $html .= '</div>';
  521. }
  522. }
  523. $html .= '<div class="clear"></div>';
  524. if (count($this->form_actions) > 0) {
  525. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  526. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  527. }
  528. }
  529. if ($hide_navigation) {
  530. $items = $this->table_data; // This is a faster way to get what we want
  531. } else {
  532. // The normal way
  533. $items = $this->get_clean_html($sort_data); // Getting the items of the table
  534. }
  535. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  536. // i.e: .whoisonline_table_grid_container instead of .grid_container
  537. // where whoisonline is the table's name like drupal's template engine
  538. if (is_array($visibility_options)) {
  539. $filter = false; // The 2nd condition of the if will be loaded
  540. } else {
  541. $filter = $visibility_options !== false;
  542. }
  543. $item_css_class = $item_css_style = $grid_css_class = $grid_css_style = '';
  544. if (!empty($grid_class)) {
  545. $grid_css_class = $grid_class['main']['class'];
  546. $item_css_class = $grid_class['item']['class'];
  547. $grid_css_style = isset($grid_class['main']['style']) ? $grid_class['main']['style'] : null;
  548. $item_css_style = isset($grid_class['item']['style']) ? $grid_class['item']['style'] : null;
  549. }
  550. $div = '';
  551. if (is_array($items) && count($items) > 0) {
  552. foreach ($items as &$row) {
  553. $i = 0;
  554. $rows = '';
  555. foreach ($row as &$element) {
  556. if ($filter ||
  557. isset($visibility_options[$i]) && $visibility_options[$i]
  558. ) {
  559. $rows .= '<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
  560. }
  561. $i++;
  562. }
  563. $div .= Display::div(
  564. $rows,
  565. [
  566. 'class' => $item_css_class.' '.$this->table_name.'_grid_item',
  567. 'style' => $item_css_style,
  568. ]
  569. );
  570. }
  571. }
  572. $html .= Display::div(
  573. $div,
  574. [
  575. 'class' => $grid_css_class.' '.$this->table_name.'_grid_container',
  576. 'style' => $grid_css_style,
  577. ]
  578. );
  579. $html .= '<div class="clear"></div>';
  580. return $html;
  581. }
  582. /**
  583. * Get the HTML-code with the navigational buttons to browse through the
  584. * data-pages.
  585. */
  586. public function get_navigation_html()
  587. {
  588. $pager = $this->get_pager();
  589. $pager_links = $pager->getLinks();
  590. $nav = $pager_links['first'].' '.$pager_links['back'];
  591. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  592. $nav .= $pager_links['next'].' '.$pager_links['last'];
  593. return $nav;
  594. }
  595. /**
  596. * Get the HTML-code with the data-table.
  597. */
  598. public function get_table_html()
  599. {
  600. $pager = $this->get_pager();
  601. $offset = $pager->getOffsetByPageId();
  602. $from = $offset[0] - 1;
  603. $table_data = $this->get_table_data($from);
  604. $this->processHeaders();
  605. if (is_array($table_data)) {
  606. $count = 1;
  607. foreach ($table_data as &$row) {
  608. $row = $this->filter_data($row);
  609. $newRow = [];
  610. if (!empty($this->columnsToHide)) {
  611. $counter = 0;
  612. foreach ($row as $index => $rowInfo) {
  613. if (!isset($this->columnsToHide[$index])) {
  614. $newRow[$counter] = $rowInfo;
  615. $counter++;
  616. }
  617. }
  618. $row = $newRow;
  619. }
  620. $this->addRow($row);
  621. if (isset($row['child_of'])) {
  622. $this->setRowAttributes(
  623. $count,
  624. ['class' => 'hidden hidden_'.$row['child_of']],
  625. true
  626. );
  627. }
  628. $count++;
  629. }
  630. }
  631. if ($this->odd_even_rows_enabled == true) {
  632. $this->altRowAttributes(
  633. 0,
  634. ['class' => 'row_odd'],
  635. ['class' => 'row_even'],
  636. true
  637. );
  638. }
  639. foreach ($this->th_attributes as $column => $attributes) {
  640. $this->setCellAttributes(0, $column, $attributes);
  641. }
  642. foreach ($this->td_attributes as $column => $attributes) {
  643. $this->setColAttributes($column, $attributes);
  644. }
  645. return $this->toHTML();
  646. }
  647. /**
  648. * This function return the items of the table.
  649. *
  650. * @param bool true for sorting table data or false otherwise
  651. *
  652. * @return array table row items
  653. */
  654. public function get_clean_html($sort = true)
  655. {
  656. $pager = $this->get_pager();
  657. $offset = $pager->getOffsetByPageId();
  658. $from = $offset[0] - 1;
  659. $table_data = $this->get_table_data($from, null, null, null, $sort);
  660. $new_table_data = [];
  661. if (is_array($table_data)) {
  662. foreach ($table_data as $index => &$row) {
  663. $row = $this->filter_data($row);
  664. $new_table_data[] = $row;
  665. }
  666. }
  667. return $new_table_data;
  668. }
  669. /**
  670. * Get the HTML-code which represents a form to select how many items a page
  671. * should contain.
  672. */
  673. public function get_page_select_form()
  674. {
  675. $total_number_of_items = $this->get_total_number_of_items();
  676. if ($total_number_of_items <= $this->default_items_per_page) {
  677. return '';
  678. }
  679. if ($this->hideItemSelector === true) {
  680. return '';
  681. }
  682. $result[] = '<form method="GET" action="'.api_get_self().'" style="display:inline;">';
  683. $param[$this->param_prefix.'direction'] = $this->direction;
  684. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  685. $param[$this->param_prefix.'column'] = $this->column;
  686. if (is_array($this->additional_parameters)) {
  687. $param = array_merge($param, $this->additional_parameters);
  688. }
  689. foreach ($param as $key => &$value) {
  690. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  691. }
  692. $result[] = '<select style="width: auto;" class="form-control" name="'.$this->param_prefix.'per_page" onchange="javascript: this.form.submit();">';
  693. $list = [10, 20, 50, 100, 500, 1000];
  694. $rowList = api_get_configuration_value('table_row_list');
  695. if (!empty($rowList) && isset($rowList['options'])) {
  696. $list = $rowList['options'];
  697. }
  698. foreach ($list as $nr) {
  699. if ($total_number_of_items <= $nr) {
  700. break;
  701. }
  702. $result[] = '<option value="'.$nr.'" '.($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  703. }
  704. $result[] = '<option value="'.$total_number_of_items.'" '.($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
  705. //}
  706. $result[] = '</select>';
  707. $result[] = '<noscript>';
  708. $result[] = '<button class="btn btn-success" type="submit">'.get_lang('Save').'</button>';
  709. $result[] = '</noscript>';
  710. $result[] = '</form>';
  711. $result = implode("\n", $result);
  712. return $result;
  713. }
  714. /**
  715. * Get the table title.
  716. */
  717. public function get_table_title()
  718. {
  719. $pager = $this->get_pager();
  720. $showed_items = $pager->getOffsetByPageId();
  721. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  722. }
  723. /**
  724. * @return array
  725. */
  726. public function getHeaders()
  727. {
  728. return $this->headers;
  729. }
  730. /**
  731. * Process headers.
  732. */
  733. public function processHeaders()
  734. {
  735. $counter = 0;
  736. foreach ($this->headers as $column => $columnInfo) {
  737. $label = $columnInfo['label'];
  738. $sortable = $columnInfo['sortable'];
  739. $th_attributes = $columnInfo['th_attributes'];
  740. $td_attributes = $columnInfo['td_attributes'];
  741. if (!empty($this->columnsToHide)) {
  742. if (isset($this->columnsToHide[$column])) {
  743. continue;
  744. }
  745. }
  746. $column = $counter;
  747. $param['direction'] = 'ASC';
  748. if ($this->column == $column && $this->direction == 'ASC') {
  749. $param['direction'] = 'DESC';
  750. }
  751. $param['page_nr'] = $this->page_nr;
  752. $param['per_page'] = $this->per_page;
  753. $param['column'] = $column;
  754. $link = $label;
  755. if ($sortable) {
  756. $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
  757. foreach ($param as $key => &$value) {
  758. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  759. }
  760. $link .= $this->get_additional_url_paramstring();
  761. $link .= '">'.$label.'</a>';
  762. if ($this->column == $column) {
  763. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  764. }
  765. }
  766. $this->setHeaderContents(0, $column, $link);
  767. if (!is_null($td_attributes)) {
  768. $this->td_attributes[$column] = $td_attributes;
  769. }
  770. if (!is_null($th_attributes)) {
  771. $this->th_attributes[$column] = $th_attributes;
  772. }
  773. $counter++;
  774. }
  775. }
  776. /**
  777. * Set the header-label.
  778. *
  779. * @param int $column The column number
  780. * @param string $label The label
  781. * @param bool $sortable Is the table sortable by this column? (defatult
  782. * = true)
  783. * @param string $th_attributes Additional attributes for the th-tag of the
  784. * table header
  785. * @param string $td_attributes Additional attributes for the td-tags of the
  786. * column
  787. */
  788. public function set_header(
  789. $column,
  790. $label,
  791. $sortable = true,
  792. $th_attributes = ['class' => 'th-header'],
  793. $td_attributes = null
  794. ) {
  795. $this->headers[$column] = [
  796. 'label' => $label,
  797. 'sortable' => $sortable,
  798. 'th_attributes' => $th_attributes,
  799. 'td_attributes' => $td_attributes,
  800. ];
  801. }
  802. /**
  803. * Get the parameter-string with additional parameters to use in the URLs
  804. * generated by this SortableTable.
  805. */
  806. public function get_additional_url_paramstring()
  807. {
  808. $param_string_parts = [];
  809. if (is_array($this->additional_parameters) && count($this->additional_parameters) > 0) {
  810. foreach ($this->additional_parameters as $key => &$value) {
  811. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  812. }
  813. }
  814. $result = implode('&amp;', $param_string_parts);
  815. foreach ($this->other_tables as $index => &$tablename) {
  816. $param = [];
  817. if (isset($_GET[$tablename.'_direction'])) {
  818. //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  819. $my_get_direction = $_GET[$tablename.'_direction'];
  820. if (!in_array($my_get_direction, ['ASC', 'DESC'])) {
  821. $param[$tablename.'_direction'] = 'ASC';
  822. } else {
  823. $param[$tablename.'_direction'] = $my_get_direction;
  824. }
  825. }
  826. if (isset($_GET[$tablename.'_page_nr'])) {
  827. $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
  828. }
  829. if (isset($_GET[$tablename.'_per_page'])) {
  830. $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
  831. }
  832. if (isset($_GET[$tablename.'_column'])) {
  833. $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
  834. }
  835. $param_string_parts = [];
  836. foreach ($param as $key => &$value) {
  837. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  838. }
  839. if (count($param_string_parts) > 0) {
  840. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  841. }
  842. }
  843. return $result;
  844. }
  845. /**
  846. * Get the parameter-string with the SortableTable-related parameters to use
  847. * in URLs.
  848. */
  849. public function get_sortable_table_param_string()
  850. {
  851. $param[$this->param_prefix.'direction'] = $this->direction;
  852. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  853. $param[$this->param_prefix.'per_page'] = $this->per_page;
  854. $param[$this->param_prefix.'column'] = $this->column;
  855. $param_string_parts = [];
  856. foreach ($param as $key => &$value) {
  857. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  858. }
  859. $res = implode('&amp;', $param_string_parts);
  860. return $res;
  861. }
  862. /**
  863. * Add a filter to a column. If another filter was already defined for the
  864. * given column, it will be overwritten.
  865. *
  866. * @param int $column The number of the column
  867. * @param string $function The name of the filter-function. This should be a
  868. * function wich requires 1 parameter and returns the filtered value.
  869. */
  870. public function set_column_filter($column, $function)
  871. {
  872. $this->column_filters[$column] = $function;
  873. }
  874. /**
  875. * List of columns to hide.
  876. *
  877. * @param int $column
  878. */
  879. public function setHideColumn($column)
  880. {
  881. $this->columnsToHide[$column] = $column;
  882. }
  883. /**
  884. * Define a list of actions which can be performed on the table-date.
  885. * If you define a list of actions, the first column of the table will be
  886. * converted into checkboxes.
  887. *
  888. * @param array $actions A list of actions. The key is the name of the
  889. * action. The value is the label to show in the select-box
  890. * @param string $checkbox_name The name of the generated checkboxes. The
  891. * value of the checkbox will be the value of the first column.
  892. */
  893. public function set_form_actions($actions, $checkbox_name = 'id')
  894. {
  895. $this->form_actions = $actions;
  896. $this->checkbox_name = $checkbox_name;
  897. }
  898. /**
  899. * Define a list of additional parameters to use in the generated URLs
  900. * <code>$parameters['action'] = 'test'; will be convert in
  901. * <input type="hidden" name="action" value="test"></code>.
  902. *
  903. * @param array $parameters
  904. */
  905. public function set_additional_parameters($parameters)
  906. {
  907. $this->additional_parameters = $parameters;
  908. }
  909. /**
  910. * Set other tables on the same page.
  911. * If you have other sortable tables on the page displaying this sortable
  912. * tables, you can define those other tables with this function. If you
  913. * don't define the other tables, there sorting and pagination will return
  914. * to their default state when sorting this table.
  915. *
  916. * @param array $tablenames an array of table names
  917. */
  918. public function set_other_tables($tablenames)
  919. {
  920. $this->other_tables = $tablenames;
  921. }
  922. /**
  923. * Transform all data in a table-row, using the filters defined by the
  924. * function set_column_filter(...) defined elsewhere in this class.
  925. * If you've defined actions, the first element of the given row will be
  926. * converted into a checkbox.
  927. *
  928. * @param array $row a row from the table
  929. *
  930. * @return array
  931. */
  932. public function filter_data($row)
  933. {
  934. $url_params = $this->get_sortable_table_param_string().'&'.$this->get_additional_url_paramstring();
  935. foreach ($this->column_filters as $column => &$function) {
  936. $firstParam = isset($row[$column]) ? $row[$column] : 0;
  937. $row[$column] = call_user_func($function, $firstParam, $url_params, $row);
  938. }
  939. if (count($this->form_actions) > 0) {
  940. if (strlen($row[0]) > 0) {
  941. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  942. if (isset($_GET[$this->param_prefix.'selectall'])) {
  943. $row[0] .= ' checked="checked"';
  944. }
  945. $row[0] .= '/>';
  946. }
  947. }
  948. if (is_array($row)) {
  949. foreach ($row as &$value) {
  950. if (empty($value)) {
  951. $value = '-';
  952. }
  953. }
  954. }
  955. return $row;
  956. }
  957. /**
  958. * Get the total number of items. This function calls the function given as
  959. * 2nd argument in the constructor of a SortableTable. Make sure your
  960. * function has the same parameters as defined here.
  961. */
  962. public function get_total_number_of_items()
  963. {
  964. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
  965. $this->total_number_of_items = call_user_func(
  966. $this->get_total_number_function,
  967. $this->getDataFunctionParams()
  968. );
  969. }
  970. return $this->total_number_of_items;
  971. }
  972. /**
  973. * @param int $value
  974. */
  975. public function setTotalNumberOfItems($value)
  976. {
  977. $this->total_number_of_items = (int) $value;
  978. }
  979. /**
  980. * Get the data to display. This function calls the function given as
  981. * 2nd argument in the constructor of a SortableTable. Make sure your
  982. * function has the same parameters as defined here.
  983. *
  984. * @param int $from index of the first item to return
  985. * @param int $per_page The number of items to return
  986. * @param int $column The number of the column on which the data should be
  987. * @param bool $sort Whether to sort or not
  988. * sorted
  989. * @param string $direction In which order should the data be sorted (ASC
  990. * or DESC)
  991. *
  992. * @return array
  993. */
  994. public function get_table_data(
  995. $from = null,
  996. $per_page = null,
  997. $column = null,
  998. $direction = null,
  999. $sort = null
  1000. ) {
  1001. $data = [];
  1002. if ($this->get_data_function !== null) {
  1003. $data = call_user_func(
  1004. $this->get_data_function,
  1005. $from,
  1006. $this->per_page,
  1007. $this->column,
  1008. $this->direction,
  1009. $this->dataFunctionParams
  1010. );
  1011. }
  1012. return $data;
  1013. }
  1014. /**
  1015. * @param array $data
  1016. */
  1017. public function setTableData($data)
  1018. {
  1019. $this->table_data = $data;
  1020. }
  1021. }