table_sort.class.php 12 KB

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