| Conditions | 14 |
| Paths | 16 |
| Total Lines | 88 |
| Code Lines | 58 |
| 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 |
||
| 151 | private function buildUserFoldersList(array $userInfo): array |
||
| 152 | { |
||
| 153 | //Build tree |
||
| 154 | $tree = new SplClassLoader('Tree\NestedTree', API_ROOT_PATH . '/../includes/libraries'); |
||
| 155 | $tree->register(); |
||
| 156 | $tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
||
| 157 | |||
| 158 | // Start by adding the manually added folders |
||
| 159 | $allowedFolders = explode(";", $userInfo['groupes_visibles']); |
||
| 160 | $readOnlyFolders = []; |
||
| 161 | $allowedFoldersByRoles = []; |
||
| 162 | $restrictedFoldersForItems = []; |
||
| 163 | $foldersLimited = []; |
||
| 164 | $foldersLimitedFull = []; |
||
| 165 | $personalFolders = []; |
||
| 166 | |||
| 167 | $userFunctionId = str_replace(";", ",", $userInfo['fonction_id']); |
||
| 168 | |||
| 169 | // Get folders from the roles |
||
| 170 | if (empty($userFunctionId) === false) { |
||
| 171 | $rows = $this->select("SELECT * FROM " . prefixTable('roles_values') . " WHERE role_id IN (".$userFunctionId.") AND type IN ('W', 'ND', 'NE', 'NDNE', 'R')"); |
||
| 172 | foreach ($rows as $record) { |
||
| 173 | if ($record['type'] === 'R') { |
||
| 174 | array_push($readOnlyFolders, $record['folder_id']); |
||
| 175 | } elseif (in_array($record['folder_id'], $allowedFolders) === false) { |
||
| 176 | array_push($allowedFoldersByRoles, $record['folder_id']); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | $allowedFoldersByRoles = array_unique($allowedFoldersByRoles); |
||
| 180 | $readOnlyFolders = array_unique($readOnlyFolders); |
||
| 181 | // Clean arrays |
||
| 182 | foreach ($allowedFoldersByRoles as $value) { |
||
| 183 | $key = array_search($value, $readOnlyFolders); |
||
| 184 | if ($key !== false) { |
||
| 185 | unset($readOnlyFolders[$key]); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | // Does this user is allowed to see other items |
||
| 191 | $inc = 0; |
||
| 192 | $rows = $this->select("SELECT id, id_tree FROM " . prefixTable('items') . " WHERE restricted_to LIKE '".$userInfo['id']."'". |
||
| 193 | (empty($userFunctionId) === false ? ' AND id_tree NOT IN ('.$userFunctionId.')' : '')); |
||
| 194 | foreach ($rows as $record) { |
||
| 195 | // Exclude restriction on item if folder is fully accessible |
||
| 196 | $restrictedFoldersForItems[$record['id_tree']][$inc] = $record['id']; |
||
| 197 | ++$inc; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Check for the users roles if some specific rights exist on items |
||
| 201 | $rows = $this->select("SELECT i.id_tree, r.item_id |
||
| 202 | FROM " . prefixTable('items') . " as i |
||
| 203 | INNER JOIN " . prefixTable('restriction_to_roles') . " as r ON (r.item_id=i.id) |
||
| 204 | WHERE ".(empty($userFunctionId) === false ? ' id_tree NOT IN ('.$userFunctionId.') AND ' : '')." i.id_tree != '' |
||
| 205 | ORDER BY i.id_tree ASC"); |
||
| 206 | foreach ($rows as $record) { |
||
| 207 | $foldersLimited[$record['id_tree']][$inc] = $record['item_id']; |
||
| 208 | array_push($foldersLimitedFull, $record['item_id']); |
||
| 209 | ++$inc; |
||
| 210 | } |
||
| 211 | |||
| 212 | // Add all personal folders |
||
| 213 | $rows = $this->select( |
||
| 214 | 'SELECT id |
||
| 215 | FROM ' . prefixTable('nested_tree') . ' |
||
| 216 | WHERE title = '.$userInfo['id'].' AND personal_folder = 1'. |
||
| 217 | (empty($userFunctionId) === false ? ' AND id NOT IN ('.$userFunctionId.')' : ''). |
||
| 218 | ' LIMIT 0,1' |
||
| 219 | ); |
||
| 220 | if (empty($rows['id']) === false) { |
||
| 221 | array_push($personalFolders, $rows['id']); |
||
| 222 | // get all descendants |
||
| 223 | $ids = $tree->getDescendants($rows['id'], false, false, true); |
||
| 224 | foreach ($ids as $id) { |
||
| 225 | array_push($personalFolders, $id); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | // All folders visibles |
||
| 230 | return array_unique( |
||
| 231 | array_filter( |
||
| 232 | array_merge( |
||
| 233 | $allowedFolders, |
||
| 234 | $foldersLimitedFull, |
||
| 235 | $allowedFoldersByRoles, |
||
| 236 | $restrictedFoldersForItems, |
||
| 237 | $readOnlyFolders, |
||
| 238 | $personalFolders |
||
| 239 | ) |
||
| 244 | } |