Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailsender.php
1<?php
2
4
9
11{
12 public static function getName(): string
13 {
14 return Loc::getMessage('BP_FIELDTYPE_MAIL_SENDER') ?: parent::getName();
15 }
16
17 protected static function getFieldOptions(Bizproc\FieldType $fieldType)
18 {
19 $options = static::makeMailboxListSelectOptions(static::getMailboxList());
20 return static::normalizeOptions($options);
21 }
22
23 public static function renderControlSingle(Bizproc\FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
24 {
25 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
26 }
27
28 public static function renderControlMultiple(Bizproc\FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
29 {
30 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
31 }
32
33 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
34 {
35 $providers = static::getMailboxList();
36 $fieldName = htmlspecialcharsbx(static::generateControlName($field));
37
38 $isPublicControl = $renderMode & FieldType::RENDER_MODE_PUBLIC;
39
40 $typeValue = $value;
41 if (is_array($typeValue))
42 {
43 $typeValue = (string)current($value);
44 }
45
46 $valueHtml = htmlspecialcharsbx((string)$typeValue);
47 $nodeAttributes = '';
48
49 if ($allowSelection && $isPublicControl)
50 {
51 $nodeAttributes = sprintf(
52 'data-role="inline-selector-target" data-select-mode="replace" data-property="%s" ',
53 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
54 );
55 }
56
57 if (!$isPublicControl)
58 {
59 $nodeAttributes .= 'style="opacity: 1" ';
60 }
61
62 $controlId = htmlspecialcharsbx(static::generateControlId($field));
63 $className = htmlspecialcharsbx(static::generateControlClassName($fieldType, $field));
64 $placeholder = htmlspecialcharsbx(Loc::getMessage('BP_FIELDTYPE_MAIL_SENDER_AUTO'));
65
66 $node = <<<HTML
67 <input
68 id="{$controlId}"
69 type="text"
70 readonly="readonly"
71 name="{$fieldName}"
72 value="{$valueHtml}"
73 class="{$className} bizproc-type-control-select-wide"
74 placeholder="{$placeholder}"
75 {$nodeAttributes}
76 >
77HTML;
78
79 if ($allowSelection && !$isPublicControl)
80 {
81 $node .= static::renderControlSelectorButton($controlId, $fieldType, 'replace');
82 }
83
84 return $node . static::getJs($providers, $controlId);
85 }
86
87 private static function getMailboxList()
88 {
89 $mailboxes = Main\Mail\Sender::prepareUserMailboxes();
90
91 return $mailboxes;
92 }
93
94 private static function makeMailboxListSelectOptions(array $mailboxes)
95 {
96 $options = [];
97 foreach ($mailboxes as $mailbox)
98 {
99 $boxValue = sprintf(
100 $mailbox['name'] ? '%s <%s>' : '%s%s',
101 $mailbox['name'], $mailbox['email']
102 );
103
104 $options[$boxValue] = $boxValue;
105 }
106
107 return $options;
108 }
109
110 private static function getJs(array $mailboxes, string $controlId)
111 {
112 $mailboxesJs = Main\Web\Json::encode($mailboxes);
113 $controlIdJs = \CUtil::JSEscape($controlId);
114
115 $textAuto = \CUtil::JSEscape(Loc::getMessage('BP_FIELDTYPE_MAIL_SENDER_AUTO'));
116 $textAdd = \CUtil::JSEscape(Loc::getMessage('BP_FIELDTYPE_MAIL_SENDER_ADD'));
117
118 return <<<HTML
119 <script>
120 BX.ready(function()
121 {
122 var mailboxSelectorValue = document.getElementById('{$controlIdJs}');
123 if (!mailboxSelectorValue)
124 {
125 return;
126 }
127
128 var mailboxes = {$mailboxesJs};
129
130 var setMailbox = function(value)
131 {
132 mailboxSelectorValue.value = value;
133 };
134
135 var getMenuItems = function()
136 {
137 var i, menuItems = [{
138 text: '{$textAuto}',
139 onclick: function(e, item)
140 {
141 this.popupWindow.close();
142 setMailbox('');
143 }
144 }];
145
146 for (i = 0; i < mailboxes.length; ++i)
147 {
148 var mailbox = mailboxes[i];
149 var mailboxName = mailbox['name'].length > 0
150 ? mailbox['name'] + ' <' + mailbox['email'] + '>'
151 : mailbox['email'];
152
153 menuItems.push({
154 text: BX.util.htmlspecialchars(mailboxName),
155 value: mailboxName,
156 onclick: function(e, item)
157 {
158 this.popupWindow.close();
159 setMailbox(item.value);
160 }
161 });
162 }
163
164 if (window.BXMainMailConfirm)
165 {
166 if (menuItems.length > 0)
167 {
168 menuItems.push({delimiter: true});
169 }
170
171 menuItems.push({
172 text: '{$textAdd}',
173 onclick: function(e, item)
174 {
175 this.popupWindow.close();
176 window.BXMainMailConfirm.showForm(function(mailbox)
177 {
178 mailboxes.push(mailbox);
179 setMailbox(mailbox['name'].length > 0
180 ? mailbox['name'] + ' <' + mailbox['email'] + '>'
181 : mailbox['email']);
182 });
183 }
184 });
185 }
186
187 return menuItems;
188 };
189
190 BX.bind(mailboxSelectorValue, 'click', function(e)
191 {
192 e.preventDefault();
193 var menuId = 'crm-sma-mailboxes' + Math.random();
194 BX.PopupMenu.show(
195 menuId,
196 this,
197 getMenuItems(),
198 {
199 autoHide: true,
200 offsetLeft: (BX.pos(this)['width'] / 2),
201 angle: { position: 'top', offset: 0 },
202 overlay: { backgroundColor: 'transparent' },
203 events:
204 {
205 onPopupClose: function()
206 {
207 this.destroy();
208 }
209 }
210 },
211 );
212 }
213 );
214 });
215 </script>
216HTML;
217 }
218}
static renderControlSingle(Bizproc\FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
static renderControlMultiple(Bizproc\FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
static getFieldOptions(Bizproc\FieldType $fieldType)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29