sortable_table.class.php 43 KB

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