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