Total Complexity | 81 |
Total Lines | 825 |
Duplicated Lines | 0 % |
Changes | 28 | ||
Bugs | 1 | Features | 2 |
Complex classes like CampaignAdmin 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 CampaignAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class CampaignAdmin extends LeftAndMain implements PermissionProvider |
||
35 | { |
||
36 | private static $allowed_actions = [ |
||
|
|||
37 | 'set', |
||
38 | 'sets', |
||
39 | 'EditForm', |
||
40 | 'campaignEditForm', |
||
41 | 'campaignCreateForm', |
||
42 | 'readCampaigns', |
||
43 | 'readCampaign', |
||
44 | 'deleteCampaign', |
||
45 | 'publishCampaign', |
||
46 | 'removeCampaignItem' |
||
47 | ]; |
||
48 | |||
49 | private static $menu_priority = 3; |
||
50 | |||
51 | /** |
||
52 | * When listing campaigns, re-sync items automatically after this many seconds. |
||
53 | * This can prevent unnecessary and expensive database requests on every view. |
||
54 | * |
||
55 | * @config |
||
56 | * @var int |
||
57 | */ |
||
58 | private static $sync_expires = 300; |
||
59 | |||
60 | private static $menu_title = 'Campaigns'; |
||
61 | |||
62 | private static $menu_icon_class = 'font-icon-page-multiple'; |
||
63 | |||
64 | private static $tree_class = ChangeSet::class; |
||
65 | |||
66 | /** |
||
67 | * Show published changesets |
||
68 | * |
||
69 | * Note: Experimental API (will be changed in the near future) |
||
70 | * |
||
71 | * @config |
||
72 | * @var bool |
||
73 | */ |
||
74 | private static $show_published = true; |
||
75 | |||
76 | /** |
||
77 | * Show inferred changesets (automatically created when you publish a page) |
||
78 | * |
||
79 | * Note: Experimental API (will be changed in the near future) |
||
80 | * |
||
81 | * @config |
||
82 | * @var bool |
||
83 | */ |
||
84 | private static $show_inferred = false; |
||
85 | |||
86 | private static $url_handlers = [ |
||
87 | 'GET sets' => 'readCampaigns', |
||
88 | 'POST set/$ID/publish' => 'publishCampaign', |
||
89 | 'GET set/$ID/$Name' => 'readCampaign', |
||
90 | 'DELETE set/$ID' => 'deleteCampaign', |
||
91 | 'campaignEditForm/$ID' => 'campaignEditForm', |
||
92 | 'campaignCreateForm' => 'campaignCreateForm', |
||
93 | 'POST removeCampaignItem/$CampaignID/$ItemID' => 'removeCampaignItem', |
||
94 | ]; |
||
95 | |||
96 | private static $url_segment = 'campaigns'; |
||
97 | |||
98 | /** |
||
99 | * Size of thumbnail width |
||
100 | * |
||
101 | * @config |
||
102 | * @var int |
||
103 | */ |
||
104 | private static $thumbnail_width = 64; |
||
105 | |||
106 | /** |
||
107 | * Size of thumbnail height |
||
108 | * |
||
109 | * @config |
||
110 | * @var int |
||
111 | */ |
||
112 | private static $thumbnail_height = 64; |
||
113 | |||
114 | private static $required_permission_codes = 'CMS_ACCESS_CampaignAdmin'; |
||
115 | |||
116 | public function getClientConfig() |
||
149 | ]); |
||
150 | } |
||
151 | |||
152 | public function init() |
||
153 | { |
||
154 | parent::init(); |
||
155 | Requirements::add_i18n_javascript('silverstripe/campaign-admin: client/lang', false, true); |
||
156 | Requirements::javascript('silverstripe/campaign-admin: client/dist/js/bundle.js'); |
||
157 | Requirements::css('silverstripe/campaign-admin: client/dist/styles/bundle.css'); |
||
158 | } |
||
159 | |||
160 | public function getEditForm($id = null, $fields = null) |
||
161 | { |
||
162 | $fields = new FieldList( |
||
163 | CampaignAdminList::create('ChangeSets') |
||
164 | ); |
||
165 | $actions = new FieldList(); |
||
166 | $form = Form::create($this, 'EditForm', $fields, $actions); |
||
167 | $form->addExtraClass('form--padded'); |
||
168 | |||
169 | // Set callback response |
||
170 | $form->setValidationResponseCallback(function () use ($form) { |
||
171 | $schemaId = $this->Link('schema/EditForm'); |
||
172 | return $this->getSchemaResponse($form, $schemaId); |
||
173 | }); |
||
174 | |||
175 | return $form; |
||
176 | } |
||
177 | |||
178 | public function EditForm($request = null) |
||
179 | { |
||
180 | // Note: Edit form doesn't have ID, and simply populates a top level gridfield |
||
181 | return $this->getEditForm(); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * REST endpoint to get a list of campaigns. |
||
186 | * |
||
187 | * @return HTTPResponse |
||
188 | */ |
||
189 | public function readCampaigns() |
||
190 | { |
||
191 | $response = new HTTPResponse(); |
||
192 | $response->addHeader('Content-Type', 'application/json'); |
||
193 | $hal = $this->getListResource(); |
||
194 | $response->setBody(json_encode($hal)); |
||
195 | return $response; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return array |
||
200 | */ |
||
201 | protected function getPlaceholderGroups() |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get list contained as a hal wrapper |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | protected function getListResource() |
||
235 | { |
||
236 | $items = $this->getListItems(); |
||
237 | $count = $items->count(); |
||
238 | /** @var string $treeClass */ |
||
239 | $treeClass = $this->config()->get('tree_class'); |
||
240 | $hal = [ |
||
241 | 'count' => $count, |
||
242 | 'total' => $count, |
||
243 | '_links' => [ |
||
244 | 'self' => [ |
||
245 | 'href' => $this->Link('items') |
||
246 | ] |
||
247 | ], |
||
248 | '_embedded' => [$treeClass => []] |
||
249 | ]; |
||
250 | /** @var ChangeSet $item */ |
||
251 | foreach ($items as $item) { |
||
252 | $sync = $this->shouldCampaignSync($item); |
||
253 | $resource = $this->getChangeSetResource($item, $sync); |
||
254 | $hal['_embedded'][$treeClass][] = $resource; |
||
255 | } |
||
256 | return $hal; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Build item resource from a changeset |
||
261 | * |
||
262 | * @param ChangeSet $changeSet |
||
263 | * @param bool $sync Set to true to force async of this changeset |
||
264 | * @return array |
||
265 | */ |
||
266 | protected function getChangeSetResource(ChangeSet $changeSet, $sync = false) |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Build item resource from a changesetitem |
||
329 | * |
||
330 | * @param ChangeSetItem $changeSetItem |
||
331 | * @return array |
||
332 | */ |
||
333 | protected function getChangeSetItemResource(ChangeSetItem $changeSetItem) |
||
334 | { |
||
335 | $baseClass = DataObject::getSchema()->baseDataClass($changeSetItem->ObjectClass); |
||
336 | $baseSingleton = DataObject::singleton($baseClass); |
||
337 | |||
338 | // Allow items to opt out of being displayed in changesets |
||
339 | if ($baseSingleton->config()->get('hide_in_campaigns')) { |
||
340 | return []; |
||
341 | } |
||
342 | |||
343 | $thumbnailWidth = (int)$this->config()->get('thumbnail_width'); |
||
344 | $thumbnailHeight = (int)$this->config()->get('thumbnail_height'); |
||
345 | $hal = [ |
||
346 | '_links' => [ |
||
347 | 'self' => [ |
||
348 | 'id' => $changeSetItem->ID, |
||
349 | 'href' => $this->ItemLink($changeSetItem->ID) |
||
350 | ] |
||
351 | ], |
||
352 | 'ID' => $changeSetItem->ID, |
||
353 | 'Created' => $changeSetItem->Created, |
||
354 | 'LastEdited' => $changeSetItem->LastEdited, |
||
355 | 'Title' => $changeSetItem->getTitle(), |
||
356 | 'ChangeType' => $changeSetItem->getChangeType(), |
||
357 | 'Added' => $changeSetItem->Added, |
||
358 | 'ObjectClass' => $changeSetItem->ObjectClass, |
||
359 | 'ObjectID' => $changeSetItem->ObjectID, |
||
360 | 'BaseClass' => $baseClass, |
||
361 | 'Singular' => $baseSingleton->i18n_singular_name(), |
||
362 | 'Plural' => $baseSingleton->i18n_plural_name(), |
||
363 | 'Thumbnail' => $changeSetItem->ThumbnailURL($thumbnailWidth, $thumbnailHeight), |
||
364 | ]; |
||
365 | // Get preview urls |
||
366 | $previews = $changeSetItem->getPreviewLinks(); |
||
367 | if ($previews) { |
||
368 | $hal['_links']['preview'] = $previews; |
||
369 | } |
||
370 | |||
371 | // Get edit link |
||
372 | $editLink = $changeSetItem->CMSEditLink(); |
||
373 | if ($editLink) { |
||
374 | $hal['_links']['edit'] = [ |
||
375 | 'href' => $editLink, |
||
376 | ]; |
||
377 | } |
||
378 | |||
379 | // Depending on whether the object was added implicitly or explicitly, set |
||
380 | // other related objects. |
||
381 | if ($changeSetItem->Added === ChangeSetItem::IMPLICITLY) { |
||
382 | $referencedItems = $changeSetItem->ReferencedBy(); |
||
383 | $referencedBy = []; |
||
384 | foreach ($referencedItems as $referencedItem) { |
||
385 | $referencedBy[] = [ |
||
386 | 'href' => $this->SetLink($referencedItem->ID), |
||
387 | 'ChangeSetItemID' => $referencedItem->ID |
||
388 | ]; |
||
389 | } |
||
390 | if ($referencedBy) { |
||
391 | $hal['_links']['referenced_by'] = $referencedBy; |
||
392 | } |
||
393 | } |
||
394 | |||
395 | $referToItems = $changeSetItem->References(); |
||
396 | $referTo = []; |
||
397 | foreach ($referToItems as $referToItem) { |
||
398 | $referTo[] = [ |
||
399 | 'ChangeSetItemID' => $referToItem->ID, |
||
400 | ]; |
||
401 | } |
||
402 | $hal['_links']['references'] = $referTo; |
||
403 | |||
404 | return $hal; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Gets viewable list of campaigns |
||
409 | * |
||
410 | * @return SS_List |
||
411 | */ |
||
412 | protected function getListItems() |
||
427 | }); |
||
428 | } |
||
429 | |||
430 | |||
431 | /** |
||
432 | * REST endpoint to get a campaign. |
||
433 | * |
||
434 | * @param HTTPRequest $request |
||
435 | * |
||
436 | * @return HTTPResponse |
||
437 | */ |
||
438 | public function readCampaign(HTTPRequest $request) |
||
439 | { |
||
440 | $response = new HTTPResponse(); |
||
441 | $accepts = $request->getAcceptMimetypes(); |
||
442 | |||
443 | //accept 'text/json' for legacy reasons |
||
444 | if (in_array('application/json', $accepts ?? []) || in_array('text/json', $accepts ?? [])) { |
||
445 | $response->addHeader('Content-Type', 'application/json'); |
||
446 | if (!$request->param('Name')) { |
||
447 | return (new HTTPResponse(null, 400)); |
||
448 | } |
||
449 | |||
450 | /** @var ChangeSet $changeSet */ |
||
451 | $changeSet = ChangeSet::get()->filter('IsInferred', 0)->byID($request->param('ID')); |
||
452 | |||
453 | if (!$changeSet) { |
||
454 | return (new HTTPResponse(null, 404)); |
||
455 | } |
||
456 | |||
457 | if (!$changeSet->canView()) { |
||
458 | return (new HTTPResponse(null, 403)); |
||
459 | } |
||
460 | |||
461 | $body = json_encode($this->getChangeSetResource($changeSet, true)); |
||
462 | return (new HTTPResponse($body, 200)) |
||
463 | ->addHeader('Content-Type', 'application/json'); |
||
464 | } else { |
||
465 | return $this->index($request); |
||
466 | } |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * REST endpoint to delete a campaign item. |
||
471 | * |
||
472 | * @param HTTPRequest $request |
||
473 | * |
||
474 | * @return HTTPResponse |
||
475 | */ |
||
476 | public function removeCampaignItem(HTTPRequest $request) |
||
477 | { |
||
478 | // Check security ID |
||
479 | if (!SecurityToken::inst()->checkRequest($request)) { |
||
480 | return new HTTPResponse(null, 400); |
||
481 | } |
||
482 | |||
483 | $campaignID = $request->param('CampaignID'); |
||
484 | $itemID = $request->param('ItemID'); |
||
485 | |||
486 | if (!$campaignID || |
||
487 | !is_numeric($campaignID) || |
||
488 | !$itemID || |
||
489 | !is_numeric($itemID)) { |
||
490 | return (new HTTPResponse(null, 400)); |
||
491 | } |
||
492 | |||
493 | /** @var ChangeSet $campaign */ |
||
494 | $campaign = ChangeSet::get()->byID($campaignID); |
||
495 | /** @var ChangeSetItem $item */ |
||
496 | $item = ChangeSetItem::get()->byID($itemID); |
||
497 | if (!$campaign || !$item) { |
||
498 | return (new HTTPResponse(null, 404)); |
||
499 | } |
||
500 | |||
501 | |||
502 | $campaign->removeObject($item->Object()); |
||
503 | |||
504 | return (new HTTPResponse(null, 204)); |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * REST endpoint to delete a campaign. |
||
509 | * |
||
510 | * @param HTTPRequest $request |
||
511 | * |
||
512 | * @return HTTPResponse |
||
513 | */ |
||
514 | public function deleteCampaign(HTTPRequest $request) |
||
515 | { |
||
516 | // Check security ID |
||
517 | if (!SecurityToken::inst()->checkRequest($request)) { |
||
518 | return new HTTPResponse(null, 400); |
||
519 | } |
||
520 | |||
521 | $id = $request->param('ID'); |
||
522 | if (!$id || !is_numeric($id)) { |
||
523 | return (new HTTPResponse(null, 400)); |
||
524 | } |
||
525 | |||
526 | $record = ChangeSet::get()->byID($id); |
||
527 | if (!$record) { |
||
528 | return (new HTTPResponse(null, 404)); |
||
529 | } |
||
530 | |||
531 | if (!$record->canDelete()) { |
||
532 | return (new HTTPResponse(null, 403)); |
||
533 | } |
||
534 | |||
535 | $record->delete(); |
||
536 | |||
537 | return (new HTTPResponse(null, 204)); |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * REST endpoint to publish a {@link ChangeSet} and all of its items. |
||
542 | * |
||
543 | * @param HTTPRequest $request |
||
544 | * |
||
545 | * @return HTTPResponse |
||
546 | */ |
||
547 | public function publishCampaign(HTTPRequest $request) |
||
548 | { |
||
549 | // Protect against CSRF on destructive action |
||
550 | if (!SecurityToken::inst()->checkRequest($request)) { |
||
551 | return (new HTTPResponse(null, 400)); |
||
552 | } |
||
553 | |||
554 | $id = $request->param('ID'); |
||
555 | if (!$id || !is_numeric($id)) { |
||
556 | return (new HTTPResponse(null, 400)); |
||
557 | } |
||
558 | |||
559 | /** @var ChangeSet $record */ |
||
560 | $record = ChangeSet::get()->byID($id); |
||
561 | if (!$record) { |
||
562 | return (new HTTPResponse(null, 404)); |
||
563 | } |
||
564 | |||
565 | if (!$record->canPublish()) { |
||
566 | return (new HTTPResponse(null, 403)); |
||
567 | } |
||
568 | |||
569 | try { |
||
570 | $record->publish(); |
||
571 | } catch (LogicException $e) { |
||
572 | return (new HTTPResponse(json_encode(['status' => 'error', 'message' => $e->getMessage()]), 401)) |
||
573 | ->addHeader('Content-Type', 'application/json'); |
||
574 | } |
||
575 | |||
576 | return (new HTTPResponse( |
||
577 | json_encode($this->getChangeSetResource($record)), |
||
578 | 200 |
||
579 | ))->addHeader('Content-Type', 'application/json'); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Url handler for edit form |
||
584 | * |
||
585 | * @param HTTPRequest $request |
||
586 | * @return Form |
||
587 | */ |
||
588 | public function campaignEditForm($request) |
||
589 | { |
||
590 | // Get ID either from posted back value, or url parameter |
||
591 | if (!$request) { |
||
592 | $this->httpError(400); |
||
593 | return null; |
||
594 | } |
||
595 | $id = $request->param('ID'); |
||
596 | if (!$id) { |
||
597 | $this->httpError(400); |
||
598 | return null; |
||
599 | } |
||
600 | return $this->getCampaignEditForm($id); |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * @todo Use GridFieldDetailForm once it can handle structured data and form schemas |
||
605 | * @todo move to FormBuilder |
||
606 | * |
||
607 | * @param int $id |
||
608 | * @return Form |
||
609 | */ |
||
610 | public function getCampaignEditForm($id) |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Url handler for create form |
||
676 | * |
||
677 | * @param HTTPRequest $request |
||
678 | * @return Form |
||
679 | */ |
||
680 | public function campaignCreateForm($request) |
||
681 | { |
||
682 | return $this->getCampaignCreateForm(); |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * Build create form |
||
687 | * @todo Move to form builder |
||
688 | * |
||
689 | * @return Form |
||
690 | */ |
||
691 | public function getCampaignCreateForm() |
||
692 | { |
||
693 | $record = ChangeSet::singleton(); |
||
694 | if (!$record->canCreate()) { |
||
695 | $this->httpError(403); |
||
696 | return null; |
||
697 | } |
||
698 | $fields = $record->getCMSFields(); |
||
699 | |||
700 | // Add standard fields |
||
701 | $fields->push(HiddenField::create('ID')); |
||
702 | $form = Form::create( |
||
703 | $this, |
||
704 | 'campaignCreateForm', |
||
705 | $fields, |
||
706 | FieldList::create( |
||
707 | FormAction::create('save', _t(__CLASS__.'.CREATE', 'Create')) |
||
708 | ->setIcon('plus'), |
||
709 | FormAction::create('cancel', _t(__CLASS__.'.CANCEL', 'Cancel')) |
||
710 | ->setUseButtonTag(true) |
||
711 | ), |
||
712 | new RequiredFields('Name') |
||
713 | ); |
||
714 | |||
715 | // Custom form handler |
||
716 | $form->setRequestHandler( |
||
717 | LeftAndMainFormRequestHandler::create($form) |
||
718 | ); |
||
719 | |||
720 | // Configure form to respond to validation errors with form schema |
||
721 | // if requested via react. |
||
722 | $form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $record) { |
||
723 | $schemaId = $this->Link('schema/campaignCreateForm'); |
||
724 | return $this->getSchemaResponse($schemaId, $form, $errors); |
||
725 | }); |
||
726 | |||
727 | $form->setNotifyUnsavedChanges(true); |
||
728 | |||
729 | return $form; |
||
730 | } |
||
731 | |||
732 | /** |
||
733 | * Save handler |
||
734 | * |
||
735 | * @param array $data |
||
736 | * @param Form $form |
||
737 | * @return HTTPResponse |
||
738 | */ |
||
739 | public function save($data, $form) |
||
740 | { |
||
741 | $errors = null; |
||
742 | |||
743 | // Existing or new record? |
||
744 | $id = empty($data['ID']) ? 0 : $data['ID']; |
||
745 | if ($id) { |
||
746 | $record = ChangeSet::get()->byID($id); |
||
747 | if ($record && !$record->canEdit()) { |
||
748 | return Security::permissionFailure($this); |
||
749 | } |
||
750 | if (!$record || !$record->ID) { |
||
751 | $this->httpError(404, "Bad record ID #" . (int)$id); |
||
752 | } |
||
753 | } else { |
||
754 | if (!ChangeSet::singleton()->canCreate()) { |
||
755 | return Security::permissionFailure($this); |
||
756 | } |
||
757 | $record = ChangeSet::create(); |
||
758 | } |
||
759 | |||
760 | $hasExistingName = Changeset::get() |
||
761 | ->filter('Name:nocase', $data['Name']) |
||
762 | ->exclude('ID', $id) |
||
763 | ->count() > 0; |
||
764 | |||
765 | if (!$hasExistingName) { |
||
766 | // save form data into record |
||
767 | $form->saveInto($record, true); |
||
768 | $record->write(); |
||
769 | $this->extend('onAfterSave', $record); |
||
770 | $message = _t(__CLASS__.'.SAVEDUP', 'Saved.'); |
||
771 | $form->setMessage($message, ValidationResult::TYPE_GOOD); |
||
772 | } else { |
||
773 | $nameDuplicateMsg = _t(__CLASS__ . '.ERROR_DUPLICATE_NAME', 'Name "{Name}" already exists', '', [ 'Name' => $data['Name']]); |
||
774 | $errors = new ValidationResult(); |
||
775 | $errors->addFieldMessage('Name', $nameDuplicateMsg); |
||
776 | $message = _t(__CLASS__.'.SAVEDERROR', 'Error.'); |
||
777 | // Need to set the form message or the field message won't show up at all |
||
778 | $form->setMessage($message, ValidationResult::TYPE_ERROR); |
||
779 | } |
||
780 | |||
781 | if ($id) { |
||
782 | $schemaId = Controller::join_links($this->Link('schema'), 'campaignEditForm', $id); |
||
783 | } else { |
||
784 | $schemaId = Controller::join_links($this->Link('schema'), 'campaignCreateForm'); |
||
785 | } |
||
786 | |||
787 | // Ensure that newly created records have all their data loaded back into the form. |
||
788 | $form->loadDataFrom($record); |
||
789 | $extra = ['record' => ['id' => $record->ID]]; |
||
790 | $response = $this->getSchemaResponse($schemaId, $form, $errors, $extra); |
||
791 | $response->addHeader('X-Status', rawurlencode($message ?? '')); |
||
792 | return $response; |
||
793 | } |
||
794 | |||
795 | /** |
||
796 | * Gets user-visible url to edit a specific {@see ChangeSet} |
||
797 | * |
||
798 | * @param $itemID |
||
799 | * @return string |
||
800 | */ |
||
801 | public function SetLink($itemID) |
||
806 | ); |
||
807 | } |
||
808 | |||
809 | /** |
||
810 | * Gets user-visible url to edit a specific {@see ChangeSetItem} |
||
811 | * |
||
812 | * @param int $itemID |
||
813 | * @return string |
||
814 | */ |
||
815 | public function ItemLink($itemID) |
||
816 | { |
||
817 | return Controller::join_links( |
||
818 | $this->Link('item'), |
||
819 | $itemID |
||
820 | ); |
||
821 | } |
||
822 | |||
823 | public function providePermissions() |
||
836 | ) |
||
837 | ) |
||
838 | ); |
||
839 | } |
||
840 | |||
841 | /** |
||
842 | * Check if the given campaign should be synced before view |
||
843 | * |
||
844 | * @param ChangeSet $item |
||
845 | * @return bool |
||
846 | */ |
||
847 | protected function shouldCampaignSync(ChangeSet $item) |
||
859 | } |
||
860 | } |
||
861 |