These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SilverStripe\CMS\Controllers; |
||
4 | |||
5 | use Page; |
||
6 | use SilverStripe\Admin\LeftAndMain; |
||
7 | use SilverStripe\CampaignAdmin\AddToCampaignHandler; |
||
8 | use SilverStripe\CMS\Model\SiteTree; |
||
9 | use SilverStripe\Control\Controller; |
||
10 | use SilverStripe\Control\HTTPRequest; |
||
11 | use SilverStripe\Control\HTTPResponse; |
||
12 | use SilverStripe\Forms\Form; |
||
13 | use SilverStripe\ORM\FieldType\DBHTMLText; |
||
14 | |||
15 | /** |
||
16 | * @package cms |
||
17 | */ |
||
18 | class CMSPageEditController extends CMSMain |
||
19 | { |
||
20 | |||
21 | private static $url_segment = 'pages/edit'; |
||
22 | |||
23 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
24 | |||
25 | private static $url_priority = 41; |
||
26 | |||
27 | private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; |
||
28 | |||
29 | private static $allowed_actions = array( |
||
30 | 'AddToCampaignForm', |
||
31 | ); |
||
32 | |||
33 | public function getClientConfig() |
||
34 | { |
||
35 | return array_merge(parent::getClientConfig(), [ |
||
0 ignored issues
–
show
|
|||
36 | 'form' => [ |
||
37 | 'AddToCampaignForm' => [ |
||
38 | 'schemaUrl' => $this->Link('schema/AddToCampaignForm') |
||
39 | ], |
||
40 | 'editorInternalLink' => [ |
||
41 | 'schemaUrl' => LeftAndMain::singleton() |
||
42 | ->Link('methodSchema/Modals/editorInternalLink'), |
||
43 | ], |
||
44 | ], |
||
45 | ]); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Action handler for adding pages to a campaign |
||
50 | * |
||
51 | * @param array $data |
||
52 | * @param Form $form |
||
53 | * @return DBHTMLText|HTTPResponse |
||
54 | */ |
||
55 | public function addtocampaign($data, $form) |
||
56 | { |
||
57 | $id = $data['ID']; |
||
58 | $record = \Page::get()->byID($id); |
||
59 | |||
60 | $handler = AddToCampaignHandler::create($this, $record); |
||
61 | $results = $handler->addToCampaign($record, $data['Campaign']); |
||
62 | if (is_null($results)) { |
||
63 | return null; |
||
64 | } |
||
65 | |||
66 | if ($this->getSchemaRequested()) { |
||
67 | // Send extra "message" data with schema response |
||
68 | $extraData = ['message' => $results]; |
||
69 | $schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id); |
||
70 | return $this->getSchemaResponse($schemaId, $form, null, $extraData); |
||
71 | } |
||
72 | |||
73 | return $results; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Url handler for add to campaign form |
||
78 | * |
||
79 | * @param HTTPRequest $request |
||
80 | * @return Form |
||
81 | */ |
||
82 | public function AddToCampaignForm($request) |
||
83 | { |
||
84 | // Get ID either from posted back value, or url parameter |
||
85 | $id = $request->param('ID') ?: $request->postVar('ID'); |
||
86 | return $this->getAddToCampaignForm($id); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param int $id |
||
91 | * @return Form |
||
92 | */ |
||
93 | public function getAddToCampaignForm($id) |
||
94 | { |
||
95 | // Get record-specific fields |
||
96 | $record = SiteTree::get()->byID($id); |
||
97 | |||
98 | View Code Duplication | if (!$record) { |
|
99 | $this->httpError(404, _t( |
||
100 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorNotFound', |
||
101 | 'That {Type} couldn\'t be found', |
||
102 | '', |
||
103 | ['Type' => Page::singleton()->i18n_singular_name()] |
||
104 | )); |
||
105 | return null; |
||
106 | } |
||
107 | View Code Duplication | if (!$record->canView()) { |
|
108 | $this->httpError(403, _t( |
||
109 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorItemPermissionDenied', |
||
110 | 'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign', |
||
111 | '', |
||
112 | ['ObjectTitle' => Page::singleton()->i18n_singular_name()] |
||
113 | )); |
||
114 | return null; |
||
115 | } |
||
116 | |||
117 | $handler = AddToCampaignHandler::create($this, $record); |
||
118 | return $handler->Form($record); |
||
119 | } |
||
120 | } |
||
121 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.