Total Complexity | 53 |
Total Lines | 662 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Index often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Index extends BackendBaseActionIndex |
||
17 | { |
||
18 | /** |
||
19 | * The form instance |
||
20 | * |
||
21 | * @var BackendForm |
||
22 | */ |
||
23 | private $form; |
||
24 | |||
25 | /** |
||
26 | * Should we show boxes for their API keys |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $needsAkismet; |
||
31 | private $needsGoogleMaps; |
||
32 | private $needsGoogleRecaptcha; |
||
33 | |||
34 | public function execute(): void |
||
52 | } |
||
53 | |||
54 | private function loadForm(): void |
||
55 | { |
||
56 | // list of default domains |
||
57 | $defaultDomains = [str_replace(['http://', 'www.', 'https://'], '', SITE_URL)]; |
||
58 | |||
59 | // create form |
||
60 | $this->form = new BackendForm('settingsIndex'); |
||
61 | |||
62 | // general settings |
||
63 | $this->form->addText( |
||
64 | 'site_title', |
||
65 | $this->get('fork.settings')->get('Core', 'site_title_' . BL::getWorkingLanguage(), SITE_DEFAULT_TITLE) |
||
66 | ); |
||
67 | |||
68 | // Google tracking settings |
||
69 | $googleTrackingAnalyticsTrackingId = $this->get('fork.settings')->get( |
||
70 | 'Core', |
||
71 | 'google_tracking_google_analytics_tracking_id', |
||
72 | $this->get('fork.settings')->get('Analytics', 'web_property_id', '') |
||
73 | ); |
||
74 | $this->form->addCheckbox( |
||
75 | 'google_tracking_google_analytics_tracking_id_enabled', |
||
76 | ($googleTrackingAnalyticsTrackingId !== '') |
||
77 | ); |
||
78 | $googleTrackingAnalyticsTrackingIdField = $this->form->addText( |
||
79 | 'google_tracking_google_analytics_tracking_id', |
||
80 | $googleTrackingAnalyticsTrackingId |
||
81 | ); |
||
82 | if ($googleTrackingAnalyticsTrackingId === '') { |
||
83 | $googleTrackingAnalyticsTrackingIdField->setAttribute('disabled', 'disabled'); |
||
84 | } |
||
85 | |||
86 | $googleTrackingTagManagerContainerId = $this->get('fork.settings')->get( |
||
87 | 'Core', |
||
88 | 'google_tracking_google_tag_manager_container_id', |
||
89 | '' |
||
90 | ); |
||
91 | $this->form->addCheckbox( |
||
92 | 'google_tracking_google_tag_manager_container_id_enabled', |
||
93 | ($googleTrackingTagManagerContainerId !== '') |
||
94 | ); |
||
95 | $googleTrackingTagManagerContainerIdField = $this->form->addText( |
||
96 | 'google_tracking_google_tag_manager_container_id', |
||
97 | $googleTrackingTagManagerContainerId |
||
98 | ); |
||
99 | if ($googleTrackingTagManagerContainerId === '') { |
||
100 | $googleTrackingTagManagerContainerIdField->setAttribute('disabled', 'disabled'); |
||
101 | } |
||
102 | |||
103 | // @deprecated fallback to site_html_header as this was used in the past. |
||
104 | $siteHtmlHeadValue = $this->get('fork.settings')->get( |
||
105 | 'Core', |
||
106 | 'site_html_head', |
||
107 | $this->get('fork.settings')->get('Core', 'site_html_header', null) |
||
108 | ); |
||
109 | $this->form->addTextarea( |
||
110 | 'site_html_head', |
||
111 | $siteHtmlHeadValue, |
||
112 | 'form-control code', |
||
113 | 'form-control danger code', |
||
114 | true |
||
115 | ); |
||
116 | $siteHtmlStartOfBodyValue = $this->get('fork.settings')->get( |
||
117 | 'Core', |
||
118 | 'site_html_start_of_body', |
||
119 | $this->get('fork.settings')->get('Core', 'site_start_of_body_scripts', null) |
||
120 | ); |
||
121 | $this->form->addTextarea( |
||
122 | 'site_html_start_of_body', |
||
123 | $siteHtmlStartOfBodyValue, |
||
124 | 'form-control code', |
||
125 | 'form-control danger code', |
||
126 | true |
||
127 | ); |
||
128 | $siteHtmlEndOfBodyValue = $this->get('fork.settings')->get( |
||
129 | 'Core', |
||
130 | 'site_html_end_of_body', |
||
131 | $this->get('fork.settings')->get('Core', 'site_html_footer', null) |
||
132 | ); |
||
133 | $this->form->addTextarea( |
||
134 | 'site_html_end_of_body', |
||
135 | $siteHtmlEndOfBodyValue, |
||
136 | 'form-control code', |
||
137 | 'form-control danger code', |
||
138 | true |
||
139 | ); |
||
140 | $this->form->addTextarea( |
||
141 | 'site_domains', |
||
142 | implode("\n", (array) $this->get('fork.settings')->get('Core', 'site_domains', $defaultDomains)), |
||
143 | 'form-control code', |
||
144 | 'form-control danger code' |
||
145 | ); |
||
146 | |||
147 | // facebook settings |
||
148 | // @deprecated remove this in Fork 6, facebook_admin_ids / facebook_app_id / facebook_app_secret should be removed |
||
149 | $this->form->addText('facebook_admin_ids', $this->get('fork.settings')->get('Core', 'facebook_admin_ids', null)); |
||
150 | $this->form->addText('facebook_application_id', $this->get('fork.settings')->get('Core', 'facebook_app_id', null)); |
||
151 | $this->form->addText( |
||
152 | 'facebook_application_secret', |
||
153 | $this->get('fork.settings')->get('Core', 'facebook_app_secret', null) |
||
154 | ); |
||
155 | |||
156 | // twitter settings |
||
157 | $this->form->addText( |
||
158 | 'twitter_site_name', |
||
159 | ltrim($this->get('fork.settings')->get('Core', 'twitter_site_name', null), '@') |
||
160 | ); |
||
161 | |||
162 | // ckfinder |
||
163 | $this->form->addText( |
||
164 | 'ckfinder_license_name', |
||
165 | $this->get('fork.settings')->get('Core', 'ckfinder_license_name', null) |
||
166 | ); |
||
167 | $this->form->addText( |
||
168 | 'ckfinder_license_key', |
||
169 | $this->get('fork.settings')->get('Core', 'ckfinder_license_key', null) |
||
170 | ); |
||
171 | $this->form->addText( |
||
172 | 'ckfinder_image_max_width', |
||
173 | $this->get('fork.settings')->get('Core', 'ckfinder_image_max_width', 1600) |
||
174 | ); |
||
175 | $this->form->addText( |
||
176 | 'ckfinder_image_max_height', |
||
177 | $this->get('fork.settings')->get('Core', 'ckfinder_image_max_height', 1200) |
||
178 | ); |
||
179 | |||
180 | // date & time formats |
||
181 | $this->form->addDropdown( |
||
182 | 'time_format', |
||
183 | BackendModel::getTimeFormats(), |
||
184 | $this->get('fork.settings')->get('Core', 'time_format') |
||
185 | ); |
||
186 | $this->form->addDropdown( |
||
187 | 'date_format_short', |
||
188 | BackendModel::getDateFormatsShort(), |
||
189 | $this->get('fork.settings')->get('Core', 'date_format_short') |
||
190 | ); |
||
191 | $this->form->addDropdown( |
||
192 | 'date_format_long', |
||
193 | BackendModel::getDateFormatsLong(), |
||
194 | $this->get('fork.settings')->get('Core', 'date_format_long') |
||
195 | ); |
||
196 | |||
197 | // number formats |
||
198 | $this->form->addDropdown( |
||
199 | 'number_format', |
||
200 | BackendModel::getNumberFormats(), |
||
201 | $this->get('fork.settings')->get('Core', 'number_format') |
||
202 | ); |
||
203 | |||
204 | $activeLanguages = []; |
||
205 | $redirectLanguages = []; |
||
206 | |||
207 | // create a list of the languages |
||
208 | foreach ($this->get('fork.settings')->get('Core', 'languages', ['en']) as $abbreviation) { |
||
209 | // is this the default language |
||
210 | $defaultLanguage = $abbreviation === SITE_DEFAULT_LANGUAGE; |
||
211 | |||
212 | // attributes |
||
213 | $activeAttributes = []; |
||
214 | $activeAttributes['id'] = 'active_language_' . $abbreviation; |
||
215 | $redirectAttributes = []; |
||
216 | $redirectAttributes['id'] = 'redirect_language_' . $abbreviation; |
||
217 | |||
218 | // fetch label |
||
219 | $label = BL::lbl(mb_strtoupper($abbreviation), 'Core'); |
||
220 | |||
221 | // default may not be unselected |
||
222 | if ($defaultLanguage) { |
||
223 | // add to attributes |
||
224 | $activeAttributes['disabled'] = 'disabled'; |
||
225 | $redirectAttributes['disabled'] = 'disabled'; |
||
226 | |||
227 | // overrule in $_POST |
||
228 | if (!isset($_POST['active_languages']) || !is_array($_POST['active_languages'])) { |
||
229 | $_POST['active_languages'] = [SITE_DEFAULT_LANGUAGE]; |
||
230 | } elseif (!in_array( |
||
231 | $abbreviation, |
||
232 | $_POST['active_languages'] |
||
233 | ) |
||
234 | ) { |
||
235 | $_POST['active_languages'][] = $abbreviation; |
||
236 | } |
||
237 | if (!isset($_POST['redirect_languages']) || !is_array($_POST['redirect_languages'])) { |
||
238 | $_POST['redirect_languages'] = [SITE_DEFAULT_LANGUAGE]; |
||
239 | } elseif (!in_array( |
||
240 | $abbreviation, |
||
241 | $_POST['redirect_languages'] |
||
242 | ) |
||
243 | ) { |
||
244 | $_POST['redirect_languages'][] = $abbreviation; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | // add to the list |
||
249 | $activeLanguages[] = [ |
||
250 | 'label' => $label, |
||
251 | 'value' => $abbreviation, |
||
252 | 'attributes' => $activeAttributes, |
||
253 | 'variables' => ['default' => $defaultLanguage], |
||
254 | ]; |
||
255 | $redirectLanguages[] = [ |
||
256 | 'label' => $label, |
||
257 | 'value' => $abbreviation, |
||
258 | 'attributes' => $redirectAttributes, |
||
259 | 'variables' => ['default' => $defaultLanguage], |
||
260 | ]; |
||
261 | } |
||
262 | |||
263 | $hasMultipleLanguages = BackendModel::getContainer()->getParameter('site.multilanguage'); |
||
264 | |||
265 | // create multilanguage checkbox |
||
266 | $this->form->addMultiCheckbox( |
||
267 | 'active_languages', |
||
268 | $activeLanguages, |
||
269 | $this->get('fork.settings')->get('Core', 'active_languages', [$hasMultipleLanguages]) |
||
270 | ); |
||
271 | $this->form->addMultiCheckbox( |
||
272 | 'redirect_languages', |
||
273 | $redirectLanguages, |
||
274 | $this->get('fork.settings')->get('Core', 'redirect_languages', [$hasMultipleLanguages]) |
||
275 | ); |
||
276 | |||
277 | // api keys are not required for every module |
||
278 | if ($this->needsAkismet) { |
||
279 | $this->form->addText( |
||
280 | 'akismet_key', |
||
281 | $this->get('fork.settings')->get('Core', 'akismet_key', null) |
||
282 | ); |
||
283 | } |
||
284 | if ($this->needsGoogleMaps) { |
||
285 | $this->form->addText( |
||
286 | 'google_maps_key', |
||
287 | $this->get('fork.settings')->get('Core', 'google_maps_key', null) |
||
288 | ); |
||
289 | } |
||
290 | if ($this->needsGoogleRecaptcha) { |
||
291 | $this->form->addText( |
||
292 | 'google_recaptcha_site_key', |
||
293 | $this->get('fork.settings')->get('Core', 'google_recaptcha_site_key', null) |
||
294 | ); |
||
295 | $this->form->addText( |
||
296 | 'google_recaptcha_secret_key', |
||
297 | $this->get('fork.settings')->get('Core', 'google_recaptcha_secret_key', null) |
||
298 | ); |
||
299 | } |
||
300 | |||
301 | // cookies |
||
302 | // @deprecated remove this in Fork 6, the privacy consent dialog should be used |
||
303 | $this->form->addCheckbox('show_cookie_bar', $this->get('fork.settings')->get('Core', 'show_cookie_bar', false)); |
||
304 | |||
305 | // privacy |
||
306 | $this->form->addCheckbox( |
||
307 | 'show_consent_dialog', |
||
308 | $this->get('fork.settings')->get('Core', 'show_consent_dialog', false) |
||
309 | ); |
||
310 | $this->form->addText( |
||
311 | 'privacy_consent_levels', |
||
312 | implode( |
||
313 | ',', |
||
314 | $this->get('fork.settings')->get( |
||
315 | 'Core', |
||
316 | 'privacy_consent_levels', |
||
317 | [] |
||
318 | ) |
||
319 | ) |
||
320 | ); |
||
321 | } |
||
322 | |||
323 | protected function parse(): void |
||
324 | { |
||
325 | parent::parse(); |
||
326 | |||
327 | // show options |
||
328 | if ($this->needsAkismet) { |
||
329 | $this->template->assign('needsAkismet', true); |
||
330 | } |
||
331 | if ($this->needsGoogleMaps) { |
||
332 | $this->template->assign('needsGoogleMaps', true); |
||
333 | } |
||
334 | if ($this->needsGoogleRecaptcha) { |
||
335 | $this->template->assign('needsGoogleRecaptcha', true); |
||
336 | } |
||
337 | |||
338 | // parse the form |
||
339 | $this->form->parse($this->template); |
||
340 | |||
341 | // parse the warnings |
||
342 | $this->parseWarnings(); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Show the warnings based on the active modules & configured settings |
||
347 | */ |
||
348 | private function parseWarnings(): void |
||
355 | } |
||
356 | |||
357 | private function validateForm(): void |
||
678 | } |
||
679 | } |
||
680 | } |
||
682 |