Conditions | 6 |
Paths | 16 |
Total Lines | 141 |
Code Lines | 101 |
Lines | 14 |
Ratio | 9.93 % |
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 |
||
89 | public function getCMSFields() |
||
90 | { |
||
91 | $mapFn = function ($groups = []) { |
||
92 | $map = []; |
||
93 | foreach ($groups as $group) { |
||
94 | // Listboxfield values are escaped, use ASCII char instead of » |
||
95 | $map[$group->ID] = $group->getBreadcrumbs(' > '); |
||
96 | } |
||
97 | asort($map); |
||
98 | return $map; |
||
99 | }; |
||
100 | $groupsMap = $mapFn(Group::get()); |
||
101 | $viewAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_VIEW_ALL', 'ADMIN'])); |
||
102 | $editAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_EDIT_ALL', 'ADMIN'])); |
||
103 | |||
104 | $fields = new FieldList( |
||
105 | new TabSet( |
||
106 | "Root", |
||
107 | $tabMain = new Tab( |
||
108 | 'Main', |
||
109 | $titleField = new TextField("Title", _t(self::class . '.SITETITLE', "Site title")), |
||
110 | $taglineField = new TextField("Tagline", _t(self::class . '.SITETAGLINE', "Site Tagline/Slogan")) |
||
111 | ), |
||
112 | $tabAccess = new Tab( |
||
113 | 'Access', |
||
114 | $viewersOptionsField = new OptionsetField( |
||
115 | "CanViewType", |
||
116 | _t(self::class . '.VIEWHEADER', "Who can view pages on this site?") |
||
117 | ), |
||
118 | $viewerGroupsField = ListboxField::create( |
||
119 | "ViewerGroups", |
||
120 | _t(SiteTree::class . '.VIEWERGROUPS', "Viewer Groups") |
||
121 | ) |
||
122 | ->setSource($groupsMap) |
||
123 | ->setAttribute( |
||
124 | 'data-placeholder', |
||
125 | _t(SiteTree::class . '.GroupPlaceholder', 'Click to select group') |
||
126 | ), |
||
127 | $editorsOptionsField = new OptionsetField( |
||
128 | "CanEditType", |
||
129 | _t(self::class . '.EDITHEADER', "Who can edit pages on this site?") |
||
130 | ), |
||
131 | $editorGroupsField = ListboxField::create( |
||
132 | "EditorGroups", |
||
133 | _t(SiteTree::class . '.EDITORGROUPS', "Editor Groups") |
||
134 | ) |
||
135 | ->setSource($groupsMap) |
||
136 | ->setAttribute( |
||
137 | 'data-placeholder', |
||
138 | _t(SiteTree::class . '.GroupPlaceholder', 'Click to select group') |
||
139 | ), |
||
140 | $topLevelCreatorsOptionsField = new OptionsetField( |
||
141 | "CanCreateTopLevelType", |
||
142 | _t(self::class . '.TOPLEVELCREATE', "Who can create pages in the root of the site?") |
||
143 | ), |
||
144 | $topLevelCreatorsGroupsField = ListboxField::create( |
||
145 | "CreateTopLevelGroups", |
||
146 | _t(self::class . '.TOPLEVELCREATORGROUPS', "Top level creators") |
||
147 | ) |
||
148 | ->setSource($groupsMap) |
||
149 | ->setAttribute( |
||
150 | 'data-placeholder', |
||
151 | _t(SiteTree::class . '.GroupPlaceholder', 'Click to select group') |
||
152 | ) |
||
153 | ) |
||
154 | ), |
||
155 | new HiddenField('ID') |
||
156 | ); |
||
157 | |||
158 | $viewersOptionsSource = []; |
||
159 | $viewersOptionsSource["Anyone"] = _t(SiteTree::class . '.ACCESSANYONE', "Anyone"); |
||
160 | $viewersOptionsSource["LoggedInUsers"] = _t( |
||
161 | SiteTree::class . '.ACCESSLOGGEDIN', |
||
162 | "Logged-in users" |
||
163 | ); |
||
164 | $viewersOptionsSource["OnlyTheseUsers"] = _t( |
||
165 | SiteTree::class . '.ACCESSONLYTHESE', |
||
166 | "Only these groups (choose from list)" |
||
167 | ); |
||
168 | $viewersOptionsField->setSource($viewersOptionsSource); |
||
169 | |||
170 | View Code Duplication | if ($viewAllGroupsMap) { |
|
171 | $viewerGroupsField->setDescription(_t( |
||
172 | SiteTree::class . '.VIEWER_GROUPS_FIELD_DESC', |
||
173 | 'Groups with global view permissions: {groupList}', |
||
174 | ['groupList' => implode(', ', array_values($viewAllGroupsMap))] |
||
175 | )); |
||
176 | } |
||
177 | |||
178 | View Code Duplication | if ($editAllGroupsMap) { |
|
179 | $editorGroupsField->setDescription(_t( |
||
180 | SiteTree::class . '.EDITOR_GROUPS_FIELD_DESC', |
||
181 | 'Groups with global edit permissions: {groupList}', |
||
182 | ['groupList' => implode(', ', array_values($editAllGroupsMap))] |
||
183 | )); |
||
184 | } |
||
185 | |||
186 | $editorsOptionsSource = []; |
||
187 | $editorsOptionsSource["LoggedInUsers"] = _t( |
||
188 | SiteTree::class . '.EDITANYONE', |
||
189 | "Anyone who can log-in to the CMS" |
||
190 | ); |
||
191 | $editorsOptionsSource["OnlyTheseUsers"] = _t( |
||
192 | SiteTree::class . '.EDITONLYTHESE', |
||
193 | "Only these groups (choose from list)" |
||
194 | ); |
||
195 | $editorsOptionsField->setSource($editorsOptionsSource); |
||
196 | |||
197 | $topLevelCreatorsOptionsField->setSource($editorsOptionsSource); |
||
198 | |||
199 | if (!Permission::check('EDIT_SITECONFIG')) { |
||
200 | $fields->makeFieldReadonly($viewersOptionsField); |
||
201 | $fields->makeFieldReadonly($viewerGroupsField); |
||
202 | $fields->makeFieldReadonly($editorsOptionsField); |
||
203 | $fields->makeFieldReadonly($editorGroupsField); |
||
204 | $fields->makeFieldReadonly($topLevelCreatorsOptionsField); |
||
205 | $fields->makeFieldReadonly($topLevelCreatorsGroupsField); |
||
206 | $fields->makeFieldReadonly($taglineField); |
||
207 | $fields->makeFieldReadonly($titleField); |
||
208 | } |
||
209 | |||
210 | if (file_exists(BASE_PATH . '/install.php')) { |
||
211 | $fields->addFieldToTab( |
||
212 | "Root.Main", |
||
213 | new LiteralField( |
||
214 | "InstallWarningHeader", |
||
215 | "<p class=\"message warning\">" . _t( |
||
216 | SiteTree::class . 'REMOVE_INSTALL_WARNING', |
||
217 | 'Warning: You should remove install.php from this SilverStripe install for security reasons.' |
||
218 | ) . "</p>" |
||
219 | ), |
||
220 | "Title" |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | $tabMain->setTitle(_t(self::class . '.TABMAIN', "Main")); |
||
225 | $tabAccess->setTitle(_t(self::class . '.TABACCESS', "Access")); |
||
226 | $this->extend('updateCMSFields', $fields); |
||
227 | |||
228 | return $fields; |
||
229 | } |
||
230 | |||
490 |