| Conditions | 44 |
| Paths | 3408 |
| Total Lines | 176 |
| Code Lines | 115 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 95 | public function Field($properties = array()) { |
||
| 96 | Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/CheckboxSetField.css'); |
||
| 97 | Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/PermissionCheckboxSetField.js'); |
||
| 98 | |||
| 99 | $uninheritedCodes = array(); |
||
| 100 | $inheritedCodes = array(); |
||
| 101 | $records = ($this->records) ? $this->records : new ArrayList(); |
||
| 102 | |||
| 103 | // Get existing values from the form record (assuming the formfield name is a join field on the record) |
||
| 104 | if(is_object($this->form)) { |
||
| 105 | $record = $this->form->getRecord(); |
||
| 106 | if( |
||
| 107 | $record |
||
| 108 | && ($record instanceof Group || $record instanceof PermissionRole) |
||
| 109 | && !$records->find('ID', $record->ID) |
||
| 110 | ) { |
||
| 111 | $records->push($record); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // Get all 'inherited' codes not directly assigned to the group (which is stored in $values) |
||
| 116 | foreach($records as $record) { |
||
| 117 | // Get all uninherited permissions |
||
| 118 | $relationMethod = $this->name; |
||
| 119 | foreach($record->$relationMethod() as $permission) { |
||
| 120 | if(!isset($uninheritedCodes[$permission->Code])) $uninheritedCodes[$permission->Code] = array(); |
||
| 121 | $uninheritedCodes[$permission->Code][] = _t( |
||
| 122 | 'PermissionCheckboxSetField.AssignedTo', 'assigned to "{title}"', |
||
| 123 | array('title' => $record->dbObject('Title')->forTemplate()) |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Special case for Group records (not PermissionRole): |
||
| 128 | // Determine inherited assignments |
||
| 129 | if(is_a($record, 'SilverStripe\\Security\\Group')) { |
||
| 130 | // Get all permissions from roles |
||
| 131 | if ($record->Roles()->Count()) { |
||
| 132 | foreach($record->Roles() as $role) { |
||
| 133 | foreach($role->Codes() as $code) { |
||
| 134 | if (!isset($inheritedCodes[$code->Code])) $inheritedCodes[$code->Code] = array(); |
||
| 135 | $inheritedCodes[$code->Code][] = _t( |
||
| 136 | 'PermissionCheckboxSetField.FromRole', |
||
| 137 | 'inherited from role "{title}"', |
||
| 138 | 'A permission inherited from a certain permission role', |
||
| 139 | array('title' => $role->dbObject('Title')->forTemplate()) |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | // Get from parent groups |
||
| 146 | $parentGroups = $record->getAncestors(); |
||
| 147 | if ($parentGroups) { |
||
| 148 | foreach ($parentGroups as $parent) { |
||
| 149 | if (!$parent->Roles()->Count()) continue; |
||
| 150 | foreach($parent->Roles() as $role) { |
||
| 151 | if ($role->Codes()) { |
||
| 152 | foreach($role->Codes() as $code) { |
||
| 153 | if (!isset($inheritedCodes[$code->Code])) $inheritedCodes[$code->Code] = array(); |
||
| 154 | $inheritedCodes[$code->Code][] = _t( |
||
| 155 | 'PermissionCheckboxSetField.FromRoleOnGroup', |
||
| 156 | 'inherited from role "%s" on group "%s"', |
||
| 157 | 'A permission inherited from a role on a certain group', |
||
| 158 | array('roletitle' => $role->dbObject('Title')->forTemplate(), 'grouptitle' => $parent->dbObject('Title')->forTemplate()) |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | if ($parent->Permissions()->Count()) { |
||
| 164 | foreach($parent->Permissions() as $permission) { |
||
| 165 | if (!isset($inheritedCodes[$permission->Code])) { |
||
| 166 | $inheritedCodes[$permission->Code] = array(); |
||
| 167 | } |
||
| 168 | $inheritedCodes[$permission->Code][] = |
||
| 169 | _t( |
||
| 170 | 'PermissionCheckboxSetField.FromGroup', |
||
| 171 | 'inherited from group "{title}"', |
||
| 172 | 'A permission inherited from a certain group', |
||
| 173 | array('title' => $parent->dbObject('Title')->forTemplate()) |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $odd = 0; |
||
| 183 | $options = ''; |
||
| 184 | $globalHidden = (array)Config::inst()->get('SilverStripe\\Security\\Permission', 'hidden_permissions'); |
||
| 185 | if($this->source) { |
||
| 186 | $privilegedPermissions = Permission::config()->privileged_permissions; |
||
| 187 | |||
| 188 | // loop through all available categorized permissions and see if they're assigned for the given groups |
||
| 189 | foreach($this->source as $categoryName => $permissions) { |
||
| 190 | $options .= "<li><h5>$categoryName</h5></li>"; |
||
| 191 | foreach($permissions as $code => $permission) { |
||
| 192 | if(in_array($code, $this->hiddenPermissions)) continue; |
||
| 193 | if(in_array($code, $globalHidden)) continue; |
||
| 194 | |||
| 195 | $value = $permission['name']; |
||
| 196 | |||
| 197 | $odd = ($odd + 1) % 2; |
||
| 198 | $extraClass = $odd ? 'odd' : 'even'; |
||
| 199 | $extraClass .= ' val' . str_replace(' ', '', $code); |
||
| 200 | $itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]+/', '', $code); |
||
| 201 | $checked = $disabled = $inheritMessage = ''; |
||
| 202 | $checked = (isset($uninheritedCodes[$code]) || isset($inheritedCodes[$code])) |
||
| 203 | ? ' checked="checked"' |
||
| 204 | : ''; |
||
| 205 | $title = $permission['help'] |
||
| 206 | ? 'title="' . htmlentities($permission['help'], ENT_COMPAT, 'UTF-8') . '" ' |
||
| 207 | : ''; |
||
| 208 | |||
| 209 | if (isset($inheritedCodes[$code])) { |
||
| 210 | // disable inherited codes, as any saving logic would be too complicate to express in this |
||
| 211 | // interface |
||
| 212 | $disabled = ' disabled="true"'; |
||
| 213 | $inheritMessage = ' (' . join(', ', $inheritedCodes[$code]) . ')'; |
||
| 214 | } elseif($this->records && $this->records->Count() > 1 && isset($uninheritedCodes[$code])) { |
||
| 215 | // If code assignments are collected from more than one "source group", |
||
| 216 | // show its origin automatically |
||
| 217 | $inheritMessage = ' (' . join(', ', $uninheritedCodes[$code]).')'; |
||
| 218 | } |
||
| 219 | |||
| 220 | // Disallow modification of "privileged" permissions unless currently logged-in user is an admin |
||
| 221 | if(!Permission::check('ADMIN') && in_array($code, $privilegedPermissions)) { |
||
| 222 | $disabled = ' disabled="true"'; |
||
| 223 | } |
||
| 224 | |||
| 225 | // If the field is readonly, always mark as "disabled" |
||
| 226 | if($this->readonly) $disabled = ' disabled="true"'; |
||
| 227 | |||
| 228 | $inheritMessage = '<small>' . $inheritMessage . '</small>'; |
||
| 229 | $icon = ($checked) ? 'accept' : 'decline'; |
||
| 230 | |||
| 231 | // If the field is readonly, add a span that will replace the disabled checkbox input |
||
| 232 | if($this->readonly) { |
||
| 233 | $options .= "<li class=\"$extraClass\">" |
||
| 234 | . "<input id=\"$itemID\"$disabled name=\"$this->name[$code]\" type=\"checkbox\"" |
||
| 235 | . " value=\"$code\"$checked class=\"checkbox\" />" |
||
| 236 | . "<label {$title}for=\"$itemID\">" |
||
| 237 | . "<span class=\"ui-button-icon-primary ui-icon btn-icon-$icon\"></span>" |
||
| 238 | . "$value$inheritMessage</label>" |
||
| 239 | . "</li>\n"; |
||
| 240 | } else { |
||
| 241 | $options .= "<li class=\"$extraClass\">" |
||
| 242 | . "<input id=\"$itemID\"$disabled name=\"$this->name[$code]\" type=\"checkbox\"" |
||
| 243 | . " value=\"$code\"$checked class=\"checkbox\" />" |
||
| 244 | . "<label {$title}for=\"$itemID\">$value$inheritMessage</label>" |
||
| 245 | . "</li>\n"; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | if($this->readonly) { |
||
| 251 | return DBField::create_field('HTMLText', |
||
| 252 | "<ul id=\"{$this->ID()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n" . |
||
| 253 | "<li class=\"help\">" . |
||
| 254 | _t( |
||
| 255 | 'Permissions.UserPermissionsIntro', |
||
| 256 | 'Assigning groups to this user will adjust the permissions they have.' |
||
| 257 | . ' See the groups section for details of permissions on individual groups.' |
||
| 258 | ) . |
||
| 259 | "</li>" . |
||
| 260 | $options . |
||
| 261 | "</ul>\n" |
||
| 262 | ); |
||
| 263 | } else { |
||
| 264 | return DBField::create_field('HTMLText', |
||
| 265 | "<ul id=\"{$this->ID()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n" . |
||
| 266 | $options . |
||
| 267 | "</ul>\n" |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 357 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.