table_sort.class.php 11 KB

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