sortable_table.class.php 41 KB

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