Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ownerentity.php
1<?php
2
4
10use Bitrix\Main\Entity\ReferenceField;
11
12Loc::loadMessages(__FILE__);
13
42{
45
46 const ENTITY_EMPTY = 0;
47
53 public static function getTableName()
54 {
55 return 'b_rest_owner_entity';
56 }
57
63 public static function getMap()
64 {
65 return [
66 new IntegerField(
67 'ID',
68 [
69 'primary' => true,
70 'autocomplete' => true
71 ]
72 ),
73 new StringField(
74 'OWNER_TYPE',
75 [
76 'required' => true,
77 'validation' => [__CLASS__, 'validateOwnerType']
78 ]
79 ),
80 new StringField(
81 'OWNER',
82 [
83 'required' => true,
84 'validation' => [__CLASS__, 'validateOwner']
85 ]
86 ),
87 new StringField(
88 'ENTITY_TYPE',
89 [
90 'required' => true,
91 'validation' => [__CLASS__, 'validateEntityType']
92 ]
93 ),
94 new StringField(
95 'ENTITY',
96 [
97 'required' => true,
98 'validation' => [__CLASS__, 'validateEntity']
99 ]
100 ),
101 new ReferenceField(
102 'DATA_APP',
103 '\Bitrix\Rest\AppTable',
104 array(
105 '=this.OWNER' => 'ref.ID',
106 )
107 )
108 ];
109 }
110
116 public static function validateOwnerType()
117 {
118 return [
119 new LengthValidator(null, 1),
120 ];
121 }
122
128 public static function validateOwner()
129 {
130 return [
131 new LengthValidator(null, 11),
132 ];
133 }
134
140 public static function validateEntityType()
141 {
142 return [
143 new LengthValidator(null, 32),
144 ];
145 }
146
152 public static function validateEntity()
153 {
154 return [
155 new LengthValidator(null, 32),
156 ];
157 }
158
164 public static function saveMulti($owner, $ownerType, $itemList)
165 {
166 if (is_array($itemList))
167 {
168 if (!empty($itemList['ENTITY_TYPE']) && !empty($itemList['ENTITY']))
169 {
170 try
171 {
172 static::add(
173 [
174 'ENTITY_TYPE' => $itemList['ENTITY_TYPE'],
175 'ENTITY' => $itemList['ENTITY'],
176 'OWNER_TYPE' => $ownerType,
177 'OWNER' => $owner,
178 ]
179 );
180 }
181 catch (\Exception $e)
182 {
183 }
184 }
185 else
186 {
187 foreach ($itemList as $entity)
188 {
189 if (!empty($entity['ENTITY_TYPE']) && !empty($entity['ENTITY']))
190 {
191 try
192 {
193 static::add(
194 [
195 'ENTITY_TYPE' => $entity['ENTITY_TYPE'],
196 'ENTITY' => $entity['ENTITY'],
197 'OWNER_TYPE' => $ownerType,
198 'OWNER' => $owner,
199 ]
200 );
201 }
202 catch (\Exception $e)
203 {
204 }
205 }
206 }
207 }
208 }
209 }
210
214 public static function deleteMulti($itemList)
215 {
216
217 if (is_array($itemList))
218 {
219 if (!empty($itemList['ENTITY_TYPE']) && !empty($itemList['ENTITY']))
220 {
221 $res = static::getList(
222 [
223 'filter' => [
224 '=ENTITY_TYPE' => $itemList['ENTITY_TYPE'],
225 '=ENTITY' => $itemList['ENTITY']
226 ]
227 ]
228 );
229 if ($item = $res->fetch())
230 {
231 static::delete($item['ID']);
232 }
233 }
234 else
235 {
236 $entityList = [];
237 foreach ($itemList as $entity)
238 {
239 if (!empty($entity['ENTITY_TYPE']) && !empty($entity['ENTITY']))
240 {
241 $entityList[$entity['ENTITY_TYPE']][] = $entity['ENTITY'];
242 }
243 }
244 $res = static::getList(
245 [
246 'filter' => [
247 '=ENTITY_TYPE' => array_keys($entityList)
248 ]
249 ]
250 );
251 while ($item = $res->fetch())
252 {
253 if (
254 !empty($entityList[$item['ENTITY_TYPE']])
255 && in_array($item['ENTITY'], $entityList[$item['ENTITY_TYPE']])
256 )
257 {
258 static::delete($item['ID']);
259 }
260 }
261 }
262 }
263 }
264
265 public static function checkApp($entityType, $entityId)
266 {
267 $res = static::getList(
268 [
269 'filter' => [
270 '=ENTITY_TYPE' => $entityType,
271 '=ENTITY' => $entityId,
272 '=OWNER_TYPE' => static::ENTITY_TYPE_APPLICATION,
273 '>OWNER' => 0
274 ],
275 'select' => [
276 'OWNER',
277 'APP_CODE' => 'DATA_APP.CODE'
278 ]
279 ]
280 );
281
282 if ($item = $res->fetch())
283 {
284 if ($item['OWNER'] > 0)
285 {
286 $url = \Bitrix\Rest\Marketplace\Url::getApplicationDetailUrl($item['APP_CODE']);
287 $appStatus = \Bitrix\Rest\AppTable::getAppStatusInfo($item['OWNER'], $url);
288 if ($appStatus['PAYMENT_NOTIFY'] == 'Y')
289 {
290 return $appStatus;
291 }
292 }
293 }
294
295 return null;
296 }
297}
static loadMessages($file)
Definition loc.php:64
static checkApp($entityType, $entityId)
static saveMulti($owner, $ownerType, $itemList)