| Conditions | 37 |
| Paths | 1293 |
| Total Lines | 130 |
| Code Lines | 76 |
| 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 |
||
| 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 | } |
||
| 294 |