forked from pimcore/pimcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperty.php
More file actions
374 lines (326 loc) · 7.41 KB
/
Copy pathProperty.php
File metadata and controls
374 lines (326 loc) · 7.41 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Model;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\Service;
/**
* @method \Pimcore\Model\Property\Dao getDao()
* @method void save()
*/
final class Property extends AbstractModel
{
/**
* @var string
*/
protected $name;
/**
* @var mixed
*/
protected $data;
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $ctype;
/**
* @var string|null
*/
protected $cpath;
/**
* @var int
*/
protected $cid;
/**
* @var bool
*/
protected $inheritable;
/**
* @var bool
*/
protected $inherited = false;
/**
* @internal
*
* @param mixed $data
*
* @return $this
*/
public function setDataFromEditmode($data)
{
// IMPORTANT: if you use this method be sure that the type of the property is already set
if (in_array($this->getType(), ['document', 'asset', 'object'])) {
$el = Element\Service::getElementByPath($this->getType(), $data);
$this->data = null;
if ($el) {
$this->data = $el->getId();
}
} elseif ($this->type == 'bool') {
$this->data = false;
if (!empty($data)) {
$this->data = true;
}
} else {
// plain text
$this->data = $data;
}
return $this;
}
/**
* @internal
*
* @param mixed $data
*
* @return $this
*/
public function setDataFromResource($data)
{
// IMPORTANT: if you use this method be sure that the type of the property is already set
// do not set data for object, asset and document here, this is loaded dynamically when calling $this->getData();
if ($this->type == 'date') {
$this->data = $data;
// $this->data = \Pimcore\Tool\Serialize::unserialize($data);
} elseif ($this->type == 'bool') {
$this->data = false;
if (!empty($data)) {
$this->data = true;
}
} else {
// plain text
$this->data = $data;
}
return $this;
}
/**
* @return int
*/
public function getCid()
{
return $this->cid;
}
/**
* enum('document','asset','object')
*
* @return string
*/
public function getCtype()
{
return $this->ctype;
}
/**
* @return mixed
*/
public function getData()
{
// lazy-load data of type asset, document, object
if (in_array($this->getType(), ['document', 'asset', 'object']) && !$this->data instanceof ElementInterface && is_numeric($this->data)) {
return Element\Service::getElementById($this->getType(), $this->data);
}
return $this->data;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* enum('text','document','asset','object','bool','select','date')
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param int $cid
*
* @return $this
*/
public function setCid($cid)
{
$this->cid = (int) $cid;
return $this;
}
/**
* enum('document','asset','object')
*
* @param string $ctype
*
* @return $this
*/
public function setCtype($ctype)
{
$this->ctype = $ctype;
return $this;
}
/**
* @param mixed $data
*
* @return $this
*/
public function setData($data)
{
if ($data instanceof ElementInterface) {
$this->setType(Service::getElementType($data));
$data = $data->getId();
}
$this->data = $data;
return $this;
}
/**
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* enum('text','document','asset','object','bool','select','date')
*
* @param string $type
*
* @return $this
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @return string|null
*/
public function getCpath()
{
return $this->cpath;
}
/**
* @return bool
*/
public function getInherited()
{
return $this->inherited;
}
/**
* Alias for getInherited()
*
* @return bool
*/
public function isInherited()
{
return $this->getInherited();
}
/**
* @param string|null $cpath
*
* @return $this
*/
public function setCpath($cpath)
{
$this->cpath = $cpath;
return $this;
}
/**
* @param bool $inherited
*
* @return $this
*/
public function setInherited($inherited)
{
$this->inherited = (bool) $inherited;
return $this;
}
/**
* @return bool
*/
public function getInheritable()
{
return $this->inheritable;
}
/**
* @param bool $inheritable
*
* @return $this
*/
public function setInheritable($inheritable)
{
$this->inheritable = (bool) $inheritable;
return $this;
}
/**
* @internal
*
* @return array
*/
public function resolveDependencies()
{
$dependencies = [];
if ($this->getData() instanceof ElementInterface) {
$elementType = Element\Service::getElementType($this->getData());
$key = $elementType . '_' . $this->getData()->getId();
$dependencies[$key] = [
'id' => $this->getData()->getId(),
'type' => $elementType,
];
}
return $dependencies;
}
/**
* Rewrites id from source to target, $idMapping contains
* array(
* "document" => array(
* SOURCE_ID => TARGET_ID,
* SOURCE_ID => TARGET_ID
* ),
* "object" => array(...),
* "asset" => array(...)
* )
*
* @internal
*
* @param array $idMapping
*/
public function rewriteIds($idMapping)
{
if (!$this->isInherited()) {
if (array_key_exists($this->getType(), $idMapping)) {
if ($this->getData() instanceof ElementInterface) {
if (array_key_exists((int) $this->getData()->getId(), $idMapping[$this->getType()])) {
$this->setData(Element\Service::getElementById($this->getType(), $idMapping[$this->getType()][$this->getData()->getId()]));
}
}
}
}
}
/**
* @internal
*
* @return array
*/
public function serialize()
{
return [
'name' => $this->getName(),
'type' => $this->getType(),
'data' => $this->getData(),
];
}
}