Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usageentity.php
1<?php
2namespace Bitrix\Rest;
3
5use \Bitrix\Main\DB\SqlQueryException;
6
35class UsageEntityTable extends Main\Entity\DataManager
36{
37
40
51 const SUB_ENTITY_TYPE_UI = 'U';
53
54 protected static $info = array();
55
61 public static function getTableName()
62 {
63 return 'b_rest_usage_entity';
64 }
65
71 public static function getMap()
72 {
73 return array(
74 'ID' => array(
75 'data_type' => 'integer',
76 'primary' => true,
77 'autocomplete' => true
78 ),
79 'ENTITY_TYPE' => array(
80 'data_type' => 'string',
81 'required' => true,
82 'values' => array(
83 self::ENTITY_TYPE_APPLICATION,
84 self::ENTITY_TYPE_WEBHOOK
85 ),
86 'validation' => array(
87 __CLASS__,
88 'validateEntityType'
89 )
90 ),
91 'ENTITY_ID' => array(
92 'data_type' => 'integer',
93 'required' => true
94 ),
95 'ENTITY_CODE' => array(
96 'data_type' => 'string',
97 'validation' => array(
98 __CLASS__,
99 'validateEntityCode'
100 )
101 ),
102 'SUB_ENTITY_TYPE' => array(
103 'data_type' => 'string',
104 'values' => array(
105 self::SUB_ENTITY_TYPE_METHOD,
106 self::SUB_ENTITY_TYPE_EVENT,
107 self::SUB_ENTITY_TYPE_PLACEMENT,
108 self::SUB_ENTITY_TYPE_ROBOT,
109 self::SUB_ENTITY_TYPE_BIZ_PROC,
110 self::SUB_ENTITY_TYPE_ACTIVITY,
111 self::SUB_ENTITY_TYPE_CONFIGURATION,
112 self::SUB_ENTITY_TYPE_SEND_MESSAGE,
113 self::SUB_ENTITY_TYPE_LANDING,
114 self::SUB_ENTITY_TYPE_LANDING_KNOWLEDGE,
115 self::SUB_ENTITY_TYPE_BI_SUPERSET,
116 ),
117 'validation' => array(
118 __CLASS__,
119 'validateSubEntityType'
120 )
121 ),
122 'SUB_ENTITY_NAME' => array(
123 'data_type' => 'string',
124 'validation' => array(
125 __CLASS__,
126 'validateSubEntityName'
127 )
128 ),
129 );
130 }
131
137 protected static function getEntityInfo($entityType, $entityId)
138 {
139 $key = $entityType.'|'.$entityId;
140 if (!isset(static::$info[$key]))
141 {
142 if ($entityType == self::ENTITY_TYPE_APPLICATION)
143 {
144 $appInfo = AppTable::getByClientId($entityId);
145 static::$info[$key] = array(
146 'ENTITY_ID' => $appInfo['ID'],
147 'ENTITY_CODE' => $appInfo['CLIENT_ID'],
148 );
149 }
150 else
151 {
152 static::$info[$key] = array(
153 'ENTITY_ID' => $entityId,
154 'ENTITY_CODE' => '',
155 );
156 }
157 }
158 return static::$info[$key];
159 }
160
171 public static function register($entityType, $entityId, $subEntityType, $subEntityName)
172 {
173 $entity = static::getEntityInfo($entityType, $entityId);
174
175 $getListParameters = [
176 'filter' => [
177 '=ENTITY_TYPE' => $entityType,
178 '=ENTITY_ID' => $entity['ENTITY_ID'],
179 '=SUB_ENTITY_TYPE' => $subEntityType,
180 '=SUB_ENTITY_NAME' => $subEntityName,
181 ],
182 'select' => [
183 'ID',
184 ],
185 'limit' => 1,
186 ];
187
188 $res = static::getList($getListParameters);
189 $element = $res->fetch();
190 if ($element)
191 {
192 return $element['ID'];
193 }
194
195 $newEntity = array(
196 'ENTITY_TYPE' => $entityType,
197 'ENTITY_ID' => $entity['ENTITY_ID'],
198 'ENTITY_CODE' => $entity['ENTITY_CODE'],
199 'SUB_ENTITY_TYPE' => $subEntityType,
200 'SUB_ENTITY_NAME' => $subEntityName
201 );
202
203 try
204 {
205 $res = static::add($newEntity);
206 return $res->isSuccess() ? $res->getId() : false;
207 }
208 catch (SqlQueryException $e)
209 {
210 if (mb_strpos($e->getMessage(), 'Duplicate entry') !== false)
211 {
212 //Try to get one more time
213 $res = static::getList($getListParameters);
214 $element = $res->fetch();
215 if ($element)
216 {
217 return $element['ID'];
218 }
219 }
220 throw $e;
221 }
222 }
223
229 public static function validateEntityType()
230 {
231 return array(new Main\Entity\Validator\Length(null, 1),);
232 }
233
239 public static function validateEntityCode()
240 {
241 return array(new Main\Entity\Validator\Length(null, 255),);
242 }
243
249 public static function validateSubEntityType()
250 {
251 return array(new Main\Entity\Validator\Length(null, 1),);
252 }
253
259 public static function validateSubEntityName()
260 {
261 return array(new Main\Entity\Validator\Length(null, 255),);
262 }
263}
static getByClientId($clientId)
Definition app.php:929
static getEntityInfo($entityType, $entityId)