sortable_table.class.php 42 KB

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