sortable_table.class.php 43 KB

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