/
SiteConfigLeftAndMain.php
177 lines (153 loc) · 5.17 KB
/
SiteConfigLeftAndMain.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace SilverStripe\SiteConfig;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Versioned\RecursivePublishable;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
class SiteConfigLeftAndMain extends LeftAndMain
{
/**
* @var string
*/
private static $url_segment = 'settings';
/**
* @var string
*/
private static $url_rule = '/$Action/$ID/$OtherID';
/**
* @var int
*/
private static $menu_priority = -1;
/**
* @var string
*/
private static $menu_title = 'Settings';
/**
* @var string
*/
private static $menu_icon_class = 'font-icon-cog';
/**
* @var string
*/
private static $tree_class = SiteConfig::class;
/**
* @var array
*/
private static $required_permission_codes = array('EDIT_SITECONFIG');
/**
* Initialises the {@link SiteConfig} controller.
*/
public function init()
{
parent::init();
if (class_exists(SiteTree::class)) {
Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js');
}
}
/**
* @param null $id Not used.
* @param null $fields Not used.
*
* @return Form
*/
public function getEditForm($id = null, $fields = null)
{
$siteConfig = SiteConfig::current_site_config();
$fields = $siteConfig->getCMSFields();
// Tell the CMS what URL the preview should show
$home = Director::absoluteBaseURL();
$fields->push(new HiddenField('PreviewURL', 'Preview URL', $home));
if ($siteConfig instanceof CMSPreviewable || $siteConfig->has_extension(CMSPreviewable::class)) {
// Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load
$fields->push($navField = new LiteralField(
'SilverStripeNavigator',
$this->getSilverStripeNavigator($siteConfig)
));
$navField->setAllowHTML(true);
}
$validator = $siteConfig->getCMSCompositeValidator();
$actions = $siteConfig->getCMSActions();
$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this,
'EditForm',
$fields,
$actions,
$validator
)->setHTMLID('Form_EditForm');
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
$request = $this->getRequest();
if ($request->isAjax() && $negotiator) {
$result = $form->forTemplate();
return $negotiator->respond($request, array(
'CurrentForm' => function () use ($result) {
return $result;
}
));
}
});
$form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form');
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
if ($form->Fields()->hasTabSet()) {
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
}
$form->setHTMLID('Form_EditForm');
$form->loadDataFrom($siteConfig);
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// Announce the capability so the frontend can decide whether to allow preview or not.
if ($siteConfig instanceof CMSPreviewable || $siteConfig->has_extension(CMSPreviewable::class)) {
$form->addExtraClass('cms-previewable');
}
// Use <button> to allow full jQuery UI styling
$actions = $actions->dataFields();
if ($actions) {
/** @var FormAction $action */
foreach ($actions as $action) {
$action->setUseButtonTag(true);
}
}
$this->extend('updateEditForm', $form);
return $form;
}
/**
* Save the current sites {@link SiteConfig} into the database.
*
* @param array $data
* @param Form $form
* @return String
*/
public function save_siteconfig($data, $form)
{
$data = $form->getData();
$siteConfig = DataObject::get_by_id(SiteConfig::class, $data['ID']);
$form->saveInto($siteConfig);
$siteConfig->write();
if ($siteConfig->hasExtension(RecursivePublishable::class)) {
$siteConfig->publishRecursive();
}
$this->response->addHeader(
'X-Status',
rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'))
);
return $form->forTemplate();
}
public function Breadcrumbs($unlinked = false)
{
return new ArrayList(array(
new ArrayData(array(
'Title' => static::menu_title(),
'Link' => $this->Link()
))
));
}
}