Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
template.php
1<?php
8namespace Bitrix\Sender;
9
15use Bitrix\Fileman\Block\Editor as BlockEditor;
16use Bitrix\Fileman\Block\EditorMail as BlockEditorMail;
17
18Loc::loadMessages(__FILE__);
19
37{
38 const LOCAL_DIR_IMG = '/images/sender/preset/template/';
39
48 public static function onPresetTemplateList($templateType = null, $templateId = null)
49 {
50 $resultList = array();
51 if($templateType && $templateType !== 'USER')
52 {
53 return $resultList;
54 }
55
56 $localPathOfIcon = static::LOCAL_DIR_IMG . 'my.png';
57 //$fullPathOfIcon = Loader::getLocal($localPathOfIcon);
58
59 // return only active templates, but if requested template by id return any
60 $filter = array();
61 if($templateId)
62 {
63 $filter['ID'] = $templateId;
64 }
65 else
66 {
67 $filter['ACTIVE'] = 'Y';
68 }
69
70 $templateDb = static::getList(array('filter' => $filter, 'order' => array('ID' => 'DESC')));
71 while($template = $templateDb->fetch())
72 {
73 $resultList[] = array(
74 'TYPE' => 'USER',
75 'ID' => $template['ID'],
76 'NAME' => $template['NAME'],
77 'ICON' => '',//(!empty($fullPathOfIcon) ? '/bitrix'.$localPathOfIcon : ''),
78 'FIELDS' => array(
79 'MESSAGE' => array(
80 'CODE' => 'MESSAGE',
81 'VALUE' => Security\Sanitizer::fixTemplateStyles($template['CONTENT']),
82 'ON_DEMAND' => static::isContentForBlockEditor($template['CONTENT'])
83 ),
84 'SUBJECT' => array(
85 'CODE' => 'SUBJECT',
86 'VALUE' => $template['NAME'],
87 ),
88 )
89 );
90 }
91
92 return $resultList;
93 }
94
100 public static function incUseCount($id)
101 {
102 return static::update($id, array(
103 'USE_COUNT' => new DB\SqlExpression('?# + 1', 'USE_COUNT'),
104 'DATE_USE' => new MainType\DateTime()
105 ))->isSuccess();
106 }
107
113 public static function getTableName()
114 {
115 return 'b_sender_preset_template';
116 }
117
123 public static function getMap()
124 {
125 return array(
126 'ID' => array(
127 'data_type' => 'integer',
128 'autocomplete' => true,
129 'primary' => true,
130 ),
131 'ACTIVE' => array(
132 'data_type' => 'string',
133 'required' => true,
134 'default_value' => 'Y',
135 ),
136 'NAME' => array(
137 'data_type' => 'string',
138 'required' => true,
139 'title' => Loc::getMessage('SENDER_ENTITY_TEMPLATE_FIELD_TITLE_NAME')
140 ),
141 'CONTENT' => array(
142 'data_type' => 'string',
143 'required' => true,
144 'title' => Loc::getMessage('SENDER_ENTITY_TEMPLATE_FIELD_TITLE_CONTENT'),
145 'save_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getSaveModificator'),
146 'fetch_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getFetchModificator'),
147 ),
148 'USE_COUNT' => array(
149 'data_type' => 'integer',
150 'default_value' => 0,
151 'required' => true,
152 ),
153 'DATE_INSERT' => array(
154 'data_type' => 'datetime',
155 'required' => true,
156 'default_value' => new MainType\DateTime(),
157 ),
158 'DATE_USE' => array(
159 'data_type' => 'datetime',
160 ),
161 );
162 }
163
168 public static function onBeforeAdd(ORM\Event $event)
169 {
170 $result = new ORM\EventResult;
171 $data = $event->getParameters();
172 $data['fields']['CONTENT'] = Security\Sanitizer::fixTemplateStyles($data['fields']['CONTENT']);
173 $result->modifyFields($data['fields']);
174
175 return $result;
176 }
177
178 public static function onAfterAdd(ORM\Event $event)
179 {
180 $result = new ORM\EventResult;
181 $data = $event->getParameters();
182 if (isset($data['fields']['CONTENT']))
183 {
184 \Bitrix\Sender\FileTable::syncFiles($data['primary']['ID'], 1, $data['fields']['CONTENT']);
185 }
186
187 return $result;
188 }
189
196 public static function onBeforeUpdate(ORM\Event $event)
197 {
198 $result = new ORM\EventResult;
199
200 $data = $event->getParameters();
201 if (array_key_exists('CONTENT', $data['fields']))
202 {
203 $data['fields']['CONTENT'] = Security\Sanitizer::fixTemplateStyles($data['fields']['CONTENT']);
204 $result->modifyFields($data['fields']);
205 \Bitrix\Sender\FileTable::syncFiles($data['primary']['ID'], 1, $data['fields']['CONTENT']);
206 }
207
208 return $result;
209 }
210
217 public static function onBeforeDelete(ORM\Event $event)
218 {
219 $result = new ORM\EventResult;
220 $data = $event->getParameters();
221 $chainListDb = MailingChainTable::getList(array(
222 'select' => array('ID', 'SUBJECT', 'MAILING_ID', 'MAILING_NAME' => 'TITLE'),
223 'filter' => array('TEMPLATE_TYPE' => 'USER', 'TEMPLATE_ID' => $data['primary']['ID']),
224 'order' => array('MAILING_NAME' => 'ASC', 'ID')
225 ));
226
227 if($chainListDb->getSelectedRowsCount() > 0)
228 {
229 $template = static::getRowById($data['primary']['ID']);
230 $messageList = array();
231 while($chain = $chainListDb->fetch())
232 {
233 $messageList[$chain['MAILING_NAME']] = '[' . $chain['ID'] . '] ' . htmlspecialcharsbx($chain['SUBJECT']) . "\n";
234 }
235
236 $message = Loc::getMessage('SENDER_ENTITY_TEMPLATE_DELETE_ERROR_TEMPLATE', array('#NAME#' => $template['NAME'])) . "\n";
237 foreach($messageList as $mailingName => $messageItem)
238 {
239 $message .= Loc::getMessage('SENDER_ENTITY_TEMPLATE_DELETE_ERROR_MAILING', array('#NAME#' => $mailingName)) . "\n" . $messageItem . "\n";
240 }
241
242 $result->addError(new ORM\EntityError($message));
243 }
244
245 if (!$result->getErrors())
246 {
247 \Bitrix\Sender\FileTable::syncFiles($data['primary']['ID'], 1, '');
248 }
249 return $result;
250 }
251
258 public static function isContentForBlockEditor($content)
259 {
260 Loader::includeModule('fileman');
261 return BlockEditor::isContentSupported($content);
262 }
263
270 public static function initEditor(array $params)
271 {
272 $fieldName = $params['FIELD_NAME'];
273 $fieldValue = $params['FIELD_VALUE'];
274 $isUserHavePhpAccess = $params['HAVE_USER_ACCESS'];
275 $showSaveTemplate = isset($params['SHOW_SAVE_TEMPLATE']) ? $params['SHOW_SAVE_TEMPLATE'] : true;
276 $site = isset($params['SITE']) ? $params['SITE'] : '';
277 $charset = isset($params['CHARSET']) ? $params['CHARSET'] : '';
278 $contentUrl = isset($params['CONTENT_URL']) ? $params['CONTENT_URL'] : '';
279 $templateTypeInput = isset($params['TEMPLATE_TYPE_INPUT']) ? $params['TEMPLATE_TYPE_INPUT'] : 'TEMPLATE_TYPE';
280 $templateIdInput = isset($params['TEMPLATE_ID_INPUT']) ? $params['TEMPLATE_ID_INPUT'] : 'TEMPLATE_ID';
281 $templateType = isset($params['TEMPLATE_TYPE']) ? $params['TEMPLATE_TYPE'] : '';
282 $templateId = isset($params['TEMPLATE_ID']) ? $params['TEMPLATE_ID'] : '';
283 $isTemplateMode = isset($params['IS_TEMPLATE_MODE']) ? (bool) $params['IS_TEMPLATE_MODE'] : true;
284 if(!empty($params['PERSONALIZE_LIST']) && is_array($params['PERSONALIZE_LIST']))
285 {
286 PostingRecipientTable::setPersonalizeList($params['PERSONALIZE_LIST']);
287 }
288
289 static $isInit;
290
291 $isDisplayBlockEditor = ($templateType && $templateId) || static::isContentForBlockEditor($fieldValue);
292
293 $editorHeight = '650px';
294 $editorWidth = '100%';
295
296 Loader::includeModule('fileman');
297
298 \CJSCore::RegisterExt("sender_editor", Array(
299 "js" => array("/bitrix/js/sender/editor/htmleditor.js"),
300 "rel" => array()
301 ));
302 \CJSCore::Init(array("sender_editor"));
303
304 ob_start();
305 ?>
306 <div id="bx-sender-visual-editor-<?=$fieldName?>" style="<?if($isDisplayBlockEditor):?>display: none;<?endif;?>">
307 <script>
308 BX.ready(function(){
309 <?if(!$isInit): $isInit = true;?>
310 var letterManager = new SenderLetterManager;
311 letterManager.setPlaceHolderList(<?=\CUtil::PhpToJSObject(PostingRecipientTable::getPersonalizeList());?>);
312 <?endif;?>
313 });
314
315 BX.message({
316 "BXEdPlaceHolderSelectorTitle" : "<?=Loc::getMessage('SENDER_TEMPLATE_EDITOR_PLACEHOLDER')?>"
317 });
318 </script>
319 <textarea id="bxed_<?=htmlspecialcharsbx($fieldName)?>"
320 name="<?=htmlspecialcharsbx($fieldName)?>"
321 style="height: <?=htmlspecialcharsbx($editorHeight)?>; width: <?=htmlspecialcharsbx($editorWidth)?>;"
322 class="typearea"
323 ><?=htmlspecialcharsbx($fieldValue)?></textarea>
324
325 </div>
326
327 <div id="bx-sender-block-editor-<?=htmlspecialcharsbx($fieldName)?>" style="<?if(!$isDisplayBlockEditor):?>display: none;<?endif;?>">
328 <br/>
329 <input type="hidden" name="<?=htmlspecialcharsbx($templateTypeInput)?>" value="<?=htmlspecialcharsbx($templateType)?>" />
330 <input type="hidden" name="<?=htmlspecialcharsbx($templateIdInput)?>" value="<?=htmlspecialcharsbx($templateId)?>" />
331 <?
332 $url = '';
333 if($isDisplayBlockEditor)
334 {
335 if($templateType && $templateId)
336 {
337 $url = '/bitrix/admin/sender_template_admin.php?';
338 $url .= 'action=get_template&template_type=' . $templateType . '&template_id=' . $templateId;
339 $url .= '&lang=' . LANGUAGE_ID . '&' . bitrix_sessid_get();
340 }
341 else
342 {
343 $url = $contentUrl;
344 }
345 }
346 echo BlockEditorMail::show(array(
347 'id' => $fieldName,
348 'charset' => $charset,
349 'site' => $site,
350 'own_result_id' => 'bxed_' . $fieldName,
351 'url' => $url,
352 'templateType' => $templateType,
353 'templateId' => $templateId,
354 'isTemplateMode' => $isTemplateMode,
355 'isUserHavePhpAccess' => $isUserHavePhpAccess,
356 ));
357 ?>
358 </div>
359
360 <?
361 if($showSaveTemplate):
362 ?>
363 <script>
364 function ToggleTemplateSaveDialog()
365 {
366 BX('TEMPLATE_ACTION_SAVE_NAME_CONT').value = '';
367
368 var currentDisplay = BX('TEMPLATE_ACTION_SAVE_NAME_CONT').style.display;
369 BX('TEMPLATE_ACTION_SAVE_NAME_CONT').style.display = BX.toggle(currentDisplay, ['inline', 'none']);
370 }
371 </script>
372 <div class="adm-detail-content-item-block-save">
373 <span>
374 <input type="checkbox" value="Y" name="TEMPLATE_ACTION_SAVE" id="TEMPLATE_ACTION_SAVE" onclick="ToggleTemplateSaveDialog();">
375 <label for="TEMPLATE_ACTION_SAVE"><?=Loc::getMessage('SENDER_TEMPLATE_EDITOR_SAVE')?></label>
376 </span>
377 <span id="TEMPLATE_ACTION_SAVE_NAME_CONT" style="display: none;"> <?=Loc::getMessage('SENDER_TEMPLATE_EDITOR_SAVE_NAME')?> <input type="text" name="TEMPLATE_ACTION_SAVE_NAME"></span>
378 </div>
379 <?
380 endif;
381
382 return ob_get_clean();
383 }
384}
static isContentSupported($content)
Definition editor.php:1183
static show(array $params)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static setPersonalizeList(array $personalizeList=null)
Definition posting.php:754
static isContentForBlockEditor($content)
Definition template.php:258
static onBeforeAdd(ORM\Event $event)
Definition template.php:168
static onPresetTemplateList($templateType=null, $templateId=null)
Definition template.php:48
static onAfterAdd(ORM\Event $event)
Definition template.php:178
static initEditor(array $params)
Definition template.php:270
static onBeforeDelete(ORM\Event $event)
Definition template.php:217
static onBeforeUpdate(ORM\Event $event)
Definition template.php:196