| Conditions | 32 |
| Paths | 1023 |
| Total Lines | 115 |
| Code Lines | 62 |
| 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 |
||
| 154 | public static function sort_table_config( |
||
| 155 | $data, |
||
| 156 | $column = 0, |
||
| 157 | $direction = SORT_ASC, |
||
| 158 | $column_show = null, |
||
| 159 | $column_order = null, |
||
| 160 | $type = SORT_REGULAR, |
||
| 161 | $doc_filter = false |
||
| 162 | ) { |
||
| 163 | if (!is_array($data) || empty($data)) { |
||
| 164 | return []; |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($column != strval(intval($column))) { |
||
| 168 | // Probably an attack |
||
| 169 | return $data; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!in_array($direction, [SORT_ASC, SORT_DESC])) { |
||
| 173 | // Probably an attack |
||
| 174 | return $data; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Change columns sort |
||
| 178 | // Here we say that the real way of how the columns are going to be order is manage by the $column_order array |
||
| 179 | if (is_array($column_order)) { |
||
| 180 | $column = isset($column_order[$column]) ? $column_order[$column] : $column; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($type == SORT_REGULAR) { |
||
| 184 | if (self::is_image_column($data, $column)) { |
||
| 185 | $type = SORT_IMAGE; |
||
| 186 | } elseif (self::is_date_column($data, $column)) { |
||
| 187 | $type = SORT_DATE; |
||
| 188 | } elseif (self::is_numeric_column($data, $column)) { |
||
| 189 | $type = SORT_NUMERIC; |
||
| 190 | } else { |
||
| 191 | $type = SORT_STRING; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | //This fixes only works in the document tool when ordering by name |
||
| 196 | if ($doc_filter && in_array($type, [SORT_STRING])) { |
||
| 197 | $folder_to_sort = []; |
||
| 198 | $new_data = []; |
||
| 199 | if (!empty($data)) { |
||
| 200 | foreach ($data as $document) { |
||
| 201 | if ($document['type'] == 'folder') { |
||
| 202 | $docs_to_sort[$document['id']] = api_strtolower($document['name']); |
||
| 203 | } else { |
||
| 204 | $folder_to_sort[$document['id']] = api_strtolower($document['name']); |
||
| 205 | } |
||
| 206 | $new_data[$document['id']] = $document; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($direction == SORT_ASC) { |
||
| 210 | if (!empty($docs_to_sort)) { |
||
| 211 | api_natsort($docs_to_sort); |
||
| 212 | } |
||
| 213 | if (!empty($folder_to_sort)) { |
||
| 214 | api_natsort($folder_to_sort); |
||
| 215 | } |
||
| 216 | } else { |
||
| 217 | if (!empty($docs_to_sort)) { |
||
| 218 | api_natrsort($docs_to_sort); |
||
| 219 | } |
||
| 220 | if (!empty($folder_to_sort)) { |
||
| 221 | api_natrsort($folder_to_sort); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | $new_data_order = []; |
||
| 226 | if (!empty($docs_to_sort)) { |
||
| 227 | foreach ($docs_to_sort as $id => $document) { |
||
| 228 | if (isset($new_data[$id])) { |
||
| 229 | $new_data_order[] = $new_data[$id]; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if (!empty($folder_to_sort)) { |
||
| 235 | foreach ($folder_to_sort as $id => $document) { |
||
| 236 | if (isset($new_data[$id])) { |
||
| 237 | $new_data_order[] = $new_data[$id]; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | $data = $new_data_order; |
||
| 242 | } |
||
| 243 | } else { |
||
| 244 | $function = self::getSortFunction($type, $direction, $column); |
||
| 245 | |||
| 246 | // Sort the content |
||
| 247 | usort($data, $function); |
||
| 248 | } |
||
| 249 | |||
| 250 | if (is_array($column_show) && !empty($column_show)) { |
||
| 251 | // We show only the columns data that were set up on the $column_show array |
||
| 252 | $new_order_data = []; |
||
| 253 | $count_data = count($data); |
||
| 254 | $count_column_show = count($column_show); |
||
| 255 | for ($j = 0; $j < $count_data; $j++) { |
||
| 256 | $k = 0; |
||
| 257 | for ($i = 0; $i < $count_column_show; $i++) { |
||
| 258 | if ($column_show[$i]) { |
||
| 259 | $new_order_data[$j][$k] = $data[$j][$i]; |
||
| 260 | } |
||
| 261 | $k++; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | // Replace the multi-arrays |
||
| 265 | $data = $new_order_data; |
||
| 266 | } |
||
| 267 | |||
| 268 | return $data; |
||
| 269 | } |
||
| 354 |