sortable_table.class.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. echo $this->return_table();
  212. }
  213. /**
  214. * Displays the table, complete with navigation buttons to browse through
  215. * the data-pages.
  216. */
  217. public function return_table() {
  218. $empty_table = false;
  219. $content = $this->get_table_html();
  220. if ($this->get_total_number_of_items() == 0) {
  221. $cols = $this->getColCount();
  222. $this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  223. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  224. $this->setCellContents(1, 0, $message_empty);
  225. $empty_table = true;
  226. }
  227. $html = '';
  228. if (!$empty_table) {
  229. $table_id = 'form_'.$this->table_name.'_id';
  230. $form = $this->get_page_select_form();
  231. $nav = $this->get_navigation_html();
  232. //Only show pagination info when there are items to paginate
  233. if ($this->get_total_number_of_items() > $this->default_items_per_page) {
  234. $html = '<table class="data_table_pagination">';
  235. $html .= '<tr>';
  236. $html .= '<td style="width:25%;">';
  237. $html .= $form;
  238. $html .= '</td>';
  239. $html .= '<td style="text-align:center;">';
  240. $html .= $this->get_table_title();
  241. $html .= '</td>';
  242. $html .= '<td style="text-align:right;width:25%;">';
  243. $html .= $nav;
  244. $html .= '</td>';
  245. $html .= '</tr>';
  246. $html .= '</table>';
  247. }
  248. if (count($this->form_actions) > 0) {
  249. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  250. $html .= '<form id ="'.$table_id.'" class="form-search" method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  251. }
  252. }
  253. $html .= $content;
  254. if (!$empty_table) {
  255. if (!empty($this->additional_parameters)) {
  256. foreach($this->additional_parameters as $key => $value) {
  257. $html .= '<input type="hidden" name ="'.Security::remove_XSS($key).'" value ="'.Security::remove_XSS($value).'" />';
  258. }
  259. }
  260. $html .= '<input type="hidden" name="action">';
  261. $html .= '<table style="width:100%;">';
  262. $html .= '<tr>';
  263. $html .= '<td>';
  264. if (count($this->form_actions) > 0) {
  265. $html .= '<div class="btn-toolbar">';
  266. $html .= '<div class="btn-group">';
  267. $html .= '<a class="btn" href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript: setCheckbox(true, \''.$table_id.'\'); return false;">'.get_lang('SelectAll').'</a>';
  268. $html .= '<a class="btn" href="?'.$params.'" onclick="javascript: setCheckbox(false, \''.$table_id.'\'); return false;">'.get_lang('UnSelectAll').'</a> ';
  269. $html .= '</div>';
  270. $html .= '<div class="btn-group">
  271. <button class="btn" onclick="javascript:return false;">'.get_lang('Actions').'</button>
  272. <button class="btn dropdown-toggle" data-toggle="dropdown">
  273. <span class="caret"></span>
  274. </button>';
  275. $html .= '<ul class="dropdown-menu">';
  276. foreach ($this->form_actions as $action => & $label) {
  277. $html .= '<li><a data-action ="'.$action.'" href="#" onclick="javascript:action_click(this, \''.$table_id.'\');">'.$label.'</a></li>';
  278. }
  279. $html .= '</ul>';
  280. $html .= '</div>';//btn-group
  281. $html .= '</div>'; //toolbar
  282. } else {
  283. $html .= $form;
  284. }
  285. $html .= '</td>';
  286. if ($this->get_total_number_of_items() > $this->default_items_per_page) {
  287. $html .= '<td style="text-align:right;">';
  288. $html .= $nav;
  289. $html .= '</td>';
  290. }else{
  291. $html .= '<td> ';
  292. $html .= '</td>';
  293. }
  294. $html .= '</tr>';
  295. $html .= '</table>';
  296. if (count($this->form_actions) > 0) {
  297. $html .= '</form>';
  298. }
  299. }
  300. return $html;
  301. }
  302. /**
  303. * This function shows the content of a table in a grid.
  304. * Should not be use to edit information (edit/delete rows) only.
  305. * */
  306. public function display_grid() {
  307. $empty_table = false;
  308. if ($this->get_total_number_of_items() == 0) {
  309. $cols = $this->getColCount();
  310. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  311. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  312. $this->setCellContents(1, 0, $message_empty);
  313. $empty_table = true;
  314. }
  315. $html = '';
  316. if (!$empty_table) {
  317. $form = $this->get_page_select_form();
  318. $nav = $this->get_navigation_html();
  319. // @todo This style css must be moved to default.css only for dev
  320. echo '<style>
  321. .main-grid { width:100%;}
  322. .sub-header { width:100%; padding-top: 10px; padding-right: 10px; padding-left: 10px; height:30px;}
  323. .grid_container { width:100%;}
  324. .grid_item { height: 120px; width:98px; float:left; padding:5px; margin:8px;}
  325. .grid_element_0 { width:100px; height: 100px; float:left; text-align:center; margin-bottom:5px;}
  326. .grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
  327. .grid_element_2 { width:150px; float:left;}
  328. .grid_selectbox { width:30%; float:left;}
  329. .grid_title { width:30%; float:left;}
  330. .grid_nav { }
  331. </style>';
  332. // @todo This also must be moved
  333. // Show only navigations if there are more than 1 page
  334. $my_pager = $this->get_pager();
  335. $html .= '<div class="main-grid">';
  336. if ($my_pager->numPages() > 1) {
  337. $html .= '<div class="sub-header">';
  338. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  339. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  340. $html .= '<div class="grid_nav">'.$nav.'</div>';
  341. $html .= '</div>';
  342. }
  343. $html .= '<div class="clear"></div>';
  344. if (count($this->form_actions) > 0) {
  345. $script= '<script>
  346. /*<![CDATA[*/
  347. function setCheckbox(value) {
  348. d = document.form_'.$this->table_name.';
  349. for (i = 0; i < d.elements.length; i++) {
  350. if (d.elements[i].type == "checkbox") {
  351. d.elements[i].checked = value;
  352. }
  353. if (value) {
  354. $(d.elements[i]).parentsUntil("tr").parent().addClass("row_selected");
  355. } else {
  356. $(d.elements[i]).parentsUntil("tr").parent().removeClass("row_selected");
  357. }
  358. }
  359. }
  360. /*]]>*/
  361. </script>';
  362. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  363. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  364. }
  365. }
  366. // Getting the items of the table
  367. $items = $this->get_clean_html(false); //no sort
  368. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  369. // i.e: .whoisonline_table_grid_container instead of .grid_container
  370. // where whoisonline is the table's name like drupal's template engine
  371. $html .= '<div class="grid_container">';
  372. if (is_array($items) && count($items) > 0) {
  373. foreach ($items as & $row) {
  374. $html .= '<div class="grid_item">';
  375. $i = 0;
  376. foreach ($row as & $element) {
  377. $html .= '<div class="grid_element_'.$i.'">'.$element.'</div>';
  378. $i++;
  379. }
  380. $html .= '</div>';
  381. }
  382. }
  383. $html .= '</div>'; //close grid_container
  384. $html .= '</div>'; //close main grid
  385. $html .= '<div class="clear"></div>';
  386. /*
  387. if (!$empty_table) {
  388. $html .= '<table style="width:100%;">';
  389. $html .= '<tr>';
  390. $html .= '<td colspan="2">';
  391. if (count($this->form_actions) > 0) {
  392. $html .= '<br />';
  393. $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
  394. $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
  395. $html .= '<select name="action">';
  396. foreach ($this->form_actions as $action => $label) {
  397. $html .= '<option value="'.$action.'">'.$label.'</option>';
  398. }
  399. $html .= '</select>';
  400. $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>';
  401. } else {
  402. $html .= $form;
  403. }
  404. $html .= '</td>';
  405. $html .= '<td style="text-align:right;">';
  406. $html .= $nav;
  407. $html .= '</td>';
  408. $html .= '</tr>';
  409. $html .= '</div>';
  410. if (count($this->form_actions) > 0) {
  411. $html .= '</form>';
  412. }
  413. }
  414. */
  415. echo $html;
  416. }
  417. /**
  418. * This function returns the content of a table in a grid
  419. * Should not be use to edit information (edit/delete rows) only.
  420. * @param array options of visibility
  421. * @param bool hide navigation optionally
  422. * @param int content per page when show navigation (optional)
  423. * @param bool sort data optionally
  424. * @return string grid html
  425. */
  426. public function display_simple_grid($visibility_options, $hide_navigation = true, $per_page = 20, $sort_data = true, $grid_class = array()) {
  427. $empty_table = false;
  428. if ($this->get_total_number_of_items() == 0) {
  429. $cols = $this->getColCount();
  430. //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
  431. $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty'));
  432. $this->setCellContents(1, 0, $message_empty);
  433. $empty_table = true;
  434. }
  435. $html = '';
  436. if (!$empty_table) {
  437. // If we show the pagination
  438. if (!$hide_navigation) {
  439. $form = '&nbsp;';
  440. if ($this->get_total_number_of_items() > $per_page) {
  441. if ($per_page > 10) {
  442. $form = $this->get_page_select_form();
  443. }
  444. $nav = $this->get_navigation_html();
  445. // This also must be moved
  446. $html = '<div class="sub-header">';
  447. $html .= '<div class="grid_selectbox">'.$form.'</div>';
  448. $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
  449. $html .= '<div class="grid_nav">'.$nav.'</div>';
  450. $html .= '</div>';
  451. }
  452. }
  453. $html .= '<div class="clear"></div>';
  454. if (count($this->form_actions) > 0) {
  455. $script= '<script>
  456. /*<![CDATA[*/
  457. function setCheckbox(value) {
  458. d = document.form_'.$this->table_name.';
  459. for (i = 0; i < d.elements.length; i++) {
  460. if (d.elements[i].type == "checkbox") {
  461. d.elements[i].checked = value;
  462. }
  463. if (value) {
  464. $(d.elements[i]).parentsUntil("tr").parent().addClass("row_selected");
  465. } else {
  466. $(d.elements[i]).parentsUntil("tr").parent().removeClass("row_selected");
  467. }
  468. }
  469. }
  470. /*]]>*/
  471. </script>';
  472. $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  473. $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
  474. }
  475. }
  476. if ($hide_navigation) {
  477. $items = $this->table_data; // This is a faster way to get what we want
  478. } else {
  479. // The normal way
  480. $items = $this->get_clean_html($sort_data); // Getting the items of the table
  481. }
  482. // Generation of style classes must be improved. Maybe we need a a table name to create style on the fly:
  483. // i.e: .whoisonline_table_grid_container instead of .grid_container
  484. // where whoisonline is the table's name like drupal's template engine
  485. if (is_array($visibility_options)) {
  486. $filter = false; // The 2nd condition of the if will be loaded
  487. } else {
  488. $filter = $visibility_options !== false;
  489. }
  490. $item_css_class = $item_css_style = $grid_css_class = $grid_css_style = '';
  491. if (!empty($grid_class)) {
  492. $grid_css_class = $grid_class['main']['class'];
  493. $item_css_class = $grid_class['item']['class'];
  494. $grid_css_style = isset($grid_class['main']['style']) ? $grid_class['main']['style'] : null;
  495. $item_css_style = isset($grid_class['item']['style']) ? $grid_class['item']['style'] : null;
  496. }
  497. $div = '';
  498. if (is_array($items) && count($items) > 0) {
  499. foreach ($items as & $row) {
  500. $i = 0;
  501. $rows = '';
  502. foreach ($row as & $element) {
  503. if ($filter || $visibility_options[$i]) {
  504. $rows .= '<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
  505. }
  506. $i++;
  507. }
  508. $div .= Display::div($rows, array('class'=>$item_css_class.' '.$this->table_name.'_grid_item', 'style' => $item_css_style));
  509. }
  510. }
  511. $html .= Display::div($div, array('class'=>$grid_css_class.' '.$this->table_name.'_grid_container', 'style' => $grid_css_style));
  512. $html .= '<div class="clear"></div>';
  513. return $html;
  514. }
  515. /**
  516. * Get the HTML-code with the navigational buttons to browse through the
  517. * data-pages.
  518. */
  519. public function get_navigation_html () {
  520. $pager = $this->get_pager();
  521. $pager_links = $pager->getLinks();
  522. $nav = $pager_links['first'].' '.$pager_links['back'];
  523. $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
  524. $nav .= $pager_links['next'].' '.$pager_links['last'];
  525. return $nav;
  526. }
  527. /**
  528. * Get the HTML-code with the data-table.
  529. */
  530. public function get_table_html() {
  531. $pager = $this->get_pager();
  532. $offset = $pager->getOffsetByPageId();
  533. $from = $offset[0] - 1;
  534. $table_data = $this->get_table_data($from);
  535. if (is_array($table_data)) {
  536. $count = 1;
  537. foreach ($table_data as & $row) {
  538. $row = $this->filter_data($row);
  539. $this->addRow($row);
  540. if (isset($row['child_of'])) {
  541. $this->setRowAttributes($count, array('class' => 'hidden hidden_'.$row['child_of']), true);
  542. }
  543. $count++;
  544. }
  545. }
  546. if ($this->odd_even_rows_enabled == true) {
  547. $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
  548. }
  549. foreach ($this->th_attributes as $column => $attributes) {
  550. $this->setCellAttributes(0, $column, $attributes);
  551. }
  552. foreach ($this->td_attributes as $column => $attributes) {
  553. $this->setColAttributes($column, $attributes);
  554. }
  555. return $this->toHTML();
  556. }
  557. /**
  558. * This function return the items of the table
  559. * @param bool true for sorting table data or false otherwise
  560. * @return array table row items
  561. */
  562. public function get_clean_html($sort = true) {
  563. $pager = $this->get_pager();
  564. $offset = $pager->getOffsetByPageId();
  565. $from = $offset[0] - 1;
  566. $table_data = $this->get_table_data($from, null, null, null, $sort);
  567. $new_table_data = array();
  568. if (is_array($table_data)) {
  569. foreach ($table_data as $index => & $row) {
  570. $row = $this->filter_data($row);
  571. $new_table_data[] = $row;
  572. }
  573. }
  574. return $new_table_data;
  575. }
  576. /**
  577. * Get the HTML-code wich represents a form to select how many items a page
  578. * should contain.
  579. */
  580. public function get_page_select_form () {
  581. $total_number_of_items = $this->get_total_number_of_items();
  582. if ($total_number_of_items <= $this->default_items_per_page) {
  583. return '';
  584. }
  585. $result[] = '<form method="GET" action="'.api_get_self().'" style="display:inline;">';
  586. $param[$this->param_prefix.'direction'] = $this->direction;
  587. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  588. $param[$this->param_prefix.'column'] = $this->column;
  589. if (is_array($this->additional_parameters)) {
  590. $param = array_merge($param, $this->additional_parameters);
  591. }
  592. foreach ($param as $key => & $value) {
  593. $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  594. }
  595. $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript: this.form.submit();">';
  596. for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) {
  597. $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
  598. }
  599. // @todo no limits
  600. //if ($total_number_of_items < 500) {
  601. $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
  602. //}
  603. $result[] = '</select>';
  604. $result[] = '<noscript>';
  605. $result[] = '<button class="btn save" type="submit">'.get_lang('Save').'</button>';
  606. $result[] = '</noscript>';
  607. $result[] = '</form>';
  608. $result = implode("\n", $result);
  609. return $result;
  610. }
  611. /**
  612. * Get the table title.
  613. */
  614. public function get_table_title () {
  615. $pager = $this->get_pager();
  616. $showed_items = $pager->getOffsetByPageId();
  617. return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
  618. }
  619. /**
  620. * Set the header-label
  621. * @param int $column The column number
  622. * @param string $label The label
  623. * @param boolean $sortable Is the table sortable by this column? (defatult
  624. * = true)
  625. * @param string $th_attributes Additional attributes for the th-tag of the
  626. * table header
  627. * @param string $td_attributes Additional attributes for the td-tags of the
  628. * column
  629. */
  630. public function set_header ($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null) {
  631. $param['direction'] = 'ASC';
  632. if ($this->column == $column && $this->direction == 'ASC') {
  633. $param['direction'] = 'DESC';
  634. }
  635. $param['page_nr'] = $this->page_nr;
  636. $param['per_page'] = $this->per_page;
  637. $param['column'] = $column;
  638. if ($sortable) {
  639. $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
  640. foreach ($param as $key => & $value) {
  641. $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
  642. }
  643. $link .= $this->get_additional_url_paramstring();
  644. $link .= '">'.$label.'</a>';
  645. if ($this->column == $column) {
  646. $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
  647. }
  648. } else {
  649. $link = $label;
  650. }
  651. $this->setHeaderContents(0, $column, $link);
  652. if (!is_null($td_attributes)) {
  653. $this->td_attributes[$column] = $td_attributes;
  654. }
  655. if (!is_null($th_attributes)) {
  656. $this->th_attributes[$column] = $th_attributes;
  657. }
  658. }
  659. /**
  660. * Get the parameter-string with additional parameters to use in the URLs
  661. * generated by this SortableTable
  662. */
  663. public function get_additional_url_paramstring () {
  664. $param_string_parts = array ();
  665. if (is_array($this->additional_parameters) && count($this->additional_parameters) > 0) {
  666. foreach ($this->additional_parameters as $key => & $value) {
  667. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  668. }
  669. }
  670. $result = implode('&amp;', $param_string_parts);
  671. foreach ($this->other_tables as $index => & $tablename) {
  672. $param = array();
  673. if (isset($_GET[$tablename.'_direction'])) {
  674. //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
  675. $my_get_direction = $_GET[$tablename.'_direction'];
  676. if (!in_array($my_get_direction, array('ASC', 'DESC'))) {
  677. $param[$tablename.'_direction'] = 'ASC';
  678. } else {
  679. $param[$tablename.'_direction'] = $my_get_direction;
  680. }
  681. }
  682. if (isset($_GET[$tablename.'_page_nr'])) {
  683. $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
  684. }
  685. if (isset($_GET[$tablename.'_per_page'])) {
  686. $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
  687. }
  688. if (isset($_GET[$tablename.'_column'])) {
  689. $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
  690. }
  691. $param_string_parts = array ();
  692. foreach ($param as $key => & $value) {
  693. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  694. }
  695. if (count($param_string_parts) > 0)
  696. $result .= '&amp;'.implode('&amp;', $param_string_parts);
  697. }
  698. return $result;
  699. }
  700. /**
  701. * Get the parameter-string with the SortableTable-related parameters to use
  702. * in URLs
  703. */
  704. public function get_sortable_table_param_string () {
  705. $param[$this->param_prefix.'direction'] = $this->direction;
  706. $param[$this->param_prefix.'page_nr'] = $this->page_nr;
  707. $param[$this->param_prefix.'per_page'] = $this->per_page;
  708. $param[$this->param_prefix.'column'] = $this->column;
  709. $param_string_parts = array ();
  710. foreach ($param as $key => & $value) {
  711. $param_string_parts[] = urlencode($key).'='.urlencode($value);
  712. }
  713. $res = implode('&amp;', $param_string_parts);
  714. return $res;
  715. }
  716. /**
  717. * Add a filter to a column. If another filter was allready defined for the
  718. * given column, it will be overwritten.
  719. * @param int $column The number of the column
  720. * @param string $function The name of the filter-function. This should be a
  721. * function wich requires 1 parameter and returns the filtered value.
  722. */
  723. public function set_column_filter ($column, $function) {
  724. $this->column_filters[$column] = $function;
  725. }
  726. /**
  727. * Define a list of actions which can be performed on the table-date.
  728. * If you define a list of actions, the first column of the table will be
  729. * converted into checkboxes.
  730. * @param array $actions A list of actions. The key is the name of the
  731. * action. The value is the label to show in the select-box
  732. * @param string $checkbox_name The name of the generated checkboxes. The
  733. * value of the checkbox will be the value of the first column.
  734. */
  735. public function set_form_actions ($actions, $checkbox_name = 'id') {
  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. $this->additional_parameters = $parameters;
  746. }
  747. /**
  748. * Set other tables on the same page.
  749. * If you have other sortable tables on the page displaying this sortable
  750. * tables, you can define those other tables with this function. If you
  751. * don't define the other tables, there sorting and pagination will return
  752. * to their default state when sorting this table.
  753. * @param array $tablenames An array of table names.
  754. */
  755. public function set_other_tables ($tablenames) {
  756. $this->other_tables = $tablenames;
  757. }
  758. /**
  759. * Transform all data in a table-row, using the filters defined by the
  760. * function set_column_filter(...) defined elsewhere in this class.
  761. * If you've defined actions, the first element of the given row will be
  762. * converted into a checkbox
  763. * @param array $row A row from the table.
  764. */
  765. public function filter_data ($row) {
  766. $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
  767. foreach ($this->column_filters as $column => & $function) {
  768. $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
  769. }
  770. if (count($this->form_actions) > 0) {
  771. if (strlen($row[0]) > 0) {
  772. $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
  773. if (isset($_GET[$this->param_prefix.'selectall'])) {
  774. $row[0] .= ' checked="checked"';
  775. }
  776. $row[0] .= '/>';
  777. }
  778. }
  779. foreach ($row as $index => & $value) {
  780. if (empty($value)) {
  781. $value = '-';
  782. }
  783. }
  784. return $row;
  785. }
  786. /**
  787. * Get the total number of items. This function calls the function given as
  788. * 2nd argument in the constructor of a SortableTable. Make sure your
  789. * function has the same parameters as defined here.
  790. */
  791. public function get_total_number_of_items () {
  792. if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
  793. $this->total_number_of_items = call_user_func($this->get_total_number_function);
  794. }
  795. return $this->total_number_of_items;
  796. }
  797. /**
  798. * Get the data to display. This function calls the function given as
  799. * 2nd argument in the constructor of a SortableTable. Make sure your
  800. * function has the same parameters as defined here.
  801. * @param int $from Index of the first item to return.
  802. * @param int $per_page The number of items to return
  803. * @param int $column The number of the column on which the data should be
  804. * @param bool $sort Whether to sort or not
  805. * sorted
  806. * @param string $direction In which order should the data be sorted (ASC
  807. * or DESC)
  808. */
  809. public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null, $sort = null) {
  810. if (!is_null($this->get_data_function)) {
  811. return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
  812. }
  813. return array ();
  814. }
  815. }
  816. /**
  817. * Sortable table which can be used for data available in an array
  818. * @package chamilo.library
  819. */
  820. class SortableTableFromArray extends SortableTable {
  821. /**
  822. * The array containing all data for this table
  823. */
  824. public $table_data;
  825. /**
  826. * Constructor
  827. * @param array $table_data
  828. * @param int $default_column
  829. * @param int $default_items_per_page
  830. */
  831. public function __construct($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename') {
  832. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
  833. $this->table_data = $table_data;
  834. }
  835. /**
  836. * Get table data to show on current page
  837. * @see SortableTable#get_table_data
  838. */
  839. public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = true) {
  840. if ($sort) {
  841. $content = TableSort :: sort_table($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC);
  842. } else {
  843. $content = $this->table_data;
  844. }
  845. return array_slice($content, $from, $this->per_page);
  846. }
  847. /**
  848. * Get total number of items
  849. * @see SortableTable#get_total_number_of_items
  850. */
  851. public function get_total_number_of_items() {
  852. return count($this->table_data);
  853. }
  854. }
  855. /**
  856. * Sortable table which can be used for data available in an array
  857. *
  858. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  859. * $column_show is an array that lets us decide which are going to be the columns to show
  860. * $column_order is an array that lets us decide the ordering of the columns
  861. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  862. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  863. * @package chamilo.library
  864. */
  865. class SortableTableFromArrayConfig extends SortableTable {
  866. /**
  867. * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
  868. */
  869. private $column_show;
  870. /**
  871. *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  872. */
  873. private $column_order;
  874. /**
  875. * The array containing all data for this table
  876. */
  877. private $table_data;
  878. private $doc_filter;
  879. /**
  880. * Constructor
  881. * @param array $table_data All the information of the table
  882. * @param int $default_column Default column that will be use in the sorts functions
  883. * @param int $default_items_per_page quantity of pages that we are going to see
  884. * @param int $tablename Name of the table
  885. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  886. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  887. * @param bool special modification to fix the document name order
  888. */
  889. 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) {
  890. $this->column_show = $column_show;
  891. $this->column_order = $column_order;
  892. $this->doc_filter = $doc_filter;
  893. parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page, $direction);
  894. $this->table_data = $table_data;
  895. }
  896. /**
  897. * Get table data to show on current page
  898. * @see SortableTable#get_table_data
  899. */
  900. public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = true) {
  901. $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);
  902. return array_slice($content, $from, $this->per_page);
  903. }
  904. /**
  905. * Get total number of items
  906. * @see SortableTable#get_total_number_of_items
  907. */
  908. public function get_total_number_of_items() {
  909. return count($this->table_data);
  910. }
  911. }