sortable_table.class.php 43 KB

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