table_sort.class.php 12 KB

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