Skip to content

Commit 8667fae

Browse files
committed
Доработки
1 parent 7941e3e commit 8667fae

File tree

5 files changed

+262
-3
lines changed

5 files changed

+262
-3
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"symfony/mailer": "^4.4 || ^5.0",
4444
"symfony/google-mailer": "^4.4 || ^5.0",
4545
"symfony/mime": "^4.4 || ^5.0",
46+
"symfony/messenger": "^4.4 || ^5.0",
4647
"symfony/monolog-bridge": "^5",
4748
"twig/twig": "~1 || ~2",
4849
"symfony/twig-bridge": "^4.4 || ^5",

lib/Bitrix/EventBridge.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Proklung\Notifier\Bitrix;
4+
5+
use Bitrix\Main\ArgumentException;
6+
use Bitrix\Main\Mail\EventMessageCompiler;
7+
use Bitrix\Main\Mail\Internal\EventMessageAttachmentTable;
8+
use Bitrix\Main\Mail\Internal\EventMessageTable;
9+
use Bitrix\Main\ObjectPropertyException;
10+
use Bitrix\Main\SystemException;
11+
12+
/**
13+
* Class EventBridge
14+
* @package Proklung\Notifier\Bitrix
15+
*
16+
* @since 28.07.2021
17+
*/
18+
class EventBridge
19+
{
20+
/**
21+
* Получить скомпилированный текст письма для события.
22+
*
23+
* @param EventInfo $eventInfo DTO события.
24+
* @param array $context Значения полей ($arFields).
25+
* @param array $sites ID сайтов.
26+
*
27+
* @return mixed
28+
*/
29+
public function compileMessage(EventInfo $eventInfo, array $context, array $sites = [])
30+
{
31+
$message = EventMessageCompiler::createInstance([
32+
'EVENT' => $eventInfo->getEventCode(),
33+
'FIELDS' => $context,
34+
'MESSAGE' => $eventInfo->getMessageData(),
35+
'SITE' => $sites,
36+
'CHARSET' => 'UTF8',
37+
]);
38+
$message->compile();
39+
40+
return $message->getMailBody();
41+
}
42+
43+
/**
44+
* @param string $codeEvent Код события.
45+
* @param array $sites ID сайтов.
46+
*
47+
* @return EventInfo[]
48+
*
49+
* @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки.
50+
*/
51+
public function getMessageTemplate(string $codeEvent, array $sites = [])
52+
{
53+
$arSites = ['s1'];
54+
if (count($sites) > 0) {
55+
$arSites = array_merge($arSites, $sites);
56+
}
57+
58+
$messageDb = EventMessageTable::getList([
59+
'select' => ['ID'],
60+
'filter' => [
61+
'=EVENT_NAME' => $codeEvent,
62+
'=EVENT_MESSAGE_SITE.SITE_ID' => $arSites,
63+
],
64+
'group' => ['ID']
65+
]);
66+
67+
$result = [];
68+
foreach ($messageDb as $arMessage) {
69+
$eventMessage = EventMessageTable::getRowById($arMessage['ID']);
70+
71+
$eventMessage['FILES'] = [];
72+
$attachmentDb = EventMessageAttachmentTable::getList([
73+
'select' => ['FILE_ID'],
74+
'filter' => ['=EVENT_MESSAGE_ID' => $arMessage['ID']],
75+
]);
76+
77+
while ($arAttachmentDb = $attachmentDb->fetch()) {
78+
$eventMessage['FILE'][] = $arAttachmentDb['FILE_ID'];
79+
}
80+
81+
$result[] = EventInfo::fromArray($eventMessage);
82+
}
83+
84+
return $result;
85+
}
86+
}

lib/Bitrix/EventInfo.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Proklung\Notifier\Bitrix;
4+
5+
/**
6+
* Class EventInfo
7+
* DTO с информацией о шаблоне битриксового события.
8+
* @package Proklung\Notifier\Bitrix
9+
*
10+
* @since 28.07.2021
11+
*/
12+
class EventInfo
13+
{
14+
/**
15+
* @var integer
16+
*/
17+
private $id;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $eventCode;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $active;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $emailFrom;
33+
34+
/**
35+
* @var string
36+
*/
37+
private $emailTo;
38+
39+
/**
40+
* @var string
41+
*/
42+
private $subject;
43+
44+
/**
45+
* @var string
46+
*/
47+
private $message;
48+
49+
/**
50+
* @var array $files
51+
*/
52+
private $files;
53+
54+
/**
55+
* @var array $arMessage Данные на события в битриксовом формате.
56+
*/
57+
private $arMessage;
58+
59+
/**
60+
* @var mixed
61+
*/
62+
private $siteId;
63+
64+
/**
65+
* EventInfo constructor.
66+
*
67+
* @param array $arMessage Данные на событие.
68+
*/
69+
public function __construct(
70+
array $arMessage
71+
) {
72+
$this->id = $arMessage['ID'];
73+
$this->eventCode = $arMessage['EVENT_NAME'];
74+
$this->active = $arMessage['ACTIVE'];
75+
$this->siteId = $arMessage['LID'];
76+
$this->emailFrom = $arMessage['EMAIL_FROM'];
77+
$this->emailTo = $arMessage['EMAIL_TO'];
78+
$this->subject = $arMessage['SUBJECT'];
79+
$this->message = $arMessage['MESSAGE'];
80+
$this->files = $arMessage['FILES'];
81+
$this->arMessage = $arMessage;
82+
}
83+
84+
/**
85+
* Статический конструктор.
86+
*
87+
* @param array $bitrixArray Битриксовый массив на событие.
88+
*
89+
* @return static
90+
*/
91+
public static function fromArray(array $bitrixArray) : self
92+
{
93+
return new static($bitrixArray);
94+
}
95+
96+
/**
97+
* @return string
98+
*/
99+
public function getEventCode(): string
100+
{
101+
return $this->eventCode;
102+
}
103+
104+
/**
105+
* @return array
106+
*/
107+
public function getMessageData(): array
108+
{
109+
return $this->arMessage;
110+
}
111+
112+
/**
113+
* @return integer
114+
*/
115+
public function getId(): int
116+
{
117+
return $this->id;
118+
}
119+
120+
/**
121+
* @return string
122+
*/
123+
public function getActive(): string
124+
{
125+
return $this->active;
126+
}
127+
128+
/**
129+
* @return string
130+
*/
131+
public function getEmailFrom(): string
132+
{
133+
return $this->emailFrom;
134+
}
135+
136+
/**
137+
* @return string
138+
*/
139+
public function getEmailTo(): string
140+
{
141+
return $this->emailTo;
142+
}
143+
144+
/**
145+
* @return string
146+
*/
147+
public function getSubject(): string
148+
{
149+
return $this->subject;
150+
}
151+
152+
/**
153+
* @return string
154+
*/
155+
public function getMessage(): string
156+
{
157+
return $this->message;
158+
}
159+
160+
/**
161+
* @return array
162+
*/
163+
public function getFiles(): array
164+
{
165+
return $this->files;
166+
}
167+
}

lib/Email/CustomEmailChannel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function notify(Notification $notification, RecipientInterface $recipient
7575
$message = $notification->asEmailMessage($recipient, $transportName);
7676
}
7777

78-
$message = $message ?: CustomEmailMessage::fromNotification($notification, $recipient, (string)$this->from);
78+
$message = $message ?: CustomEmailMessage::fromNotification($notification, $recipient, $this->from);
7979
$email = $message->getMessage();
8080
if ($email instanceof Email) {
8181
if (!$email->getFrom()) {

lib/Email/CustomEmailMessage.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,24 @@ public function __construct(RawMessage $message, ?Envelope $envelope = null)
4545
/**
4646
* @param Notification $notification
4747
* @param EmailRecipientInterface $recipient
48-
* @param string $from Поле from из конфига.
48+
* @param string|null $from Поле from из конфига.
4949
*
5050
* @return static
5151
*/
5252
public static function fromNotification(
5353
Notification $notification,
5454
EmailRecipientInterface $recipient,
55-
string $from
55+
?string $from = null
5656
): self {
5757
if ('' === $recipient->getEmail()) {
5858
throw new InvalidArgumentException(sprintf('"%s" needs an email, it cannot be empty.', __CLASS__));
5959
}
6060

61+
// Если не передали поле from снаружи, то берем из EmailRecipientInterface
62+
if (!$from) {
63+
$from = $recipient->getEmail();
64+
}
65+
6166
$content = $notification->getContent() ?: $notification->getSubject();
6267

6368
if (!class_exists(NotificationEmail::class)) {

0 commit comments

Comments
 (0)