/
View.php
201 lines (178 loc) · 6.2 KB
/
View.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
<?php
namespace Concrete\Core\Attribute;
use Concrete\Core\Attribute\Context\AttributeTypeSettingsContext;
use Concrete\Core\Attribute\Context\BasicFormContext;
use Concrete\Core\Attribute\Context\BasicSearchContext;
use Concrete\Core\Attribute\Context\ComposerContext;
use Concrete\Core\Filesystem\TemplateLocation;
use Concrete\Core\Form\Context\ContextInterface;
use Concrete\Core\Filesystem\TemplateLocator;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use Loader;
use Concrete\Core\Attribute\Value\Value as AttributeValue;
use Concrete\Core\Attribute\Key\Key as AttributeKey;
use Environment;
use Concrete\Core\View\AbstractView;
class View extends AbstractView
{
protected $attributeValue;
protected $attributeKey;
protected $attributeObject;
protected $attributeType;
protected $attributePkgHandle;
protected $templateLocator;
protected $controllerAction;
protected function getValue()
{
return $this->attributeValue;
}
protected function getAttributeKey()
{
return $this->attributeKey;
}
/**
* @param ObjectInterface $attributeObject
*/
public function setAttributeObject(ObjectInterface $attributeObject): void
{
$this->attributeObject = $attributeObject;
}
protected function constructView($mixed)
{
if ($mixed instanceof AttributeValueInterface) {
$this->attributeValue = $mixed;
$this->attributeKey = $mixed->getAttributeKey();
$this->attributeType = $mixed->getAttributeTypeObject();
} else {
if ($mixed instanceof AttributeKeyInterface) {
$this->attributeKey = $mixed;
$this->attributeType = $this->attributeKey->getAttributeType();
} else {
$this->attributeType = $mixed;
}
}
$this->attributePkgHandle = $this->attributeType->getPackageHandle();
}
/**
* @deprecated
*/
protected function getContextFromString($string)
{
switch($string) {
case 'form':
$context = new BasicFormContext();
break;
case 'composer':
$context = new ComposerContext();
break;
case 'search':
$context = new BasicSearchContext();
break;
case 'type_form':
$context = new AttributeTypeSettingsContext();
break;
case 'label':
$context = 'label';
break;
}
return $context;
}
/**
* @param $start ContextInterface
*/
public function start($context)
{
if (is_string($context)) {
$context = $this->getContextFromString($context);
}
$atHandle = $this->attributeType->getAttributeTypeHandle();
$this->setupController();
if (is_object($context)) {
$this->templateLocator = new TemplateLocator();
foreach($context->getControlTemplates() as $template) {
$pkgHandle = $template[1] ? $template[1] : $this->attributePkgHandle;
$location = new TemplateLocation(DIRNAME_ATTRIBUTES . '/' . $atHandle . '/' . $template[0] . '.php', $pkgHandle);
$this->templateLocator->addLocation($location);
}
foreach($context->getActions() as $method) {
if (method_exists($this->controller, $method)) {
$this->controllerAction = $method;
break;
}
}
} else if ($context == 'label') {
// sigh. legacy
$this->controllerAction = 'label';
}
}
public function startRender()
{
if (isset($this->templateLocator)) {
$location = $this->templateLocator->getLocation();
$file = basename($location->getFile());
// turn /path/to/my/file/view.php into view
$name = substr($file, 0, strpos($file, '.php'));
$js = $this->controller->getAttributeTypeFileURL($name . '.js');
$css = $this->controller->getAttributeTypeFileURL($name . '.css');
$html = Loader::helper('html');
if ($js != false) {
$this->addOutputAsset($html->javascript($js));
}
if ($css != false) {
$this->addOutputAsset($html->css($css));
}
}
}
public function setupRender()
{
$this->runControllerTask();
if (isset($this->templateLocator)) {
$file = $this->templateLocator->getFile();
$this->setViewTemplate($file);
}
}
public function setupController()
{
if (!$this->controller) {
$this->controller = $this->attributeType->getController();
$this->controller->setAttributeKey($this->attributeKey);
$this->controller->setAttributeValue($this->attributeValue);
if (isset($this->attributeObject)) {
$this->controller->setAttributeObject($this->attributeObject);
}
if (is_object($this->attributeKey)) {
$this->controller->set('akID', $this->attributeKey->getAttributeKeyID());
}
}
}
public function runControllerTask()
{
$this->controller->on_start();
$this->controller->runAction($this->controllerAction);
$this->controller->on_before_render();
}
public function action($action)
{
$arguments = func_get_args();
if (is_object($this->attributeKey)) {
array_unshift($arguments, '/ccm/system/attribute/action/key', $this->attributeKey->getAttributeKeyID());
} else {
array_unshift($arguments, '/ccm/system/attribute/action/type', $this->controller->attributeType->getAttributeTypeID());
}
return (string) app(ResolverManagerInterface::class)->resolve($arguments);
}
public function finishRender($contents)
{
echo $contents;
}
protected function onBeforeGetContents()
{
}
protected function onAfterGetContents()
{
}
public function field($fieldName)
{
return $this->controller->field($fieldName);
}
}