sortable_table.class.php 42 KB

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