Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
audience.php
1<?
2
4
8
9abstract class Audience extends BaseApiObject
10{
11 const TYPE_FACEBOOK = 'facebook';
12 const TYPE_VKONTAKTE = 'vkontakte';
13 const TYPE_MYCOM = 'mycom';
14 const TYPE_YANDEX = 'yandex';
15 const TYPE_GOOGLE = 'google';
16
17 const ENUM_CONTACT_TYPE_EMAIL = 'email'; // email
18 const ENUM_CONTACT_TYPE_PHONE = 'phone'; // phone
19 const ENUM_CONTACT_TYPE_IDFA_GAID = 'idfa_gaid'; // IDFA (Identifier For Advertising) or device ID (Android ID and UDID on iOS)
20 const ENUM_CONTACT_TYPE_INTERNAL_ID = 'int'; // internal social net id like Vk ID or Fb ID
21
25
26 protected $accountId;
27 protected $audienceId;
28 protected static $listRowMap = [
29 'ID' => 'ID',
30 'NAME' => 'NAME',
31 'COUNT_VALID' => 'COUNT',
32 'COUNT_MATCHED' => 'COUNT',
33 'SUPPORTED_CONTACT_TYPES' => [
34 self::ENUM_CONTACT_TYPE_EMAIL,
35 self::ENUM_CONTACT_TYPE_PHONE,
36 self::ENUM_CONTACT_TYPE_IDFA_GAID,
37 self::ENUM_CONTACT_TYPE_INTERNAL_ID
38 ],
39 ];
40 protected $isQueueModeEnabled = false;
41 protected $isQueueAutoRemove = true;
42 protected $queueDaysAutoRemove = 7;
43 protected $emptyResponse = null;
44
45 public function __construct($accountId = null)
46 {
47 $this->accountId = $accountId;
48 parent::__construct();
49 }
50
51 public function setAccountId($accountId)
52 {
53 return $this->accountId = $accountId;
54 }
55
56 public static function normalizeEmail($email)
57 {
58 return trim(mb_strtolower($email));
59 }
60
61 public static function normalizePhone($phone)
62 {
63 return preg_replace("/[^\+0-9]/", '', $phone);
64 }
65
66 public static function isSupportMultiTypeContacts()
67 {
68 return true;
69 }
70
71 public static function isSupportAccount()
72 {
73 return true;
74 }
75
76 public static function isSupportAddAudience()
77 {
78 return false;
79 }
80
81 public static function isAddingRequireContacts()
82 {
83 return false;
84 }
85
86 public static function isSupportRemoveContacts()
87 {
88 return true;
89 }
90
91 public static function isSupportCreateLookalikeFromSegments(): bool
92 {
93 return true;
94 }
95
97 {
98 return false;
99 }
100
101 public static function getUrlAudienceList()
102 {
103 return static::URL_AUDIENCE_LIST;
104 }
105
106 public static function getMaxContactsPerPacket()
107 {
108 return static::MAX_CONTACTS_PER_PACKET;
109 }
110
111 public static function getMinContactsForActivating()
112 {
113 return static::MIN_CONTACTS_FOR_ACTIVATING;
114 }
115
116 public function disableQueueAutoRemove()
117 {
118 $this->isQueueAutoRemove = false;
119 }
120
121 public function enableQueueAutoRemove($daysNumber = null)
122 {
123 $this->isQueueAutoRemove = true;
124 if ($daysNumber)
125 {
126 $this->queueDaysAutoRemove = $daysNumber;
127 }
128 }
129
130 public function disableQueueMode()
131 {
132 $this->isQueueModeEnabled = false;
133 }
134
135 public function enableQueueMode()
136 {
137 $this->isQueueModeEnabled = true;
138 }
139
140 public function isQueueModeEnabled()
141 {
142 return $this->isQueueModeEnabled;
143 }
144
145 public function getById($itemId)
146 {
147 $itemsResult = $this->getList();
148 while($itemData = $itemsResult->fetch())
149 {
150 $itemData = $this->normalizeListRow($itemData);
151 if ($itemData['ID'] == $itemId)
152 {
153 return $itemData;
154 }
155 }
156
157 return null;
158 }
159
160 protected function normalizeContacts(array $contacts = array())
161 {
162 $data = array();
163 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
164 {
165 if (!isset($contacts[$contactType]))
166 {
167 continue;
168 }
169
170 $contactsCount = count($contacts[$contactType]);
171 for ($i = 0; $i < $contactsCount; $i++)
172 {
173 if (empty($contacts[$contactType][$i]))
174 {
175 continue;
176 }
177
178 $contactPhone = null;
179 $contact = $contacts[$contactType][$i];
180 switch ($contactType)
181 {
182 case self::ENUM_CONTACT_TYPE_EMAIL:
183 $contact = static::normalizeEmail($contact);
184 break;
185
186 case self::ENUM_CONTACT_TYPE_PHONE:
187 $contact = static::normalizePhone($contact);
188 if (mb_substr($contact, 0, 1) == '8' && mb_strlen($contact) > 8)
189 {
190 $contactPhone = '+7'.mb_substr($contact, 1);
191 }
192 break;
193 }
194
195 if ($contact)
196 {
197 $data[$contactType][] = $contact;
198 }
199
200 if ($contactPhone)
201 {
202 $data[$contactType][] = $contactPhone;
203 }
204 }
205 }
206
207 return $data;
208 }
209
210 protected function addToQueue($audienceId, $contacts, $options = [], $isRemove = false)
211 {
212 $dateAutoRemove = null;
213 if ($this->isQueueAutoRemove && $this->queueDaysAutoRemove > 0)
214 {
215 $dateAutoRemove = new DateTime();
216 $dateAutoRemove->add($this->queueDaysAutoRemove . ' DAY');
217 }
218
219 if ($isRemove)
220 {
222 }
223 else if ($this->isQueueAutoRemove)
224 {
226 }
227 else
228 {
230 }
231
232
233 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
234 {
235 if (!isset($contacts[$contactType]))
236 {
237 continue;
238 }
239
240 $contactsCount = count($contacts[$contactType]);
241 for ($i = 0; $i < $contactsCount; $i++)
242 {
243 $contact = $contacts[$contactType][$i];
244 $resultDb = QueueTable::add(array(
245 'TYPE' => static::TYPE_CODE,
246 'ACCOUNT_ID' => $this->accountId,
247 'CLIENT_ID' => $this->service instanceof IMultiClientService ? $this->service->getClientId() : null,
248 'AUDIENCE_ID' => $audienceId,
249 'PARENT_ID' => $options['parentId'] ?: null,
250 'CONTACT_TYPE' => $contactType,
251 'VALUE' => $contact,
252 'ACTION' => $action,
253 'DATE_AUTO_REMOVE' => $dateAutoRemove,
254 ));
255 $resultDb->isSuccess();
256 }
257 }
258
259 return true;
260 }
261
262 protected function deleteFromQueue($audienceId, $contacts)
263 {
264 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
265 {
266 if (!isset($contacts[$contactType]))
267 {
268 continue;
269 }
270
271 $contactsCount = count($contacts[$contactType]);
272 for ($i = 0; $i < $contactsCount; $i++)
273 {
274 $contact = $contacts[$contactType][$i];
275 $itemDb = QueueTable::getList(array(
276 'select' => array('ID'),
277 'filter' => array(
278 'TYPE' => static::TYPE_CODE,
279 'ACCOUNT_ID' => $this->accountId,
280 'AUDIENCE_ID' => $audienceId,
281 'CONTACT_TYPE' => $contactType,
282 'VALUE' => $contact,
283 )
284 ));
285 while ($item = $itemDb->fetch())
286 {
287 $result = QueueTable::delete($item['ID']);
288 $result->isSuccess();
289 }
290 }
291 }
292
293 return true;
294 }
295
302 public function addContacts($audienceId, array $contacts, array $options)
303 {
304 $contacts = $this->normalizeContacts($contacts);
305 if ($this->isQueueModeEnabled())
306 {
307 $this->addToQueue($audienceId, $contacts, $options, false);
308 if ($this->emptyResponse === null)
309 {
310 $this->emptyResponse = Response::create(static::TYPE_CODE);
311 $this->emptyResponse->setData(array());
312 }
313
314 return $this->emptyResponse;
315 }
316 else
317 {
318 return $this->importContacts($audienceId, $contacts, $options);
319 }
320 }
321
328 public function deleteContacts($audienceId, array $contacts, array $options)
329 {
330 if ($this->isQueueModeEnabled())
331 {
332 $this->addToQueue($audienceId, $contacts, $options, true);
333 $response = Response::create(static::TYPE_CODE);
334 $response->setData(array());
335 return $response;
336 }
337 else
338 {
339 return $this->removeContacts($audienceId, $contacts, $options);
340 }
341 }
342
349 abstract public function add(array $data);
350
356 abstract public function getList();
357
358
367 abstract protected function importContacts($audienceId, array $contacts, array $options);
368
369
378 abstract protected function removeContacts($audienceId, array $contacts, array $options);
379
380 public function createLookalike($sourceAudienceId, array $options)
381 {
382 throw new NotImplementedException('Method '.static::class.'::`addLookalike()` not implemented.');
383 }
384
385 public function isQueueProcessed($parentId)
386 {
387 return !QueueTable::getCount(['=PARENT_ID' => $parentId]);
388 }
389}
deleteContacts($audienceId, array $contacts, array $options)
Definition audience.php:328
removeContacts($audienceId, array $contacts, array $options)
deleteFromQueue($audienceId, $contacts)
Definition audience.php:262
addToQueue($audienceId, $contacts, $options=[], $isRemove=false)
Definition audience.php:210
__construct($accountId=null)
Definition audience.php:45
enableQueueAutoRemove($daysNumber=null)
Definition audience.php:121
importContacts($audienceId, array $contacts, array $options)
normalizeContacts(array $contacts=array())
Definition audience.php:160
addContacts($audienceId, array $contacts, array $options)
Definition audience.php:302
static isSupportCreateLookalikeFromSegments()
Definition audience.php:91
createLookalike($sourceAudienceId, array $options)
Definition audience.php:380