tablesort.lib.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 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. switch ($type)
  84. {
  85. case SORT_REGULAR :
  86. if (TableSort::is_image_column($data, $column))
  87. {
  88. return TableSort::sort_table($data, $column, $direction, SORT_IMAGE);
  89. }
  90. elseif (TableSort::is_date_column($data, $column))
  91. {
  92. return TableSort::sort_table($data, $column, $direction, SORT_DATE);
  93. }
  94. elseif (TableSort::is_numeric_column($data, $column))
  95. {
  96. return TableSort::sort_table($data, $column, $direction, SORT_NUMERIC);
  97. }
  98. return TableSort::sort_table($data, $column, $direction, SORT_STRING);
  99. break;
  100. case SORT_NUMERIC :
  101. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  102. break;
  103. case SORT_STRING :
  104. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  105. break;
  106. case SORT_IMAGE :
  107. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  108. break;
  109. case SORT_DATE :
  110. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  111. }
  112. $function_body = '$el1 = $a['.$column.']; $el2 = $b['.$column.']; return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  113. // Sort the content
  114. usort($data, create_function('$a,$b', $function_body));
  115. return $data;
  116. }
  117. /**
  118. * Checks if a column of a 2D-array contains only numeric values
  119. * @param array $data The data-array
  120. * @param int $column The index of the column to check
  121. * @return bool true if column contains only dates, false otherwise
  122. * @todo Take locale into account (eg decimal point or comma ?)
  123. * @author bart.mollet@hogent.be
  124. */
  125. function is_numeric_column($data, $column)
  126. {
  127. $is_numeric = true;
  128. foreach ($data as $index => $row)
  129. {
  130. $is_numeric &= is_numeric(strip_tags($row[$column]));
  131. }
  132. return $is_numeric;
  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. ?>