| Conditions | 18 |
| Paths | 128 |
| Total Lines | 114 |
| Code Lines | 70 |
| 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 |
||
| 87 | private function buildUserFoldersList($userInfo) |
||
| 88 | { |
||
| 89 | // Start by adding the manually added folders |
||
| 90 | $allowedFolders = $userInfo['groupes_visibles']; |
||
| 91 | $readOnlyFolders = []; |
||
| 92 | $allowedFoldersByRoles = []; |
||
| 93 | $restrictedFoldersForItems = []; |
||
| 94 | $foldersLimited = []; |
||
| 95 | $foldersLimitedFull = []; |
||
| 96 | |||
| 97 | $userFunctionId = str_replace(";", ",", $userInfo['fonction_id']); |
||
| 98 | |||
| 99 | // Get folders from the roles |
||
| 100 | if (empty($userFunctionId) === false) { |
||
| 101 | $rows = $this->select("SELECT * FROM " . prefixTable('roles_values') . " WHERE role_id IN (".$userFunctionId.") AND type IN ('W', 'ND', 'NE', 'NDNE', 'R')"); |
||
| 102 | foreach ($rows as $record) { |
||
| 103 | if ($record['type'] === 'R') { |
||
| 104 | array_push($readOnlyFolders, $record['folder_id']); |
||
| 105 | } elseif (in_array($record['folder_id'], $allowedFolders) === false) { |
||
| 106 | array_push($allowedFoldersByRoles, $record['folder_id']); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | $allowedFoldersByRoles = array_unique($allowedFoldersByRoles); |
||
| 110 | $readOnlyFolders = array_unique($readOnlyFolders); |
||
| 111 | // Clean arrays |
||
| 112 | foreach ($allowedFoldersByRoles as $value) { |
||
| 113 | $key = array_search($value, $readOnlyFolders); |
||
| 114 | if ($key !== false) { |
||
| 115 | unset($readOnlyFolders[$key]); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // Does this user is allowed to see other items |
||
| 121 | $rows = $this->select("SELECT id, id_tree FROM " . prefixTable('items') . " WHERE WHERE restricted_to LIKE '".$userInfo['id']."'". |
||
| 122 | (empty($userFunctionId) === false ? ' AND id_tree NOT IN ('.$userFunctionId.')' : '')); |
||
| 123 | foreach ($rows as $record) { |
||
| 124 | // Exclude restriction on item if folder is fully accessible |
||
| 125 | //if (in_array($record['id_tree'], $allowedFolders) === false) { |
||
| 126 | $restrictedFoldersForItems[$record['id_tree']][$inc] = $record['id']; |
||
| 127 | //} |
||
| 128 | } |
||
| 129 | |||
| 130 | // Check for the users roles if some specific rights exist on items |
||
| 131 | $rows = $this->select("SELECT i.id_tree, r.item_id |
||
| 132 | FROM " . prefixTable('items') . " as i |
||
| 133 | INNER JOIN " . prefixTable('restriction_to_roles') . " as r ON (r.item_id=i.id) |
||
| 134 | WHERE ".(empty($userFunctionId) === false ? ' id_tree NOT IN ('.$userFunctionId.') AND ' : '')." i.id_tree != '' |
||
| 135 | ORDER BY i.id_tree ASC"); |
||
| 136 | foreach ($rows as $record) { |
||
| 137 | $foldersLimited[$record['id_tree']][$inc] = $record['item_id']; |
||
| 138 | array_push($foldersLimitedFull, $record['item_id']); |
||
| 139 | } |
||
| 140 | |||
| 141 | // TODO ajouter perso folders |
||
| 142 | $rows = $this->select( |
||
| 143 | 'SELECT id |
||
| 144 | FROM ' . prefixTable('nested_tree') . ' |
||
| 145 | WHERE title = '.$userInfo['id'].' AND personal_folder = 1'. |
||
| 146 | (empty($userFunctionId) === false ? ' AND id NOT IN ('.$userFunctionId.')' : ''). |
||
| 147 | ' LIMIT 0,1' |
||
| 148 | ); |
||
| 149 | if (empty($rows['id']) === false) { |
||
| 150 | array_push($personalFolders, $rows['id']); |
||
| 151 | array_push($allowedFolders, $rows['id']); |
||
| 152 | // get all descendants |
||
| 153 | $ids = $tree->getDescendants($rows['id'], false, false, true); |
||
| 154 | foreach ($ids as $id) { |
||
| 155 | array_push($allowedFolders, $id); |
||
| 156 | array_push($personalFolders, $id); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // Exclude all other PF |
||
| 161 | $where = new WhereClause('and'); |
||
| 162 | $where->add('personal_folder=%i', 1); |
||
| 163 | if ( |
||
| 164 | (int) $enablePfFeature === 1 |
||
| 165 | && (int) $globalsPersonalFolders === 1 |
||
| 166 | ) { |
||
| 167 | $where->add('title=%s', $globalsUserId); |
||
| 168 | $where->negateLast(); |
||
| 169 | } |
||
| 170 | $persoFlds = DB::query( |
||
| 171 | 'SELECT id |
||
| 172 | FROM ' . prefixTable('nested_tree') . ' |
||
| 173 | WHERE %l', |
||
| 174 | $wheres |
||
| 175 | ); |
||
| 176 | |||
| 177 | $rows = $this->select( |
||
| 178 | 'SELECT id |
||
| 179 | FROM ' . prefixTable('nested_tree') . ' |
||
| 180 | WHERE title != '.$userInfo['id'].' AND personal_folder = 1' |
||
| 181 | ); |
||
| 182 | |||
| 183 | foreach ($rows as $persoFldId) { |
||
| 184 | array_push($noAccessPersonalFolders, $persoFldId['id']); |
||
| 185 | } |
||
| 186 | |||
| 187 | // All folders visibles |
||
| 188 | $allowedFolders = array_merge( |
||
| 189 | $allowedFolders, |
||
| 190 | $foldersLimitedFull, |
||
| 191 | $allowedFoldersByRoles, |
||
| 192 | $restrictedFoldersForItems, |
||
| 193 | $readOnlyFolders |
||
| 194 | ); |
||
| 195 | // Exclude from allowed folders all the specific user forbidden folders |
||
| 196 | if (count($noAccessFolders) > 0) { |
||
| 197 | $allowedFolders = array_diff($allowedFolders, $noAccessFolders); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $allowedFolders; |
||
| 201 | } |
||
| 202 | } |