1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: jgulledge |
||
5 | * Date: 9/30/2017 |
||
6 | * Time: 2:44 PM |
||
7 | */ |
||
8 | |||
9 | namespace LCI\Blend\Blendable; |
||
10 | |||
11 | |||
12 | use LCI\Blend\Helpers\MIGXTemplateVariableInput; |
||
13 | use LCI\Blend\Helpers\TemplateVariableInput; |
||
14 | use LCI\Blend\Helpers\TVInput\OptionValues; |
||
15 | |||
16 | class TemplateVariable extends Element |
||
17 | { |
||
18 | protected $template_names = []; |
||
19 | |||
20 | protected $detach_template_names = []; |
||
21 | |||
22 | /** @var array - [name => context, ...] */ |
||
23 | protected $media_sources = []; |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $opt_cache_key = 'elements/template-variables'; |
||
27 | |||
28 | /** @var string ~ the xPDO class name */ |
||
29 | protected $xpdo_simple_object_class = 'modTemplateVar'; |
||
30 | |||
31 | /** |
||
32 | * @return TemplateVariable |
||
33 | */ |
||
34 | public function getCurrentVersion() |
||
35 | { |
||
36 | /** @var TemplateVariable $element */ |
||
37 | $element = new self($this->modx, $this->blender, $this->getFieldName()); |
||
38 | return $element->setSeedsDir($this->getSeedsDir()); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $template_name |
||
43 | * @param int $rank |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function attachToTemplate($template_name, $rank = 0) |
||
48 | { |
||
49 | $this->template_names[] = [ |
||
50 | 'name' => $template_name, |
||
51 | 'rank' => $rank |
||
52 | ]; |
||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $template_name detach |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function detachFromTemplate($template_name) |
||
61 | { |
||
62 | $this->detach_template_names[] = $template_name; |
||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getFieldCaption() |
||
70 | { |
||
71 | return $this->blendable_xpdo_simple_object_data['caption']; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getFieldDefaultText() |
||
78 | { |
||
79 | return $this->blendable_xpdo_simple_object_data['default_text']; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getFieldDisplay() |
||
86 | { |
||
87 | return $this->blendable_xpdo_simple_object_data['display']; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getFieldElements() |
||
94 | { |
||
95 | return $this->blendable_xpdo_simple_object_data['elements']; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return TemplateVariableInput |
||
100 | */ |
||
101 | public function getInputPropertyHelper() |
||
102 | { |
||
103 | return new TemplateVariableInput($this->getFieldType()); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param string $type |
||
108 | * @return MIGXTemplateVariableInput |
||
109 | */ |
||
110 | public function getMIGXInputPropertyHelper(string $type='migx') |
||
111 | { |
||
112 | $this->setFieldType($type); |
||
113 | return new MIGXTemplateVariableInput($this->getFieldType()); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getFieldInputProperties() |
||
120 | { |
||
121 | return $this->blendable_xpdo_simple_object_data['input_properties']; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getFieldOutputProperties() |
||
128 | { |
||
129 | return $this->blendable_xpdo_simple_object_data['output_properties']; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return int |
||
134 | */ |
||
135 | public function getFieldRank() |
||
136 | { |
||
137 | return $this->blendable_xpdo_simple_object_data['rank']; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getFieldType() |
||
144 | { |
||
145 | return $this->blendable_xpdo_simple_object_data['type']; |
||
146 | } |
||
147 | |||
148 | // Setters: |
||
149 | /** |
||
150 | * @param string $value max characters: 80 |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setFieldCaption($value) |
||
154 | { |
||
155 | $this->blendable_xpdo_simple_object_data['caption'] = $value; |
||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string $value |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function setFieldDefaultText($value) |
||
164 | { |
||
165 | $this->blendable_xpdo_simple_object_data['default_text'] = $value; |
||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param string $value max characters: 20 |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setFieldDisplay($value) |
||
174 | { |
||
175 | $this->blendable_xpdo_simple_object_data['display'] = $value; |
||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $value ~ this is Input Option Values, this is setting it raw |
||
181 | * @deprecated use makeInputOptionValues() |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setFieldElements($value) |
||
185 | { |
||
186 | $this->blendable_xpdo_simple_object_data['elements'] = $value; |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return OptionValues |
||
192 | */ |
||
193 | public function makeInputOptionValues() |
||
194 | { |
||
195 | $this->blendable_xpdo_simple_object_data['elements'] = new OptionValues(); |
||
196 | |||
197 | return $this->blendable_xpdo_simple_object_data['elements']; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param array $value |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function setFieldInputProperties($value) |
||
205 | { |
||
206 | $this->blendable_xpdo_simple_object_data['input_properties'] = $value; |
||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string|array $value |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function setFieldOutputProperties($value) |
||
215 | { |
||
216 | $this->blendable_xpdo_simple_object_data['output_properties'] = $value; |
||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string $name ~ name of the media source |
||
222 | * @param string $context - default is web |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function setMediaSource($name = 'Filesystem', $context='web') |
||
226 | { |
||
227 | $this->media_sources[$name] = $context; |
||
228 | if ($context == 'web') { |
||
229 | // This probably is not needed any longer |
||
230 | $this->blendable_xpdo_simple_object_data['source'] = $name; |
||
231 | } |
||
232 | |||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param int $value |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function setFieldRank($value) |
||
241 | { |
||
242 | $this->blendable_xpdo_simple_object_data['rank'] = $value; |
||
243 | return $this; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param string $value max characters: 20, default options: |
||
248 | * autotag, checkbox, date, listbox, listbox-multiple, email, file, |
||
249 | * hidden, image, number, option [radio], resourcelist, richtext, tag, text, textarea, url |
||
250 | * @see https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function setFieldType($value) |
||
254 | { |
||
255 | $this->blendable_xpdo_simple_object_data['type'] = $value; |
||
256 | return $this; |
||
257 | } |
||
258 | |||
259 | protected function attachRelatedPieces() |
||
260 | { |
||
261 | if (count($this->template_names) > 0) { |
||
262 | $tvs = []; |
||
263 | foreach ($this->template_names as $template_name_data) { |
||
264 | // get the Template: |
||
265 | $template = $this->modx->getObject('modTemplate', ['templatename' => $template_name_data['name']]); |
||
266 | if ($template) { |
||
267 | $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $this->xPDOSimpleObject->getPrimaryKey(), 'templateid' => $template->getPrimaryKey()]); |
||
0 ignored issues
–
show
|
|||
268 | |||
269 | if (!$tvt) { |
||
270 | $tvt = $this->modx->newObject('modTemplateVarTemplate'); |
||
271 | } |
||
272 | $tvt->set('templateid', $template->get('id')); |
||
273 | $tvt->set('rand', $template_name_data['rank']); |
||
274 | |||
275 | $tvs[] = $tvt; |
||
276 | } else { |
||
277 | $this->error = true; |
||
278 | |||
279 | } |
||
280 | |||
281 | } |
||
282 | $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates'); |
||
283 | } |
||
284 | |||
285 | if (count($this->detach_template_names) > 0) { |
||
286 | $tvs = []; |
||
0 ignored issues
–
show
|
|||
287 | foreach ($this->detach_template_names as $template_name) { |
||
288 | // get the Template: |
||
289 | $template = $this->modx->getObject('modTemplate', ['templatename' => $template_name]); |
||
290 | if ($template) { |
||
291 | $tvt = $this->modx->getObject( |
||
292 | 'modTemplateVarTemplate', |
||
293 | [ |
||
294 | 'templateid' => $template->get('id'), |
||
295 | 'tmplvarid' => $this->xPDOSimpleObject->get('id') |
||
296 | ] |
||
297 | ); |
||
298 | |||
299 | if (is_object($tvt)) { |
||
300 | $tvt->remove(); |
||
301 | } |
||
302 | } else { |
||
303 | $this->error = true; |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * This method is called just after a successful blend/save() |
||
311 | */ |
||
312 | protected function attachRelatedPiecesAfterSave() |
||
313 | { |
||
314 | foreach ($this->media_sources as $name => $context) { |
||
315 | /** @var /modMediaSourceElement $modMediaSourceElement */ |
||
316 | $modMediaSourceElement = $this->modx->getObject( |
||
317 | 'sources.modMediaSourceElement', |
||
318 | [ |
||
319 | 'object_class' => 'modTemplateVar', |
||
320 | 'object' => $this->xPDOSimpleObject->get('id'), |
||
321 | 'context_key' => $context |
||
322 | ] |
||
323 | ); |
||
324 | |||
325 | if (!is_object($modMediaSourceElement)) { |
||
326 | $modMediaSourceElement = $this->modx->newObject('sources.modMediaSourceElement'); |
||
327 | |||
328 | $modMediaSourceElement->set('object_class', 'modTemplateVar'); |
||
329 | $modMediaSourceElement->set('object', $this->xPDOSimpleObject->get('id')); |
||
330 | $modMediaSourceElement->set('context_key', $context); |
||
331 | } |
||
332 | |||
333 | $modMediaSourceElement->set('source', $this->convertSource($name)); |
||
334 | |||
335 | if (!$modMediaSourceElement->save()) { |
||
336 | $this->blender->out('Template variable ' .$this->xPDOSimpleObject->get('name'). |
||
337 | 'did not attached the media source: '.$name); |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.