table_sort.class.php 10 KB

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