table_sort.class.php 9.4 KB

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