Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
persontype.php
1<?php
2
3
5
6
8use Bitrix\Main\Entity\ExpressionField;
13
14Loc::loadMessages(__FILE__);
15
17{
18 //region Actions
19 public function getFieldsAction()
20 {
21 $view = $this->getViewManager()
22 ->getView($this);
23
24 return ['PERSON_TYPE'=>$view->prepareFieldInfos(
25 $view->getFields()
26 )];
27 }
28
29 public function addAction(array $fields)
30 {
31 $r = new Result();
32
33 $personTypeId = 0;
34 $salePersonType = new \CSalePersonType();
35
36 if(isset($fields['ID']))
37 unset($fields['ID']);
38
39 if(isset($fields['CODE']))
40 $r = $this->isCodeUniq($fields['CODE']);
41
42 if($r->isSuccess())
43 {
44 $personTypeId = $salePersonType->Add($fields);
45 if ((int)$personTypeId<=0)
46 {
47 if ($ex = self::getApplication()->GetException())
48 {
49 self::getApplication()->ResetException();
50 self::getApplication()->ThrowException($ex->GetString(), 200750000006);
51
52 $r->addError(new Error($ex->GetString(), $ex->GetID()));
53 }
54 else
55 $r->addError(new Error('add person type error', 200750000001));
56 }
57 }
58
59 if($r->isSuccess())
60 {
61 return ['PERSON_TYPE'=>$this->get($personTypeId)];
62 }
63 else
64 {
65 $this->addErrors($r->getErrors());
66 return null;
67 }
68 }
69
70 public function updateAction($id, array $fields)
71 {
72 $salePersonType = new \CSalePersonType();
73
74 $r = $this->exists($id);
75 if($r->isSuccess())
76 {
77 if(isset($fields['CODE']))
78 $r = $this->isCodeUniq($fields['CODE'], $id);
79
80 if($r->isSuccess())
81 {
82 if (!$salePersonType->Update($id, $fields))
83 {
84 if ($ex = self::getApplication()->GetException())
85 {
86 self::getApplication()->ResetException();
87 self::getApplication()->ThrowException($ex->GetString(), 200750000007);
88
89 $r->addError(new Error($ex->GetString(), $ex->GetID()));
90 }
91 else
92 $r->addError(new Error('update person type error', 200750000002));
93 }
94 }
95 }
96
97 if($r->isSuccess())
98 {
99 return ['PERSON_TYPE'=>$this->get($id)];
100 }
101 else
102 {
103 $this->addErrors($r->getErrors());
104 return null;
105 }
106 }
107
108 public function getAction($id)
109 {
110 $r = $this->exists($id);
111 if($r->isSuccess())
112 {
113 return ['PERSON_TYPE'=>$this->get($id)];
114 }
115 else
116 {
117 $this->addErrors($r->getErrors());
118 return null;
119 }
120 }
121
122 public function deleteAction($id)
123 {
124 $salePersonType = new \CSalePersonType();
125
126 $r = $this->exists($id);
127 if($r->isSuccess())
128 {
129 $fields = $this->get($id);
130 if ($fields['CODE'] === 'CRM_COMPANY' || $fields['CODE'] === 'CRM_CONTACT')
131 {
132 $r->addError(new Error('person type code is protected', 200750000003));
133 }
134 else
135 {
136 if (!$salePersonType->Delete($id))
137 {
138 if ($ex = self::getApplication()->GetException())
139 {
140 self::getApplication()->ResetException();
141 self::getApplication()->ThrowException($ex->GetString(), 200750000008);
142
143 $r->addError(new Error($ex->GetString(), $ex->GetID()));
144 }
145 else
146 $r->addError(new Error( 'delete person type error', 200750000004));
147 }
148 }
149 }
150
151 if($r->isSuccess())
152 {
153 return true;
154 }
155 else
156 {
157 $this->addErrors($r->getErrors());
158 return null;
159 }
160 }
161
162 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
163 {
164 $select = empty($select) ? ['*'] : $select;
165 $order = empty($order) ? ['ID'=>'ASC'] : $order;
166
167 $items = \Bitrix\Sale\PersonType::getList(
168 [
169 'select' => $select,
170 'filter' => $filter,
171 'order' => $order,
172 'offset' => $pageNavigation->getOffset(),
173 'limit' => $pageNavigation->getLimit(),
174 ]
175 );
176
177 return new Page('PERSON_TYPES', $items, function() use ($filter)
178 {
179 return (int) \Bitrix\Sale\PersonType::getList([
180 'select' => ['CNT'],
181 'filter' => $filter,
182 'runtime' => [
183 new ExpressionField('CNT', 'COUNT(ID)')
184 ]
185 ])->fetch()['CNT'];
186 });
187 }
188 //end region
189
190 protected function get($id)
191 {
192 $r = \Bitrix\Sale\PersonType::getList(['filter'=>['ID'=>$id]])->fetchAll();
193 return $r? $r[0]:[];
194 }
195
196 protected function isCodeUniq($code, $id=null)
197 {
198 $r = new Result();
199
200 if (\Bitrix\Sale\PersonType::getList(['filter'=>['CODE'=>$code, '!ID'=>$id]])->fetchAll())
201 $r->addError(new Error('person type code exists', 200750000005));
202
203 return $r;
204 }
205
206 protected function exists($id)
207 {
208 $r = new Result();
209 if($this->get($id)['ID']<=0)
210 $r->addError(new Error('person type is not exists', 200740400001));
211
212 return $r;
213 }
214
215 protected function checkModifyPermissionEntity()
216 {
217 $r = new Result();
218
219 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
220 if ($saleModulePermissions < "W")
221 {
222 $r->addError(new Error('Access Denied', 200040300020));
223 }
224 return $r;
225 }
226
227 protected function checkReadPermissionEntity()
228 {
229 $r = new Result();
230
231 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
232 if ($saleModulePermissions == "D")
233 {
234 $r->addError(new Error('Access Denied', 200040300010));
235 }
236 return $r;
237 }
238}
static loadMessages($file)
Definition loc.php:64
updateAction($id, array $fields)
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
static getList(array $parameters=[])