| Conditions | 13 |
| Paths | 130 |
| Total Lines | 145 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 88 | public function getEditForm($id = null, $fields = null) { |
||
| 89 | // TODO Duplicate record fetching (see parent implementation) |
||
| 90 | if(!$id) $id = $this->currentPageID(); |
||
| 91 | $form = parent::getEditForm($id); |
||
| 92 | |||
| 93 | // TODO Duplicate record fetching (see parent implementation) |
||
| 94 | $record = $this->getRecord($id); |
||
| 95 | |||
| 96 | if($record && !$record->canView()) { |
||
| 97 | return Security::permissionFailure($this); |
||
| 98 | } |
||
| 99 | |||
| 100 | $memberList = GridField::create( |
||
| 101 | 'Members', |
||
| 102 | false, |
||
| 103 | Member::get(), |
||
| 104 | $memberListConfig = GridFieldConfig_RecordEditor::create() |
||
| 105 | ->addComponent(new GridFieldButtonRow('after')) |
||
| 106 | ->addComponent(new GridFieldExportButton('buttons-after-left')) |
||
| 107 | )->addExtraClass("members_grid"); |
||
| 108 | |||
| 109 | if($record && method_exists($record, 'getValidator')) { |
||
| 110 | $validator = $record->getValidator(); |
||
| 111 | } else { |
||
| 112 | $validator = Member::singleton()->getValidator(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** @var GridFieldDetailForm $detailForm */ |
||
| 116 | $detailForm = $memberListConfig->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDetailForm'); |
||
| 117 | $detailForm |
||
| 118 | ->setValidator($validator); |
||
| 119 | |||
| 120 | $groupList = GridField::create( |
||
| 121 | 'Groups', |
||
| 122 | false, |
||
| 123 | Group::get(), |
||
| 124 | GridFieldConfig_RecordEditor::create() |
||
| 125 | ); |
||
| 126 | /** @var GridFieldDataColumns $columns */ |
||
| 127 | $columns = $groupList->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns'); |
||
| 128 | $columns->setDisplayFields(array( |
||
| 129 | 'Breadcrumbs' => Group::singleton()->fieldLabel('Title') |
||
| 130 | )); |
||
| 131 | $columns->setFieldFormatting(array( |
||
| 132 | 'Breadcrumbs' => function($val, $item) { |
||
| 133 | /** @var Group $item */ |
||
| 134 | return Convert::raw2xml($item->getBreadcrumbs(' > ')); |
||
| 135 | } |
||
| 136 | )); |
||
| 137 | |||
| 138 | $fields = new FieldList( |
||
| 139 | $root = new TabSet( |
||
| 140 | 'Root', |
||
| 141 | $usersTab = new Tab('Users', _t('SecurityAdmin.Users', 'Users'), |
||
| 142 | |||
| 143 | new LiteralField('MembersCautionText', |
||
| 144 | sprintf('<div class="alert alert-warning" role="alert">%s</div>', |
||
| 145 | _t( |
||
| 146 | 'SecurityAdmin.MemberListCaution', |
||
| 147 | 'Caution: Removing members from this list will remove them from all groups and the database' |
||
| 148 | ) |
||
| 149 | ) |
||
| 150 | ), |
||
| 151 | $memberList |
||
| 152 | ), |
||
| 153 | $groupsTab = new Tab('Groups', singleton('SilverStripe\\Security\\Group')->i18n_plural_name(), |
||
| 154 | $groupList |
||
| 155 | ) |
||
| 156 | ), |
||
| 157 | // necessary for tree node selection in LeftAndMain.EditForm.js |
||
| 158 | new HiddenField('ID', false, 0) |
||
| 159 | ); |
||
| 160 | |||
| 161 | // Add import capabilities. Limit to admin since the import logic can affect assigned permissions |
||
| 162 | if(Permission::check('ADMIN')) { |
||
| 163 | $fields->addFieldsToTab('Root.Users', array( |
||
| 164 | new HeaderField('ImportUsersHeader', _t('SecurityAdmin.IMPORTUSERS', 'Import users'), 3), |
||
| 165 | new LiteralField( |
||
| 166 | 'MemberImportFormIframe', |
||
| 167 | sprintf( |
||
| 168 | '<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
| 169 | . '</iframe>', |
||
| 170 | $this->Link('memberimport') |
||
| 171 | ) |
||
| 172 | ) |
||
| 173 | )); |
||
| 174 | $fields->addFieldsToTab('Root.Groups', array( |
||
| 175 | new HeaderField('ImportGroupsHeader', _t('SecurityAdmin.IMPORTGROUPS', 'Import groups'), 3), |
||
| 176 | new LiteralField( |
||
| 177 | 'GroupImportFormIframe', |
||
| 178 | sprintf( |
||
| 179 | '<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
| 180 | . '</iframe>', |
||
| 181 | $this->Link('groupimport') |
||
| 182 | ) |
||
| 183 | ) |
||
| 184 | )); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Tab nav in CMS is rendered through separate template |
||
| 188 | $root->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
||
| 189 | |||
| 190 | // Add roles editing interface |
||
| 191 | $rolesTab = null; |
||
| 192 | if(Permission::check('APPLY_ROLES')) { |
||
| 193 | $rolesField = GridField::create('Roles', |
||
| 194 | false, |
||
| 195 | PermissionRole::get(), |
||
| 196 | GridFieldConfig_RecordEditor::create() |
||
| 197 | ); |
||
| 198 | |||
| 199 | $rolesTab = $fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.TABROLES', 'Roles')); |
||
| 200 | $rolesTab->push($rolesField); |
||
| 201 | } |
||
| 202 | |||
| 203 | $actionParam = $this->getRequest()->param('Action'); |
||
| 204 | if($actionParam == 'groups') { |
||
| 205 | $groupsTab->addExtraClass('ui-state-active'); |
||
| 206 | } elseif($actionParam == 'users') { |
||
| 207 | $usersTab->addExtraClass('ui-state-active'); |
||
| 208 | } elseif($actionParam == 'roles' && $rolesTab) { |
||
| 209 | $rolesTab->addExtraClass('ui-state-active'); |
||
| 210 | } |
||
| 211 | |||
| 212 | $actions = new FieldList(); |
||
| 213 | |||
| 214 | $form = Form::create( |
||
| 215 | $this, |
||
| 216 | 'EditForm', |
||
| 217 | $fields, |
||
| 218 | $actions |
||
| 219 | )->setHTMLID('Form_EditForm'); |
||
| 220 | $form->addExtraClass('cms-edit-form'); |
||
| 221 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
| 222 | // Tab nav in CMS is rendered through separate template |
||
| 223 | if($form->Fields()->hasTabSet()) { |
||
| 224 | $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
||
| 225 | } |
||
| 226 | $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); |
||
| 227 | $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
||
| 228 | |||
| 229 | $this->extend('updateEditForm', $form); |
||
| 230 | |||
| 231 | return $form; |
||
| 232 | } |
||
| 233 | |||
| 365 |