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