| Conditions | 31 |
| Paths | 1022 |
| Total Lines | 112 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 168 | public static function sort_table_config( |
||
| 169 | $data, |
||
| 170 | $column = 0, |
||
| 171 | $direction = SORT_ASC, |
||
| 172 | $column_show = null, |
||
| 173 | $column_order = null, |
||
| 174 | $type = SORT_REGULAR, |
||
| 175 | $doc_filter = false |
||
| 176 | ) { |
||
| 177 | if (!is_array($data) || empty($data)) { |
||
| 178 | return []; |
||
| 179 | } |
||
| 180 | |||
| 181 | $column = (int) $column; |
||
| 182 | |||
| 183 | if (!in_array($direction, [SORT_ASC, SORT_DESC])) { |
||
| 184 | // Probably an attack |
||
| 185 | return $data; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Change columns sort |
||
| 189 | // Here we say that the real way of how the columns are going to be order is manage by the $column_order array |
||
| 190 | if (is_array($column_order)) { |
||
| 191 | $column = isset($column_order[$column]) ? $column_order[$column] : $column; |
||
| 192 | } |
||
| 193 | |||
| 194 | if (SORT_REGULAR == $type) { |
||
| 195 | if (self::is_image_column($data, $column)) { |
||
| 196 | $type = SORT_IMAGE; |
||
| 197 | } elseif (self::is_date_column($data, $column)) { |
||
| 198 | $type = SORT_DATE; |
||
| 199 | } elseif (self::is_numeric_column($data, $column)) { |
||
| 200 | $type = SORT_NUMERIC; |
||
| 201 | } else { |
||
| 202 | $type = SORT_STRING; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | //This fixes only works in the document tool when ordering by name |
||
| 207 | if ($doc_filter && in_array($type, [SORT_STRING])) { |
||
| 208 | $folder_to_sort = []; |
||
| 209 | $new_data = []; |
||
| 210 | if (!empty($data)) { |
||
| 211 | foreach ($data as $document) { |
||
| 212 | if ('folder' === $document['type']) { |
||
| 213 | $docs_to_sort[$document['id']] = api_strtolower($document['name']); |
||
| 214 | } else { |
||
| 215 | $folder_to_sort[$document['id']] = api_strtolower($document['name']); |
||
| 216 | } |
||
| 217 | $new_data[$document['id']] = $document; |
||
| 218 | } |
||
| 219 | |||
| 220 | if (SORT_ASC == $direction) { |
||
| 221 | if (!empty($docs_to_sort)) { |
||
| 222 | api_natsort($docs_to_sort); |
||
| 223 | } |
||
| 224 | if (!empty($folder_to_sort)) { |
||
| 225 | api_natsort($folder_to_sort); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | if (!empty($docs_to_sort)) { |
||
| 229 | api_natrsort($docs_to_sort); |
||
| 230 | } |
||
| 231 | if (!empty($folder_to_sort)) { |
||
| 232 | api_natrsort($folder_to_sort); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | $new_data_order = []; |
||
| 237 | if (!empty($docs_to_sort)) { |
||
| 238 | foreach ($docs_to_sort as $id => $document) { |
||
| 239 | if (isset($new_data[$id])) { |
||
| 240 | $new_data_order[] = $new_data[$id]; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!empty($folder_to_sort)) { |
||
| 246 | foreach ($folder_to_sort as $id => $document) { |
||
| 247 | if (isset($new_data[$id])) { |
||
| 248 | $new_data_order[] = $new_data[$id]; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | $data = $new_data_order; |
||
| 253 | } |
||
| 254 | } else { |
||
| 255 | $function = self::getSortFunction($type, $direction, $column); |
||
| 256 | |||
| 257 | // Sort the content |
||
| 258 | usort($data, $function); |
||
| 259 | } |
||
| 260 | |||
| 261 | if (is_array($column_show) && !empty($column_show)) { |
||
| 262 | // We show only the columns data that were set up on the $column_show array |
||
| 263 | $new_order_data = []; |
||
| 264 | $count_data = count($data); |
||
| 265 | $count_column_show = count($column_show); |
||
| 266 | for ($j = 0; $j < $count_data; $j++) { |
||
| 267 | $k = 0; |
||
| 268 | for ($i = 0; $i < $count_column_show; $i++) { |
||
| 269 | if ($column_show[$i]) { |
||
| 270 | $new_order_data[$j][$k] = $data[$j][$i]; |
||
| 271 | } |
||
| 272 | $k++; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | // Replace the multi-arrays |
||
| 276 | $data = $new_order_data; |
||
| 277 | } |
||
| 278 | |||
| 279 | return $data; |
||
| 280 | } |
||
| 365 |