/
CollectionAttributeControl.php
227 lines (200 loc) · 6.96 KB
/
CollectionAttributeControl.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
namespace Concrete\Core\Page\Type\Composer\Control;
use Concrete\Core\Attribute\Context\ComposerContext;
use Concrete\Core\Validation\ResponseInterface;
use Controller;
use CollectionAttributeKey;
use Concrete\Core\Page\Page;
use Environment;
class CollectionAttributeControl extends Control
{
protected $akID;
protected $ak = false;
protected $ptComposerControlTypeHandle = 'collection_attribute';
public function __sleep()
{
return array('akID');
}
public function setAttributeKeyID($akID)
{
$this->akID = $akID;
$this->setPageTypeComposerControlIdentifier($akID);
}
public function getPageTypeComposerControlName()
{
return $this->getAttributeKeyObject()->getAttributeKeyDisplayName('text');
}
public function pageTypeComposerFormControlSupportsValidation()
{
return true;
}
public function export($node)
{
$ak = $this->getAttributeKeyObject();
$node->addAttribute('handle', $ak->getAttributeKeyHandle());
}
public function removePageTypeComposerControlFromPage()
{
$ak = $this->getAttributeKeyObject();
if (is_object($ak)) {
$this->page->clearAttribute($ak);
}
}
/**
* @return CollectionAttributeKey
*/
public function getAttributeKeyObject()
{
if (!$this->ak) {
$this->ak = CollectionAttributeKey::getByID($this->akID);
}
return $this->ak;
}
public function getAttributeKeyID()
{
return $this->akID;
}
public function getPageTypeComposerControlCustomTemplates()
{
return array();
}
public function canPageTypeComposerControlSetPageName()
{
$ak = $this->getAttributeKeyObject();
if (is_object($ak) && $ak->getAttributeKeyHandle() == 'meta_title') {
return true;
}
return false;
}
public function getPageTypeComposerControlPageNameValue(Page $c)
{
$ak = $this->getAttributeKeyObject();
if (is_object($ak)) {
return $c->getAttribute($ak->getAttributeKeyHandle());
}
}
public function getPageTypeComposerControlDraftValue()
{
if (is_object($this->page)) {
$ak = $this->getAttributeKeyObject();
$c = $this->page;
if (is_object($ak)) {
return $c->getAttributeValueObject($ak);
}
} else {
$control = $this->getPageTypeComposerFormLayoutSetControlObject();
if (is_object($control)) {
$set = $control->getPageTypeComposerFormLayoutSetObject();
if (is_object($set)) {
$type = $set->getPageTypeObject();
// Does this page type have default attributes for this
// attribute ?
$defaults = $type->getPageTypePageTemplateDefaultPageObject();
if (is_object($defaults)) {
$value = $defaults->getAttributeValue($this->getAttributeKeyObject());
if (is_object($value)) {
return $value;
}
}
}
}
}
}
public function shouldPageTypeComposerControlStripEmptyValuesFromPage()
{
return true;
}
public function isPageTypeComposerControlValueEmpty()
{
$ak = $this->getAttributeKeyObject();
$c = $this->page;
if (!is_object($ak)) {
return true;
}
return $c->getAttribute($ak) == '';
}
public function render($label, $customTemplate, $description)
{
$ak = $this->getAttributeKeyObject();
if (is_object($ak)) {
$env = Environment::get();
$set = $this->getPageTypeComposerFormLayoutSetControlObject()->getPageTypeComposerFormLayoutSetObject();
$control = $this;
$context = new ComposerContext();
$context->setTooltip($description);
if ($this->isPageTypeComposerControlRequiredByDefault() || $this->isPageTypeComposerFormControlRequiredOnThisRequest()) {
$context->setRequired(true);
}
$view = $ak->getControlView($context);
$view->setValue($this->getPageTypeComposerControlDraftValue());
$view->setLabel($label);
$renderer = $view->getControlRenderer();
print $renderer->render();
}
}
public function publishToPage(Page $c, $data, $controls)
{
// the data for this actually doesn't come from $data. Attributes have their own way of gettin data.
$ak = $this->getAttributeKeyObject();
if (is_object($ak)) {
$controller = $ak->getController();
$value = $controller->createAttributeValueFromRequest();
$c->setAttribute($ak, $value);
}
}
protected function isFormSubmission()
{
$ak = $this->getAttributeKeyObject();
$controller = $ak->getController();
if (is_object($controller)) {
return $controller->requestFieldExists();
}
}
public function validate()
{
$ak = $this->getAttributeKeyObject();
if (is_object($ak)) {
$e = \Core::make('error');
if ($this->isFormSubmission()) {
$controller = $ak->getController();
$validator = $controller->getValidator();
$control = $this->getPageTypeComposerFormLayoutSetControlObject();
$response = $validator->validateSaveValueRequest(
$controller, \Request::createFromGlobals()
);
/**
* @var $response ResponseInterface
*/
if (!$response->isValid()) {
$error = $response->getErrorObject();
$e->add($error);
}
} else {
$value = $this->getPageTypeComposerControlDraftValue();
if (!is_object($value)) {
$control = $this->getPageTypeComposerFormLayoutSetControlObject();
$e->add(t('The field %s is required', $control->getPageTypeComposerControlLabel()));
} else {
$controller = $ak->getController();
$validator = $controller->getValidator();
$response = $validator->validateCurrentAttributeValue($controller, $value);
if (!$response->isValid()) {
$error = $response->getErrorObject();
$e->add($error);
}
}
}
return $e;
}
}
public function addAssetsToRequest(Controller $cnt)
{
$ak = $this->getAttributeKeyObject();
$akc = $ak->getController();
$akc->setupAndRun('composer');
}
public function objectExists()
{
return $this->getAttributeKeyObject() !== null;
}
}