Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
form.php
1<?php
2
3namespace Bitrix\Seo\LeadAds;
4
11
12abstract class Form extends BaseApiObject
13{
14 public const URL_FORM_LIST = '';
15 public const USE_GROUP_AUTH = false;
16 public const FIELD_MAP = [];
17
19 protected static $listRowMap = array(
20 'ID' => 'ID',
21 'NAME' => 'NAME',
22 );
23
25 protected $accountId;
26
28 protected $formId;
29
35 public function __construct(string $accountId = null)
36 {
37 $this->accountId = $accountId;
38 parent::__construct();
39 }
40
48 public function setAccountId(string $accountId): Form
49 {
50 $this->accountId = $accountId;
51
52 return $this;
53 }
54
58 public function getAccountId(): ?string
59 {
60 return $this->accountId;
61 }
62
66 protected static function getFieldMapper(): Mapper
67 {
68 static $mapper;
69 return $mapper = $mapper ?? new LeadAds\Mapper(static::FIELD_MAP);
70 }
71
72
78 abstract public function getForm($formId): LeadAds\Response\FormResponse;
79
85 abstract public function getList(): LeadAds\Response\FormResponse;
86
93 abstract public function getResult(WebHook\Payload\LeadItem $item): Result;
94
101 abstract public function add(array $data): Response;
102
108 abstract public function register($formId): Retargeting\Response;
109
117 abstract public function unlink(string $id): bool;
118
119
128 public function getRegisteredGroups(): array
129 {
130 $rows = Internals\CallbackSubscriptionTable::getList([
131 'select' => ['GROUP_ID'],
132 'filter' => [
133 '=TYPE' => static::TYPE_CODE,
134 ]
135 ])->fetchAll();
136
137 return array_column($rows, 'GROUP_ID');
138 }
139
150 public function unRegisterGroup(string $groupId) : bool
151 {
152 $row = Internals\CallbackSubscriptionTable::getRow([
153 'filter' => [
154 '=TYPE' => static::TYPE_CODE,
155 '=GROUP_ID' => $groupId
156 ]
157 ]);
158
159 return $row && Internals\CallbackSubscriptionTable::delete($row['ID'])->isSuccess();
160 }
161
170 public function registerGroup(string $groupId): bool
171 {
172 $hasGroup = false;
173
174 $list = Internals\CallbackSubscriptionTable::getList([
175 'filter' => [
176 '=TYPE' => static::TYPE_CODE
177 ]
178 ]);
179 foreach ($list as $row)
180 {
181 if ($row['GROUP_ID'] === $groupId)
182 {
183 $hasGroup = true;
184 continue;
185 }
186
187 Internals\CallbackSubscriptionTable::delete($row['ID']);
188 }
189 if ($hasGroup)
190 {
191 return true;
192 }
193
194 $callbackSubscriptionResult = Internals\CallbackSubscriptionTable::add([
195 'TYPE' => static::TYPE_CODE,
196 'GROUP_ID' => $groupId
197 ]);
198
199 return $callbackSubscriptionResult->isSuccess();
200 }
201
208 public static function convertField(Field $field): array
209 {
210 foreach ($item = $field->toArray() as $key => $value)
211 {
212 if (empty($value))
213 {
214 unset($item[$key]);
215 }
216 }
217
218 return $item;
219 }
220
227 public static function convertFields(array $fields): array
228 {
229 return array_map(
230 static function($field)
231 {
232 return static::convertField($field);
233 },
234 $fields
235 );
236 }
237
243 public static function getUrlFormList(): string
244 {
245 return static::URL_FORM_LIST;
246 }
247
248 public function loadLeads($formId): \Bitrix\Main\Result
249 {
250 return new \Bitrix\Main\Result();
251 }
252
258 public static function isSupportAccount(): bool
259 {
260 return true;
261 }
262
268 public static function getPrivacyPolicyUrl(): string
269 {
270 switch ($langId = Context::getCurrent()->getLanguage())
271 {
272 case 'ua':
273 case 'ru':
274 case 'kz':
275 case 'by':
276 return "https://www.bitrix24.{$langId}/about/privacy.php";
277 case 'de':
278 return "https://www.bitrix24.{$langId}/privacy/";
279
280 default:
281 return 'https://www.bitrix24.com/privacy/';
282 }
283 }
284
285 protected function registerFormWebHook($adsFormId, array $parameters = []): bool
286 {
287 return WebHook\Service::create(
288 Service::getEngineCode(static::TYPE_CODE),
289 $adsFormId
290 )->register($parameters);
291 }
292
293 protected function registerForm($adsFormId)
294 {
295 return WebHook\Service::registerForm($adsFormId);
296 }
297
298 public function unregisterForms()
299 {
300
301 }
302
303 public function unregisterForm($adsFormId): bool
304 {
305 return WebHook\Service::unregisterForm($adsFormId);
306 }
307
308 protected function removeFormWebHook($adsFormId): bool
309 {
310 return WebHook\Service::create(
311 Service::getEngineCode(static::TYPE_CODE),
312 $adsFormId
313 )->remove();
314 }
315
321 public static function isGroupAuthUsed(): bool
322 {
323 return static::USE_GROUP_AUTH;
324 }
325
326 protected function getAuthParameters(): array
327 {
328 return [];
329 }
330
336 public function getGroupAuthAdapter(): ?Retargeting\AuthAdapter
337 {
338 if (!self::isGroupAuthUsed())
339 {
340 return null;
341 }
342
343 $adapter = Retargeting\AuthAdapter::create(static::TYPE_CODE . '.groups', $this->service);
344 $adapter = $adapter->setParameters($this->getAuthParameters());
345 $row = Internals\CallbackSubscriptionTable::getRow([
346 'filter' => [
347 '=TYPE' => static::TYPE_CODE,
348 ]
349 ]);
350
351 if ($row && $row['HAS_AUTH'] !== 'Y' && $adapter->hasAuth())
352 {
353 Internals\CallbackSubscriptionTable::update($row['ID'], ['HAS_AUTH' => 'Y']);
354 }
355
356 return $adapter;
357 }
358}
static getCurrent()
Definition context.php:241
registerForm($adsFormId)
Definition form.php:293
static convertField(Field $field)
Definition form.php:208
static getUrlFormList()
Definition form.php:243
static isGroupAuthUsed()
Definition form.php:321
registerGroup(string $groupId)
Definition form.php:170
unRegisterGroup(string $groupId)
Definition form.php:150
registerFormWebHook($adsFormId, array $parameters=[])
Definition form.php:285
unregisterForm($adsFormId)
Definition form.php:303
static getPrivacyPolicyUrl()
Definition form.php:268
static isSupportAccount()
Definition form.php:258
static convertFields(array $fields)
Definition form.php:227
__construct(string $accountId=null)
Definition form.php:35
setAccountId(string $accountId)
Definition form.php:48
removeFormWebHook($adsFormId)
Definition form.php:308
getResult(WebHook\Payload\LeadItem $item)
static getFieldMapper()
Definition form.php:66
static getEngineCode($type)
Definition service.php:129