sortable_table.class.php 37 KB

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