1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: jgulledge |
5
|
|
|
* Date: 9/30/2017 |
6
|
|
|
* Time: 11:47 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace LCI\Blend\Blendable; |
10
|
|
|
|
11
|
|
|
use LCI\Blend\Helpers\ElementProperty; |
12
|
|
|
|
13
|
|
|
abstract class Element extends Blendable |
14
|
|
|
{ |
15
|
|
|
use DescriptionGetterAndSetter; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $opt_cache_key = 'elements'; |
19
|
|
|
|
20
|
|
|
/** @var string ex: modResource */ |
21
|
|
|
protected $xpdo_simple_object_class = 'modElement'; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $unique_key_column = 'name'; |
25
|
|
|
|
26
|
|
|
/** @var array ~ this should match data to be inserted via xPDO, ex [column_name => value, ...] */ |
27
|
|
|
protected $blendable_xpdo_simple_object_data = [ |
28
|
|
|
'category' => '', |
29
|
|
|
//'cache_type' => 0,//bool |
30
|
|
|
//content: 'snippet', plugincode |
31
|
|
|
'description' => '', // 191 |
32
|
|
|
'editor_type' => 0, // int? |
33
|
|
|
'locked' => 0, // bool |
34
|
|
|
'name' => '', |
35
|
|
|
'property_preprocess' => 0, // bool |
36
|
|
|
'properties' => '', //?? |
37
|
|
|
'source' => 1, |
38
|
|
|
'static' => 0, |
39
|
|
|
'static_file' => '' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** @var array ~ ['setMethodName' => 'setMethodActualName', 'setDoNotUseMethod' => false] overwrite in child classes */ |
43
|
|
|
protected $load_from_array_aliases = [ |
44
|
|
|
'setFieldProperties' => 'mergePropertiesFromArray' |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** @var bool */ |
48
|
|
|
protected $overwrite_static = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getFieldCode() |
54
|
|
|
{ |
55
|
|
|
return $this->getFieldContent(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getFieldContent() |
62
|
|
|
{ |
63
|
|
|
return $this->blendable_xpdo_simple_object_data['content']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
|
|
public function getFieldEditorType() |
70
|
|
|
{ |
71
|
|
|
return $this->blendable_xpdo_simple_object_data['editor_type']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function getFieldLocked() |
78
|
|
|
{ |
79
|
|
|
return $this->blendable_xpdo_simple_object_data['locked']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getFieldName() |
86
|
|
|
{ |
87
|
|
|
return $this->blendable_xpdo_simple_object_data['name']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function getFieldPropertyPreprocess() |
94
|
|
|
{ |
95
|
|
|
return $this->blendable_xpdo_simple_object_data['property_preprocess']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Setters: |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $category ~ nest like so: Category=>Child=>Child |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setFieldCategory($category) |
106
|
|
|
{ |
107
|
|
|
$this->blendable_xpdo_simple_object_data['category'] = $category; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $code ~ if not doing static file then set the Elements code here |
113
|
|
|
* @param bool $overwrite_static ~ if the setAsStatic is ran, false will keep the static content code, true will overwrite the static file |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setFieldCode($code, $overwrite_static = false) |
117
|
|
|
{ |
118
|
|
|
$this->blendable_xpdo_simple_object_data['content'] = $code; |
119
|
|
|
$this->overwrite_static = $overwrite_static; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* duplicate method for setCode, matches MODX naming |
125
|
|
|
* @param string $code ~ if not doing static file then set the Elements code here |
126
|
|
|
* @param bool $overwrite_static ~ if the setAsStatic is ran, false will keep the static content code, true will overwrite the static file |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function setFieldContent($code, $overwrite_static = false) |
130
|
|
|
{ |
131
|
|
|
return $this->setFieldCode($code, $overwrite_static); |
132
|
|
|
} |
133
|
|
|
/** |
134
|
|
|
* @param int $value |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setFieldEditorType($value) |
138
|
|
|
{ |
139
|
|
|
$this->blendable_xpdo_simple_object_data['editor_type'] = $value; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $value |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setFieldLocked($value) |
148
|
|
|
{ |
149
|
|
|
$this->blendable_xpdo_simple_object_data['locked'] = $value; |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $name |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setFieldName($name) |
158
|
|
|
{ |
159
|
|
|
$this->blendable_xpdo_simple_object_data['name'] = $name; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param bool $value |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function setFieldPropertyPreprocess($value) |
168
|
|
|
{ |
169
|
|
|
$this->blendable_xpdo_simple_object_data['property_preprocess'] = $value; |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $file - the file path |
175
|
|
|
* @param string $media_source ~ name of the media source |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setAsStatic($file, $media_source = 'Filesystem') |
180
|
|
|
{ |
181
|
|
|
$this->blendable_xpdo_simple_object_data['source'] = $media_source; |
182
|
|
|
$this->blendable_xpdo_simple_object_data['static'] = true; |
183
|
|
|
$this->blendable_xpdo_simple_object_data['static_file'] = $file; |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param ElementProperty $elementProperty |
189
|
|
|
*/ |
190
|
|
|
public function setElementProperty(ElementProperty $elementProperty) |
191
|
|
|
{ |
192
|
|
|
$this->blendable_xpdo_simple_object_data['properties'][$elementProperty->getName()] = $elementProperty->toArray(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $name |
197
|
|
|
* @param string $value |
198
|
|
|
* @param string $description |
199
|
|
|
* @param string $type |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function setProperty($name, $value, $description, $type='textfield') |
204
|
|
|
{ |
205
|
|
|
$property = new ElementProperty($name); |
206
|
|
|
$property |
207
|
|
|
->setValue($value) |
208
|
|
|
->setDescription($description) |
209
|
|
|
->setType($type); |
210
|
|
|
|
211
|
|
|
$this->properties[$name] = $property->toArray(); |
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param array $data |
217
|
|
|
* |
218
|
|
|
* @return $this |
219
|
|
|
*/ |
220
|
|
|
public function setProperties(array $data) |
221
|
|
|
{ |
222
|
|
|
$this->blendable_xpdo_simple_object_data['properties'] = $data; |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $category |
229
|
|
|
* @return int |
230
|
|
|
*/ |
231
|
|
|
protected function convertCategory($category) |
232
|
|
|
{ |
233
|
|
|
$categories = explode('=>', $category); |
234
|
|
|
|
235
|
|
|
$category_names = []; |
236
|
|
|
$lineage = ''; |
237
|
|
|
|
238
|
|
|
$count = 0; |
239
|
|
|
foreach ($categories as $category) { |
|
|
|
|
240
|
|
|
if (!empty($lineage)) { |
241
|
|
|
$lineage .= '=>'; |
242
|
|
|
} |
243
|
|
|
$lineage .= trim($category); |
244
|
|
|
|
245
|
|
|
$category_names[$count++] = ['name' => $category, 'lineage' => $lineage]; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$category_id = 0; |
249
|
|
|
$category_map = $this->blender->getCategoryMap(); |
250
|
|
|
$refresh = false; |
251
|
|
|
foreach ($category_names as $count => $name_data) { |
252
|
|
|
$category = $name_data['name']; |
253
|
|
|
$lineage = $name_data['lineage']; |
254
|
|
|
|
255
|
|
|
if (isset($category_map['lineage'][$lineage]) && isset($category_map['lineage'][$lineage]['id'])) { |
256
|
|
|
$category_id = $category_map['lineage'][$lineage]['id']; |
257
|
|
|
|
258
|
|
|
} else { |
259
|
|
|
$newCategory = $this->modx->newObject('modCategory'); |
260
|
|
|
$newCategory->fromArray([ |
261
|
|
|
'parent' => $category_id, |
262
|
|
|
'category' => $category, |
263
|
|
|
'rank' => 0 |
264
|
|
|
]); |
265
|
|
|
$newCategory->save(); |
266
|
|
|
$category_id = $newCategory->get('id'); |
267
|
|
|
$refresh = true; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
$this->blender->getCategoryMap($refresh); |
271
|
|
|
return $category_id; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param array $media_source |
276
|
|
|
* @return int |
277
|
|
|
*/ |
278
|
|
|
protected function convertSource($media_source) |
279
|
|
|
{ |
280
|
|
|
if (empty($media_source)) { |
281
|
|
|
return 1; |
282
|
|
|
} |
283
|
|
|
$id = 1; |
284
|
|
|
|
285
|
|
|
$mediaSource = $this->modx->getObject('modMediaSource', ['name' => $media_source]); |
286
|
|
|
if (is_object($mediaSource)) { |
287
|
|
|
$id = $mediaSource->get('id'); |
288
|
|
|
} |
289
|
|
|
return $id; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param int $category_id |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
|
|
public function seedCategory($category_id = 0) |
298
|
|
|
{ |
299
|
|
|
$categories = $this->blender->getCategoryMap(); |
300
|
|
|
if (isset($categories['ids'][$category_id]) && isset($categories['ids'][$category_id]['lineage'])) { |
301
|
|
|
return $categories['ids'][$category_id]['lineage']; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return ''; |
305
|
|
|
} |
306
|
|
|
/** |
307
|
|
|
* @param int $media_source_id |
308
|
|
|
* @return string |
309
|
|
|
*/ |
310
|
|
|
protected function seedSource($media_source_id) |
311
|
|
|
{ |
312
|
|
|
$name = 'Filesystem'; |
313
|
|
|
|
314
|
|
|
$mediaSource = $this->modx->getObject('modMediaSource', $media_source_id); |
315
|
|
|
if (is_object($mediaSource)) { |
316
|
|
|
$name = $mediaSource->get('name'); |
317
|
|
|
} |
318
|
|
|
return $name; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|