Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LeftAndMain 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LeftAndMain, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | class LeftAndMain extends Controller implements PermissionProvider { |
||
68 | |||
69 | /** |
||
70 | * Enable front-end debugging (increases verbosity) in dev mode. |
||
71 | * Will be ignored in live environments. |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private static $client_debugging = true; |
||
76 | |||
77 | /** |
||
78 | * The current url segment attached to the LeftAndMain instance |
||
79 | * |
||
80 | * @config |
||
81 | * @var string |
||
82 | */ |
||
83 | private static $url_segment; |
||
84 | |||
85 | /** |
||
86 | * @config |
||
87 | * @var string |
||
88 | */ |
||
89 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
90 | |||
91 | /** |
||
92 | * @config |
||
93 | * @var string |
||
94 | */ |
||
95 | private static $menu_title; |
||
96 | |||
97 | /** |
||
98 | * @config |
||
99 | * @var string |
||
100 | */ |
||
101 | private static $menu_icon; |
||
102 | |||
103 | /** |
||
104 | * @config |
||
105 | * @var int |
||
106 | */ |
||
107 | private static $menu_priority = 0; |
||
108 | |||
109 | /** |
||
110 | * @config |
||
111 | * @var int |
||
112 | */ |
||
113 | private static $url_priority = 50; |
||
114 | |||
115 | /** |
||
116 | * A subclass of {@link DataObject}. |
||
117 | * |
||
118 | * Determines what is managed in this interface, through |
||
119 | * {@link getEditForm()} and other logic. |
||
120 | * |
||
121 | * @config |
||
122 | * @var string |
||
123 | */ |
||
124 | private static $tree_class = null; |
||
125 | |||
126 | /** |
||
127 | * The url used for the link in the Help tab in the backend |
||
128 | * |
||
129 | * @config |
||
130 | * @var string |
||
131 | */ |
||
132 | private static $help_link = '//userhelp.silverstripe.org/framework/en/3.3'; |
||
133 | |||
134 | /** |
||
135 | * @var array |
||
136 | */ |
||
137 | private static $allowed_actions = [ |
||
|
|||
138 | 'index', |
||
139 | 'save', |
||
140 | 'savetreenode', |
||
141 | 'getsubtree', |
||
142 | 'updatetreenodes', |
||
143 | 'printable', |
||
144 | 'show', |
||
145 | 'EditorToolbar', |
||
146 | 'EditForm', |
||
147 | 'AddForm', |
||
148 | 'batchactions', |
||
149 | 'BatchActionsForm', |
||
150 | 'schema', |
||
151 | ]; |
||
152 | |||
153 | private static $url_handlers = [ |
||
154 | 'GET schema/$FormName/$ItemID' => 'schema' |
||
155 | ]; |
||
156 | |||
157 | private static $dependencies = [ |
||
158 | 'schema' => '%$FormSchema' |
||
159 | ]; |
||
160 | |||
161 | /** |
||
162 | * Assign themes to use for cms |
||
163 | * |
||
164 | * @config |
||
165 | * @var array |
||
166 | */ |
||
167 | private static $admin_themes = [ |
||
168 | '/framework/admin/themes/cms-forms', |
||
169 | SSViewer::DEFAULT_THEME, |
||
170 | ]; |
||
171 | |||
172 | /** |
||
173 | * Codes which are required from the current user to view this controller. |
||
174 | * If multiple codes are provided, all of them are required. |
||
175 | * All CMS controllers require "CMS_ACCESS_LeftAndMain" as a baseline check, |
||
176 | * and fall back to "CMS_ACCESS_<class>" if no permissions are defined here. |
||
177 | * See {@link canView()} for more details on permission checks. |
||
178 | * |
||
179 | * @config |
||
180 | * @var array |
||
181 | */ |
||
182 | private static $required_permission_codes; |
||
183 | |||
184 | /** |
||
185 | * @config |
||
186 | * @var String Namespace for session info, e.g. current record. |
||
187 | * Defaults to the current class name, but can be amended to share a namespace in case |
||
188 | * controllers are logically bundled together, and mainly separated |
||
189 | * to achieve more flexible templating. |
||
190 | */ |
||
191 | private static $session_namespace; |
||
192 | |||
193 | /** |
||
194 | * Register additional requirements through the {@link Requirements} class. |
||
195 | * Used mainly to work around the missing "lazy loading" functionality |
||
196 | * for getting css/javascript required after an ajax-call (e.g. loading the editform). |
||
197 | * |
||
198 | * YAML configuration example: |
||
199 | * <code> |
||
200 | * LeftAndMain: |
||
201 | * extra_requirements_javascript: |
||
202 | * mysite/javascript/myscript.js: |
||
203 | * </code> |
||
204 | * |
||
205 | * @config |
||
206 | * @var array |
||
207 | */ |
||
208 | private static $extra_requirements_javascript = array(); |
||
209 | |||
210 | /** |
||
211 | * YAML configuration example: |
||
212 | * <code> |
||
213 | * LeftAndMain: |
||
214 | * extra_requirements_css: |
||
215 | * mysite/css/mystyle.css: |
||
216 | * media: screen |
||
217 | * </code> |
||
218 | * |
||
219 | * @config |
||
220 | * @var array See {@link extra_requirements_javascript} |
||
221 | */ |
||
222 | private static $extra_requirements_css = array(); |
||
223 | |||
224 | /** |
||
225 | * @config |
||
226 | * @var array See {@link extra_requirements_javascript} |
||
227 | */ |
||
228 | private static $extra_requirements_themedCss = array(); |
||
229 | |||
230 | /** |
||
231 | * If true, call a keepalive ping every 5 minutes from the CMS interface, |
||
232 | * to ensure that the session never dies. |
||
233 | * |
||
234 | * @config |
||
235 | * @var boolean |
||
236 | */ |
||
237 | private static $session_keepalive_ping = true; |
||
238 | |||
239 | /** |
||
240 | * Value of X-Frame-Options header |
||
241 | * |
||
242 | * @config |
||
243 | * @var string |
||
244 | */ |
||
245 | private static $frame_options = 'SAMEORIGIN'; |
||
246 | |||
247 | /** |
||
248 | * @var PjaxResponseNegotiator |
||
249 | */ |
||
250 | protected $responseNegotiator; |
||
251 | |||
252 | /** |
||
253 | * Gets the combined configuration of all LeafAndMain subclasses required by the client app. |
||
254 | * |
||
255 | * @return array |
||
256 | * |
||
257 | * WARNING: Experimental API |
||
258 | */ |
||
259 | public function getCombinedClientConfig() { |
||
260 | $combinedClientConfig = ['sections' => []]; |
||
261 | $cmsClassNames = CMSMenu::get_cms_classes('SilverStripe\\Admin\\LeftAndMain', true, CMSMenu::URL_PRIORITY); |
||
262 | |||
263 | foreach ($cmsClassNames as $className) { |
||
264 | $combinedClientConfig['sections'][$className] = Injector::inst()->get($className)->getClientConfig(); |
||
265 | } |
||
266 | |||
267 | // Pass in base url (absolute and relative) |
||
268 | $combinedClientConfig['baseUrl'] = Director::baseURL(); |
||
269 | $combinedClientConfig['absoluteBaseUrl'] = Director::absoluteBaseURL(); |
||
270 | $combinedClientConfig['adminUrl'] = AdminRootController::admin_url(); |
||
271 | |||
272 | // Get "global" CSRF token for use in JavaScript |
||
273 | $token = SecurityToken::inst(); |
||
274 | $combinedClientConfig[$token->getName()] = $token->getValue(); |
||
275 | |||
276 | // Set env |
||
277 | $combinedClientConfig['environment'] = Director::get_environment_type(); |
||
278 | $combinedClientConfig['debugging'] = $this->config()->client_debugging; |
||
279 | |||
280 | return Convert::raw2json($combinedClientConfig); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Returns configuration required by the client app. |
||
285 | * |
||
286 | * @return array |
||
287 | * |
||
288 | * WARNING: Experimental API |
||
289 | */ |
||
290 | public function getClientConfig() { |
||
291 | return [ |
||
292 | // Trim leading/trailing slash to make it easier to concatenate URL |
||
293 | // and use in routing definitions. |
||
294 | 'url' => trim($this->Link(), '/'), |
||
295 | ]; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Gets a JSON schema representing the current edit form. |
||
300 | * |
||
301 | * WARNING: Experimental API. |
||
302 | * |
||
303 | * @param HTTPRequest $request |
||
304 | * @return HTTPResponse |
||
305 | */ |
||
306 | public function schema($request) { |
||
307 | $response = $this->getResponse(); |
||
308 | $formName = $request->param('FormName'); |
||
309 | $itemID = $request->param('ItemID'); |
||
310 | |||
311 | if (!$formName) { |
||
312 | return (new HTTPResponse('Missing request params', 400)); |
||
313 | } |
||
314 | |||
315 | if(!$this->hasMethod("get{$formName}")) { |
||
316 | return (new HTTPResponse('Form not found', 404)); |
||
317 | } |
||
318 | |||
319 | if(!$this->hasAction($formName)) { |
||
320 | return (new HTTPResponse('Form not accessible', 401)); |
||
321 | } |
||
322 | |||
323 | $form = $this->{"get{$formName}"}($itemID); |
||
324 | |||
325 | $response->addHeader('Content-Type', 'application/json'); |
||
326 | $response->setBody(Convert::raw2json($this->getSchemaForForm($form))); |
||
327 | |||
328 | return $response; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Given a form, generate a response containing the requested form |
||
333 | * schema if X-Formschema-Request header is set. |
||
334 | * |
||
335 | * @param Form $form |
||
336 | * @return HTTPResponse |
||
337 | */ |
||
338 | protected function getSchemaResponse($form) { |
||
339 | $request = $this->getRequest(); |
||
340 | if($request->getHeader('X-Formschema-Request')) { |
||
341 | $data = $this->getSchemaForForm($form); |
||
342 | $response = new HTTPResponse(Convert::raw2json($data)); |
||
343 | $response->addHeader('Content-Type', 'application/json'); |
||
344 | return $response; |
||
345 | } |
||
346 | return null; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Returns a representation of the provided {@link Form} as structured data, |
||
351 | * based on the request data. |
||
352 | * |
||
353 | * @param Form $form |
||
354 | * @return array |
||
355 | */ |
||
356 | protected function getSchemaForForm(Form $form) { |
||
357 | $request = $this->getRequest(); |
||
358 | $return = null; |
||
359 | |||
360 | // Valid values for the "X-Formschema-Request" header are "schema" and "state". |
||
361 | // If either of these values are set they will be stored in the $schemaParst array |
||
362 | // and used to construct the response body. |
||
363 | if ($schemaHeader = $request->getHeader('X-Formschema-Request')) { |
||
364 | $schemaParts = array_filter(explode(',', $schemaHeader), function($value) { |
||
365 | $validHeaderValues = ['schema', 'state']; |
||
366 | return in_array(trim($value), $validHeaderValues); |
||
367 | }); |
||
368 | } else { |
||
369 | $schemaParts = ['schema']; |
||
370 | } |
||
371 | |||
372 | $return = ['id' => $form->FormName()]; |
||
373 | |||
374 | if (in_array('schema', $schemaParts)) { |
||
375 | $return['schema'] = $this->schema->getSchema($form); |
||
376 | } |
||
377 | |||
378 | if (in_array('state', $schemaParts)) { |
||
379 | $return['state'] = $this->schema->getState($form); |
||
380 | } |
||
381 | |||
382 | return $return; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @param Member $member |
||
387 | * @return boolean |
||
388 | */ |
||
389 | public function canView($member = null) { |
||
390 | if(!$member && $member !== FALSE) $member = Member::currentUser(); |
||
391 | |||
392 | // cms menus only for logged-in members |
||
393 | if(!$member) return false; |
||
394 | |||
395 | // alternative extended checks |
||
396 | if($this->hasMethod('alternateAccessCheck')) { |
||
397 | $alternateAllowed = $this->alternateAccessCheck(); |
||
398 | if($alternateAllowed === false) { |
||
399 | return false; |
||
400 | } |
||
401 | } |
||
402 | |||
403 | // Check for "CMS admin" permission |
||
404 | if(Permission::checkMember($member, "CMS_ACCESS_LeftAndMain")) { |
||
405 | return true; |
||
406 | } |
||
407 | |||
408 | // Check for LeftAndMain sub-class permissions |
||
409 | $codes = $this->getRequiredPermissions(); |
||
410 | if($codes === false) { // allow explicit FALSE to disable subclass check |
||
411 | return true; |
||
412 | } |
||
413 | foreach((array)$codes as $code) { |
||
414 | if(!Permission::checkMember($member, $code)) { |
||
415 | return false; |
||
416 | } |
||
417 | } |
||
418 | |||
419 | return true; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Get list of required permissions |
||
424 | * |
||
425 | * @return array|string|bool Code, array of codes, or false if no permission required |
||
426 | */ |
||
427 | public static function getRequiredPermissions() { |
||
428 | $class = get_called_class(); |
||
429 | $code = Config::inst()->get($class, 'required_permission_codes', Config::FIRST_SET); |
||
430 | if ($code === false) { |
||
431 | return false; |
||
432 | } |
||
433 | if ($code) { |
||
434 | return $code; |
||
435 | } |
||
436 | return "CMS_ACCESS_" . $class; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @uses LeftAndMainExtension->init() |
||
441 | * @uses LeftAndMainExtension->accessedCMS() |
||
442 | * @uses CMSMenu |
||
443 | */ |
||
444 | protected function init() { |
||
445 | parent::init(); |
||
446 | |||
447 | SSViewer::config()->update('rewrite_hash_links', false); |
||
448 | ContentNegotiator::config()->update('enabled', false); |
||
449 | |||
450 | // set language |
||
451 | $member = Member::currentUser(); |
||
452 | if(!empty($member->Locale)) { |
||
453 | i18n::set_locale($member->Locale); |
||
454 | } |
||
455 | if(!empty($member->DateFormat)) { |
||
456 | i18n::config()->date_format = $member->DateFormat; |
||
457 | } |
||
458 | if(!empty($member->TimeFormat)) { |
||
459 | i18n::config()->time_format = $member->TimeFormat; |
||
460 | } |
||
461 | |||
462 | // can't be done in cms/_config.php as locale is not set yet |
||
463 | CMSMenu::add_link( |
||
464 | 'Help', |
||
465 | _t('LeftAndMain.HELP', 'Help', 'Menu title'), |
||
466 | $this->config()->help_link, |
||
467 | -2, |
||
468 | array( |
||
469 | 'target' => '_blank' |
||
470 | ) |
||
471 | ); |
||
472 | |||
473 | // Allow customisation of the access check by a extension |
||
474 | // Also all the canView() check to execute Controller::redirect() |
||
475 | if(!$this->canView() && !$this->getResponse()->isFinished()) { |
||
476 | // When access /admin/, we should try a redirect to another part of the admin rather than be locked out |
||
477 | $menu = $this->MainMenu(); |
||
478 | foreach($menu as $candidate) { |
||
479 | if( |
||
480 | $candidate->Link && |
||
481 | $candidate->Link != $this->Link() |
||
482 | && $candidate->MenuItem->controller |
||
483 | && singleton($candidate->MenuItem->controller)->canView() |
||
484 | ) { |
||
485 | $this->redirect($candidate->Link); |
||
486 | return; |
||
487 | } |
||
488 | } |
||
489 | |||
490 | if(Member::currentUser()) { |
||
491 | Session::set("BackURL", null); |
||
492 | } |
||
493 | |||
494 | // if no alternate menu items have matched, return a permission error |
||
495 | $messageSet = array( |
||
496 | 'default' => _t( |
||
497 | 'LeftAndMain.PERMDEFAULT', |
||
498 | "You must be logged in to access the administration area; please enter your credentials below." |
||
499 | ), |
||
500 | 'alreadyLoggedIn' => _t( |
||
501 | 'LeftAndMain.PERMALREADY', |
||
502 | "I'm sorry, but you can't access that part of the CMS. If you want to log in as someone else, do" |
||
503 | . " so below." |
||
504 | ), |
||
505 | 'logInAgain' => _t( |
||
506 | 'LeftAndMain.PERMAGAIN', |
||
507 | "You have been logged out of the CMS. If you would like to log in again, enter a username and" |
||
508 | . " password below." |
||
509 | ), |
||
510 | ); |
||
511 | |||
512 | Security::permissionFailure($this, $messageSet); |
||
513 | return; |
||
514 | } |
||
515 | |||
516 | // Don't continue if there's already been a redirection request. |
||
517 | if($this->redirectedTo()) { |
||
518 | return; |
||
519 | } |
||
520 | |||
521 | // Audit logging hook |
||
522 | if(empty($_REQUEST['executeForm']) && !$this->getRequest()->isAjax()) $this->extend('accessedCMS'); |
||
523 | |||
524 | // Set the members html editor config |
||
525 | if(Member::currentUser()) { |
||
526 | HTMLEditorConfig::set_active_identifier(Member::currentUser()->getHtmlEditorConfigForCMS()); |
||
527 | } |
||
528 | |||
529 | // Set default values in the config if missing. These things can't be defined in the config |
||
530 | // file because insufficient information exists when that is being processed |
||
531 | $htmlEditorConfig = HTMLEditorConfig::get_active(); |
||
532 | $htmlEditorConfig->setOption('language', i18n::get_tinymce_lang()); |
||
533 | |||
534 | Requirements::customScript(" |
||
535 | window.ss = window.ss || {}; |
||
536 | window.ss.config = " . $this->getCombinedClientConfig() . "; |
||
537 | "); |
||
538 | |||
539 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/bundle-lib.js', [ |
||
540 | 'provides' => [ |
||
541 | THIRDPARTY_DIR . '/jquery/jquery.js', |
||
542 | THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js', |
||
543 | THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js', |
||
544 | THIRDPARTY_DIR . '/jquery-cookie/jquery.cookie.js', |
||
545 | THIRDPARTY_DIR . '/jquery-query/jquery.query.js', |
||
546 | THIRDPARTY_DIR . '/jquery-form/jquery.form.js', |
||
547 | THIRDPARTY_DIR . '/jquery-ondemand/jquery.ondemand.js', |
||
548 | THIRDPARTY_DIR . '/jquery-changetracker/lib/jquery.changetracker.js', |
||
549 | THIRDPARTY_DIR . '/jstree/jquery.jstree.js', |
||
550 | FRAMEWORK_ADMIN_DIR . '/thirdparty/jquery-notice/jquery.notice.js', |
||
551 | FRAMEWORK_ADMIN_DIR . '/thirdparty/jsizes/lib/jquery.sizes.js', |
||
552 | FRAMEWORK_ADMIN_DIR . '/thirdparty/jlayout/lib/jlayout.border.js', |
||
553 | FRAMEWORK_ADMIN_DIR . '/thirdparty/jlayout/lib/jquery.jlayout.js', |
||
554 | FRAMEWORK_ADMIN_DIR . '/thirdparty/chosen/chosen/chosen.jquery.js', |
||
555 | FRAMEWORK_ADMIN_DIR . '/thirdparty/jquery-hoverIntent/jquery.hoverIntent.js', |
||
556 | FRAMEWORK_DIR . '/client/dist/js/TreeDropdownField.js', |
||
557 | FRAMEWORK_DIR . '/client/dist/js/DateField.js', |
||
558 | FRAMEWORK_DIR . '/client/dist/js/HtmlEditorField.js', |
||
559 | FRAMEWORK_DIR . '/client/dist/js/TabSet.js', |
||
560 | FRAMEWORK_DIR . '/client/dist/js/GridField.js', |
||
561 | FRAMEWORK_DIR . '/client/dist/js/i18n.js', |
||
562 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/sspath.js', |
||
563 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/ssui.core.js' |
||
564 | ] |
||
565 | ]); |
||
566 | |||
567 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/bundle-legacy.js', [ |
||
568 | 'provides' => [ |
||
569 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Layout.js', |
||
570 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.js', |
||
571 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.ActionTabSet.js', |
||
572 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Panel.js', |
||
573 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Tree.js', |
||
574 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Content.js', |
||
575 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.EditForm.js', |
||
576 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Menu.js', |
||
577 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Preview.js', |
||
578 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.BatchActions.js', |
||
579 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.FieldHelp.js', |
||
580 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.FieldDescriptionToggle.js', |
||
581 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.TreeDropdownField.js', |
||
582 | FRAMEWORK_ADMIN_DIR . '/client/dist/js/AddToCampaignForm.js' |
||
583 | ] |
||
584 | ]); |
||
585 | |||
586 | Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/client/lang', false, true); |
||
587 | Requirements::add_i18n_javascript(FRAMEWORK_ADMIN_DIR . '/client/lang', false, true); |
||
588 | |||
589 | if ($this->config()->session_keepalive_ping) { |
||
590 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Ping.js'); |
||
591 | } |
||
592 | |||
593 | if (Director::isDev()) { |
||
594 | // TODO Confuses jQuery.ondemand through document.write() |
||
595 | Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/src/jquery.entwine.inspector.js'); |
||
596 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/leaktools.js'); |
||
597 | } |
||
598 | |||
599 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/bundle-framework.js'); |
||
600 | |||
601 | Requirements::css(FRAMEWORK_ADMIN_DIR . '/thirdparty/jquery-notice/jquery.notice.css'); |
||
602 | Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css'); |
||
603 | Requirements::css(THIRDPARTY_DIR . '/jstree/themes/apple/style.css'); |
||
604 | Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/TreeDropdownField.css'); |
||
605 | Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css'); |
||
606 | Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/GridField.css'); |
||
607 | |||
608 | // Custom requirements |
||
609 | $extraJs = $this->stat('extra_requirements_javascript'); |
||
610 | |||
611 | if($extraJs) { |
||
612 | foreach($extraJs as $file => $config) { |
||
613 | if(is_numeric($file)) { |
||
614 | $file = $config; |
||
615 | } |
||
616 | |||
617 | Requirements::javascript($file); |
||
618 | } |
||
619 | } |
||
620 | |||
621 | $extraCss = $this->stat('extra_requirements_css'); |
||
622 | |||
623 | View Code Duplication | if($extraCss) { |
|
624 | foreach($extraCss as $file => $config) { |
||
625 | if(is_numeric($file)) { |
||
626 | $file = $config; |
||
627 | $config = array(); |
||
628 | } |
||
629 | |||
630 | Requirements::css($file, isset($config['media']) ? $config['media'] : null); |
||
631 | } |
||
632 | } |
||
633 | |||
634 | $extraThemedCss = $this->stat('extra_requirements_themedCss'); |
||
635 | |||
636 | View Code Duplication | if($extraThemedCss) { |
|
637 | foreach ($extraThemedCss as $file => $config) { |
||
638 | if(is_numeric($file)) { |
||
639 | $file = $config; |
||
640 | $config = array(); |
||
641 | } |
||
642 | |||
643 | Requirements::themedCSS($file, isset($config['media']) ? $config['media'] : null); |
||
644 | } |
||
645 | } |
||
646 | |||
647 | $dummy = null; |
||
648 | $this->extend('init', $dummy); |
||
649 | |||
650 | // Assign default cms theme and replace user-specified themes |
||
651 | SSViewer::set_themes($this->config()->admin_themes); |
||
652 | |||
653 | //set the reading mode for the admin to stage |
||
654 | Versioned::set_stage(Versioned::DRAFT); |
||
655 | } |
||
656 | |||
657 | public function handleRequest(HTTPRequest $request, DataModel $model = null) { |
||
683 | |||
684 | /** |
||
685 | * Overloaded redirection logic to trigger a fake redirect on ajax requests. |
||
686 | * While this violates HTTP principles, its the only way to work around the |
||
687 | * fact that browsers handle HTTP redirects opaquely, no intervention via JS is possible. |
||
688 | * In isolation, that's not a problem - but combined with history.pushState() |
||
689 | * it means we would request the same redirection URL twice if we want to update the URL as well. |
||
690 | * See LeftAndMain.js for the required jQuery ajaxComplete handlers. |
||
691 | * |
||
692 | * @param string $url |
||
693 | * @param int $code |
||
694 | * @return HTTPResponse|string |
||
695 | */ |
||
696 | public function redirect($url, $code=302) { |
||
718 | |||
719 | /** |
||
720 | * @param HTTPRequest $request |
||
721 | * @return HTTPResponse |
||
722 | */ |
||
723 | public function index($request) { |
||
726 | |||
727 | /** |
||
728 | * If this is set to true, the "switchView" context in the |
||
729 | * template is shown, with links to the staging and publish site. |
||
730 | * |
||
731 | * @return boolean |
||
732 | */ |
||
733 | public function ShowSwitchView() { |
||
736 | |||
737 | |||
738 | //------------------------------------------------------------------------------------------// |
||
739 | // Main controllers |
||
740 | |||
741 | /** |
||
742 | * You should implement a Link() function in your subclass of LeftAndMain, |
||
743 | * to point to the URL of that particular controller. |
||
744 | * |
||
745 | * @param string $action |
||
746 | * @return string |
||
747 | */ |
||
748 | public function Link($action = null) { |
||
765 | |||
766 | /** |
||
767 | * @deprecated 5.0 |
||
768 | */ |
||
769 | public static function menu_title_for_class($class) { |
||
773 | |||
774 | /** |
||
775 | * Get menu title for this section (translated) |
||
776 | * |
||
777 | * @param string $class Optional class name if called on LeftAndMain directly |
||
778 | * @param bool $localise Determine if menu title should be localised via i18n. |
||
779 | * @return string Menu title for the given class |
||
780 | */ |
||
781 | public static function menu_title($class = null, $localise = true) { |
||
802 | |||
803 | /** |
||
804 | * Return styling for the menu icon, if a custom icon is set for this class |
||
805 | * |
||
806 | * Example: static $menu-icon = '/path/to/image/'; |
||
807 | * @param string $class |
||
808 | * @return string |
||
809 | */ |
||
810 | public static function menu_icon_for_class($class) { |
||
811 | $icon = Config::inst()->get($class, 'menu_icon', Config::FIRST_SET); |
||
812 | if (!empty($icon)) { |
||
813 | $class = strtolower(Convert::raw2htmlname(str_replace('\\', '-', $class))); |
||
814 | return ".icon.icon-16.icon-{$class} { background-image: url('{$icon}'); } "; |
||
815 | } |
||
816 | return ''; |
||
817 | } |
||
818 | |||
819 | /** |
||
820 | * @param HTTPRequest $request |
||
821 | * @return HTTPResponse |
||
822 | * @throws HTTPResponse_Exception |
||
823 | */ |
||
824 | public function show($request) { |
||
829 | |||
830 | /** |
||
831 | * Caution: Volatile API. |
||
832 | * |
||
833 | * @return PjaxResponseNegotiator |
||
834 | */ |
||
835 | public function getResponseNegotiator() { |
||
836 | if(!$this->responseNegotiator) { |
||
837 | $controller = $this; |
||
838 | $this->responseNegotiator = new PjaxResponseNegotiator( |
||
839 | array( |
||
840 | 'CurrentForm' => function() use(&$controller) { |
||
841 | return $controller->getEditForm()->forTemplate(); |
||
842 | }, |
||
843 | 'Content' => function() use(&$controller) { |
||
844 | return $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); |
||
845 | }, |
||
846 | 'Breadcrumbs' => function() use (&$controller) { |
||
847 | return $controller->renderWith([ |
||
848 | 'type' => 'Includes', |
||
849 | 'SilverStripe\\Admin\\CMSBreadcrumbs' |
||
850 | ]); |
||
851 | }, |
||
852 | 'default' => function() use(&$controller) { |
||
853 | return $controller->renderWith($controller->getViewer('show')); |
||
854 | } |
||
855 | ), |
||
856 | $this->getResponse() |
||
857 | ); |
||
858 | } |
||
859 | return $this->responseNegotiator; |
||
860 | } |
||
861 | |||
862 | //------------------------------------------------------------------------------------------// |
||
863 | // Main UI components |
||
864 | |||
865 | /** |
||
866 | * Returns the main menu of the CMS. This is also used by init() |
||
867 | * to work out which sections the user has access to. |
||
868 | * |
||
869 | * @param bool $cached |
||
870 | * @return SS_List |
||
871 | */ |
||
872 | public function MainMenu($cached = true) { |
||
873 | if(!isset($this->_cache_MainMenu) || !$cached) { |
||
874 | // Don't accidentally return a menu if you're not logged in - it's used to determine access. |
||
875 | if(!Member::currentUser()) return new ArrayList(); |
||
876 | |||
877 | // Encode into DO set |
||
878 | $menu = new ArrayList(); |
||
879 | $menuItems = CMSMenu::get_viewable_menu_items(); |
||
880 | |||
881 | // extra styling for custom menu-icons |
||
882 | $menuIconStyling = ''; |
||
883 | |||
884 | if($menuItems) { |
||
885 | /** @var CMSMenuItem $menuItem */ |
||
886 | foreach($menuItems as $code => $menuItem) { |
||
887 | // alternate permission checks (in addition to LeftAndMain->canView()) |
||
888 | if( |
||
889 | isset($menuItem->controller) |
||
890 | && $this->hasMethod('alternateMenuDisplayCheck') |
||
891 | && !$this->alternateMenuDisplayCheck($menuItem->controller) |
||
892 | ) { |
||
893 | continue; |
||
894 | } |
||
895 | |||
896 | $linkingmode = "link"; |
||
897 | |||
898 | if($menuItem->controller && get_class($this) == $menuItem->controller) { |
||
899 | $linkingmode = "current"; |
||
900 | } else if(strpos($this->Link(), $menuItem->url) !== false) { |
||
901 | if($this->Link() == $menuItem->url) { |
||
902 | $linkingmode = "current"; |
||
903 | |||
904 | // default menu is the one with a blank {@link url_segment} |
||
905 | } else if(singleton($menuItem->controller)->stat('url_segment') == '') { |
||
906 | if($this->Link() == AdminRootController::admin_url()) { |
||
907 | $linkingmode = "current"; |
||
908 | } |
||
909 | |||
910 | } else { |
||
911 | $linkingmode = "current"; |
||
912 | } |
||
913 | } |
||
914 | |||
915 | // already set in CMSMenu::populate_menu(), but from a static pre-controller |
||
916 | // context, so doesn't respect the current user locale in _t() calls - as a workaround, |
||
917 | // we simply call LeftAndMain::menu_title() again |
||
918 | // if we're dealing with a controller |
||
919 | if($menuItem->controller) { |
||
920 | $title = LeftAndMain::menu_title($menuItem->controller); |
||
921 | } else { |
||
922 | $title = $menuItem->title; |
||
923 | } |
||
924 | |||
925 | // Provide styling for custom $menu-icon. Done here instead of in |
||
926 | // CMSMenu::populate_menu(), because the icon is part of |
||
927 | // the CMS right pane for the specified class as well... |
||
928 | if($menuItem->controller) { |
||
929 | $menuIcon = LeftAndMain::menu_icon_for_class($menuItem->controller); |
||
930 | if (!empty($menuIcon)) { |
||
931 | $menuIconStyling .= $menuIcon; |
||
932 | } |
||
933 | } |
||
934 | |||
935 | $menu->push(new ArrayData(array( |
||
936 | "MenuItem" => $menuItem, |
||
937 | "AttributesHTML" => $menuItem->getAttributesHTML(), |
||
938 | "Title" => Convert::raw2xml($title), |
||
939 | "Code" => $code, |
||
940 | "Icon" => strtolower($code), |
||
941 | "Link" => $menuItem->url, |
||
942 | "LinkingMode" => $linkingmode |
||
943 | ))); |
||
944 | } |
||
945 | } |
||
946 | if ($menuIconStyling) Requirements::customCSS($menuIconStyling); |
||
947 | |||
948 | $this->_cache_MainMenu = $menu; |
||
949 | } |
||
950 | |||
951 | return $this->_cache_MainMenu; |
||
952 | } |
||
953 | |||
954 | public function Menu() { |
||
957 | |||
958 | /** |
||
959 | * @todo Wrap in CMSMenu instance accessor |
||
960 | * @return ArrayData A single menu entry (see {@link MainMenu}) |
||
961 | */ |
||
962 | public function MenuCurrentItem() { |
||
966 | |||
967 | /** |
||
968 | * Return a list of appropriate templates for this class, with the given suffix using |
||
969 | * {@link SSViewer::get_templates_by_class()} |
||
970 | * |
||
971 | * @param string $suffix |
||
972 | * @return array |
||
973 | */ |
||
974 | public function getTemplatesWithSuffix($suffix) { |
||
975 | $templates = SSViewer::get_templates_by_class(get_class($this), $suffix, __CLASS__); |
||
976 | return SSViewer::chooseTemplate($templates); |
||
977 | } |
||
978 | |||
979 | public function Content() { |
||
982 | |||
983 | /** |
||
984 | * Render $PreviewPanel content |
||
985 | * |
||
986 | * @return DBHTMLText |
||
987 | */ |
||
988 | public function PreviewPanel() { |
||
989 | $template = $this->getTemplatesWithSuffix('_PreviewPanel'); |
||
990 | // Only render sections with preview panel |
||
991 | if ($template) { |
||
995 | |||
996 | public function getRecord($id) { |
||
1008 | |||
1009 | /** |
||
1010 | * @param bool $unlinked |
||
1011 | * @return ArrayList |
||
1012 | */ |
||
1013 | public function Breadcrumbs($unlinked = false) { |
||
1042 | |||
1043 | /** |
||
1044 | * @return String HTML |
||
1045 | */ |
||
1046 | public function SiteTreeAsUL() { |
||
1051 | |||
1052 | /** |
||
1053 | * Gets the current search filter for this request, if available |
||
1054 | * |
||
1055 | * @throws InvalidArgumentException |
||
1056 | * @return LeftAndMain_SearchFilter |
||
1057 | */ |
||
1058 | protected function getSearchFilter() { |
||
1074 | |||
1075 | /** |
||
1076 | * Get a site tree HTML listing which displays the nodes under the given criteria. |
||
1077 | * |
||
1078 | * @param string $className The class of the root object |
||
1079 | * @param string $rootID The ID of the root object. If this is null then a complete tree will be |
||
1080 | * shown |
||
1081 | * @param string $childrenMethod The method to call to get the children of the tree. For example, |
||
1082 | * Children, AllChildrenIncludingDeleted, or AllHistoricalChildren |
||
1083 | * @param string $numChildrenMethod |
||
1084 | * @param callable $filterFunction |
||
1085 | * @param int $nodeCountThreshold |
||
1086 | * @return string Nested unordered list with links to each page |
||
1087 | */ |
||
1088 | public function getSiteTreeFor($className, $rootID = null, $childrenMethod = null, $numChildrenMethod = null, |
||
1221 | |||
1222 | /** |
||
1223 | * Get a subtree underneath the request param 'ID'. |
||
1224 | * If ID = 0, then get the whole tree. |
||
1225 | * |
||
1226 | * @param HTTPRequest $request |
||
1227 | * @return string |
||
1228 | */ |
||
1229 | public function getsubtree($request) { |
||
1245 | |||
1246 | /** |
||
1247 | * Allows requesting a view update on specific tree nodes. |
||
1248 | * Similar to {@link getsubtree()}, but doesn't enforce loading |
||
1249 | * all children with the node. Useful to refresh views after |
||
1250 | * state modifications, e.g. saving a form. |
||
1251 | * |
||
1252 | * @param HTTPRequest $request |
||
1253 | * @return string JSON |
||
1254 | */ |
||
1255 | public function updatetreenodes($request) { |
||
1299 | |||
1300 | /** |
||
1301 | * Save handler |
||
1302 | * |
||
1303 | * @param array $data |
||
1304 | * @param Form $form |
||
1305 | * @return HTTPResponse |
||
1306 | */ |
||
1307 | public function save($data, $form) { |
||
1349 | |||
1350 | /** |
||
1351 | * Create new item. |
||
1352 | * |
||
1353 | * @param string|int $id |
||
1354 | * @param bool $setID |
||
1355 | * @return DataObject |
||
1356 | */ |
||
1357 | public function getNewItem($id, $setID = true) { |
||
1365 | |||
1366 | public function delete($data, $form) { |
||
1382 | |||
1383 | /** |
||
1384 | * Update the position and parent of a tree node. |
||
1385 | * Only saves the node if changes were made. |
||
1386 | * |
||
1387 | * Required data: |
||
1388 | * - 'ID': The moved node |
||
1389 | * - 'ParentID': New parent relation of the moved node (0 for root) |
||
1390 | * - 'SiblingIDs': Array of all sibling nodes to the moved node (incl. the node itself). |
||
1391 | * In case of a 'ParentID' change, relates to the new siblings under the new parent. |
||
1392 | * |
||
1393 | * @param HTTPRequest $request |
||
1394 | * @return HTTPResponse JSON string with a |
||
1395 | * @throws HTTPResponse_Exception |
||
1396 | */ |
||
1397 | public function savetreenode($request) { |
||
1494 | |||
1495 | public function CanOrganiseSitetree() { |
||
1498 | |||
1499 | /** |
||
1500 | * Retrieves an edit form, either for display, or to process submitted data. |
||
1501 | * Also used in the template rendered through {@link Right()} in the $EditForm placeholder. |
||
1502 | * |
||
1503 | * This is a "pseudo-abstract" methoed, usually connected to a {@link getEditForm()} |
||
1504 | * method in an entwine subclass. This method can accept a record identifier, |
||
1505 | * selected either in custom logic, or through {@link currentPageID()}. |
||
1506 | * The form usually construct itself from {@link DataObject->getCMSFields()} |
||
1507 | * for the specific managed subclass defined in {@link LeftAndMain::$tree_class}. |
||
1508 | * |
||
1509 | * @param HTTPRequest $request Optionally contains an identifier for the |
||
1510 | * record to load into the form. |
||
1511 | * @return Form Should return a form regardless wether a record has been found. |
||
1512 | * Form might be readonly if the current user doesn't have the permission to edit |
||
1513 | * the record. |
||
1514 | */ |
||
1515 | /** |
||
1516 | * @return Form |
||
1517 | */ |
||
1518 | public function EditForm($request = null) { |
||
1521 | |||
1522 | /** |
||
1523 | * Calls {@link SiteTree->getCMSFields()} |
||
1524 | * |
||
1525 | * @param Int $id |
||
1526 | * @param FieldList $fields |
||
1527 | * @return Form |
||
1528 | */ |
||
1529 | public function getEditForm($id = null, $fields = null) { |
||
1664 | |||
1665 | /** |
||
1666 | * Returns a placeholder form, used by {@link getEditForm()} if no record is selected. |
||
1667 | * Our javascript logic always requires a form to be present in the CMS interface. |
||
1668 | * |
||
1669 | * @return Form |
||
1670 | */ |
||
1671 | public function EmptyForm() { |
||
1699 | |||
1700 | /** |
||
1701 | * Return the CMS's HTML-editor toolbar |
||
1702 | */ |
||
1703 | public function EditorToolbar() { |
||
1706 | |||
1707 | /** |
||
1708 | * Renders a panel containing tools which apply to all displayed |
||
1709 | * "content" (mostly through {@link EditForm()}), for example a tree navigation or a filter panel. |
||
1710 | * Auto-detects applicable templates by naming convention: "<controller classname>_Tools.ss", |
||
1711 | * and takes the most specific template (see {@link getTemplatesWithSuffix()}). |
||
1712 | * To explicitly disable the panel in the subclass, simply create a more specific, empty template. |
||
1713 | * |
||
1714 | * @return String HTML |
||
1715 | */ |
||
1716 | View Code Duplication | public function Tools() { |
|
1725 | |||
1726 | /** |
||
1727 | * Renders a panel containing tools which apply to the currently displayed edit form. |
||
1728 | * The main difference to {@link Tools()} is that the panel is displayed within |
||
1729 | * the element structure of the form panel (rendered through {@link EditForm}). |
||
1730 | * This means the panel will be loaded alongside new forms, and refreshed upon save, |
||
1731 | * which can mean a performance hit, depending on how complex your panel logic gets. |
||
1732 | * Any form fields contained in the returned markup will also be submitted with the main form, |
||
1733 | * which might be desired depending on the implementation details. |
||
1734 | * |
||
1735 | * @return String HTML |
||
1736 | */ |
||
1737 | View Code Duplication | public function EditFormTools() { |
|
1746 | |||
1747 | /** |
||
1748 | * Batch Actions Handler |
||
1749 | */ |
||
1750 | public function batchactions() { |
||
1753 | |||
1754 | /** |
||
1755 | * @return Form |
||
1756 | */ |
||
1757 | public function BatchActionsForm() { |
||
1788 | |||
1789 | public function printable() { |
||
1802 | |||
1803 | /** |
||
1804 | * Used for preview controls, mainly links which switch between different states of the page. |
||
1805 | * |
||
1806 | * @return DBHTMLText |
||
1807 | */ |
||
1808 | public function getSilverStripeNavigator() { |
||
1816 | |||
1817 | /** |
||
1818 | * Identifier for the currently shown record, |
||
1819 | * in most cases a database ID. Inspects the following |
||
1820 | * sources (in this order): |
||
1821 | * - GET/POST parameter named 'ID' |
||
1822 | * - URL parameter named 'ID' |
||
1823 | * - Session value namespaced by classname, e.g. "CMSMain.currentPage" |
||
1824 | * |
||
1825 | * @return int |
||
1826 | */ |
||
1827 | public function currentPageID() { |
||
1841 | |||
1842 | /** |
||
1843 | * Forces the current page to be set in session, |
||
1844 | * which can be retrieved later through {@link currentPageID()}. |
||
1845 | * Keep in mind that setting an ID through GET/POST or |
||
1846 | * as a URL parameter will overrule this value. |
||
1847 | * |
||
1848 | * @param int $id |
||
1849 | */ |
||
1850 | public function setCurrentPageID($id) { |
||
1854 | |||
1855 | /** |
||
1856 | * Uses {@link getRecord()} and {@link currentPageID()} |
||
1857 | * to get the currently selected record. |
||
1858 | * |
||
1859 | * @return DataObject |
||
1860 | */ |
||
1861 | public function currentPage() { |
||
1864 | |||
1865 | /** |
||
1866 | * Compares a given record to the currently selected one (if any). |
||
1867 | * Used for marking the current tree node. |
||
1868 | * |
||
1869 | * @param DataObject $record |
||
1870 | * @return bool |
||
1871 | */ |
||
1872 | public function isCurrentPage(DataObject $record) { |
||
1875 | |||
1876 | /** |
||
1877 | * @return String |
||
1878 | */ |
||
1879 | protected function sessionNamespace() { |
||
1883 | |||
1884 | /** |
||
1885 | * URL to a previewable record which is shown through this controller. |
||
1886 | * The controller might not have any previewable content, in which case |
||
1887 | * this method returns FALSE. |
||
1888 | * |
||
1889 | * @return String|boolean |
||
1890 | */ |
||
1891 | public function LinkPreview() { |
||
1894 | |||
1895 | /** |
||
1896 | * Return the version number of this application. |
||
1897 | * Uses the number in <mymodule>/silverstripe_version |
||
1898 | * (automatically replaced by build scripts). |
||
1899 | * If silverstripe_version is empty, |
||
1900 | * then attempts to get it from composer.lock |
||
1901 | * |
||
1902 | * @return string |
||
1903 | */ |
||
1904 | public function CMSVersion() { |
||
1963 | |||
1964 | /** |
||
1965 | * @return array |
||
1966 | */ |
||
1967 | public function SwitchView() { |
||
1973 | |||
1974 | /** |
||
1975 | * @return SiteConfig |
||
1976 | */ |
||
1977 | public function SiteConfig() { |
||
1980 | |||
1981 | /** |
||
1982 | * The href for the anchor on the Silverstripe logo. |
||
1983 | * Set by calling LeftAndMain::set_application_link() |
||
1984 | * |
||
1985 | * @config |
||
1986 | * @var String |
||
1987 | */ |
||
1988 | private static $application_link = '//www.silverstripe.org/'; |
||
1989 | |||
1990 | /** |
||
1991 | * @return String |
||
1992 | */ |
||
1993 | public function ApplicationLink() { |
||
1996 | |||
1997 | /** |
||
1998 | * The application name. Customisable by calling |
||
1999 | * LeftAndMain::setApplicationName() - the first parameter. |
||
2000 | * |
||
2001 | * @config |
||
2002 | * @var String |
||
2003 | */ |
||
2004 | private static $application_name = 'SilverStripe'; |
||
2005 | |||
2006 | /** |
||
2007 | * Get the application name. |
||
2008 | * |
||
2009 | * @return string |
||
2010 | */ |
||
2011 | public function getApplicationName() { |
||
2014 | |||
2015 | /** |
||
2016 | * @return string |
||
2017 | */ |
||
2018 | public function Title() { |
||
2023 | |||
2024 | /** |
||
2025 | * Return the title of the current section. Either this is pulled from |
||
2026 | * the current panel's menu_title or from the first active menu |
||
2027 | * |
||
2028 | * @return string |
||
2029 | */ |
||
2030 | public function SectionTitle() { |
||
2042 | |||
2043 | /** |
||
2044 | * Same as {@link ViewableData->CSSClasses()}, but with a changed name |
||
2045 | * to avoid problems when using {@link ViewableData->customise()} |
||
2046 | * (which always returns "ArrayData" from the $original object). |
||
2047 | * |
||
2048 | * @return String |
||
2049 | */ |
||
2050 | public function BaseCSSClasses() { |
||
2053 | |||
2054 | /** |
||
2055 | * @return String |
||
2056 | */ |
||
2057 | public function Locale() { |
||
2060 | |||
2061 | public function providePermissions() { |
||
2106 | |||
2107 | } |
||
2108 |