Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pushtable.php
1<?php
2namespace Bitrix\Pull\Model;
3
5use Bitrix\Main\Entity\Event;
6use Bitrix\Main\Entity\FieldError;
7use Bitrix\Main\Entity\Result;
10
11
46class PushTable extends Main\Entity\DataManager
47{
53 public static function getTableName()
54 {
55 return 'b_pull_push';
56 }
57
63 public static function getMap()
64 {
65 return array(
66 'ID' => array(
67 'data_type' => 'integer',
68 'primary' => true,
69 'autocomplete' => true,
70 //'title' => Loc::getMessage('PUSH_ENTITY_ID_FIELD'),
71 ),
72 'USER_ID' => array(
73 'data_type' => 'integer',
74 'required' => true,
75 //'title' => Loc::getMessage('PUSH_ENTITY_USER_ID_FIELD'),
76 ),
77 'DEVICE_TYPE' => array(
78 'data_type' => 'string',
79 'validation' => array(__CLASS__, 'validateDeviceType'),
80 //'title' => Loc::getMessage('PUSH_ENTITY_DEVICE_TYPE_FIELD'),
81 ),
82 'APP_ID' => array(
83 'data_type' => 'string',
84 'validation' => array(__CLASS__, 'validateAppId'),
85 //'title' => Loc::getMessage('PUSH_ENTITY_APP_ID_FIELD'),
86 ),
87 'UNIQUE_HASH' => array(
88 'data_type' => 'string',
89 'validation' => array(__CLASS__, 'validateUniqueHash'),
90 //'title' => Loc::getMessage('PUSH_ENTITY_UNIQUE_HASH_FIELD'),
91 ),
92 'DEVICE_ID' => array(
93 'data_type' => 'string',
94 'validation' => array(__CLASS__, 'validateDeviceId'),
95 //'title' => Loc::getMessage('PUSH_ENTITY_DEVICE_ID_FIELD'),
96 ),
97 'DEVICE_NAME' => array(
98 'data_type' => 'string',
99 'validation' => array(__CLASS__, 'validateDeviceName'),
100 //'title' => Loc::getMessage('PUSH_ENTITY_DEVICE_NAME_FIELD'),
101 ),
102 'DEVICE_TOKEN' => array(
103 'data_type' => 'string',
104 'required' => false,
105 'validation' => array(__CLASS__, 'validateDeviceToken'),
106 //'title' => Loc::getMessage('PUSH_ENTITY_DEVICE_TOKEN_FIELD'),
107 ),
108 'VOIP_TYPE' => array(
109 'data_type' => 'string',
110 ),
111 'VOIP_TOKEN' => array(
112 'data_type' => 'string',
113 ),
114 'DATE_CREATE' => array(
115 'data_type' => 'datetime',
116 'required' => true,
117 'default_value' => new \Bitrix\Main\Type\DateTime,
118 //'title' => Loc::getMessage('PUSH_ENTITY_DATE_CREATE_FIELD'),
119 ),
120 'DATE_AUTH' => array(
121 'data_type' => 'datetime',
122 'default_value' => new \Bitrix\Main\Type\DateTime,
123 //'title' => Loc::getMessage('PUSH_ENTITY_DATE_AUTH_FIELD'),
124 ),
125 'USER' => array(
126 'data_type' => 'Bitrix\Main\User',
127 'reference' => array('=this.USER_ID' => 'ref.ID'),
128 ),
129 );
130 }
136 public static function validateDeviceType()
137 {
138 return array(
139 new Main\Entity\Validator\Length(null, 50),
140 );
141 }
147 public static function validateAppId()
148 {
149 return array(
150 new Main\Entity\Validator\Length(null, 50),
151 );
152 }
158 public static function validateUniqueHash()
159 {
160 return array(
161 new Main\Entity\Validator\Length(null, 50),
162 );
163 }
164
172 public static function checkFields(Result $result, $primary, array $data)
173 {
174 parent::checkFields($result, $primary, $data);
175 $pushManager = new \CPushManager();
176 $availableDataTypes = array_keys($pushManager->getServices());
177
178 if ($result instanceof Entity\AddResult)
179 {
180 $entity = self::getEntity();
181 $tokensReceived = !empty($data["DEVICE_TOKEN"]) || !empty($data["VOIP_TOKEN"]);
182 $checkToken = function($token) {
183 return $token == null || preg_match('~^[a-f0-9]{64}$~i', $token);
184 };
185
186 if (!$data["DEVICE_TYPE"] || !in_array($data["DEVICE_TYPE"], $availableDataTypes))
187 {
188 $result->addError(new Entity\FieldError($entity->getField("DEVICE_TYPE"), "Wrong field value", FieldError::INVALID_VALUE));
189 }
190 if(!$tokensReceived)
191 {
192 $result->addError(new Entity\FieldError($entity->getField("DEVICE_TYPE"), "Tokens were not received", FieldError::INVALID_VALUE));
193 }
194
195 if ($data["DEVICE_TYPE"] == "APPLE")
196 {
197 if (!$checkToken($data["DEVICE_TOKEN"]) || !$checkToken($data["DEVICE_TOKEN_VOIP"]))
198 $result->addError(new Entity\FieldError($entity->getField("DEVICE_TYPE"), "Wrong format of token for iOS", FieldError::INVALID_VALUE));
199 }
200 }
201 }
202
203
204 public static function onBeforeAdd(Event $event)
205 {
206 $result = new Entity\EventResult;
207 $data = $event->getParameter("fields");
208
209 if(!isset($data["APP_ID"]))
210 {
211 $data["APP_ID"] = defined("MOBILEAPP_DEFAULT_APP_ID") ? MOBILEAPP_DEFAULT_APP_ID : "unknown";
212 }
213
214 if(!isset($data["DEVICE_NAME"]))
215 {
216 $data["DEVICE_NAME"] = $data["DEVICE_ID"];
217 }
218
219 $data["UNIQUE_HASH"] = \CPullPush::getUniqueHash($data["USER_ID"], $data["APP_ID"]);
220 $data["DATE_AUTH"] = new Main\Type\DateTime();
221 $result->modifyFields($data);
222
223 return $result;
224 }
225
226 public static function onAfterAdd(Event $event)
227 {
228 parent::onAfterAdd($event);
229 \CAgent::AddAgent("CPullPush::cleanTokens();", "pull", "N", 43200, "", "Y", ConvertTimeStamp(time() + \CTimeZone::GetOffset() + 30, "FULL"));
230 }
231
232
233 public static function onBeforeUpdate(Event $event)
234 {
235 parent::onBeforeUpdate($event);
236
237 $result = new Entity\EventResult;
238 $data = $event->getParameter("fields");
239 $data["UNIQUE_HASH"] = \CPullPush::getUniqueHash($data["USER_ID"], $data["APP_ID"]);
240 $data["DATE_AUTH"] = new Main\Type\DateTime();
241 $result->modifyFields($data);
242
243 return $result;
244 }
245
246
252 public static function validateDeviceId()
253 {
254 return array(
255 new Main\Entity\Validator\Length(null, 255),
256 );
257 }
263 public static function validateDeviceName()
264 {
265 return array(
266 new Main\Entity\Validator\Length(null, 50),
267 );
268 }
274 public static function validateDeviceToken()
275 {
276 return array(
277 new Main\Entity\Validator\Length(null, 255),
278 );
279 }
280}
281
282class_alias("Bitrix\\Pull\\Model\\PushTable", "Bitrix\\Pull\\PushTable", false);
getParameter($key)
Definition event.php:80
addError(Error $error)
Definition result.php:50
static onAfterAdd(Event $event)
static onBeforeAdd(Event $event)
static checkFields(Result $result, $primary, array $data)
static onBeforeUpdate(Event $event)