sortable_table.class.php 43 KB

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