Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketpropertiesitembase.php
1<?php
2
3namespace Bitrix\Sale;
4
9
10Loc::loadMessages(__FILE__);
11
17{
21 public static function getAvailableFields()
22 {
23 return array(
24 'NAME' => 'NAME',
25 'VALUE' => 'VALUE',
26 'CODE' => 'CODE',
27 'SORT' => 'SORT',
28 'XML_ID' => 'XML_ID'
29 );
30 }
31
35 protected static function getMeaningfulFields()
36 {
37 return array();
38 }
39
43 public static function getRegistryEntity()
44 {
46 }
47
54 private static function createBasketPropertyItemObject(array $fields = [])
55 {
56 $registry = Registry::getInstance(static::getRegistryType());
57 $basketPropertyItemClassName = $registry->getBasketPropertyItemClassName();
58
59 return new $basketPropertyItemClassName($fields);
60 }
61
69 public static function create(BasketPropertiesCollectionBase $basketPropertiesCollection)
70 {
71 $basketPropertyItem = static::createBasketPropertyItemObject();
72 $basketPropertyItem->setCollection($basketPropertiesCollection);
73
74 $basketPropertyItem->setField('XML_ID', static::generateXmlId());
75
76 return $basketPropertyItem;
77 }
78
82 protected static function generateXmlId()
83 {
84 return uniqid('bx_');
85 }
86
95 public function save()
96 {
97 $this->checkCallingContext();
98
99 $result = new Result();
100
101 $id = $this->getId();
102
103 $fields = $this->fields->getChangedValues();
104
105 if (!empty($fields) && is_array($fields))
106 {
107 $map = static::getFieldsMap();
108 foreach ($map as $key => $value)
109 {
110 if ($value instanceof Main\Entity\StringField)
111 {
112 $fieldName = $value->getName();
113 if (array_key_exists($fieldName, $fields))
114 {
115 if (!empty($fields[$fieldName]) && mb_strlen($fields[$fieldName]) > $value->getSize())
116 {
117 $fields[$fieldName] = mb_substr($fields[$fieldName], 0, $value->getSize());
118 }
119 }
120 }
121 }
122 }
123
124 if ($id > 0)
125 {
126 if (!empty($fields) && is_array($fields))
127 {
128 $r = $this->updateInternal($id, $fields);
129 if (!$r->isSuccess())
130 {
131 $result->addErrors($r->getErrors());
132 return $result;
133 }
134
135 if ($resultData = $r->getData())
136 {
137 $result->setData($resultData);
138 }
139 }
140 }
141 else
142 {
144 $collection = $this->getCollection();
145 $basketItem = $collection->getBasketItem();
146 $fields['BASKET_ID'] = ($basketItem) ? $basketItem->getId() : 0;
147 $this->setFieldNoDemand('BASKET_ID', $fields['BASKET_ID']);
148
149 $r = $this->addInternal($fields);
150 if (!$r->isSuccess())
151 {
152 $result->addErrors($r->getErrors());
153 return $result;
154 }
155
156 if ($resultData = $r->getData())
157 {
158 $result->setData($resultData);
159 }
160
161 $id = $r->getId();
162 $this->setFieldNoDemand('ID', $id);
163 }
164
165 if ($id > 0)
166 {
167 $result->setId($id);
168 }
169
170 return $result;
171 }
172
173 /*
174 * @return void
175 */
176 private function checkCallingContext()
177 {
179 $collection = $this->getCollection();
180
181 $basketItem = $collection->getBasketItem();
182
183 $basket = $basketItem->getBasket();
184
185 $order = $basket->getOrder();
186
187 if ($order)
188 {
189 if (!$order->isSaveRunning())
190 {
191 trigger_error("Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
192 }
193 }
194 else
195 {
196 if (!$basket->isSaveRunning())
197 {
198 trigger_error("Incorrect call to the save process. Use method save() on \Bitrix\Sale\Basket entity", E_USER_WARNING);
199 }
200 }
201 }
202
207 public function verify()
208 {
209 $result = new Result();
210
211 $map = static::getFieldsMap();
212
213 $fieldValues = $fields = $this->fields->getValues();
214
215 $propertyName = (!empty($fieldValues['NAME'])) ? $fieldValues['NAME'] : "";
216 if ($this->getId() > 0)
217 {
218 $fields = $this->fields->getChangedValues();
219 }
220
221 foreach ($map as $key => $value)
222 {
223 if ($value instanceof Main\Entity\StringField)
224 {
225 $fieldName = $value->getName();
226 if (!empty($fields[$fieldName]) && mb_strlen($fields[$fieldName]) > $value->getSize())
227 {
228 if ($fieldName === 'NAME')
229 {
230 $propertyName = mb_substr($propertyName, 0, 50)."...";
231 }
232
233 $result->addError(
234 new ResultWarning(
236 "SALE_BASKET_ITEM_PROPERTY_MAX_LENGTH_ERROR",
237 array(
238 "#PROPERTY_NAME#" => $propertyName,
239 "#FIELD_TITLE#" => $fieldName,
240 "#MAX_LENGTH#" => $value->getSize()
241 )
242 ),
243 'SALE_BASKET_ITEM_PROPERTY_MAX_LENGTH_ERROR'
244 )
245 );
246 }
247 }
248 }
249
250 return $result;
251 }
252
260 public static function loadForBasketItem($id)
261 {
262 if (intval($id) <= 0)
263 {
264 throw new Main\ArgumentNullException("id");
265 }
266
267 $result = [];
268
269 $dbRes = static::getList([
270 'filter' => ["=BASKET_ID" => $id],
271 'order' => ["SORT" => "ASC", "ID" => "ASC"],
272 ]);
273
274 while ($property = $dbRes->fetch())
275 {
276 $result[] = static::createBasketPropertyItemObject($property);
277 }
278
279 return $result;
280 }
281
288 public static function loadForBasket($idList)
289 {
290 $result = [];
291
292 $dbRes = static::getList(
293 array(
294 'filter' => array("@BASKET_ID" => $idList),
295 'order' => array("SORT" => "ASC", "ID" => "ASC"),
296 )
297 );
298
299 while ($property = $dbRes->fetch())
300 {
301 $result[$property['BASKET_ID']][] = static::createBasketPropertyItemObject($property);
302 }
303
304 return $result;
305 }
306
307
308
313 abstract protected function addInternal(array $data);
314
320 abstract protected function updateInternal($primary, array $data);
321
326 public static function getList(array $parameters = [])
327 {
328 throw new NotImplementedException();
329 }
330
336 public static function getEntityEventName()
337 {
338 return 'SaleBasketPropertyItem';
339 }
340}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static create(BasketPropertiesCollectionBase $basketPropertiesCollection)
updateInternal($primary, array $data)
const ENTITY_BASKET_PROPERTY_ITEM
Definition registry.php:34
static getInstance($type)
Definition registry.php:183