tablesort.lib.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * This is a library with some functions to sort tabular data
  23. *
  24. * @package dokeos.library
  25. ==============================================================================
  26. */
  27. define('SORT_DATE', 3);
  28. define('SORT_IMAGE',4);
  29. class TableSort
  30. {
  31. /**
  32. * Create a string to use in sorting.
  33. * @param string $txt The string to convert
  34. * @author Ren� Haentjens
  35. */
  36. function orderingstring($txt)
  37. {
  38. return api_strtolower($txt);
  39. }
  40. /**
  41. * Sort 2-dimensional table.
  42. * @param array $data The data to be sorted.
  43. * @param int $column The column on which the data should be sorted (default = 0)
  44. * @param string $direction The direction to sort (SORT_ASC (default) or SORT_DESC)
  45. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,
  46. * SORT_STRING,SORT_DATE,SORT_IMAGE)
  47. * @return array The sorted dataset
  48. * @author bart.mollet@hogent.be
  49. */
  50. function sort_table($data, $column = 0, $direction = SORT_ASC, $type = SORT_REGULAR)
  51. {
  52. if(!is_array($data) or count($data)==0){return array();}
  53. if($column != strval(intval($column))){return $data;} //probably an attack
  54. if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;} // probably an attack
  55. $compare_function = '';
  56. if ($type == SORT_REGULAR)
  57. {
  58. if (TableSort::is_image_column($data, $column))
  59. {
  60. $type = SORT_IMAGE;
  61. }
  62. elseif (TableSort::is_date_column($data, $column))
  63. {
  64. $type = SORT_DATE;
  65. }
  66. elseif (TableSort::is_numeric_column($data, $column))
  67. {
  68. $type = SORT_NUMERIC;
  69. }
  70. else
  71. {
  72. $type = SORT_STRING;
  73. }
  74. }
  75. switch ($type)
  76. {
  77. case SORT_NUMERIC :
  78. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  79. break;
  80. case SORT_IMAGE :
  81. $compare_function = 'api_strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  82. break;
  83. case SORT_DATE :
  84. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  85. break;
  86. case SORT_STRING :
  87. default:
  88. $compare_function = 'api_strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  89. break;
  90. }
  91. $function_body = '$el1 = $a['.$column.']; $el2 = $b['.$column.']; return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  92. // Sort the content
  93. usort($data, create_function('$a,$b', $function_body));
  94. return $data;
  95. }
  96. /**
  97. * Checks if a column of a 2D-array contains only numeric values
  98. * @param array $data The data-array
  99. * @param int $column The index of the column to check
  100. * @return bool true if column contains only dates, false otherwise
  101. * @todo Take locale into account (eg decimal point or comma ?)
  102. * @author bart.mollet@hogent.be
  103. */
  104. function is_numeric_column($data, $column)
  105. {
  106. $is_numeric = true;
  107. foreach ($data as $index => $row)
  108. {
  109. $is_numeric &= is_numeric(strip_tags($row[$column]));
  110. }
  111. return $is_numeric;
  112. }
  113. /**
  114. * Checks if a column of a 2D-array contains only dates (GNU date syntax)
  115. * @param array $data The data-array
  116. * @param int $column The index of the column to check
  117. * @return bool true if column contains only dates, false otherwise
  118. * @author bart.mollet@hogent.be
  119. */
  120. function is_date_column($data, $column)
  121. {
  122. $is_date = true;
  123. foreach ($data as $index => $row)
  124. {
  125. if(strlen(strip_tags($row[$column])) != 0 )
  126. {
  127. $check_date = strtotime(strip_tags($row[$column]));
  128. // strtotime Returns a timestamp on success, FALSE otherwise.
  129. // Previous to PHP 5.1.0, this function would return -1 on failure.
  130. $is_date &= ($check_date != -1 && $check_date != false);
  131. }
  132. else
  133. {
  134. $is_date &= false;
  135. }
  136. }
  137. return $is_date;
  138. }
  139. /**
  140. * Checks if a column of a 2D-array contains only images (<img src="
  141. * path/file.ext" alt=".."/>)
  142. * @param array $data The data-array
  143. * @param int $column The index of the column to check
  144. * @return bool true if column contains only images, false otherwise
  145. * @author bart.mollet@hogent.be
  146. */
  147. function is_image_column($data, $column)
  148. {
  149. $is_image = true;
  150. foreach ($data as $index => $row)
  151. {
  152. $is_image &= strlen(trim(strip_tags($row[$column],'<img>'))) > 0; // at least one img-tag
  153. $is_image &= strlen(trim(strip_tags($row[$column]))) == 0; // and no text outside attribute-values
  154. }
  155. return $is_image;
  156. }
  157. /**
  158. * Sort 2-dimensional table. It is possile of change the columns that will be show and the way that the columns are sorted.
  159. * @param array $data The data to be sorted.
  160. * @param int $column The column on which the data should be sorted (default = 0)
  161. * @param string $direction The direction to sort (SORT_ASC (default) orSORT_DESC)
  162. * @param array $column_show The columns that we will show in the table i.e: $column_show=array('1','0','1') we will show the 1st and the 3th column.
  163. * @param array $column_order Changes how the columns will be sorted ie. $column_order=array('1','4','3','4') The 2nd column will be sorted like the 4 column
  164. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,SORT_STRING,SORT_DATE,SORT_IMAGE) *
  165. * @return array The sorted dataset
  166. * @author bart.mollet@hogent.be
  167. */
  168. function sort_table_config($data, $column = 0, $direction = SORT_ASC, $column_show=null, $column_order=null,$type = SORT_REGULAR)
  169. {
  170. if(!is_array($data) or count($data)==0){return array();}
  171. if($column != strval(intval($column))){return $data;} //probably an attack
  172. if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;} // probably an attack
  173. $compare_function = '';
  174. // Change columns sort
  175. // Here we say that the real way of how the columns are going to be order is manage by the $column_order array
  176. if(is_array($column_order))
  177. {
  178. for($i=0;$i<count($column_order);$i++)
  179. {
  180. if ($column== $i+1)
  181. {
  182. $column=$column_order[$i];
  183. }
  184. }
  185. }
  186. switch ($type)
  187. {
  188. case SORT_REGULAR :
  189. if (TableSort::is_image_column($data, $column))
  190. {
  191. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_IMAGE);
  192. }
  193. elseif (TableSort::is_date_column($data, $column))
  194. {
  195. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_DATE);
  196. }
  197. elseif (TableSort::is_numeric_column($data, $column))
  198. {
  199. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_NUMERIC);
  200. }
  201. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_STRING);
  202. break;
  203. case SORT_NUMERIC :
  204. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  205. break;
  206. case SORT_IMAGE :
  207. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  208. break;
  209. case SORT_DATE :
  210. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  211. break;
  212. case SORT_STRING :
  213. default:
  214. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  215. break;
  216. }
  217. $function_body = '$el1 = $a['.$column.']; ' .
  218. '$el2 = $b['.$column.']; ' .
  219. 'return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  220. // Sort the content
  221. usort($data, create_function('$a,$b', $function_body));
  222. // We show only the columns data that were set up on the $column_show array
  223. $new_order_data=array();
  224. if(is_array($column_show))
  225. {
  226. for ($j=0;$j<count($data);$j++)
  227. {
  228. $k=0;
  229. for ($i=0;$i<count($column_show);$i++)
  230. {
  231. if ($column_show[$i])
  232. {
  233. $new_order_data[$j][$k]=$data[$j][$i];
  234. }
  235. $k++;
  236. }
  237. }
  238. //replace the multi-arrays
  239. $data=$new_order_data;
  240. }
  241. else
  242. {
  243. return $data;
  244. }
  245. return $data;
  246. }
  247. }
  248. ?>