Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
adapter.php
1<?php
10
15
20class Adapter implements iBase
21{
22 public const PATTERN_STATUS_ITEM = 'ENTITY_ID_';
24 protected static $list;
26 protected static $statuses = [];
27 protected ?\Bitrix\Crm\Service\Factory $factory = null;
28
30 protected $message;
31
33 protected $tester;
34
36 protected $configuration;
37
39 protected $transport;
40
42 protected $fields = array();
43
45 protected $siteId = null;
46
48 protected $siteData = null;
49
51 protected $recipientCode;
52
54 protected $recipientId;
55
57 protected $recipientType;
58
60 protected $recipientData;
61
63 protected $readTracker;
64
66 protected $clickTracker;
67
69 protected $unsubTracker;
70
78 public static function getInstance($code)
79 {
80 return isset(self::$list[$code]) ? self::$list[$code] : self::create($code);
81 }
82
90 public static function create($code)
91 {
93 if (!$message)
94 {
95 throw new ArgumentException($code);
96 }
97
98 return new static($message);
99 }
100
106 public function __construct(iBase $message)
107 {
108 $this->message = $message;
109 $this->loadConfiguration();
110
111 $this->readTracker = new Tracker(Tracker::TYPE_READ);
112 $this->clickTracker = new Tracker(Tracker::TYPE_CLICK);
113 $this->unsubTracker = new Tracker(Tracker::TYPE_UNSUB);
114 }
115
121 public function getName()
122 {
123 return $this->message->getName();
124 }
125
131 public function getTransport()
132 {
133 if ($this->transport)
134 {
135 return $this->transport;
136 }
137
138 $transportCode = $this->configuration->get('TRANSPORT_CODE') ?: current($this->message->getSupportedTransports());
139 //$transportConfigId = $this->configuration->get('TRANSPORT_CONFIGURATION_ID');
140 $this->transport = Transport\Adapter::create($transportCode);
141 $this->transport->saveConfiguration($this->getConfiguration());
142 $this->transport->loadConfiguration();
143
144 return $this->transport;
145 }
146
153 public function setTransport(Transport\Adapter $transport)
154 {
155 $this->transport = $transport;
156 }
157
163 public function getCode()
164 {
165 return $this->message->getCode();
166 }
167
173 public function getId()
174 {
175 return $this->configuration->getId();
176 }
177
183 public function getSupportedTransports()
184 {
185 return $this->message->getSupportedTransports();
186 }
187
194 {
195 return $this->getTransport()->getSupportedRecipientTypes();
196 }
197
203 public function getTester()
204 {
205 if (!$this->tester)
206 {
207 $this->tester = new Tester($this);
208 }
209
210 return $this->tester;
211 }
212
219 public function loadConfiguration($id = null)
220 {
221 $this->configuration = $this->message->loadConfiguration($id);
223 /*
224 if (!$this->configuration)
225 {
226 $this->configuration = $this->message->loadConfiguration($id);
227 }
228
229 return $this->configuration;
230 */
231 }
232
239 public function setConfigurationData(array $data)
240 {
241 foreach ($data as $key => $value)
242 {
243 $this->configuration->set($key, $value);
244 }
245 }
246
252 public function getConfiguration()
253 {
255 }
256
264 {
265 $result = $this->message->saveConfiguration($configuration);
266 if ($result === null)
267 {
268 $result = new Result();
269 }
270
271 return $result;
272 }
273
280 public function copyConfiguration($id)
281 {
282 $result = $this->message->copyConfiguration($id);
283 if ($result === null)
284 {
285 $result = new Result();
286 }
287
288 return $result;
289 }
290
297 public function getField($key)
298 {
299 return isset($this->fields[$key]) ? $this->fields[$key] : null;
300 }
301
307 public function getFields()
308 {
309 return $this->fields;
310 }
311
318 public function setFields(array $fields)
319 {
320 foreach ($fields as $key => $field)
321 {
322 $fields[$key] = nl2br(htmlspecialcharsbx((string) $field, ENT_COMPAT, false));
323 }
324
325 $this->fields = $fields;
326 }
327
335 public function replaceFields($content = "", $replaceChar = '#', $crmEntityTypeId = null)
336 {
337 $from = [];
338 $to = [];
339 foreach ($this->getFields() as $code => $value)
340 {
341 $handledCode = $this->preHandleReplaceCode($code, $crmEntityTypeId);
342 $from[] = "$replaceChar$handledCode$replaceChar";
343 $value = $this->preHandleReaplaceValue($code, $value);
344 $to[] = nl2br(htmlspecialcharsbx((string)$value, ENT_COMPAT, false));
345 }
346
347 return Integration\Sender\Mail\TransportMail::replaceTemplate(str_replace($from, $to, $content));
348 }
354 public function getTo()
355 {
357 }
358
364 public function getRecipientCode()
365 {
367 }
368
375 public function setRecipientCode($code)
376 {
377 $this->recipientCode = $code;
378 }
379
385 public function getRecipientId()
386 {
387 return $this->recipientId;
388 }
389
396 public function setRecipientId($id)
397 {
398 $this->recipientId = $id;
399 }
400
406 public function getRecipientType()
407 {
409 }
410
417 public function setRecipientType($type)
418 {
419 $this->recipientType = $type;
420 }
421
427 public function getRecipientData()
428 {
430 }
431
438 public function setRecipientData(array $data)
439 {
440 $this->recipientData = $data;
441 }
442
448 public function getReadTracker()
449 {
450 return $this->readTracker;
451 }
452
458 public function getClickTracker()
459 {
460 return $this->clickTracker;
461 }
462
468 public function getUnsubTracker()
469 {
470 return $this->unsubTracker;
471 }
472
478 public function send()
479 {
480 if (!$this->getTransport())
481 {
482 return false;
483 }
484
485 return $this->getTransport()->send($this);
486 }
487
493 public function getSendDuration()
494 {
495 if (!$this->getTransport())
496 {
497 return 0;
498 }
499
500 return $this->getTransport()->getDuration($this);
501 }
502
509 public function setSiteId($id = null)
510 {
511 $this->siteId = $id;
512 }
513
519 public function getSiteId()
520 {
521 $siteData = $this->getSiteData($this->siteId);
522 return isset($siteData['LID']) ? $siteData['LID'] : SITE_ID;
523 }
524
530 public function getCharset()
531 {
532 $siteData = $this->getSiteData($this->siteId);
533 return isset($siteData['CHARSET']) ? $siteData['CHARSET'] : SITE_CHARSET;
534 }
535
541 public function getSiteName()
542 {
543 $siteData = $this->getSiteData($this->siteId);
544 return isset($siteData['SITE_NAME']) ? $siteData['SITE_NAME'] : SITE_ID;
545 }
546
552 public function getSiteServerName()
553 {
554 $siteData = $this->getSiteData($this->siteId);
555 return isset($siteData['SERVER_NAME']) ? $siteData['SERVER_NAME'] : null;
556 }
557
566 private function getSiteData($id)
567 {
568 if ($this->siteData !== null)
569 {
571 }
572
573 $this->siteData = array();
574 $siteDb = SiteTable::getList(array(
575 'select'=>array('LID', 'SERVER_NAME', 'NAME', 'CHARSET'=>'CULTURE.CHARSET'),
576 'filter' => array('=LID' => $id ?: SITE_ID)
577 ));
578 if($site = $siteDb->fetch())
579 {
580 $site['SITE_NAME'] = $site['NAME'];
581 unset($site['NAME']);
582 $this->siteData = $site;
583 }
584
585 return $this->siteData;
586 }
587
593 public function isAds()
594 {
595 return $this->message instanceof iAds;
596 }
597
603 public function isMarketing()
604 {
605 return $this->message instanceof iMarketing;
606 }
607
613 public function isMailing()
614 {
615 return $this->message instanceof iMailable;
616 }
617
623 public function isReturnCustomer()
624 {
625 return $this->message instanceof iReturnCustomer;
626 }
627
633 public function isMasterYandex(): bool
634 {
635 return $this->message instanceof iMasterYandex;
636 }
637
643 public function isHidden()
644 {
645 return ($this->message instanceof iHideable && $this->message->isHidden());
646 }
647
653 public function isAvailable()
654 {
655 if ($this->message instanceof iAds)
656 {
657 return Integration\Bitrix24\Service::isAdAvailable();
658 }
659
660 if ($this->message instanceof iReturnCustomer)
661 {
662 return Integration\Bitrix24\Service::isRcAvailable();
663 }
664
665 switch ($this->getCode())
666 {
667 case iBase::CODE_MAIL:
668 return Integration\Bitrix24\Service::isEmailAvailable();
670 return Integration\Bitrix24\Service::isMasterYandexAvailable();
671
672 default:
673 return Integration\Bitrix24\Service::isMailingsAvailable();
674 }
675 }
676
682 public function hasStatistics()
683 {
684 switch ($this->getCode())
685 {
686 case iBase::CODE_MAIL:
687 return true;
688
689 default:
690 return false;
691 }
692 }
693
700 public function getAudioValue($optionCode, $newValue)
701 {
702 if ($this->message instanceof iAudible)
703 {
704 return $this->message->getAudioValue($optionCode, $newValue);
705 }
706 return $newValue;
707 }
708
709 public function onBeforeStart()
710 {
711 if ($this->message instanceof iBeforeAfter)
712 {
713 return $this->message->onBeforeStart();
714 }
715 return new \Bitrix\Main\Result();
716 }
717
718 public function onAfterEnd()
719 {
720 if ($this->message instanceof iBeforeAfter)
721 {
722 return $this->message->onAfterEnd();
723 }
724 return new \Bitrix\Main\Result();
725 }
726
730 public function getEntityCode()
731 {
732 return $this->message->getEntityCode();
733 }
734
735 public function preHandleReaplaceValue($code, $value)
736 {
737 $parts = explode('.', $code, 2);
738 if (count($parts) === 1)
739 {
740 return $value;
741 }
742
743 $entityId = $parts[1];
744
745 if (!self::$statuses)
746 {
747 $refClass = new \ReflectionClass(\Bitrix\Crm\StatusTable::class);
748 $refConstants = $refClass->getConstants();
749 if (is_iterable($refConstants))
750 {
751 foreach ($refConstants as $key => $refConstant)
752 {
753 if (mb_strpos($key, self::PATTERN_STATUS_ITEM) !== false)
754 {
755 self::$statuses[] = $refConstant;
756 }
757 }
758 }
759 }
760
761 if (!in_array($entityId, self::$statuses, true))
762 {
763 return $value;
764 }
765
766 $values = \Bitrix\Crm\StatusTable::getStatusesList($entityId);
767 if (array_key_exists($value, $values))
768 {
769 return $values[$value];
770 }
771
772 return $value;
773 }
774
775 public function preHandleReplaceCode($code, $crmEntityTypeId)
776 {
777 $parts = explode('.', $code, 2);
778 if (count($parts) === 1)
779 {
780 return $code;
781 }
782
783 if (
784 $crmEntityTypeId !== null
785 && (!$this->factory || $this->factory->getEntityTypeId() !== $crmEntityTypeId)
786 )
787 {
788 $this->factory = \Bitrix\Crm\Service\Container::getInstance()->getFactory($crmEntityTypeId);
789 }
790
791 if ($this->factory)
792 {
793 $parts[1] = $this->factory->getCommonFieldNameByMap($parts[1]);
794 return implode('.', $parts);
795 }
796
797 return $code;
798 }
799}
__construct(iBase $message)
Definition adapter.php:106
setConfigurationData(array $data)
Definition adapter.php:239
Bitrix Crm Service Factory $factory
Definition adapter.php:27
setTransport(Transport\Adapter $transport)
Definition adapter.php:153
replaceFields($content="", $replaceChar='#', $crmEntityTypeId=null)
Definition adapter.php:335
getAudioValue($optionCode, $newValue)
Definition adapter.php:700
preHandleReplaceCode($code, $crmEntityTypeId)
Definition adapter.php:775
saveConfiguration(Configuration $configuration)
Definition adapter.php:263
preHandleReaplaceValue($code, $value)
Definition adapter.php:735