Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
price.php
1<?php
2
3
5
6
12
13final class Price extends Base
14{
15 public function getFields()
16 {
17 return [
18 'ID'=>[
19 'TYPE'=>DataType::TYPE_INT,
20 'ATTRIBUTES'=>[
21 Attributes::READONLY
22 ]
23 ],
24 'PRODUCT_ID'=>[
25 'TYPE'=>DataType::TYPE_INT,
26 'ATTRIBUTES'=>[
27 Attributes::REQUIRED,
28 Attributes::IMMUTABLE
29 ]
30 ],
31 'EXTRA_ID'=>[
32 'TYPE'=>DataType::TYPE_INT,
33 ],
34 'CATALOG_GROUP_ID'=>[
35 'TYPE'=>DataType::TYPE_INT,
36 'ATTRIBUTES'=>[
37 Attributes::REQUIRED,
38 Attributes::IMMUTABLE
39 ]
40 ],
41 'PRICE'=>[
42 'TYPE'=>DataType::TYPE_FLOAT,
43 'ATTRIBUTES'=>[
44 Attributes::REQUIRED_ADD
45 ]
46 ],
47 'CURRENCY'=>[
48 'TYPE'=>DataType::TYPE_STRING,
49 'ATTRIBUTES'=>[
50 Attributes::REQUIRED_ADD
51 ]
52 ],
53 'TIMESTAMP_X'=>[
54 'TYPE'=>DataType::TYPE_DATETIME,
55 'ATTRIBUTES'=>[
56 Attributes::READONLY
57 ]
58 ],
59 'QUANTITY_FROM'=>[
60 'TYPE'=>DataType::TYPE_INT
61 ],
62 'QUANTITY_TO'=>[
63 'TYPE'=>DataType::TYPE_INT
64 ],
65 'PRICE_SCALE'=>[
66 'TYPE'=>DataType::TYPE_FLOAT,
67 'ATTRIBUTES'=>[
68 Attributes::READONLY
69 ]
70 ],
71 ];
72 }
73
74 public function convertKeysToSnakeCaseArguments($name, $arguments)
75 {
76 if ($name == 'modify')
77 {
78 if(isset($arguments['fields']))
79 {
80 $fields = $arguments['fields'];
81 if(!empty($fields))
82 $arguments['fields'] = $this->convertKeysToSnakeCaseFields($fields);
83 }
84 }
85 else
86 {
87 $arguments = parent::convertKeysToSnakeCaseArguments($name, $arguments);
88 }
89
90 return $arguments;
91 }
92
93 public function internalizeArguments($name, $arguments): array
94 {
95 if($name == 'modify')
96 {
97 $fields = $arguments['fields'];
98 $arguments['fields'] = $this->internalizeFieldsModify($fields);
99 }
100 else
101 {
102 parent::internalizeArguments($name, $arguments);
103 }
104
105 return $arguments;
106 }
107
108 public function internalizeFieldsModify($fields, $fieldsInfo=[])
109 {
110 $result = [];
111
112 $fieldsInfo = empty($fieldsInfo)? $this->getFields():$fieldsInfo;
113 $listFieldsInfoAdd = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::HIDDEN, Attributes::READONLY], 'ignoredFields'=>['PRODUCT_ID']]]);
114 $listFieldsInfoUpdate = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::HIDDEN, Attributes::READONLY, Attributes::IMMUTABLE], 'skipFields'=>['ID']]]);
115
116 if(isset($fields['PRODUCT']['ID']))
117 $result['PRODUCT']['ID'] = (int)$fields['PRODUCT']['ID'];
118
119 if(isset($fields['PRODUCT']['PRICES']))
120 {
121 foreach ($fields['PRODUCT']['PRICES'] as $k=>$item)
122 {
123 $result['PRODUCT']['PRICES'][$k] = $this->internalizeFields($item,
124 $this->isNewItem($item)? $listFieldsInfoAdd:$listFieldsInfoUpdate
125 );
126 }
127 }
128
129 return $result;
130 }
131
132 public function checkArguments($name, $arguments): Result
133 {
134 if($name == 'modify')
135 {
136 $fields = $arguments['fields'];
137 return $this->checkFieldsModify($fields);
138 }
139 else
140 {
141 return parent::checkArguments($name, $arguments);
142 }
143 }
144
145 public function checkFieldsModify($fields)
146 {
147 $r = new Result();
148
149 $emptyFields = [];
150 if(!isset($fields['PRODUCT']['ID']))
151 {
152 $emptyFields[] = '[product][id]';
153 }
154 if(!isset($fields['PRODUCT']['PRICES']) || !is_array($fields['PRODUCT']['PRICES']))
155 {
156 $emptyFields[] = '[product][prices][]';
157 }
158
159 if(count($emptyFields)>0)
160 {
161 $r->addError(new Error('Required fields: '.implode(', ', $emptyFields)));
162 }
163 else
164 {
165 $required = $this->checkRequiredFieldsModify($fields);
166 if(!$required->isSuccess())
167 $r->addError(new Error('Required fields: '.implode(' ', $required->getErrorMessages())));
168 }
169
170 return $r;
171 }
172
173 public function checkRequiredFieldsModify($fields)
174 {
175 $r = new Result();
176
177 $listFieldsInfoAdd = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::HIDDEN, Attributes::READONLY], 'ignoredFields'=>['PRODUCT_ID']]]);
178 $listFieldsInfoUpdate = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::HIDDEN, Attributes::READONLY, Attributes::IMMUTABLE]]]);
179
180 foreach ($fields['PRODUCT']['PRICES'] as $k=>$item)
181 {
182 $required = $this->checkRequiredFields($item,
183 $this->isNewItem($item)? $listFieldsInfoAdd:$listFieldsInfoUpdate
184 );
185 if(!$required->isSuccess())
186 {
187 $r->addError(new Error('[prices]['.$k.'] - '.implode(', ', $required->getErrorMessages()).'.'));
188 }
189 }
190 return $r;
191 }
192
193 public function externalizeResult($name, $fields): array
194 {
195 if ($name == 'modify')
196 {
197 return $this->externalizeFieldsModify($fields);
198 }
199 else
200 {
201 parent::externalizeResult($name, $fields);
202 }
203
204 return $fields;
205 }
206
207 public function externalizeFieldsModify($fields)
208 {
209 return $this->externalizeListFields($fields);
210 }
211
212 private function isNewItem($fields)
213 {
214 return (isset($fields['ID']) === false);
215 }
216}
internalizeArguments($name, $arguments)
Definition price.php:93
convertKeysToSnakeCaseArguments($name, $arguments)
Definition price.php:74
checkArguments($name, $arguments)
Definition price.php:132
internalizeFieldsModify($fields, $fieldsInfo=[])
Definition price.php:108
externalizeResult($name, $fields)
Definition price.php:193