1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CMSFactory; |
4
|
|
|
|
5
|
|
|
use CMSFactory\DependencyInjection\DependencyInjectionProvider; |
6
|
|
|
use Currency\Currency; |
7
|
|
|
use CustomFieldsData; |
8
|
|
|
use CustomFieldsDataQuery; |
9
|
|
|
use CustomFieldsQuery; |
10
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
11
|
|
|
use MY_Controller; |
12
|
|
|
use MY_Form_validation; |
13
|
|
|
use Propel\Runtime\Connection\ConnectionInterface; |
14
|
|
|
|
15
|
|
|
class PropelBaseModelClass implements \Serializable |
16
|
|
|
{ |
|
|
|
|
17
|
|
|
|
18
|
|
|
public $customFields; |
19
|
|
|
|
20
|
|
|
public $customData; |
21
|
|
|
|
22
|
|
|
public $hasCustomData = false; |
23
|
|
|
|
24
|
|
|
public $entityId; |
25
|
|
|
|
26
|
|
|
private $entities_locale = [ |
27
|
|
|
'product', |
28
|
|
|
'category', |
29
|
|
|
'brand', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param MY_Form_validation $validator |
34
|
|
|
* @return mixed |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function validateCustomData($validator) { |
37
|
|
|
if (!empty($this->entityName)) { |
|
|
|
|
38
|
|
|
$this->collectCustomData($this->entityName, $this->getId()); |
|
|
|
|
39
|
|
|
|
40
|
|
|
if ($this->hasCustomData !== false) { |
41
|
|
|
foreach ($_POST['custom_field'] as $key_post => $value_post) { |
42
|
|
|
foreach ($this->customFields as $key => $value) { |
43
|
|
|
if ((int) $key_post == $value['Id']) { |
44
|
|
|
|
45
|
|
|
$validator_str = ''; |
46
|
|
|
if ($value['IsRequired'] && $this->curentPostEntitySave($key)) { |
47
|
|
|
$validator_str = 'required'; |
48
|
|
|
} |
49
|
|
|
if ($value['Validators'] && $this->curentPostEntitySave($key)) { |
50
|
|
|
$validator_str .= '|' . $value['Validators']; |
51
|
|
|
} |
52
|
|
|
$name = array_shift($value['CustomFieldsI18ns'])['FieldLabel']; |
53
|
|
|
$validator->set_rules("custom_field[$key]", $name, $validator_str); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $validator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $entityName |
64
|
|
|
* @param int $id |
65
|
|
|
* @return bool |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public function collectCustomData($entityName, $id) { |
68
|
|
|
$this->entityId = $id; |
69
|
|
|
$this->customFields = CustomFieldsQuery::create() |
70
|
|
|
->joinWithI18n(MY_Controller::defaultLocale()) |
71
|
|
|
->filterByIsActive(1) |
72
|
|
|
->filterByEntity($entityName) |
73
|
|
|
->find() |
74
|
|
|
->toArray($keyColumn = 'id'); |
75
|
|
|
|
76
|
|
|
if (count($this->customFields)) { |
77
|
|
|
$this->hasCustomData = true; |
78
|
|
|
} else { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $key |
85
|
|
|
* @return bool |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
public function curentPostEntitySave($key) { |
88
|
|
|
$entity = CustomFieldsQuery::create()->findPk($key); |
89
|
|
|
if ($entity) { |
90
|
|
|
return $entity->getEntity() == $this->entityName; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function saveCustomData() { |
95
|
|
|
|
96
|
|
|
$locale = in_array($this->entityName, $this->entities_locale, true) ? chose_language() : MY_Controller::defaultLocale(); |
|
|
|
|
97
|
|
|
|
98
|
|
|
if ($this->hasCustomData === false) { |
99
|
|
|
$this->collectCustomData($this->entityName, $this->getId()); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
$data = $_POST['custom_field']; |
102
|
|
|
|
103
|
|
|
foreach ($this->customFields as $fieldObject) { |
104
|
|
|
if (!array_key_exists($fieldObject['Id'], $_POST['custom_field'])) { |
105
|
|
|
$data[$fieldObject['Id']] = ''; |
106
|
|
|
} |
107
|
|
|
foreach ($data as $key => $value) { |
108
|
|
|
if ((int) $key == $fieldObject['Id']) { |
109
|
|
|
|
110
|
|
|
$objCustomData = CustomFieldsDataQuery::create() |
111
|
|
|
->filterByentityId($this->entityId) |
112
|
|
|
->filterByfieldId($key) |
113
|
|
|
->filterByLocale($locale) |
114
|
|
|
->findOne(); |
115
|
|
|
if ($objCustomData) { |
116
|
|
|
$objCustomData->setdata($value); |
117
|
|
|
$objCustomData->save(); |
118
|
|
|
break; |
119
|
|
|
} else { |
120
|
|
|
$fieldObject = new CustomFieldsData(); |
121
|
|
|
$fieldObject->setentityId($this->entityId); |
122
|
|
|
$fieldObject->setfieldId($key); |
123
|
|
|
$fieldObject->setdata($value); |
124
|
|
|
$fieldObject->setLocale($locale); |
125
|
|
|
$fieldObject->save(); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $attributeName |
|
|
|
|
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public function getLabel($attributeName) { |
138
|
|
|
if (method_exists($this, 'attributeLabels')) { |
139
|
|
|
$labels = $this->attributeLabels(); |
|
|
|
|
140
|
|
|
|
141
|
|
|
if (isset($labels[$attributeName])) { |
142
|
|
|
return $labels[$attributeName]; |
143
|
|
|
} else { |
144
|
|
|
return $attributeName; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $column |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function getVirtual($column) { |
154
|
|
|
$column = strtolower($column); |
155
|
|
|
return $this->getVirtualColumn($column); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $name |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function getVirtualColumn($name) { |
163
|
|
|
$name = strtolower($name); |
164
|
|
|
|
165
|
|
|
if (!$this->hasVirtualColumn($name)) { |
|
|
|
|
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
return parent::getVirtualColumn($name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Convert model attribute(by default Price). e.g. "99.99 $" |
173
|
|
|
* |
174
|
|
|
* @param string $attributeName Optional. Attribute name to convert. |
175
|
|
|
* @access public |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function toCurrency($attributeName = 'Price', $cId = null, $convertForTemplate = false) { |
179
|
|
|
$attributeName = strtolower($attributeName); |
180
|
|
|
$get = 'get' . $attributeName; |
181
|
|
|
|
182
|
|
|
if (!$convertForTemplate) { |
183
|
|
|
if ($attributeName == 'origprice') { |
184
|
|
|
return Currency::create()->convert($this->getVirtualColumn('origprice'), $cId); |
185
|
|
|
} |
186
|
|
|
return Currency::create()->convert($this->$get(), $cId); |
187
|
|
|
} else { |
188
|
|
|
if ($attributeName == 'origprice') { |
189
|
|
|
return Currency::create()->convertForTemplate($this->getVirtualColumn('origprice'), $cId); |
190
|
|
|
} |
191
|
|
|
return Currency::create()->convertForTemplate($this->$get(), $cId); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Simple getter. |
197
|
|
|
* |
198
|
|
|
* @param $name |
|
|
|
|
199
|
|
|
* @return |
|
|
|
|
200
|
|
|
*/ |
201
|
|
|
public function __get($name) { |
202
|
|
|
if (isset($this->$name)) { |
203
|
|
|
return $this->$name; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$call = 'get' . $name; |
207
|
|
|
if (method_exists($this, $call)) { |
208
|
|
|
return $this->$call(); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return CacheProvider |
|
|
|
|
214
|
|
|
*/ |
215
|
|
|
public function getCache() { |
216
|
|
|
|
217
|
|
|
return DependencyInjectionProvider::getContainer()->get('cache'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function __call($name, $param) { |
221
|
|
|
|
222
|
|
|
if (preg_match('/get(\w+)/', $name, $matches)) { |
223
|
|
|
$virtualColumn = $matches[1]; |
224
|
|
|
$virtualColumn = strtolower($virtualColumn); |
225
|
|
|
if ($this->hasVirtualColumn($virtualColumn)) { |
|
|
|
|
226
|
|
|
return $this->getVirtualColumn($virtualColumn); |
227
|
|
|
} |
228
|
|
|
// no lcfirst in php<5.3... |
229
|
|
|
$virtualColumn[0] = strtolower($virtualColumn[0]); |
230
|
|
|
if ($this->hasVirtualColumn($virtualColumn)) { |
|
|
|
|
231
|
|
|
return $this->getVirtualColumn($virtualColumn); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function setVirtualColumn($name, $value) { |
237
|
|
|
|
238
|
|
|
$name = strtolower($name); |
239
|
|
|
|
240
|
|
|
parent::setVirtualColumn($name, $value); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function preSave(ConnectionInterface $con = null) { |
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function postSave(ConnectionInterface $con = null) { |
248
|
|
|
return true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function preInsert(ConnectionInterface $con = null) { |
252
|
|
|
return true; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function postInsert(ConnectionInterface $con = null) { |
256
|
|
|
return true; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function preUpdate(ConnectionInterface $con = null) { |
260
|
|
|
return true; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function postUpdate(ConnectionInterface $con = null) { |
264
|
|
|
return true; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function postDelete(ConnectionInterface $con = null) { |
268
|
|
|
return true; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function preDelete(ConnectionInterface $con = null) { |
272
|
|
|
return true; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* String representation of object |
277
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php |
278
|
|
|
* @return string the string representation of the object or null |
279
|
|
|
* @since 5.1.0 |
280
|
|
|
*/ |
281
|
|
|
public function serialize() { |
282
|
|
|
$this->prepareToSleep(); |
283
|
|
|
|
284
|
|
|
$cls = new \ReflectionClass($this); |
285
|
|
|
$propertyNames = []; |
286
|
|
|
$serializableProperties = array_diff($cls->getProperties(), $cls->getProperties(\ReflectionProperty::IS_STATIC)); |
287
|
|
|
|
288
|
|
|
foreach ($serializableProperties as $property) { |
289
|
|
|
|
290
|
|
|
$propertyName = $property->getName(); |
291
|
|
|
$propertyValue = $this->$propertyName; |
292
|
|
|
|
293
|
|
|
if ($propertyValue !== null) { |
294
|
|
|
$propertyNames[$propertyName] = $propertyValue; |
295
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return serialize($propertyNames); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
private function prepareToSleep() { |
304
|
|
|
$this->collSCategoriesRelatedById = null; |
|
|
|
|
305
|
|
|
$this->collSCategoryI18ns = null; |
|
|
|
|
306
|
|
|
$this->collSProductss = null; |
|
|
|
|
307
|
|
|
$this->collShopProductCategoriess = null; |
|
|
|
|
308
|
|
|
$this->collShopProductPropertiesCategoriess = null; |
|
|
|
|
309
|
|
|
$this->collProducts = null; |
|
|
|
|
310
|
|
|
$this->collProperties = null; |
|
|
|
|
311
|
|
|
$this->aSCategory = null; |
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Constructs the object |
316
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php |
317
|
|
|
* @param string $serialized <p> |
|
|
|
|
318
|
|
|
* The string representation of the object. |
319
|
|
|
* </p> |
|
|
|
|
320
|
|
|
* @return void |
|
|
|
|
321
|
|
|
* @since 5.1.0 |
322
|
|
|
*/ |
323
|
|
|
public function unserialize($serialized) { |
324
|
|
|
$serialized = unserialize($serialized); |
325
|
|
|
|
326
|
|
|
foreach ($serialized as $key => $item) { |
327
|
|
|
|
328
|
|
|
$this->$key = $item; |
329
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |