1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
persontype.php
См. документацию.
1<?php
2
3
4namespace Bitrix\Sale\Controller;
5
6
7use Bitrix\Main\Engine\Response\DataType\Page;
8use Bitrix\Main\Entity\ExpressionField;
9use Bitrix\Main\Error;
10use Bitrix\Main\Loader;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Main\SiteTable;
13use Bitrix\Main\UI\PageNavigation;
14use Bitrix\Sale\Result;
15
16Loc::loadMessages(__FILE__);
17
19{
20 //region Actions
21 public function getFieldsAction()
22 {
23 $view = $this->getViewManager()
24 ->getView($this)
25 ;
26
27 return [
28 'PERSON_TYPE' => $view->prepareFieldInfos($view->getFields()),
29 ];
30 }
31
32 public function addAction(array $fields)
33 {
34 $r = new Result();
35
36 $personTypeId = 0;
37 $salePersonType = new \CSalePersonType();
38
39 if (isset($fields['ID']))
40 {
41 unset($fields['ID']);
42 }
43
44 if (isset($fields['CODE']))
45 {
46 $r = $this->isCodeUniq($fields['CODE']);
47 }
48
49 if (Loader::includeModule('bitrix24'))
50 {
51 $selectedSite = SiteTable::getRow(['select' => ['LID'], 'filter' => ['DEF' => 'Y']]);
52
53 if (isset($selectedSite))
54 {
55 $fields['LID'] = $selectedSite['LID'];
56 }
57 }
58
59 if ($r->isSuccess())
60 {
61 $personTypeId = (int)$salePersonType->Add($fields);
62 if ($personTypeId <= 0)
63 {
64 if ($ex = self::getApplication()->GetException())
65 {
66 self::getApplication()->ResetException();
67 self::getApplication()->ThrowException($ex->GetString(), 200750000006);
68
69 $r->addError(new Error($ex->GetString(), $ex->GetID()));
70 }
71 else
72 {
73 $r->addError(new Error('add person type error', 200750000001));
74 }
75 }
76 }
77
78 if ($r->isSuccess())
79 {
80 return ['PERSON_TYPE' => $this->get($personTypeId)];
81 }
82 else
83 {
84 $this->addErrors($r->getErrors());
85
86 return null;
87 }
88 }
89
90 public function updateAction($id, array $fields)
91 {
92 $salePersonType = new \CSalePersonType();
93
94 $r = $this->exists($id);
95 if ($r->isSuccess())
96 {
97 if (isset($fields['CODE']))
98 {
99 $r = $this->isCodeUniq($fields['CODE'], $id);
100 }
101
102 if ($r->isSuccess())
103 {
104 if (!$salePersonType->Update($id, $fields))
105 {
106 if ($ex = self::getApplication()->GetException())
107 {
108 self::getApplication()->ResetException();
109 self::getApplication()->ThrowException($ex->GetString(), 200750000007);
110
111 $r->addError(new Error($ex->GetString(), $ex->GetID()));
112 }
113 else
114 {
115 $r->addError(new Error('update person type error', 200750000002));
116 }
117 }
118 }
119 }
120
121 if ($r->isSuccess())
122 {
123 return ['PERSON_TYPE' => $this->get($id)];
124 }
125 else
126 {
127 $this->addErrors($r->getErrors());
128
129 return null;
130 }
131 }
132
133 public function getAction($id)
134 {
135 $r = $this->exists($id);
136
137 if ($r->isSuccess())
138 {
139 return ['PERSON_TYPE' => $this->get($id)];
140 }
141 else
142 {
143 $this->addErrors($r->getErrors());
144
145 return null;
146 }
147 }
148
149 public function deleteAction($id)
150 {
151 $salePersonType = new \CSalePersonType();
152
153 $r = $this->exists($id);
154 if ($r->isSuccess())
155 {
156 $fields = $this->get($id);
157 if ($fields['CODE'] === 'CRM_COMPANY' || $fields['CODE'] === 'CRM_CONTACT')
158 {
159 $r->addError(new Error('person type code is protected', 200750000003));
160 }
161 else
162 {
163 if (!$salePersonType->Delete($id))
164 {
165 if ($ex = self::getApplication()->GetException())
166 {
167 self::getApplication()->ResetException();
168 self::getApplication()->ThrowException($ex->GetString(), 200750000008);
169
170 $r->addError(new Error($ex->GetString(), $ex->GetID()));
171 }
172 else
173 {
174 $r->addError(new Error( 'delete person type error', 200750000004));
175 }
176 }
177 }
178 }
179
180 if ($r->isSuccess())
181 {
182 return true;
183 }
184 else
185 {
186 $this->addErrors($r->getErrors());
187
188 return null;
189 }
190 }
191
192 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
193 {
194 $select = empty($select) ? ['*'] : $select;
195 $order = empty($order) ? ['ID'=>'ASC'] : $order;
196
198 [
199 'select' => $select,
200 'filter' => $filter,
201 'order' => $order,
202 'offset' => $pageNavigation->getOffset(),
203 'limit' => $pageNavigation->getLimit(),
204 ]
205 );
206
207 return new Page(
208 'PERSON_TYPES',
209 $items,
210 function () use ($filter)
211 {
213 'select' => ['CNT'],
214 'filter' => $filter,
215 'runtime' => [
216 new ExpressionField('CNT', 'COUNT(ID)')
217 ]
218 ])->fetch()['CNT'];
219 },
220 );
221 }
222 //end region
223
224 protected function get($id)
225 {
226 $r = \Bitrix\Sale\PersonType::getList(['filter'=>['ID'=>$id]])
227 ->fetchAll()
228 ;
229
230 return $r? $r[0]:[];
231 }
232
233 protected function isCodeUniq($code, $id=null)
234 {
235 $r = new Result();
236
237 if (\Bitrix\Sale\PersonType::getList(['filter'=>['CODE'=>$code, '!ID'=>$id]])->fetchAll())
238 $r->addError(new Error('person type code exists', 200750000005));
239
240 return $r;
241 }
242
243 protected function exists($id)
244 {
245 $r = new Result();
246 if($this->get($id)['ID']<=0)
247 $r->addError(new Error('person type is not exists', 200740400001));
248
249 return $r;
250 }
251
252 protected function checkModifyPermissionEntity()
253 {
254 $r = new Result();
255
256 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
257 if ($saleModulePermissions < "W")
258 {
259 $r->addError(new Error('Access Denied', 200040300020));
260 }
261
262 return $r;
263 }
264
265 protected function checkReadPermissionEntity()
266 {
267 $r = new Result();
268
269 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
270 if ($saleModulePermissions == "D")
271 {
272 $r->addError(new Error('Access Denied', 200040300010));
273 }
274
275 return $r;
276 }
277}
Определения error.php:15
isCodeUniq($code, $id=null)
Определения persontype.php:233
addAction(array $fields)
Определения persontype.php:32
updateAction($id, array $fields)
Определения persontype.php:90
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Определения persontype.php:192
static getList(array $parameters=[])
Определения persontype.php:100
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения aliases.php:54
$order
Определения payment.php:8
$saleModulePermissions
Определения tools.php:21
$items
Определения template.php:224
$fields
Определения yandex_run.php:501