sortable_table.class.php 42 KB

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