1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\RecordsTraits\HasSmartProperties; |
4
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as PropertyValue; |
6
|
|
|
use ByTIC\Models\SmartProperties\Properties\Definitions\Definition; |
7
|
|
|
use Nip\Records\RecordManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RecordTrait |
11
|
|
|
* @package ByTIC\Models\SmartProperties\RecordsTraits\HasStatus |
12
|
|
|
* |
13
|
|
|
* @property string $status |
14
|
|
|
* @method RecordManager|RecordsTrait getManager() |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
trait RecordTrait |
18
|
|
|
{ |
19
|
|
|
use \ByTIC\Models\SmartProperties\RecordsTraits\AbstractTrait\RecordTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var PropertyValue[] |
23
|
|
|
*/ |
24
|
|
|
protected $smartProperties = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $name |
28
|
|
|
* @return PropertyValue |
29
|
|
|
*/ |
30
|
9 |
|
public function getSmartProperty($name) |
31
|
|
|
{ |
32
|
9 |
|
$this->checkSmartProperty($name); |
33
|
|
|
|
34
|
9 |
|
return $this->smartProperties[$name]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
*/ |
40
|
9 |
|
public function checkSmartProperty($name) |
41
|
|
|
{ |
42
|
9 |
|
if (!isset($this->smartProperties[$name])) { |
43
|
8 |
|
$this->initSmartProperty($name); |
44
|
|
|
} |
45
|
9 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
*/ |
50
|
8 |
|
public function initSmartProperty($name) |
51
|
|
|
{ |
52
|
8 |
|
if ($this->getManager()->hasSmartPropertyDefinition($name)) { |
53
|
8 |
|
$definition = $this->getManager()->getSmartPropertyDefinition($name); |
54
|
8 |
|
$property = $this->getNewSmartPropertyFromDefinition($definition); |
55
|
8 |
|
$this->setSmartProperty($name, $property); |
56
|
|
|
} |
57
|
8 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Definition $definition |
61
|
|
|
* @return PropertyValue |
62
|
|
|
*/ |
63
|
8 |
|
public function getNewSmartPropertyFromDefinition($definition) |
64
|
|
|
{ |
65
|
8 |
|
$name = $definition->getName(); |
66
|
8 |
|
$value = $this->getSmartPropertyValueFromDefinition($definition); |
67
|
|
|
|
68
|
8 |
|
return $this->getNewSmartPropertyFromValue($name, $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Definition $definition |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
8 |
|
public function getSmartPropertyValueFromDefinition($definition) |
76
|
|
|
{ |
77
|
8 |
|
$field = $definition->getField(); |
78
|
8 |
|
$value = $this->getAttributeFromArray($field); |
|
|
|
|
79
|
8 |
|
if ($value === null) { |
80
|
6 |
|
$value = $definition->getDefaultValue(); |
81
|
|
|
} |
82
|
|
|
|
83
|
8 |
|
return $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $name |
88
|
|
|
* @param $value |
89
|
|
|
* @return PropertyValue |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
8 |
|
public function getNewSmartPropertyFromValue($name, $value) |
93
|
|
|
{ |
94
|
8 |
|
$object = clone $this->getManager()->getSmartPropertyItem($name, $value); |
95
|
8 |
|
$object->setItem($this); |
|
|
|
|
96
|
|
|
|
97
|
8 |
|
return $object; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $name |
102
|
|
|
* @param PropertyValue|string $value |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
9 |
|
protected function setSmartProperty($name, $value) |
106
|
|
|
{ |
107
|
9 |
|
$definition = $this->getManager()->getSmartPropertyDefinition($name); |
108
|
9 |
|
$field = $definition->getField(); |
|
|
|
|
109
|
9 |
|
if ($value instanceof PropertyValue) { |
110
|
9 |
|
$this->setDataValue($field, $value->getName()); |
|
|
|
|
111
|
9 |
|
$this->smartProperties[$name] = $value; |
112
|
9 |
|
return; |
113
|
|
|
} |
114
|
4 |
|
if (!empty($value)) { |
115
|
4 |
|
$currentProperty = $this->getSmartProperty($name); |
116
|
4 |
|
if (is_object($currentProperty) && $currentProperty->getName() === $value) { |
117
|
1 |
|
return; |
118
|
|
|
} |
119
|
4 |
|
$this->setDataValue($field, $value); |
120
|
4 |
|
$this->smartProperties[$name] = $this->getNewSmartPropertyFromValue($name, $value); |
121
|
|
|
} |
122
|
4 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $name |
126
|
|
|
* @param $value |
127
|
|
|
* @return bool |
128
|
|
|
* @throws \Exception |
129
|
|
|
*/ |
130
|
1 |
|
public function updateSmartProperty($name, $value) |
131
|
|
|
{ |
132
|
1 |
|
if (!empty($value)) { |
133
|
1 |
|
$newStatus = $this->getNewSmartPropertyFromValue($name, $value); |
134
|
1 |
|
$this->setSmartProperty($name, $newStatus); |
135
|
1 |
|
$return = $newStatus->update(); |
136
|
1 |
|
return $return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|