| Total Complexity | 63 |
| Total Lines | 276 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TableSort often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableSort, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 [email protected] |
||
| 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 | |||
| 46 | if ($type == SORT_REGULAR) { |
||
| 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 | } else { |
||
| 54 | $type = SORT_STRING; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | $compare_operator = $direction == SORT_ASC ? '>' : '<='; |
||
| 59 | switch ($type) { |
||
| 60 | case SORT_NUMERIC: |
||
| 61 | $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);'; |
||
| 62 | break; |
||
| 63 | case SORT_IMAGE: |
||
| 64 | $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;'; |
||
| 65 | break; |
||
| 66 | case SORT_DATE: |
||
| 67 | $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));'; |
||
| 68 | break; |
||
| 69 | case SORT_STRING: |
||
| 70 | default: |
||
| 71 | $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;'; |
||
| 72 | break; |
||
| 73 | } |
||
| 74 | |||
| 75 | // Sort the content |
||
| 76 | usort($data, create_function('$a, $b', $compare_function)); |
||
| 77 | |||
| 78 | return $data; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sorts 2-dimensional table. It is possile changing the columns that will be shown and the way that the columns are to be sorted. |
||
| 83 | * @param array $data The data to be sorted. |
||
| 84 | * @param int $column The column on which the data should be sorted (default = 0) |
||
| 85 | * @param string $direction The direction to sort (SORT_ASC (default) orSORT_DESC) |
||
| 86 | * @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. |
||
| 87 | * @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] |
||
| 88 | * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_DATE, SORT_IMAGE) |
||
| 89 | * @return array The sorted dataset |
||
| 90 | * @author [email protected] |
||
| 91 | */ |
||
| 92 | public static function sort_table_config( |
||
| 93 | $data, |
||
| 94 | $column = 0, |
||
| 95 | $direction = SORT_ASC, |
||
| 96 | $column_show = null, |
||
| 97 | $column_order = null, |
||
| 98 | $type = SORT_REGULAR, |
||
| 99 | $doc_filter = false |
||
| 100 | ) { |
||
| 101 | if (!is_array($data) || empty($data)) { |
||
| 102 | return []; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($column != strval(intval($column))) { |
||
| 106 | // Probably an attack |
||
| 107 | return $data; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!in_array($direction, [SORT_ASC, SORT_DESC])) { |
||
| 111 | // Probably an attack |
||
| 112 | return $data; |
||
| 113 | } |
||
| 114 | |||
| 115 | // Change columns sort |
||
| 116 | // Here we say that the real way of how the columns are going to be order is manage by the $column_order array |
||
| 117 | if (is_array($column_order)) { |
||
| 118 | $column = isset($column_order[$column]) ? $column_order[$column] : $column; |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($type == SORT_REGULAR) { |
||
| 122 | if (self::is_image_column($data, $column)) { |
||
| 123 | $type = SORT_IMAGE; |
||
| 124 | } elseif (self::is_date_column($data, $column)) { |
||
| 125 | $type = SORT_DATE; |
||
| 126 | } elseif (self::is_numeric_column($data, $column)) { |
||
| 127 | $type = SORT_NUMERIC; |
||
| 128 | } else { |
||
| 129 | $type = SORT_STRING; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | //This fixes only works in the document tool when ordering by name |
||
| 134 | if ($doc_filter && in_array($type, [SORT_STRING])) { |
||
| 135 | $folder_to_sort = []; |
||
| 136 | $new_data = []; |
||
| 137 | if (!empty($data)) { |
||
| 138 | foreach ($data as $document) { |
||
| 139 | if ($document['type'] == 'folder') { |
||
| 140 | $docs_to_sort[$document['id']] = api_strtolower($document['name']); |
||
| 141 | } else { |
||
| 142 | $folder_to_sort[$document['id']] = api_strtolower($document['name']); |
||
| 143 | } |
||
| 144 | $new_data[$document['id']] = $document; |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($direction == SORT_ASC) { |
||
| 148 | if (!empty($docs_to_sort)) { |
||
| 149 | api_natsort($docs_to_sort); |
||
| 150 | } |
||
| 151 | if (!empty($folder_to_sort)) { |
||
| 152 | api_natsort($folder_to_sort); |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | if (!empty($docs_to_sort)) { |
||
| 156 | api_natrsort($docs_to_sort); |
||
| 157 | } |
||
| 158 | if (!empty($folder_to_sort)) { |
||
| 159 | api_natrsort($folder_to_sort); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $new_data_order = []; |
||
| 164 | if (!empty($docs_to_sort)) { |
||
| 165 | foreach ($docs_to_sort as $id => $document) { |
||
| 166 | if (isset($new_data[$id])) { |
||
| 167 | $new_data_order[] = $new_data[$id]; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!empty($folder_to_sort)) { |
||
| 173 | foreach ($folder_to_sort as $id => $document) { |
||
| 174 | if (isset($new_data[$id])) { |
||
| 175 | $new_data_order[] = $new_data[$id]; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | $data = $new_data_order; |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | $compare_operator = $direction == SORT_ASC ? '>' : '<='; |
||
| 183 | switch ($type) { |
||
| 184 | case SORT_NUMERIC: |
||
| 185 | $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);'; |
||
| 186 | break; |
||
| 187 | case SORT_IMAGE: |
||
| 188 | $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;'; |
||
| 189 | break; |
||
| 190 | case SORT_DATE: |
||
| 191 | $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));'; |
||
| 192 | break; |
||
| 193 | case SORT_STRING: |
||
| 194 | default: |
||
| 195 | $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;'; |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Sort the content |
||
| 200 | usort($data, create_function('$a, $b', $compare_function)); |
||
| 201 | } |
||
| 202 | |||
| 203 | if (is_array($column_show) && !empty($column_show)) { |
||
| 204 | // We show only the columns data that were set up on the $column_show array |
||
| 205 | $new_order_data = []; |
||
| 206 | $count_data = count($data); |
||
| 207 | $count_column_show = count($column_show); |
||
| 208 | for ($j = 0; $j < $count_data; $j++) { |
||
| 209 | $k = 0; |
||
| 210 | for ($i = 0; $i < $count_column_show; $i++) { |
||
| 211 | if ($column_show[$i]) { |
||
| 212 | $new_order_data[$j][$k] = $data[$j][$i]; |
||
| 213 | } |
||
| 214 | $k++; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | // Replace the multi-arrays |
||
| 218 | $data = $new_order_data; |
||
| 219 | } |
||
| 220 | |||
| 221 | return $data; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Checks whether a column of a 2D-array contains only numeric values |
||
| 226 | * @param array $data The data-array |
||
| 227 | * @param int $column The index of the column to check |
||
| 228 | * @return bool TRUE if column contains only dates, FALSE otherwise |
||
| 229 | * @todo Take locale into account (eg decimal point or comma ?) |
||
| 230 | * @author [email protected] |
||
| 231 | */ |
||
| 232 | private static function is_numeric_column(& $data, $column) |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Checks whether a column of a 2D-array contains only dates (GNU date syntax) |
||
| 246 | * @param array $data The data-array |
||
| 247 | * @param int $column The index of the column to check |
||
| 248 | * @return bool TRUE if column contains only dates, FALSE otherwise |
||
| 249 | * @author [email protected] |
||
| 250 | */ |
||
| 251 | private static function is_date_column(& $data, $column) |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Checks whether a column of a 2D-array contains only images (<img src="path/file.ext" alt=".."/>) |
||
| 272 | * @param array $data The data-array |
||
| 273 | * @param int $column The index of the column to check |
||
| 274 | * @return bool TRUE if column contains only images, FALSE otherwise |
||
| 275 | * @author [email protected] |
||
| 276 | */ |
||
| 277 | private static function is_image_column(& $data, $column) |
||
| 292 | } |
||
| 293 | } |
||
| 294 |