tablesort.lib.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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) {
  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. $compare_operator = $direction == SORT_ASC ? '>' : '<=';
  104. switch ($type) {
  105. case SORT_NUMERIC:
  106. $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
  107. break;
  108. case SORT_IMAGE:
  109. $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
  110. break;
  111. case SORT_DATE:
  112. $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
  113. break;
  114. case SORT_STRING:
  115. default:
  116. $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
  117. break;
  118. }
  119. // Sort the content
  120. usort($data, create_function('$a, $b', $compare_function));
  121. if (is_array($column_show)) {
  122. // We show only the columns data that were set up on the $column_show array
  123. $new_order_data = array();
  124. $count_data = count($data);
  125. $count_column_show = count($column_show);
  126. for ($j = 0; $j < $count_data; $j++) {
  127. $k = 0;
  128. for ($i = 0; $i < $count_column_show; $i++) {
  129. if ($column_show[$i]) {
  130. $new_order_data[$j][$k] = $data[$j][$i];
  131. }
  132. $k++;
  133. }
  134. }
  135. // Replace the multi-arrays
  136. $data = $new_order_data;
  137. }
  138. return $data;
  139. }
  140. /**
  141. * Checks whether a column of a 2D-array contains only numeric values
  142. * @param array $data The data-array
  143. * @param int $column The index of the column to check
  144. * @return bool TRUE if column contains only dates, FALSE otherwise
  145. * @todo Take locale into account (eg decimal point or comma ?)
  146. * @author bart.mollet@hogent.be
  147. */
  148. private function is_numeric_column(& $data, $column) {
  149. $is_numeric = true;
  150. foreach ($data as $index => & $row) {
  151. $is_numeric &= is_numeric(strip_tags($row[$column]));
  152. if (!$is_numeric) {
  153. break;
  154. }
  155. }
  156. return $is_numeric;
  157. }
  158. /**
  159. * Checks whether a column of a 2D-array contains only dates (GNU date syntax)
  160. * @param array $data The data-array
  161. * @param int $column The index of the column to check
  162. * @return bool TRUE if column contains only dates, FALSE otherwise
  163. * @author bart.mollet@hogent.be
  164. */
  165. private function is_date_column(& $data, $column) {
  166. $is_date = true;
  167. foreach ($data as $index => & $row) {
  168. if (strlen(strip_tags($row[$column])) != 0) {
  169. $check_date = strtotime(strip_tags($row[$column]));
  170. // strtotime Returns a timestamp on success, FALSE otherwise.
  171. // Previous to PHP 5.1.0, this function would return -1 on failure.
  172. $is_date &= ($check_date != -1 && $check_date);
  173. } else {
  174. $is_date &= false;
  175. }
  176. if (!$is_date) {
  177. break;
  178. }
  179. }
  180. return $is_date;
  181. }
  182. /**
  183. * Checks whether a column of a 2D-array contains only images (<img src="path/file.ext" alt=".."/>)
  184. * @param array $data The data-array
  185. * @param int $column The index of the column to check
  186. * @return bool TRUE if column contains only images, FALSE otherwise
  187. * @author bart.mollet@hogent.be
  188. */
  189. private function is_image_column(& $data, $column) {
  190. $is_image = true;
  191. foreach ($data as $index => & $row) {
  192. $is_image &= strlen(trim(strip_tags($row[$column], '<img>'))) > 0; // at least one img-tag
  193. $is_image &= strlen(trim(strip_tags($row[$column]))) == 0; // and no text outside attribute-values
  194. if (!$is_image) {
  195. break;
  196. }
  197. }
  198. return $is_image;
  199. }
  200. }