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 SebastianBergmann\CodeCoverage\Report\PHP; |
13
|
|
|
|
14
|
|
|
class Template extends Element |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $opt_cache_key = 'elements/templates'; |
18
|
|
|
|
19
|
|
|
/** @var string ~ the xPDO class name */ |
20
|
|
|
protected $xpdo_simple_object_class = 'modTemplate'; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $unique_key_column = 'templatename'; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $tv_names = []; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
protected $detach_tvs = []; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected $tv_seeds = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return \LCI\Blend\Blendable\Template |
36
|
|
|
*/ |
37
|
|
|
public function getCurrentVersion() |
38
|
|
|
{ |
39
|
|
|
/** @var \LCI\Blend\Blendable\Template $element */ |
40
|
|
|
$element = new self($this->modx, $this->blender, $this->getFieldName()); |
41
|
|
|
return $element->setSeedsDir($this->getSeedsDir()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getFieldIcon() |
48
|
|
|
{ |
49
|
|
|
return $this->blendable_xpdo_simple_object_data['icon']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getFieldName() |
56
|
|
|
{ |
57
|
|
|
return $this->getFieldTemplateName(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getFieldTemplateName() |
64
|
|
|
{ |
65
|
|
|
return $this->blendable_xpdo_simple_object_data['templatename']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getFieldTemplateType() |
72
|
|
|
{ |
73
|
|
|
return $this->blendable_xpdo_simple_object_data['template_type']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $icon |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setFieldIcon($icon) |
81
|
|
|
{ |
82
|
|
|
$this->blendable_xpdo_simple_object_data['icon'] = $icon; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
/** |
86
|
|
|
* @param string $name |
87
|
|
|
* @return $this |
88
|
|
|
* @throws \LCI\Blend\Exception\BlendableKeyLengthException |
89
|
|
|
*/ |
90
|
|
|
public function setFieldName($name) |
91
|
|
|
{ |
92
|
|
|
return $this->setFieldTemplateName($name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $name |
97
|
|
|
* @return $this |
98
|
|
|
* @throws \LCI\Blend\Exception\BlendableKeyLengthException |
99
|
|
|
*/ |
100
|
|
|
public function setFieldTemplateName($name) |
101
|
|
|
{ |
102
|
|
|
$this->setUniqueCriteria($name); |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $name |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setFieldTemplateType($name) |
111
|
|
|
{ |
112
|
|
|
$this->blendable_xpdo_simple_object_data['template_type'] = $name; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $tvs |
118
|
|
|
*/ |
119
|
|
|
public function setTvs($tvs) |
120
|
|
|
{ |
121
|
|
|
$this->tv_seeds = $tvs; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $tv_name |
126
|
|
|
* @param int $rank |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function attachTemplateVariable($tv_name, $rank = 0) |
131
|
|
|
{ |
132
|
|
|
if (!isset($this->related_data['attach'])) { |
133
|
|
|
$this->related_data['attach'] = []; |
134
|
|
|
} |
135
|
|
|
$this->related_data['attach'][] = [ |
136
|
|
|
'name' => $tv_name, |
137
|
|
|
'rank' => $rank |
138
|
|
|
]; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $tv_name |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function detachTemplateVariable($tv_name) |
147
|
|
|
{ |
148
|
|
|
if (!isset($this->related_data['detach'])) { |
149
|
|
|
$this->related_data['detach'] = []; |
150
|
|
|
} |
151
|
|
|
$this->related_data['detach'][] = $tv_name; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @deprecated use detachTemplateVariable |
157
|
|
|
* @param $tv_name |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function detachTV($tv_name) |
161
|
|
|
{ |
162
|
|
|
return $this->detachTemplateVariable($tv_name); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function attachRelatedPieces() |
166
|
|
|
{ |
167
|
|
|
if (isset($this->related_data['seeds'])) { |
168
|
|
|
foreach ($this->related_data['seeds'] as $tv) { |
169
|
|
|
// blend the the TV from seed: |
170
|
|
|
$tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
171
|
|
|
$tvSeed |
172
|
|
|
->setSeedsDir($this->getSeedsDir()) |
173
|
|
|
->blendFromSeed($tv['seed_key'], true); |
174
|
|
|
|
175
|
|
|
$this->attachTemplateVariable($tv['name'], $tv['rank']); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (isset($this->related_data['attach']) && count($this->related_data['attach']) > 0) { |
180
|
|
|
$tvs = []; |
181
|
|
|
foreach ($this->related_data['attach'] as $tv_name_data) { |
182
|
|
|
// get the TV: |
183
|
|
|
$tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name_data['name']]); |
184
|
|
|
if ($tv) { |
185
|
|
|
$tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $tv->get('id'), 'templateid' => $this->xPDOSimpleObject->getPrimaryKey()]); |
|
|
|
|
186
|
|
|
|
187
|
|
|
if (!$tvt) { |
188
|
|
|
$tvt = $this->modx->newObject('modTemplateVarTemplate'); |
189
|
|
|
} |
190
|
|
|
$tvt->set('tmplvarid', $tv->get('id')); |
191
|
|
|
$tvt->set('rand', $tv_name_data['rank']); |
192
|
|
|
|
193
|
|
|
$tvs[] = $tvt; |
194
|
|
|
} else { |
195
|
|
|
$this->error = true; |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
$this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates'); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function attachRelatedPiecesAfterSave() |
205
|
|
|
{ |
206
|
|
|
if (isset($this->related_data['detach']) && count($this->related_data['detach']) > 0) { |
207
|
|
|
foreach ($this->related_data['detach'] as $tv_name) { |
208
|
|
|
// get the TV: |
209
|
|
|
$tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name]); |
210
|
|
|
if ($tv) { |
211
|
|
|
$templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate', array( |
212
|
|
|
'tmplvarid' => $tv->get('id'), |
213
|
|
|
'templateid' => $this->xPDOSimpleObject->get('id'), |
214
|
|
|
)); |
215
|
|
|
if ($templateVarTemplate && $templateVarTemplate instanceof \modTemplateVarTemplate) { |
|
|
|
|
216
|
|
|
$templateVarTemplate->remove(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* |
225
|
|
|
*/ |
226
|
|
|
protected function onDeleteRevertRelatedPieces() |
227
|
|
|
{ |
228
|
|
|
if (!isset($this->related_data['seeds']) || empty($this->related_data['seeds'])) { |
229
|
|
|
// If this is a seed then the TVs are not in the revert file but the seed file |
230
|
|
|
$name = $this->getFieldTemplateName(); |
231
|
|
|
if (empty ($name) && isset($this->current_xpdo_simple_object_data['templatename'])) { |
232
|
|
|
$name = $this->current_xpdo_simple_object_data['templatename']; |
233
|
|
|
} |
234
|
|
|
$this->loadObjectDataFromSeed($this->blender->getSeedKeyFromName($name)); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (isset($this->related_data['seeds'])) { |
238
|
|
|
|
239
|
|
|
foreach ($this->related_data['seeds'] as $tv) { |
240
|
|
|
// seed the TV: |
241
|
|
|
$tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
242
|
|
|
$tvSeed |
243
|
|
|
->setSeedsDir($this->getSeedsDir()) |
244
|
|
|
->revertBlend(); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @var string $type blend or revert |
251
|
|
|
*/ |
252
|
|
|
protected function seedRelated($type = 'blend') |
253
|
|
|
{ |
254
|
|
|
// get all related TVs: |
255
|
|
|
$tv_keys = []; |
256
|
|
|
if (is_object($this->xPDOSimpleObject)) { |
257
|
|
|
$tvTemplates = $this->xPDOSimpleObject->getMany('TemplateVarTemplates'); |
258
|
|
|
/** @var \modTemplateVarTemplate $tvTemplate */ |
259
|
|
|
foreach ($tvTemplates as $tvTemplate) { |
260
|
|
|
/** @var \modTemplateVar $tv */ |
261
|
|
|
$tv = $tvTemplate->getOne('TemplateVar'); |
262
|
|
|
$tv_name = $tv->get('name'); |
263
|
|
|
|
264
|
|
|
$tvSeed = new TemplateVariable($this->modx, $this->blender, $tv_name); |
265
|
|
|
|
266
|
|
|
if ($this->type == 'revert') { |
267
|
|
|
$seed_key = $tvSeed |
268
|
|
|
->setSeedsDir($this->getSeedsDir()) |
269
|
|
|
->makeHistory(); |
270
|
|
|
|
271
|
|
|
} else { |
272
|
|
|
$seed_key = $tvSeed |
273
|
|
|
->setSeedsDir($this->getSeedsDir()) |
274
|
|
|
->seed(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$tv_keys[] = [ |
278
|
|
|
'seed_key' => $seed_key, |
279
|
|
|
'name' => $tv_name, |
280
|
|
|
'rank' => $tvTemplate->get('rank') |
281
|
|
|
]; |
282
|
|
|
$this->blender->out('TV '.$tv_name.' has been seeded: '.$seed_key); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$this->related_data['seeds'] = $tv_keys; |
286
|
|
|
|
287
|
|
|
} elseif ($type != 'revert') { |
288
|
|
|
$this->related_data['seeds'] = $tv_keys; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
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.