Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
crm.php
1<?php
3
4use Bitrix\Crm\CompanyTable;
5use Bitrix\Crm\Requisite\EntityLink;
6use Bitrix\Crm\Settings\CompanySettings;
10use Bitrix\SalesCenter\Integration\CrmManager;
11
12class Crm
13{
14 protected const CACHE_TAG = 'landing_crm_contacts';
15 protected const OPTION_DATA_CODE = 'shop_master_data';
16
17 protected const ID_KEY = 'ID';
18 protected const COMPANY_KEY = 'COMPANY';
19 protected const PHONE_KEY = 'PHONE';
20 protected const EMAIL_KEY = 'EMAIL';
21
22 protected const DEFAULT_COMPANY = 'Company24';
23 protected const DEFAULT_PHONE = '+123456789';
24 protected const DEFAULT_EMAIL = 'info@company24.com';
25
26 protected const DEFAULT_CONTACTS = [
27 self::COMPANY_KEY => self::DEFAULT_COMPANY,
28 self::PHONE_KEY => self::DEFAULT_PHONE,
29 self::EMAIL_KEY => self::DEFAULT_EMAIL,
30 ];
31
38 protected static function addNewCompany(string $title, string $phone): void
39 {
40 if (!Loader::includeModule('crm'))
41 {
42 return;
43 }
44
45 $company = new \CCrmCompany(false);
46 $fields = [
47 'TITLE' => $title,
48 'OPENED' => CompanySettings::getCurrent()->getOpenedFlag() ? 'Y' : 'N',
49 'IS_MY_COMPANY' => 'Y',
50 'FM' => [
51 'PHONE' => [
52 'n0' => [
53 'VALUE' => $phone,
54 'VALUE_TYPE' => 'WORK'
55 ]
56 ]
57 ]
58 ];
59 $company->add($fields);
60 }
61
68 public static function setContacts(int $siteId, array $data): void
69 {
70 $shopStoredData = unserialize(Manager::getOption(self::OPTION_DATA_CODE, ''), ['allowed_classes' => false]);
71 $shopStoredData = is_array($shopStoredData) ? $shopStoredData : [];
72 if (isset($shopStoredData[$siteId]))
73 {
74 unset($shopStoredData[$siteId]);
75 }
76 $shopStoredData[$siteId] = [];
77
78 $contacts = self::getContactsRaw();
79 if (!$contacts)
80 {
82 $data[self::COMPANY_KEY],
83 $data[self::PHONE_KEY]
84 );
85 }
86
87 foreach (self::DEFAULT_CONTACTS as $key => $value)
88 {
89 if (!($data[$key] ?? ''))
90 {
91 continue;
92 }
93 if ($data[$key] !== ($contacts[$key] ?? ''))
94 {
95 $shopStoredData[$siteId][$key] = $data[$key];
96 }
97 }
98
99 Manager::setOption(self::OPTION_DATA_CODE, serialize($shopStoredData));
101 }
102
107 public static function getContactsRaw(): array
108 {
109 static $contacts = null;
110
111 if ($contacts === null)
112 {
113 $contacts = [];
114 }
115 else
116 {
117 return $contacts;
118 }
119
120 if (
121 !Loader::includeModule('crm')
122 || !Loader::includeModule('salescenter')
123 )
124 {
125 return $contacts;
126 }
127
128 if (!Manager::isB24())
129 {
130 return $contacts;
131 }
132
133 $defaultCompanyId = EntityLink::getDefaultMyCompanyId();
134
135 if (!$contacts && $defaultCompanyId === 0)
136 {
137 return $contacts;
138 }
139
140 $contacts[self::ID_KEY] = $defaultCompanyId;
141 $contacts[self::COMPANY_KEY] = CrmManager::getPublishedCompanyName() ?: self::DEFAULT_COMPANY;
142
143 // get just first phone or email
144
145 $phones = CrmManager::getPublishedCompanyPhone();
146 $contacts[self::PHONE_KEY] = empty($phones) || $phones['ID'] == 0
148 : $phones['VALUE'];
149
150 $emails = CrmManager::getPublishedCompanyEmail();
151 $contacts[self::EMAIL_KEY] = empty($emails)
153 : $emails['VALUE'];
154
155 return $contacts;
156 }
157
163 public static function getContacts(int $siteId): array
164 {
165 $cache = new \CPHPCache;
166 $cacheManager = $GLOBALS['CACHE_MANAGER'];
167 $cacheTag = self::CACHE_TAG;
168 $cacheDir = '/landing/crm_contacts';
169 if ($cache->initCache(8640000, $cacheTag . $siteId, $cacheDir))
170 {
171 return $cache->getVars();
172 }
173
174 $cache->startDataCache();
175 $cacheManager->startTagCache($cacheDir);
176 $cacheManager->registerTag($cacheTag);
177
178 $contacts = self::getContactsRaw();
179 if (!$contacts)
180 {
181 $contacts = self::DEFAULT_CONTACTS;
182 }
183
184 $shopData = unserialize(Manager::getOption(self::OPTION_DATA_CODE, ''), ['allowed_classes' => false]);
185 if (isset($shopData[$siteId]))
186 {
187 $contacts = array_merge(
188 $contacts,
189 $shopData[$siteId]
190 );
191 }
192
193 foreach ($contacts as &$value)
194 {
195 if (is_array($value))
196 {
197 $value = array_shift($value);
198 }
199 }
200 unset($value);
201
202 $cacheManager->endTagCache();
203 $cache->endDataCache($contacts);
204
205 return $contacts;
206 }
207
212 public static function clearContactsCache(): void
213 {
214 if (defined('BX_COMP_MANAGED_CACHE'))
215 {
216 Manager::getCacheManager()->clearByTag(self::CACHE_TAG);
217 }
218 }
219
225 public static function onAfterCompanyChange(array $fields): void
226 {
227 $companyId = $fields['ID'] ?? null;
228 if ($companyId === EntityLink::getDefaultMyCompanyId())
229 {
231 }
232 }
233
241 public static function getReplacesForContent(int $siteId, bool $attributesReplace = true): array
242 {
243 $replace = [];
244
245 $crmContacts = self::getContacts($siteId);
246 $replace['#crmCompanyTitle'] = \htmlspecialcharsbx($crmContacts['COMPANY']);
247
248 if (!empty($crmContacts['PHONE']))
249 {
250 $phone = $crmContacts['PHONE'];
251 $phone = \htmlspecialcharsbx($phone);
252 $replace['#crmPhoneTitle1'] = $phone;// a-tag inside
253 if ($attributesReplace)
254 {
255 $replace['#crmPhone1'] = $phone;// a-href inside
256 }
257 }
258
259 if (!empty($crmContacts['EMAIL']))
260 {
261 $email = $crmContacts['EMAIL'];
262 $email = \htmlspecialcharsbx($email);
263 $replace['#crmEmailTitle1'] = $email;// a-tag inside
264 if ($attributesReplace)
265 {
266 $replace['#crmEmail1'] = $email;// a-href inside
267 }
268 }
269
270 return $replace;
271 }
272
279 public static function getReplaceRequisiteCompanyNameForContent(string $xmlId): array
280 {
281 $companyId = (int)(explode("_", $xmlId)[1] ?? 0);
282 $requisites = self::getMyRequisites();
283 if (isset($requisites[$companyId]['companyTitle']) && $companyId > 0)
284 {
285 $companyTitle = $requisites[$companyId]['companyTitle'];
286 }
287 if (!isset($companyTitle))
288 {
289 $companyTitle = 'Company24';
290 }
291 $replace['#requisiteCompanyTitle'] = $companyTitle;
292
293 return $replace;
294 }
295
300 public static function getMyRequisites(): array
301 {
302 if (!Loader::includeModule('crm'))
303 {
304 return [];
305 }
306
307 $fullData = [];
308
309 $companies = CompanyTable::query()
310 ->setSelect(['ID', 'TITLE'])
311 ->where('IS_MY_COMPANY', 'Y')
312 ->setOrder(['DATE_MODIFY' => 'desc'])
313 ->fetchAll()
314 ;
315 foreach ($companies as $company)
316 {
317 $requisites = [];
318 $bankRequisites = [];
319 $requisitesRaw = \CCrmEntitySelectorHelper::PrepareRequisiteData(
320 \CCrmOwnerType::Company,
321 $company['ID'],
322 [
323 'VIEW_FORMATTED' => true,
324 'SKIP_CHECK_MY_COMPANY_PERMISSION' => true,
325 ]
326 );
327 foreach ($requisitesRaw as $requisite)
328 {
329 $requisiteData = Encoding::convertEncoding(json_decode($requisite['requisiteData'], 1), 'UTF-8', SITE_CHARSET);
330 if (!empty($requisiteData['viewData']['fields']))
331 {
332 $requisites[$requisite['requisiteId']] = [
333 'id' => $requisite['requisiteId'],
334 'title' => $requisiteData['viewData']['title'] ?? null,
335 'data' => $requisiteData['viewData']['fields'],
336 ];
337 }
338 if (!empty($requisiteData['bankDetailViewDataList']))
339 {
340 $bankRequisites[$requisite['requisiteId']] = [
341 'id' => $requisite['requisiteId'],
342 'title' => $requisiteData['viewData']['title'] ?? null,
343 'bankData' => $requisiteData['bankDetailViewDataList'][0]['viewData']['fields'] ?? null,
344 ];
345 }
346 }
347
348 if (empty($requisites))
349 {
350 continue;
351 }
352
353 $fullData[$company['ID']] = [
354 'companyId' => $company['ID'],
355 'companyTitle' => $company['TITLE'],
356 'requisites' => $requisites,
357 'bankRequisites' => $bankRequisites,
358 ];
359 }
360
361 return $fullData;
362 }
363
370 public static function getMyRequisitesPlainList(array $requisites = [], string $requisitesType = 'requisites'): array
371 {
372 $requisitesPlainList = [];
373 foreach ($requisites as $company)
374 {
375 foreach ($company[$requisitesType] as $requisite)
376 {
377 $requisitesPlainList["{$company['companyId']}_{$requisite['id']}"]
378 = "{$company['companyTitle']}: {$requisite['title']} : {$requisite['id']}";
379 }
380 }
381
382 return $requisitesPlainList;
383 }
384
390 public static function getCompanyCommunications(int $companyId): array
391 {
392 if (!Loader::includeModule('crm'))
393 {
394 return [];
395 }
396
397 $data = [];
398
399 $res = \CCrmFieldMulti::getList(
400 ['ID' => 'asc'],
401 [
402 'ENTITY_ID' => \CCrmOwnerType::CompanyName,
403 'ELEMENT_ID' => $companyId,
404 ]
405 );
406 while ($row = $res->Fetch())
407 {
408 $row = array_change_key_case($row);
409 $row['type_id'] = mb_strtolower($row['type_id']);
410
411 if (!isset($data[$row['type_id']]))
412 {
413 $data[$row['type_id']] = [];
414 }
415 $data[$row['type_id']][] = $row['value'];
416 }
417
418 return $data;
419 }
420}
static getReplaceRequisiteCompanyNameForContent(string $xmlId)
Definition crm.php:279
static getReplacesForContent(int $siteId, bool $attributesReplace=true)
Definition crm.php:241
static setContacts(int $siteId, array $data)
Definition crm.php:68
static getMyRequisitesPlainList(array $requisites=[], string $requisitesType='requisites')
Definition crm.php:370
static onAfterCompanyChange(array $fields)
Definition crm.php:225
static getCompanyCommunications(int $companyId)
Definition crm.php:390
static getContacts(int $siteId)
Definition crm.php:163
static addNewCompany(string $title, string $phone)
Definition crm.php:38
static getOption($code, $default=null)
Definition manager.php:160
static setOption($code, $value)
Definition manager.php:171
static getCacheManager()
Definition manager.php:89
$GLOBALS['____1444769544']
Definition license.php:1