sortable_table.class.php 43 KB

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