29 public const TYPE_CODE = LeadAds\Service::TYPE_VKONTAKTE;
35 public const URL_FORM_LIST =
'https://www.facebook.com/ads/manager/audiences/manage/';
39 [
'CRM_NAME' => LeadAds\Field::TYPE_NAME,
'ADS_NAME' =>
'first_name'],
40 [
'CRM_NAME' => LeadAds\Field::TYPE_LAST_NAME,
'ADS_NAME' =>
'last_name'],
41 [
'CRM_NAME' => LeadAds\Field::TYPE_EMAIL,
'ADS_NAME' =>
'email'],
42 [
'CRM_NAME' => LeadAds\Field::TYPE_PHONE,
'ADS_NAME' =>
'phone'],
43 [
'CRM_NAME' => LeadAds\Field::TYPE_BIRTHDAY,
'ADS_NAME'=>
'birth_date'],
44 [
'CRM_NAME' => LeadAds\Field::TYPE_AGE,
'ADS_NAME'=>
'age'],
45 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_FULL,
'ADS_NAME'=>
'location'],
46 [
'CRM_NAME' => LeadAds\Field::TYPE_PATRONYMIC_NAME,
'ADS_NAME'=>
'patronymic_name'],
47 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_COUNTRY,
'ADS_NAME'=>
'country'],
48 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_CITY,
'ADS_NAME'=>
'city'],
49 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_STREET_ADDRESS,
'ADS_NAME'=>
'address'],
50 [
'CRM_NAME' => LeadAds\Field::TYPE_INPUT,
'ADS_NAME'=>
'question'],
63 $row = LeadAds\Internals\CallbackSubscriptionTable::getRow([
65 '=TYPE' => static::TYPE_CODE,
70 'URL_PARAMETERS' => [
'group_ids' => $row[
'GROUP_ID']]
82 $mapper = static::getFieldMapper();
83 $adsName = $mapper->getAdsName($field->getName());
93 'type' => $field->getType(),
94 'key' => $field->getKey(),
95 'label' => $field->getLabel(),
98 if (!empty($field->getOptions()))
100 $item[
'options'] = array_map(
101 static function ($option)
103 if (is_numeric($key = $option[
'key']))
105 $key = static::$fieldKeyPrefix . $key;
109 'label' => $option[
'label'],
140 'methodName'=>
'leadads.form.get',
143 'group_id'=> (
int) $this->accountId,
160 public function unlink(
string $id) : bool
170 if ($confirmationCodeResponse instanceof
Error)
173 $confirmationCodeResponse
182 'SECURITY_CODE' => $secretKey = Random::getString(32),
183 'CONFIRMATION_CODE' => $confirmationCodeResponse,
189 $response->addError(
new Error(
'Can not register Form web hook.'));
195 if ($callbackServiceResponse instanceof
Error)
197 $response->addError($callbackServiceResponse);
211 $row = LeadAds\Internals\CallbackSubscriptionTable::getRow(
213 'select' => [
'ID',
'CALLBACK_SERVER_ID'],
215 '=TYPE' => static::TYPE_CODE,
216 '=GROUP_ID' => $this->accountId
223 return new Error(
"Group is not registred.");
226 if (!empty($row[
'CALLBACK_SERVER_ID']))
228 return $row[
'CALLBACK_SERVER_ID'];
231 $responseSetCallbackSettings = $this->
getRequest()->send([
232 'methodName' =>
'leadads.callback.server.settings.set',
234 'group_id' => $this->accountId,
235 'lead_forms_new' => 1,
239 if (!$responseSetCallbackSettings->isSuccess() || 1 !== current($responseSetCallbackSettings->getData()))
241 return new Error(
'Can not set Callback server settings.');
249 $row = LeadAds\Internals\CallbackSubscriptionTable::getRow([
251 '=TYPE' => static::TYPE_CODE,
252 '=GROUP_ID' => $groupId
256 if ($row && !empty($row[
'CALLBACK_SERVER_ID']))
259 'methodName' =>
'leadads.callback.server.delete',
261 'group_id' => $groupId,
262 'server_id' => $row[
'CALLBACK_SERVER_ID'],
275 'methodName' =>
'leadads.callback.server.code.get',
277 'group_id' => $this->accountId,
281 if (!$response->isSuccess())
283 return new Error(
'Can not get confirmation code for Callback server.');
286 $responseData = $response->getData();
288 return empty($responseData[
'code'])
289 ?
new Error(
'Can not get confirmation code for Callback server.')
290 : $responseData[
'code']
296 $text = Encoding::convertEncoding(
302 return mb_substr($text, 0, $length);
320 'methodName' =>
'leadads.form.list',
326 $items = array_filter($response->getData(), fn ($item) => ((
int)$item[
'status'] === self::STATUS_FORM_ACTIVE));
327 $result = array_merge($result, $items);
329 if (count($response->getData()) < $limit)
331 $response->setData($result);
335 if ($limit < self::LIMIT_MAX)
340 $offset += count($response->getData());
343 return new FormResponse(
344 new VkontakteFormBuilder($this::getFieldMapper()),
359 $result->setId($item->getLeadId());
360 foreach ($item->getAnswers() as $key => $values)
362 foreach ($values as $index => $value)
364 if (mb_strpos($value, static::$fieldKeyPrefix) !== 0)
369 $values[$index] = mb_substr($value, mb_strlen(static::$fieldKeyPrefix));
372 $result->addFieldValues($key, $values);
389 return parent::unRegisterGroup($groupId);
401 return (
new Retargeting\Services\ResponseVkontakte())
402 ->addError(
new Error(
'VK lead ads form register: Empty formId.'))
405 if (!isset($this->accountId))
407 return (
new Retargeting\Services\ResponseVkontakte())
408 ->addError(
new Error(
'VK lead ads form register: Empty accountId.'))
415 return (
new Retargeting\Services\ResponseVkontakte())
416 ->addError(
new Error(
'VK lead ads form register: Empty formId.'))
425 if (!isset(
$formId) || !isset($this->accountId))
439 if (!$response->isSuccess())
442 ->addError(
new Error(
'Can not load leads'));
445 $leads = $response->getData();
446 if (count($leads) === $limit)
453 'offset' => count($leads),
456 if (!$response->isSuccess())
461 $leads = array_merge($leads, $response->getData());
463 if (count($response->getData()) < $limit)
470 return (
new Result())->setData($leads);
476 'methodName' =>
'leadads.form.leads.load',
478 'form_id' => $formId,
479 'limit' => $params[
'limit'],
480 'offset' => $params[
'offset'],
484 if ($response->isSuccess())