table_sort.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a library with some functions to sort tabular data
  5. *
  6. * @package chamilo.library
  7. */
  8. define('SORT_DATE', 3);
  9. define('SORT_IMAGE', 4);
  10. /**
  11. * @package chamilo.library
  12. */
  13. class TableSort
  14. {
  15. /**
  16. * Sorts 2-dimensional table.
  17. * @param array $data The data to be sorted.
  18. * @param int $column The column on which the data should be sorted (default = 0)
  19. * @param int $direction The direction to sort (SORT_ASC (default) or SORT_DESC)
  20. * @param int $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,
  21. * SORT_STRING,SORT_DATE,SORT_IMAGE)
  22. * @return array The sorted dataset
  23. * @author bart.mollet@hogent.be
  24. */
  25. public static function sort_table($data, $column = 0, $direction = SORT_ASC, $type = SORT_REGULAR)
  26. {
  27. if (!is_array($data) || empty($data)) {
  28. return array();
  29. }
  30. if ($column != strval(intval($column))) {
  31. // Probably an attack
  32. return $data;
  33. }
  34. if (!in_array($direction, array(SORT_ASC, SORT_DESC))) {
  35. // Probably an attack
  36. return $data;
  37. }
  38. if ($type == SORT_REGULAR) {
  39. if (self::is_image_column($data, $column)) {
  40. $type = SORT_IMAGE;
  41. } elseif (self::is_date_column($data, $column)) {
  42. $type = SORT_DATE;
  43. } elseif (self::is_numeric_column($data, $column)) {
  44. $type = SORT_NUMERIC;
  45. } else {
  46. $type = SORT_STRING;
  47. }
  48. }
  49. $compare_operator = $direction == SORT_ASC ? '>' : '<=';
  50. switch ($type) {
  51. case SORT_NUMERIC:
  52. $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
  53. break;
  54. case SORT_IMAGE:
  55. $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
  56. break;
  57. case SORT_DATE:
  58. $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
  59. break;
  60. case SORT_STRING:
  61. default:
  62. $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
  63. break;
  64. }
  65. // Sort the content
  66. usort($data, create_function('$a, $b', $compare_function));
  67. return $data;
  68. }
  69. /**
  70. * Sorts 2-dimensional table. It is possile changing the columns that will be shown and the way that the columns are to be sorted.
  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) orSORT_DESC)
  74. * @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.
  75. * @param array $column_order Changes how the columns will be sorted ie. $column_order = array('0','3','2','3') The column [1] will be sorted like the column [3]
  76. * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_DATE, SORT_IMAGE)
  77. * @return array The sorted dataset
  78. * @author bart.mollet@hogent.be
  79. */
  80. public static function sort_table_config(
  81. $data,
  82. $column = 0,
  83. $direction = SORT_ASC,
  84. $column_show = null,
  85. $column_order = null,
  86. $type = SORT_REGULAR,
  87. $doc_filter = false
  88. ) {
  89. if (!is_array($data) || empty($data)) {
  90. return array();
  91. }
  92. if ($column != strval(intval($column))) {
  93. // Probably an attack
  94. return $data;
  95. }
  96. if (!in_array($direction, array(SORT_ASC, SORT_DESC))) {
  97. // Probably an attack
  98. return $data;
  99. }
  100. // Change columns sort
  101. // Here we say that the real way of how the columns are going to be order is manage by the $column_order array
  102. if (is_array($column_order)) {
  103. $column = isset($column_order[$column]) ? $column_order[$column] : $column;
  104. }
  105. if ($type == SORT_REGULAR) {
  106. if (self::is_image_column($data, $column)) {
  107. $type = SORT_IMAGE;
  108. } elseif (self::is_date_column($data, $column)) {
  109. $type = SORT_DATE;
  110. } elseif (self::is_numeric_column($data, $column)) {
  111. $type = SORT_NUMERIC;
  112. } else {
  113. $type = SORT_STRING;
  114. }
  115. }
  116. //This fixes only works in the document tool when ordering by name
  117. if ($doc_filter && in_array($type, array(SORT_STRING))) {
  118. $folder_to_sort = array();
  119. $new_data = array();
  120. if (!empty($data)) {
  121. foreach ($data as $document) {
  122. if ($document['type'] == 'folder') {
  123. $docs_to_sort[$document['id']] = api_strtolower($document['name']);
  124. } else {
  125. $folder_to_sort[$document['id']] = api_strtolower($document['name']);
  126. }
  127. $new_data[$document['id']] = $document;
  128. }
  129. if ($direction == SORT_ASC) {
  130. if (!empty($docs_to_sort)) {
  131. api_natsort($docs_to_sort);
  132. }
  133. if (!empty($folder_to_sort)) {
  134. api_natsort($folder_to_sort);
  135. }
  136. } else {
  137. if (!empty($docs_to_sort)) {
  138. api_natrsort($docs_to_sort);
  139. }
  140. if (!empty($folder_to_sort)) {
  141. api_natrsort($folder_to_sort);
  142. }
  143. }
  144. $new_data_order = array();
  145. if (!empty($docs_to_sort)) {
  146. foreach ($docs_to_sort as $id => $document) {
  147. if (isset($new_data[$id])) {
  148. $new_data_order[] = $new_data[$id];
  149. }
  150. }
  151. }
  152. if (!empty($folder_to_sort)) {
  153. foreach ($folder_to_sort as $id => $document) {
  154. if (isset($new_data[$id])) {
  155. $new_data_order[] = $new_data[$id];
  156. }
  157. }
  158. }
  159. $data = $new_data_order;
  160. }
  161. } else {
  162. $compare_operator = $direction == SORT_ASC ? '>' : '<=';
  163. switch ($type) {
  164. case SORT_NUMERIC:
  165. $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
  166. break;
  167. case SORT_IMAGE:
  168. $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
  169. break;
  170. case SORT_DATE:
  171. $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
  172. break;
  173. case SORT_STRING:
  174. default:
  175. $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
  176. break;
  177. }
  178. // Sort the content
  179. usort($data, create_function('$a, $b', $compare_function));
  180. }
  181. if (is_array($column_show) && !empty($column_show)) {
  182. // We show only the columns data that were set up on the $column_show array
  183. $new_order_data = array();
  184. $count_data = count($data);
  185. $count_column_show = count($column_show);
  186. for ($j = 0; $j < $count_data; $j++) {
  187. $k = 0;
  188. for ($i = 0; $i < $count_column_show; $i++) {
  189. if ($column_show[$i]) {
  190. $new_order_data[$j][$k] = $data[$j][$i];
  191. }
  192. $k++;
  193. }
  194. }
  195. // Replace the multi-arrays
  196. $data = $new_order_data;
  197. }
  198. return $data;
  199. }
  200. /**
  201. * Checks whether a column of a 2D-array contains only numeric values
  202. * @param array $data The data-array
  203. * @param int $column The index of the column to check
  204. * @return bool TRUE if column contains only dates, FALSE otherwise
  205. * @todo Take locale into account (eg decimal point or comma ?)
  206. * @author bart.mollet@hogent.be
  207. */
  208. private static function is_numeric_column(& $data, $column)
  209. {
  210. $is_numeric = true;
  211. foreach ($data as $index => & $row) {
  212. $is_numeric &= is_numeric(strip_tags($row[$column]));
  213. if (!$is_numeric) {
  214. break;
  215. }
  216. }
  217. return $is_numeric;
  218. }
  219. /**
  220. * Checks whether a column of a 2D-array contains only dates (GNU date syntax)
  221. * @param array $data The data-array
  222. * @param int $column The index of the column to check
  223. * @return bool TRUE if column contains only dates, FALSE otherwise
  224. * @author bart.mollet@hogent.be
  225. */
  226. private static function is_date_column(& $data, $column)
  227. {
  228. $is_date = true;
  229. foreach ($data as $index => & $row) {
  230. if (strlen(strip_tags($row[$column])) != 0) {
  231. $check_date = strtotime(strip_tags($row[$column]));
  232. // strtotime Returns a timestamp on success, FALSE otherwise.
  233. // Previous to PHP 5.1.0, this function would return -1 on failure.
  234. $is_date &= ($check_date != -1 && $check_date);
  235. } else {
  236. $is_date &= false;
  237. }
  238. if (!$is_date) {
  239. break;
  240. }
  241. }
  242. return $is_date;
  243. }
  244. /**
  245. * Checks whether a column of a 2D-array contains only images (<img src="path/file.ext" alt=".."/>)
  246. * @param array $data The data-array
  247. * @param int $column The index of the column to check
  248. * @return bool TRUE if column contains only images, FALSE otherwise
  249. * @author bart.mollet@hogent.be
  250. */
  251. private static function is_image_column(& $data, $column)
  252. {
  253. $is_image = true;
  254. foreach ($data as $index => & $row) {
  255. $is_image &= strlen(trim(strip_tags($row[$column], '<img>'))) > 0; // at least one img-tag
  256. $is_image &= strlen(trim(strip_tags($row[$column]))) == 0; // and no text outside attribute-values
  257. if (!$is_image) {
  258. break;
  259. }
  260. }
  261. return $is_image;
  262. }
  263. }