tablesort.lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * String to lowercase (keep accents).
  33. * @param string $txt The string to convert
  34. * @author Ren� Haentjens
  35. * This function is 8859-1 specific and should be adapted when Dokeos is
  36. * used with other charsets.
  37. */
  38. function strtolower_keepaccents($txt)
  39. {
  40. return strtolower(strtr($txt, "������������������������������", "������������������������������"));
  41. }
  42. /**
  43. * String to lowercase.
  44. * @param string $txt The string to convert
  45. * @author Ren� Haentjens
  46. * This function is 8859-1 specific and should be adapted when Dokeos is
  47. * used with other charsets.
  48. */
  49. function strtolower_eorlatin($txt)
  50. {
  51. return str_replace(array ("�", "�"), array ("ae", "ss"), strtr(TableSort::strtolower_keepaccents($txt), "�����������������������������", "aaaaaaceeeeiiiidnoooooouuuuyy"));
  52. // do not replace "�" by "th", leave it at the end of the alphabet...
  53. // do not replace any of "$&��������", though they resemble letters...
  54. }
  55. /**
  56. * Create a string to use in sorting.
  57. * @param string $txt The string to convert
  58. * @author Ren� Haentjens
  59. * This function is 8859-1 specific and should be adapted when Dokeos is
  60. * used with other charsets.
  61. * See http://anubis.dkuug.dk/CEN/TC304/EOR/eorhome.html
  62. * Function 'orderingstring' can be used to implement EOR level 1 ordering,
  63. * for 8859- 1.
  64. */
  65. function orderingstring($txt)
  66. {
  67. return ereg_replace("[^0-9a-z�]", "", TableSort::strtolower_eorlatin($txt));
  68. }
  69. /**
  70. * Sort 2-dimensional table.
  71. * @param array $data The data to be sorted.
  72. * @param int $column The column on which the data should be sorted (default = 0)
  73. * @param string $direction The direction to sort (SORT_ASC (default) or SORT_DESC)
  74. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,
  75. * SORT_STRING,SORT_DATE,SORT_IMAGE)
  76. * @return array The sorted dataset
  77. * @author bart.mollet@hogent.be
  78. */
  79. function sort_table($data, $column = 0, $direction = SORT_ASC, $type = SORT_REGULAR)
  80. {
  81. if(!is_array($data) or count($data)==0){return array();}
  82. if($column != strval(intval($column))){return $data;} //probably an attack
  83. if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;} // probably an attack
  84. $compare_function = '';
  85. switch ($type)
  86. {
  87. case SORT_REGULAR :
  88. if (TableSort::is_image_column($data, $column))
  89. {
  90. return TableSort::sort_table($data, $column, $direction, SORT_IMAGE);
  91. }
  92. elseif (TableSort::is_date_column($data, $column))
  93. {
  94. return TableSort::sort_table($data, $column, $direction, SORT_DATE);
  95. }
  96. elseif (TableSort::is_numeric_column($data, $column))
  97. {
  98. return TableSort::sort_table($data, $column, $direction, SORT_NUMERIC);
  99. }
  100. return TableSort::sort_table($data, $column, $direction, SORT_STRING);
  101. break;
  102. case SORT_NUMERIC :
  103. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  104. break;
  105. case SORT_IMAGE :
  106. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  107. break;
  108. case SORT_DATE :
  109. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  110. case SORT_STRING :
  111. default:
  112. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  113. break;
  114. }
  115. $function_body = '$el1 = $a['.$column.']; $el2 = $b['.$column.']; return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  116. // Sort the content
  117. usort($data, create_function('$a,$b', $function_body));
  118. return $data;
  119. }
  120. /**
  121. * Checks if a column of a 2D-array contains only numeric values
  122. * @param array $data The data-array
  123. * @param int $column The index of the column to check
  124. * @return bool true if column contains only dates, false otherwise
  125. * @todo Take locale into account (eg decimal point or comma ?)
  126. * @author bart.mollet@hogent.be
  127. */
  128. function is_numeric_column($data, $column)
  129. {
  130. $is_numeric = true;
  131. foreach ($data as $index => $row)
  132. {
  133. $is_numeric &= is_numeric(strip_tags($row[$column]));
  134. }
  135. return $is_numeric;
  136. }
  137. /**
  138. * Checks if a column of a 2D-array contains only dates (GNU date syntax)
  139. * @param array $data The data-array
  140. * @param int $column The index of the column to check
  141. * @return bool true if column contains only dates, false otherwise
  142. * @author bart.mollet@hogent.be
  143. */
  144. function is_date_column($data, $column)
  145. {
  146. $is_date = true;
  147. foreach ($data as $index => $row)
  148. {
  149. if(strlen(strip_tags($row[$column])) != 0 )
  150. {
  151. $check_date = strtotime(strip_tags($row[$column]));
  152. // strtotime Returns a timestamp on success, FALSE otherwise.
  153. // Previous to PHP 5.1.0, this function would return -1 on failure.
  154. $is_date &= ($check_date != -1 && $check_date != false);
  155. }
  156. else
  157. {
  158. $is_date &= false;
  159. }
  160. }
  161. return $is_date;
  162. }
  163. /**
  164. * Checks if a column of a 2D-array contains only images (<img src="
  165. * path/file.ext" alt=".."/>)
  166. * @param array $data The data-array
  167. * @param int $column The index of the column to check
  168. * @return bool true if column contains only images, false otherwise
  169. * @author bart.mollet@hogent.be
  170. */
  171. function is_image_column($data, $column)
  172. {
  173. $is_image = true;
  174. foreach ($data as $index => $row)
  175. {
  176. $is_image &= strlen(trim(strip_tags($row[$column],'<img>'))) > 0; // at least one img-tag
  177. $is_image &= strlen(trim(strip_tags($row[$column]))) == 0; // and no text outside attribute-values
  178. }
  179. return $is_image;
  180. }
  181. /**
  182. * Sort 2-dimensional table. It is possile of change the columns that will be show and the way that the columns are sorted.
  183. * @param array $data The data to be sorted.
  184. * @param int $column The column on which the data should be sorted (default = 0)
  185. * @param string $direction The direction to sort (SORT_ASC (default) orSORT_DESC)
  186. * @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.
  187. * @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
  188. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,SORT_STRING,SORT_DATE,SORT_IMAGE) *
  189. * @return array The sorted dataset
  190. * @author bart.mollet@hogent.be
  191. */
  192. function sort_table_config($data, $column = 0, $direction = SORT_ASC, $column_show=null, $column_order=null,$type = SORT_REGULAR)
  193. {
  194. if(!is_array($data) or count($data)==0){return array();}
  195. if($column != strval(intval($column))){return $data;} //probably an attack
  196. if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;} // probably an attack
  197. $compare_function = '';
  198. // Change columns sort
  199. // Here we say that the real way of how the columns are going to be order is manage by the $column_order array
  200. if(is_array($column_order))
  201. {
  202. for($i=0;$i<count($column_order);$i++)
  203. {
  204. if ($column== $i+1)
  205. {
  206. $column=$column_order[$i];
  207. }
  208. }
  209. }
  210. switch ($type)
  211. {
  212. case SORT_REGULAR :
  213. if (TableSort::is_image_column($data, $column))
  214. {
  215. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_IMAGE);
  216. }
  217. elseif (TableSort::is_date_column($data, $column))
  218. {
  219. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_DATE);
  220. }
  221. elseif (TableSort::is_numeric_column($data, $column))
  222. {
  223. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_NUMERIC);
  224. }
  225. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_STRING);
  226. break;
  227. case SORT_NUMERIC :
  228. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  229. break;
  230. case SORT_STRING :
  231. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  232. break;
  233. case SORT_IMAGE :
  234. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  235. break;
  236. case SORT_DATE :
  237. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  238. }
  239. $function_body = '$el1 = $a['.$column.']; ' .
  240. '$el2 = $b['.$column.']; ' .
  241. 'return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  242. // Sort the content
  243. usort($data, create_function('$a,$b', $function_body));
  244. // We show only the columns data that were set up on the $column_show array
  245. $new_order_data=array();
  246. if(is_array($column_show))
  247. {
  248. for ($j=0;$j<count($data);$j++)
  249. {
  250. $k=0;
  251. for ($i=0;$i<count($column_show);$i++)
  252. {
  253. if ($column_show[$i])
  254. {
  255. $new_order_data[$j][$k]=$data[$j][$i];
  256. }
  257. $k++;
  258. }
  259. }
  260. //replace the multi-arrays
  261. $data=$new_order_data;
  262. }
  263. else
  264. {
  265. return $data;
  266. }
  267. return $data;
  268. }
  269. }
  270. ?>