/
CMSPageEditController.php
130 lines (111 loc) · 3.9 KB
/
CMSPageEditController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace SilverStripe\CMS\Controllers;
use Page;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\CampaignAdmin\AddToCampaignHandler;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\Form;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\ValidationResult;
/**
* @package cms
*/
class CMSPageEditController extends CMSMain
{
private static $url_segment = 'pages/edit';
private static $url_rule = '/$Action/$ID/$OtherID';
private static $url_priority = 41;
private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
private static $allowed_actions = [
'AddToCampaignForm',
];
public function getClientConfig()
{
return ArrayLib::array_merge_recursive(parent::getClientConfig(), [
'form' => [
'AddToCampaignForm' => [
'schemaUrl' => $this->Link('schema/AddToCampaignForm'),
],
'editorInternalLink' => [
'schemaUrl' => LeftAndMain::singleton()
->Link('methodSchema/Modals/editorInternalLink'),
],
'editorAnchorLink' => [
'schemaUrl' => LeftAndMain::singleton()
->Link('methodSchema/Modals/editorAnchorLink/:pageid'),
],
],
]);
}
/**
* Action handler for adding pages to a campaign
*/
public function addtocampaign(array $data, Form $form): HTTPResponse
{
$id = $data['ID'];
$record = \Page::get()->byID($id);
$handler = AddToCampaignHandler::create($this, $record);
$response = $handler->addToCampaign($record, $data);
$message = $response->getBody();
if (empty($message)) {
return $response;
}
if ($this->getSchemaRequested()) {
// Send extra "message" data with schema response
$extraData = ['message' => $message];
$schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id);
return $this->getSchemaResponse($schemaId, $form, null, $extraData);
}
return $response;
}
/**
* Url handler for add to campaign form
*
* @param HTTPRequest $request
* @return Form
*/
public function AddToCampaignForm($request)
{
// Get ID either from posted back value, or url parameter
$id = $request->param('ID') ?: $request->postVar('ID');
return $this->getAddToCampaignForm($id);
}
/**
* @param int $id
* @return Form
*/
public function getAddToCampaignForm($id)
{
// Get record-specific fields
$record = SiteTree::get()->byID($id);
if (!$record) {
$this->httpError(404, _t(
__CLASS__ . '.ErrorNotFound',
'That {Type} couldn\'t be found',
'',
['Type' => Page::singleton()->i18n_singular_name()]
));
return null;
}
if (!$record->canView()) {
$this->httpError(403, _t(
__CLASS__.'.ErrorItemPermissionDenied',
'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign',
'',
['ObjectTitle' => Page::singleton()->i18n_singular_name()]
));
return null;
}
$handler = AddToCampaignHandler::create($this, $record);
$form = $handler->Form($record);
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $id) {
$schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id);
return $this->getSchemaResponse($schemaId, $form, $errors);
});
return $form;
}
}