Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
propertyvalue.php
1<?php
2
3
5
6
9use Bitrix\Main\Entity\ExpressionField;
11use Bitrix\Sale;
14
16{
17
18 public function getPrimaryAutoWiredParameter()
19 {
20 return new ExactParameter(
21 Sale\PropertyValue::class,
22 'propertyValue',
23 function($className, $id) {
24 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
25
27 $propertyValueClass = $registry->getPropertyValueClassName();
28
29 $r = $propertyValueClass::getList([
30 'select'=>['ORDER_ID'],
31 'filter'=>['ID'=>$id]
32 ]);
33
34 if($row = $r->fetch())
35 {
37 $orderClass = $registry->getOrderClassName();
38
39 $propertyValue = $orderClass::load($row['ORDER_ID'])
40 ->getPropertyCollection()
41 ->getItemById($id);
42 if ($propertyValue)
43 {
44 return $propertyValue;
45 }
46 }
47 else
48 {
49 $this->addError(new Error('property value is not exists', 201040400001));
50 }
51 return null;
52 }
53 );
54 }
55
56 //region Actions
57 public function getFieldsAction()
58 {
59 $view = $this->getViewManager()
60 ->getView($this);
61
62 return ['PROPERTY_VALUE'=>$view->prepareFieldInfos(
63 $view->getFields()
64 )];
65 }
66
67 public function modifyAction(array $fields)
68 {
69 $builder = $this->getBuilder();
70 $builder->buildEntityProperties($fields);
71
72 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
73 {
74 $this->addErrors($builder->getErrorsContainer()->getErrors());
75 return null;
76 }
77
78 $order = $builder->getOrder();
79
80 $r = $order->save();
81 if(!$r->isSuccess())
82 {
83 $this->addErrors($r->getErrors());
84 return null;
85 }
86
87 return ['PROPERTY_VALUES'=>$this->toArray($order)['ORDER']['PROPERTY_VALUES']];
88 }
89
90 public function deleteAction(Sale\PropertyValue $propertyValue)
91 {
92 $r = $propertyValue->delete();
93 if($r->isSuccess())
94 {
95 $r = $propertyValue
96 ->getCollection()
97 ->getOrder()
98 ->save();
99 }
100
101 if(!$r->isSuccess())
102 $this->addErrors($r->getErrors());
103
104 return $r->isSuccess();
105 }
106
107 public function getAction(Sale\PropertyValue $propertyValue)
108 {
109 return ['PROPERTY_VALUE'=>$this->getItem($propertyValue)];
110 }
111
112 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
113 {
114 $select = empty($select) ? ['*'] : $select;
115 $order = empty($order) ? ['ID'=>'ASC'] : $order;
116 $runtime = [
117 new \Bitrix\Main\Entity\ReferenceField(
118 'ORDER_PROPS',
119 '\Bitrix\Sale\Internals\OrderPropsTable',
120 array('=this.ORDER_PROPS_ID' => 'ref.ID')
121 )
122 ];
123
124 $payments = \Bitrix\Sale\PropertyValue::getList(
125 [
126 'select' => $select,
127 'filter' => $filter,
128 'order' => $order,
129 'offset' => $pageNavigation->getOffset(),
130 'limit' => $pageNavigation->getLimit(),
131 'runtime' => $runtime,
132 ]
133 )->fetchAll();
134
135 return new Page('PROPERTY_VALUES', $payments, function() use ($select, $filter, $runtime)
136 {
137 return (int) \Bitrix\Sale\PropertyValue::getList([
138 'select' => ['CNT'],
139 'filter' => $filter,
140 'runtime' => [new ExpressionField('CNT', 'COUNT(ID)')]
141 ])->fetch()['CNT'];
142 });
143 }
144 //end region
145
146 protected function getItem(Sale\PropertyValue $propertyValue, array $fields=[])
147 {
148 return current(
149 array_filter(
150 $propertyValue
151 ->getCollection()
152 ->toArray(),
153 function ($item) use ($propertyValue){
154 if($propertyValue->getPropertyId() == $item['ORDER_PROPS_ID'])
155 {
156 return $item;
157 }
158 }
159 )
160 );
161 }
162
163 static public function prepareFields($fields)
164 {
165 $data = null;
166
167 if(isset($fields['PROPERTY_VALUES']))
168 {
169 foreach($fields['PROPERTY_VALUES'] as $field)
170 {
171 $data[$field['ORDER_PROPS_ID']]=$field['VALUE'];
172 }
173 }
174 return is_array($data)?['PROPERTIES'=>$data]:[];
175 }
176
177 protected function checkModifyPermissionEntity()
178 {
179 $r = new Result();
180
181 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
182 if ($saleModulePermissions < "W")
183 {
184 $r->addError(new Error('Access Denied', 200040300020));
185 }
186 return $r;
187 }
188
189 protected function checkReadPermissionEntity()
190 {
191 $r = new Result();
192
193 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
194 if ($saleModulePermissions == "D")
195 {
196 $r->addError(new Error('Access Denied', 200040300010));
197 }
198 return $r;
199 }
200
201 protected function checkPermissionEntity($name, $arguments=[])
202 {
203 $name = mb_strtolower($name);
204
205 if($name == 'modify')
206 {
207 $r = $this->checkModifyPermissionEntity();
208 }
209 else
210 {
211 $r = parent::checkPermissionEntity($name);
212 }
213 return $r;
214 }
215}
static getList(array $parameters)
Definition entity.php:78
toArray(\Bitrix\Sale\Order $order, array $fields=[])
getBuilder(SettingsContainer $settings=null)
getItem(Sale\PropertyValue $propertyValue, array $fields=[])
deleteAction(Sale\PropertyValue $propertyValue)
getAction(Sale\PropertyValue $propertyValue)
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
checkPermissionEntity($name, $arguments=[])