sortable_table.class.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. /**
  12. * This class allows you to display a sortable data-table. It is possible to
  13. * split the data in several pages.
  14. * Using this class you can:
  15. * - automatically create checkboxes of the first table column
  16. * - a "select all" and "deselect all" link is added
  17. * - only if you provide a list of actions for the selected items
  18. * - click on the table header to sort the data
  19. * - choose how many items you see per page
  20. * - navigate through all data-pages
  21. * @package chamilo.library
  22. */
  23. class SortableTable extends HTML_Table
  24. {
  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. * @param int $table_id
  116. */
  117. public function __construct(
  118. $table_name = 'table',
  119. $get_total_number_function = null,
  120. $get_data_function = null,
  121. $default_column = 1,
  122. $default_items_per_page = 20,
  123. $default_order_direction = 'ASC',
  124. $table_id = null
  125. ) {
  126. if (empty($table_id)) {
  127. $table_id = $table_name.uniqid();
  128. }
  129. $this->table_id = $table_id;
  130. parent :: __construct(array( 'id' => $table_id, 'class' => 'table table-striped table-hover table-bordered'));
  131. $this->table_name = $table_name;
  132. $this->additional_parameters = array ();
  133. $this->param_prefix = $table_name.'_';
  134. $this->page_nr = isset($_SESSION[$this->param_prefix.'page_nr']) ? intval($_SESSION[$this->param_prefix.'page_nr']) : 1;
  135. $this->page_nr = isset($_GET[$this->param_prefix.'page_nr']) ? intval($_GET[$this->param_prefix.'page_nr']) : $this->page_nr;
  136. $this->column = isset($_SESSION[$this->param_prefix.'column']) ? intval($_SESSION[$this->param_prefix.'column']) : $default_column;
  137. $this->column = isset($_GET[$this->param_prefix.'column']) ? intval($_GET[$this->param_prefix.'column']) : $this->column;
  138. // Default direction
  139. if (in_array(strtoupper($default_order_direction), array('ASC', 'DESC'))) {
  140. $this->direction = $default_order_direction;
  141. }
  142. if (isset($_SESSION[$this->param_prefix.'direction'])) {
  143. $my_session_direction = $_SESSION[$this->param_prefix.'direction'];
  144. if (!in_array($my_session_direction, array('ASC', 'DESC'))) {
  145. $this->direction = 'ASC';
  146. } else {
  147. if ($my_session_direction == 'ASC') {
  148. $this->direction = 'ASC';
  149. } elseif ($my_session_direction == 'DESC') {
  150. $this->direction = 'DESC';
  151. }
  152. }
  153. }
  154. if (isset($_GET[$this->param_prefix.'direction'])) {
  155. $my_get_direction = $_GET[$this->param_prefix.'direction'];
  156. if (!in_array($my_get_direction, array('ASC', 'DESC'))) {
  157. $this->direction = 'ASC';
  158. } else {
  159. if ($my_get_direction == 'ASC') {
  160. $this->direction = 'ASC';
  161. } elseif ($my_get_direction == 'DESC') {
  162. $this->direction = 'DESC';
  163. }
  164. }
  165. }
  166. // Allow to change paginate in multiples tabs
  167. unset($_SESSION[$this->param_prefix.'per_page']);
  168. $this->per_page = isset ($_SESSION[$this->param_prefix.'per_page']) ? intval($_SESSION[$this->param_prefix.'per_page']) : $default_items_per_page;
  169. $this->per_page = isset ($_GET[$this->param_prefix.'per_page']) ? intval($_GET[$this->param_prefix.'per_page']) : $this->per_page;
  170. $_SESSION[$this->param_prefix.'per_page'] = $this->per_page;
  171. $_SESSION[$this->param_prefix.'direction'] = $this->direction ;
  172. $_SESSION[$this->param_prefix.'page_nr'] = $this->page_nr;
  173. $_SESSION[$this->param_prefix.'column'] = $this->column;
  174. $this->pager = null;
  175. $this->default_items_per_page = $default_items_per_page;
  176. $this->total_number_of_items = -1;
  177. $this->get_total_number_function = $get_total_number_function;
  178. $this->get_data_function = $get_data_function;
  179. $this->column_filters = array();
  180. $this->form_actions = array();
  181. $this->checkbox_name = null;
  182. $this->td_attributes = array();
  183. $this->th_attributes = array();
  184. $this->other_tables = array();
  185. }
  186. /**
  187. * Get the Pager object to split the showed data in several pages
  188. */
  189. public function get_pager()
  190. {
  191. if (is_null($this->pager)) {
  192. $total_number_of_items = $this->get_total_number_of_items();
  193. $params['mode'] = 'Sliding';
  194. $params['perPage'] = $this->per_page;
  195. $params['totalItems'] = $total_number_of_items;
  196. $params['urlVar'] = $this->param_prefix.'page_nr';
  197. $params['currentPage'] = $this->page_nr;
  198. $icon_attributes = array('style' => 'vertical-align: middle;');
  199. $params['prevImg'] = Display :: return_icon('action_prev.png', get_lang('PreviousPage'), $icon_attributes);
  200. $params['nextImg'] = Display :: return_icon('action_next.png', get_lang('NextPage'), $icon_attributes);
  201. $params['firstPageText'] = Display :: return_icon('action_first.png', get_lang('FirstPage'), $icon_attributes);
  202. $params['lastPageText'] = Display :: return_icon('action_last.png', get_lang('LastPage'), $icon_attributes);
  203. $params['firstPagePre'] = '';
  204. $params['lastPagePre'] = '';
  205. $params['firstPagePost'] = '';
  206. $params['lastPagePost'] = '';
  207. $params['spacesBeforeSeparator'] = '';
  208. $params['spacesAfterSeparator'] = '';
  209. $query_vars = array_keys($_GET);
  210. $query_vars_needed = array ($this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page');
  211. if (count($this->additional_parameters) > 0) {
  212. $query_vars_needed = array_merge($query_vars_needed, array_keys($this->additional_parameters));
  213. }
  214. $query_vars_exclude = array_diff($query_vars, $query_vars_needed);
  215. $params['excludeVars'] = $query_vars_exclude;
  216. $this->pager = & Pager::factory($params);
  217. }
  218. return $this->pager;
  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 = '<table class="data_table_pagination">';
  247. $html .= '<tr>';
  248. $html .= '<td style="width:25%;">';
  249. $html .= $form;
  250. $html .= '</td>';
  251. $html .= '<td style="text-align:center;">';
  252. $html .= $this->get_table_title();
  253. $html .= '</td>';
  254. $html .= '<td style="text-align:right;width:25%;">';
  255. $html .= $nav;
  256. $html .= '</td>';
  257. $html .= '</tr>';
  258. $html .= '</table>';
  259. }
  260. if (count($this->form_actions) > 0) {
  261. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  262. $html .= '<form id ="'.$table_id.'" class="form-search" method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  263. }
  264. }
  265. $html .= $content;
  266. if (!$empty_table) {
  267. if (!empty($this->additional_parameters)) {
  268. foreach($this->additional_parameters as $key => $value) {
  269. $html .= '<input type="hidden" name ="'.Security::remove_XSS($key).'" value ="'.Security::remove_XSS($value).'" />';
  270. }
  271. }
  272. $html .= '<input type="hidden" name="action">';
  273. $html .= '<table style="width:100%;">';
  274. $html .= '<tr>';
  275. $html .= '<td>';
  276. // Actions in table
  277. if (count($this->form_actions) > 0) {
  278. $html .= '<div class="btn-toolbar">';
  279. $html .= '<div class="btn-group">';
  280. $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>';
  281. $html .= '<a class="btn btn-default" href="?'.$params.'" onclick="javascript: setCheckbox(false, \''.$table_id.'\'); return false;">'.get_lang('UnSelectAll').'</a> ';
  282. $html .= '</div>';
  283. $html .= '<div class="btn-group">
  284. <button class="btn btn-default" onclick="javascript:return false;">'.get_lang('Actions').'</button>
  285. <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  286. <span class="caret"></span>
  287. </button>';
  288. $html .= '<ul class="dropdown-menu">';
  289. foreach ($this->form_actions as $action => & $label) {
  290. $html .= '<li>
  291. <a data-action ="'.$action.'" href="#" onclick="javascript:action_click(this, \''.$table_id.'\');">'.$label.'</a>
  292. </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. 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 || $visibility_options[$i]) {
  493. $rows .= '<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
  494. }
  495. $i++;
  496. }
  497. $div .= Display::div($rows, array('class'=>$item_css_class.' '.$this->table_name.'_grid_item', 'style' => $item_css_style));
  498. }
  499. }
  500. $html .= Display::div($div, array('class'=>$grid_css_class.' '.$this->table_name.'_grid_container', 'style' => $grid_css_style));
  501. $html .= '<div class="clear"></div>';
  502. return $html;
  503. }
  504. /**
  505. * Get the HTML-code with the navigational buttons to browse through the
  506. * data-pages.
  507. */
  508. public function get_navigation_html()
  509. {
  510. $pager = $this->get_pager();
  511. $pager_links = $pager->getLinks();
  512. $nav = '<div class="btn-toolbar" role="toolbar">';
  513. $nav .= $pager_links['first'].' '.$pager_links['back'];
  514. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  515. $nav .= $pager_links['next'].' '.$pager_links['last'];
  516. $nav .= '</div>';
  517. return $nav;
  518. }
  519. /**
  520. * Get the HTML-code with the data-table.
  521. */
  522. public function get_table_html() {
  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. $this->addRow($row);
  532. if (isset($row['child_of'])) {
  533. $this->setRowAttributes($count, array('class' => 'hidden hidden_'.$row['child_of']), true);
  534. }
  535. $count++;
  536. }
  537. }
  538. if ($this->odd_even_rows_enabled == true) {
  539. $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
  540. }
  541. foreach ($this->th_attributes as $column => $attributes) {
  542. $this->setCellAttributes(0, $column, $attributes);
  543. }
  544. foreach ($this->td_attributes as $column => $attributes) {
  545. $this->setColAttributes($column, $attributes);
  546. }
  547. return $this->toHTML();
  548. }
  549. /**
  550. * This function return the items of the table
  551. * @param bool true for sorting table data or false otherwise
  552. * @return array table row items
  553. */
  554. public function get_clean_html($sort = true)
  555. {
  556. $pager = $this->get_pager();
  557. $offset = $pager->getOffsetByPageId();
  558. $from = $offset[0] - 1;
  559. $table_data = $this->get_table_data($from, null, null, null, $sort);
  560. $new_table_data = array();
  561. if (is_array($table_data)) {
  562. foreach ($table_data as & $row) {
  563. $row = $this->filter_data($row);
  564. $new_table_data[] = $row;
  565. }
  566. }
  567. return $new_table_data;
  568. }
  569. /**
  570. * Get the HTML-code wich represents a form to select how many items a page
  571. * should contain.
  572. */
  573. public function get_page_select_form ()
  574. {
  575. $total_number_of_items = $this->get_total_number_of_items();
  576. if ($total_number_of_items <= $this->default_items_per_page) {
  577. return '';
  578. }
  579. $result[] = '<form method="GET" action="'.api_get_self().'" style="display:inline;">';
  580. $param[$this->param_prefix.'direction'] = $this->direction;
  581. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  582. $param[$this->param_prefix.'column'] = $this->column;
  583. if (is_array($this->additional_parameters)) {
  584. $param = array_merge($param, $this->additional_parameters);
  585. }
  586. foreach ($param as $key => & $value) {
  587. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  588. }
  589. $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript: this.form.submit();">';
  590. for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) {
  591. $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  592. }
  593. // @todo no limits
  594. //if ($total_number_of_items < 500) {
  595. $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
  596. //}
  597. $result[] = '</select>';
  598. $result[] = '<noscript>';
  599. $result[] = '<button class="btn save" type="submit">'.get_lang('Save').'</button>';
  600. $result[] = '</noscript>';
  601. $result[] = '</form>';
  602. $result = implode("\n", $result);
  603. return $result;
  604. }
  605. /**
  606. * Get the table title.
  607. */
  608. public function get_table_title()
  609. {
  610. $pager = $this->get_pager();
  611. $showed_items = $pager->getOffsetByPageId();
  612. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  613. }
  614. /**
  615. * Set the header-label
  616. * @param int $column The column number
  617. * @param string $label The label
  618. * @param boolean $sortable Is the table sortable by this column? (defatult
  619. * = true)
  620. * @param string $th_attributes Additional attributes for the th-tag of the
  621. * table header
  622. * @param string $td_attributes Additional attributes for the td-tags of the
  623. * column
  624. */
  625. public function set_header($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null)
  626. {
  627. $param['direction'] = 'ASC';
  628. if ($this->column == $column && $this->direction == 'ASC') {
  629. $param['direction'] = 'DESC';
  630. }
  631. $param['page_nr'] = $this->page_nr;
  632. $param['per_page'] = $this->per_page;
  633. $param['column'] = $column;
  634. if ($sortable) {
  635. $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
  636. foreach ($param as $key => & $value) {
  637. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  638. }
  639. $link .= $this->get_additional_url_paramstring();
  640. $link .= '">'.$label.'</a>';
  641. if ($this->column == $column) {
  642. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  643. }
  644. } else {
  645. $link = $label;
  646. }
  647. $this->setHeaderContents(0, $column, $link);
  648. if (!is_null($td_attributes)) {
  649. $this->td_attributes[$column] = $td_attributes;
  650. }
  651. if (!is_null($th_attributes)) {
  652. $this->th_attributes[$column] = $th_attributes;
  653. }
  654. }
  655. /**
  656. * Get the parameter-string with additional parameters to use in the URLs
  657. * generated by this SortableTable
  658. */
  659. public function get_additional_url_paramstring()
  660. {
  661. $param_string_parts = array ();
  662. if (is_array($this->additional_parameters) && count($this->additional_parameters) > 0) {
  663. foreach ($this->additional_parameters as $key => & $value) {
  664. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  665. }
  666. }
  667. $result = implode('&amp;', $param_string_parts);
  668. foreach ($this->other_tables as & $tablename) {
  669. $param = array();
  670. if (isset($_GET[$tablename.'_direction'])) {
  671. //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  672. $my_get_direction = $_GET[$tablename.'_direction'];
  673. if (!in_array($my_get_direction, array('ASC', 'DESC'))) {
  674. $param[$tablename.'_direction'] = 'ASC';
  675. } else {
  676. $param[$tablename.'_direction'] = $my_get_direction;
  677. }
  678. }
  679. if (isset($_GET[$tablename.'_page_nr'])) {
  680. $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
  681. }
  682. if (isset($_GET[$tablename.'_per_page'])) {
  683. $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
  684. }
  685. if (isset($_GET[$tablename.'_column'])) {
  686. $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
  687. }
  688. $param_string_parts = array ();
  689. foreach ($param as $key => & $value) {
  690. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  691. }
  692. if (count($param_string_parts) > 0)
  693. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  694. }
  695. return $result;
  696. }
  697. /**
  698. * Get the parameter-string with the SortableTable-related parameters to use
  699. * in URLs
  700. */
  701. public function get_sortable_table_param_string()
  702. {
  703. $param[$this->param_prefix.'direction'] = $this->direction;
  704. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  705. $param[$this->param_prefix.'per_page'] = $this->per_page;
  706. $param[$this->param_prefix.'column'] = $this->column;
  707. $param_string_parts = array ();
  708. foreach ($param as $key => & $value) {
  709. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  710. }
  711. $res = implode('&amp;', $param_string_parts);
  712. return $res;
  713. }
  714. /**
  715. * Add a filter to a column. If another filter was allready defined for the
  716. * given column, it will be overwritten.
  717. * @param int $column The number of the column
  718. * @param string $function The name of the filter-function. This should be a
  719. * function wich requires 1 parameter and returns the filtered value.
  720. */
  721. public function set_column_filter($column, $function)
  722. {
  723. $this->column_filters[$column] = $function;
  724. }
  725. /**
  726. * Define a list of actions which can be performed on the table-date.
  727. * If you define a list of actions, the first column of the table will be
  728. * converted into checkboxes.
  729. * @param array $actions A list of actions. The key is the name of the
  730. * action. The value is the label to show in the select-box
  731. * @param string $checkbox_name The name of the generated checkboxes. The
  732. * value of the checkbox will be the value of the first column.
  733. */
  734. public function set_form_actions($actions, $checkbox_name = 'id')
  735. {
  736. $this->form_actions = $actions;
  737. $this->checkbox_name = $checkbox_name;
  738. }
  739. /**
  740. * Define a list of additional parameters to use in the generated URLs
  741. * <code>$parameters['action'] = 'test'; will be convert in <input type="hidden" name="action" value="test"></code>
  742. * @param array $parameters
  743. */
  744. public function set_additional_parameters($parameters)
  745. {
  746. $this->additional_parameters = $parameters;
  747. }
  748. /**
  749. * Set other tables on the same page.
  750. * If you have other sortable tables on the page displaying this sortable
  751. * tables, you can define those other tables with this function. If you
  752. * don't define the other tables, there sorting and pagination will return
  753. * to their default state when sorting this table.
  754. * @param array $tablenames An array of table names.
  755. */
  756. public function set_other_tables($tablenames)
  757. {
  758. $this->other_tables = $tablenames;
  759. }
  760. /**
  761. * Transform all data in a table-row, using the filters defined by the
  762. * function set_column_filter(...) defined elsewhere in this class.
  763. * If you've defined actions, the first element of the given row will be
  764. * converted into a checkbox
  765. * @param array $row A row from the table.
  766. */
  767. public function filter_data($row)
  768. {
  769. $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  770. foreach ($this->column_filters as $column => & $function) {
  771. $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
  772. }
  773. if (count($this->form_actions) > 0) {
  774. if (strlen($row[0]) > 0) {
  775. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  776. if (isset($_GET[$this->param_prefix.'selectall'])) {
  777. $row[0] .= ' checked="checked"';
  778. }
  779. $row[0] .= '/>';
  780. }
  781. }
  782. foreach ($row as $index => & $value) {
  783. if (empty($value)) {
  784. $value = '-';
  785. }
  786. }
  787. return $row;
  788. }
  789. /**
  790. * Get the total number of items. This function calls the function given as
  791. * 2nd argument in the constructor of a SortableTable. Make sure your
  792. * function has the same parameters as defined here.
  793. */
  794. public function get_total_number_of_items()
  795. {
  796. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
  797. $this->total_number_of_items = call_user_func($this->get_total_number_function);
  798. }
  799. return $this->total_number_of_items;
  800. }
  801. /**
  802. * Get the data to display. This function calls the function given as
  803. * 2nd argument in the constructor of a SortableTable. Make sure your
  804. * function has the same parameters as defined here.
  805. * @param int $from Index of the first item to return.
  806. * @param int $per_page The number of items to return
  807. * @param int $column The number of the column on which the data should be
  808. * @param bool $sort Whether to sort or not
  809. * sorted
  810. * @param string $direction In which order should the data be sorted (ASC
  811. * or DESC)
  812. */
  813. public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null, $sort = null)
  814. {
  815. if (!is_null($this->get_data_function)) {
  816. return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
  817. }
  818. return array ();
  819. }
  820. }
  821. /**
  822. * Sortable table which can be used for data available in an array
  823. * @package chamilo.library
  824. */
  825. class SortableTableFromArray extends SortableTable
  826. {
  827. /**
  828. * The array containing all data for this table
  829. */
  830. public $table_data;
  831. /**
  832. * Constructor
  833. * @param array $table_data
  834. * @param int $default_column
  835. * @param int $default_items_per_page
  836. */
  837. public function __construct($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename')
  838. {
  839. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
  840. $this->table_data = $table_data;
  841. }
  842. /**
  843. * Get table data to show on current page
  844. * @see SortableTable#get_table_data
  845. */
  846. public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = true)
  847. {
  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. return count($this->table_data);
  862. }
  863. }
  864. /**
  865. * Sortable table which can be used for data available in an array
  866. *
  867. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  868. * $column_show is an array that lets us decide which are going to be the columns to show
  869. * $column_order is an array that lets us decide the ordering of the columns
  870. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  871. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  872. * @package chamilo.library
  873. */
  874. class SortableTableFromArrayConfig extends SortableTable
  875. {
  876. /**
  877. * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
  878. */
  879. private $column_show;
  880. /**
  881. *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  882. */
  883. private $column_order;
  884. /**
  885. * The array containing all data for this table
  886. */
  887. private $table_data;
  888. private $doc_filter;
  889. /**
  890. * Constructor
  891. * @param array $table_data All the information of the table
  892. * @param int $default_column Default column that will be use in the sorts functions
  893. * @param int $default_items_per_page quantity of pages that we are going to see
  894. * @param int $tablename Name of the table
  895. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  896. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  897. * @param bool special modification to fix the document name order
  898. */
  899. 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)
  900. {
  901. $this->column_show = $column_show;
  902. $this->column_order = $column_order;
  903. $this->doc_filter = $doc_filter;
  904. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page, $direction);
  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. $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);
  914. return array_slice($content, $from, $this->per_page);
  915. }
  916. /**
  917. * Get total number of items
  918. * @see SortableTable#get_total_number_of_items
  919. */
  920. public function get_total_number_of_items()
  921. {
  922. return count($this->table_data);
  923. }
  924. }