tablesort.lib.php 9.3 KB

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