sortabletable.class.php 33 KB

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