tablesort.lib.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 =
  73. * 0)
  74. * @param string $direction The direction to sort (SORT_ASC (default) or
  75. * SORT_DESC)
  76. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,
  77. * SORT_STRING,SORT_DATE,SORT_IMAGE)
  78. * @return array The sorted dataset
  79. * @author bart.mollet@hogent.be
  80. */
  81. function sort_table($data, $column = 0, $direction = SORT_ASC, $type = SORT_REGULAR)
  82. {
  83. if(!is_array($data) or count($data)==0){return array();}
  84. switch ($type)
  85. {
  86. case SORT_REGULAR :
  87. if (TableSort::is_image_column($data, $column))
  88. {
  89. return TableSort::sort_table($data, $column, $direction, SORT_IMAGE);
  90. }
  91. elseif (TableSort::is_date_column($data, $column))
  92. {
  93. return TableSort::sort_table($data, $column, $direction, SORT_DATE);
  94. }
  95. elseif (TableSort::is_numeric_column($data, $column))
  96. {
  97. return TableSort::sort_table($data, $column, $direction, SORT_NUMERIC);
  98. }
  99. return TableSort::sort_table($data, $column, $direction, SORT_STRING);
  100. break;
  101. case SORT_NUMERIC :
  102. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  103. break;
  104. case SORT_STRING :
  105. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  106. break;
  107. case SORT_IMAGE :
  108. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  109. break;
  110. case SORT_DATE :
  111. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  112. }
  113. $function_body = '$el1 = $a['.$column.']; $el2 = $b['.$column.']; return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  114. // Sort the content
  115. usort($data, create_function('$a,$b', $function_body));
  116. return $data;
  117. }
  118. /**
  119. * Checks if a column of a 2D-array contains only numeric values
  120. * @param array $data The data-array
  121. * @param int $column The index of the column to check
  122. * @return bool true if column contains only dates, false otherwise
  123. * @todo Take locale into account (eg decimal point or comma ?)
  124. * @author bart.mollet@hogent.be
  125. */
  126. function is_numeric_column($data, $column)
  127. {
  128. $is_numeric = true;
  129. foreach ($data as $index => $row)
  130. {
  131. $is_numeric &= is_numeric(strip_tags($row[$column]));
  132. }
  133. }
  134. /**
  135. * Checks if a column of a 2D-array contains only dates (GNU date syntax)
  136. * @param array $data The data-array
  137. * @param int $column The index of the column to check
  138. * @return bool true if column contains only dates, false otherwise
  139. * @author bart.mollet@hogent.be
  140. */
  141. function is_date_column($data, $column)
  142. {
  143. $is_date = true;
  144. foreach ($data as $index => $row)
  145. {
  146. if(strlen(strip_tags($row[$column])) != 0 )
  147. {
  148. $check_date = strtotime(strip_tags($row[$column]));
  149. // strtotime Returns a timestamp on success, FALSE otherwise.
  150. // Previous to PHP 5.1.0, this function would return -1 on failure.
  151. $is_date &= ($check_date != -1 && $check_date != false);
  152. }
  153. else
  154. {
  155. $is_date &= false;
  156. }
  157. }
  158. return $is_date;
  159. }
  160. /**
  161. * Checks if a column of a 2D-array contains only images (<img src="
  162. * path/file.ext" alt=".."/>)
  163. * @param array $data The data-array
  164. * @param int $column The index of the column to check
  165. * @return bool true if column contains only images, false otherwise
  166. * @author bart.mollet@hogent.be
  167. */
  168. function is_image_column($data, $column)
  169. {
  170. $is_image = true;
  171. foreach ($data as $index => $row)
  172. {
  173. $is_image &= strlen(trim(strip_tags($row[$column],'<img>'))) > 0; // at least one img-tag
  174. $is_image &= strlen(trim(strip_tags($row[$column]))) == 0; // and no text outside attribute-values
  175. }
  176. return $is_image;
  177. }
  178. /**
  179. * Sort 2-dimensional table. It is possile of change the columns that will be show and the way that the columns are sorted.
  180. * @param array $data The data to be sorted.
  181. * @param int $column The column on which the data should be sorted (default = 0)
  182. * @param string $direction The direction to sort (SORT_ASC (default) orSORT_DESC)
  183. * @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.
  184. * @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
  185. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,SORT_STRING,SORT_DATE,SORT_IMAGE) *
  186. * @return array The sorted dataset
  187. * @author bart.mollet@hogent.be
  188. */
  189. function sort_table_config($data, $column = 0, $direction = SORT_ASC, $column_show=null, $column_order=null,$type = SORT_REGULAR)
  190. {
  191. if(!is_array($data) || count($data)==0)
  192. {
  193. return array();
  194. }
  195. // Change columns sort
  196. // Here we say that the real way of how the columns are going to be order is manage by the $column_order array
  197. if(is_array($column_order))
  198. {
  199. for($i=0;$i<count($column_order);$i++)
  200. {
  201. if ($column== $i+1)
  202. {
  203. $column=$column_order[$i];
  204. }
  205. }
  206. }
  207. switch ($type)
  208. {
  209. case SORT_REGULAR :
  210. if (TableSort::is_image_column($data, $column))
  211. {
  212. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_IMAGE);
  213. }
  214. elseif (TableSort::is_date_column($data, $column))
  215. {
  216. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_DATE);
  217. }
  218. elseif (TableSort::is_numeric_column($data, $column))
  219. {
  220. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_NUMERIC);
  221. }
  222. return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_STRING);
  223. break;
  224. case SORT_NUMERIC :
  225. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  226. break;
  227. case SORT_STRING :
  228. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  229. break;
  230. case SORT_IMAGE :
  231. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  232. break;
  233. case SORT_DATE :
  234. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  235. }
  236. $function_body = '$el1 = $a['.$column.']; ' .
  237. '$el2 = $b['.$column.']; ' .
  238. 'return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  239. // Sort the content
  240. usort($data, create_function('$a,$b', $function_body));
  241. // We show only the columns data that were set up on the $column_show array
  242. $new_order_data=array();
  243. if(is_array($column_show))
  244. {
  245. for ($j=0;$j<count($data);$j++)
  246. {
  247. $k=0;
  248. for ($i=0;$i<count($column_show);$i++)
  249. {
  250. if ($column_show[$i])
  251. {
  252. $new_order_data[$j][$k]=$data[$j][$i];
  253. }
  254. $k++;
  255. }
  256. }
  257. //replace the multi-arrays
  258. $data=$new_order_data;
  259. }
  260. else
  261. {
  262. return $data;
  263. }
  264. return $data;
  265. }
  266. }
  267. ?>