Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
crmcontacts.php
1<?php
2
4
5use \Bitrix\Crm\Requisite\EntityLink;
6use \Bitrix\Landing\Block;
7use \Bitrix\Main\Loader;
8use \Bitrix\SalesCenter\Integration\CrmManager;
9use \Bitrix\Landing;
10
12{
13 protected const DEFAULT_COMPANY = 'Company24';
14 protected const DEFAULT_PHONES = ['+123456789'];
15 protected const DEFAULT_EMAILS = ['info@company24.com'];
16 protected const DEFAULT_CONTACTS = [
17 self::COMPANY_KEY => self::DEFAULT_COMPANY,
18 self::PHONES_KEY => self::DEFAULT_PHONES,
19 self::EMAILS_KEY => self::DEFAULT_EMAILS,
20 ];
21
22 protected const COMPANY_KEY = 'company';
23 protected const PHONES_KEY = 'phones';
24 protected const EMAILS_KEY = 'emails';
25
26 public const STATUS_CRM_OK = 10;
27 public const STATUS_CRM_DEFAULT = 11;
28 public const STATUS_CRM_NO_SALESCENTER = 12;
29 public const STATUS_CONNECTOR_OK = 20;
30 public const STATUS_CONNECTOR_OLD_CRM = 21;
31 public const STATUS_CONNECTOR_DEFAULT = 22;
32 public const STATUS_SMN_DEFAULT = 30;
33 protected static $status;
34
41 public static function processing(Block $block): void
42 {
43 $content = $block->getContent();
44 if (!$content)
45 {
46 return;
47 }
48
49 // get requisites from my companies
50 $contacts = self::getContacts();
51 $company = $contacts[self::COMPANY_KEY];
52 $phones = $contacts[self::PHONES_KEY];
53 $emails = $contacts[self::EMAILS_KEY];
54
55 // todo: just one contact to replace
56 // if phones or email found, replace markers
57 $replaced = 0;
58 $content = preg_replace_callback(
59 '/#(PHONE|EMAIL)([\d]+)#/',
60 static function ($matches) use ($phones, $emails)
61 {
62 $key = $matches[2];
63 $sources = ($matches[1] === 'PHONE') ? $phones : $emails;
64
65 return $sources[$key] ?? $sources[0];
66 },
67 $content,
68 -1,
69 $replaced
70 );
71 if (mb_strpos($content, '#COMPANY#') !== false)
72 {
73 $content = str_replace('#COMPANY#', $company, $content);
74 $replaced++;
75 }
76
77 if ($replaced)
78 {
79 $block->saveContent($content);
80 $block->save();
81 }
82 }
83
92 public static function getContacts(): array
93 {
95 {
96 if (
97 !Loader::includeModule('crm')
98 || !Loader::includeModule('salescenter')
99 )
100 {
101 self::$status = self::STATUS_CRM_NO_SALESCENTER;
103 }
104
105 if (EntityLink::getDefaultMyCompanyId() === 0)
106 {
107 self::$status = self::STATUS_CRM_DEFAULT;
109 }
110
111 $contacts[self::COMPANY_KEY] = CrmManager::getPublishedCompanyName() ?: self::DEFAULT_COMPANY;
112
113 // get just first phone or email
114 $phones = CrmManager::getPublishedCompanyPhone();
115 $contacts[self::PHONES_KEY] = empty($phones) || $phones['ID'] == 0
117 : [$phones['VALUE']];
118
119 $emails = CrmManager::getPublishedCompanyEmail();
120 $contacts[self::EMAILS_KEY] = empty($emails)
122 : [$emails['VALUE']];
123
124 self::$status = self::STATUS_CRM_OK;
125 return $contacts;
126 }
127
129 {
130 // todo: now not work in BUS, try later
131 // $client = ApClient::init();
132 // if ($client)
133 // {
134 // $resContacts = $client->call('salescenter.myMethod');
135 // if (empty($resContacts['error']))
136 // {
137 // // todo: check empty contacts here?
138 // if (isset($resContacts['result']) && is_array($resContacts['result']))
139 // {
140 // self::$status = self::STATUS_CONNECTOR_OK;
141 // return $resContacts['result'];
142 // }
143 // }
144 // elseif ($resContacts['error'] === 'ERROR_METHOD_NOT_FOUND')
145 // {
146 // self::$status = self::STATUS_CONNECTOR_OLD_CRM;
147 // return self::DEFAULT_CONTACTS;
148 // }
149 // }
150 // self::$status = self::STATUS_CONNECTOR_DEFAULT;
151 // return self::DEFAULT_CONTACTS;
152 }
153
154 // default
155 self::$status = self::STATUS_SMN_DEFAULT;
157 }
158
163 public static function getStatus()
164 {
165 return self::$status;
166 }
167}
save(array $additionalFields=[])
Definition block.php:3136
saveContent(string $content, $designed=false)
Definition block.php:3108