1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
eventmessagethemecompiler.php
См. документацию.
1<?php
8namespace Bitrix\Main\Mail;
9
10use Bitrix\Main\Application;
11use Bitrix\Main\IO as IO;
12use Bitrix\Main\ObjectNotFoundException as ObjectNotFoundException;
13
15{
19 protected static $instance = null;
20
21 protected $siteTemplateId;
22 protected $siteId;
23 protected $languageId;
24
25 protected $themePath = '';
26 protected $themeProlog;
27 protected $themeEpilog;
28 protected $themeStylesString = '';
29 protected $resultString = '';
30 protected $body;
31 protected $contentTypeHtml = false;
32
33 protected $params = array();
34 protected $arStyle = array();
35 protected $replaceCallback = array();
36 protected $currentResourceOrder = 100;
37
46 public function __construct($siteTemplateId = null, $body, $isHtml = true)
47 {
48 $this->contentTypeHtml = $isHtml;
49 $this->siteTemplateId = $siteTemplateId;
51 $this->setBody($body);
52 }
53
62 public static function createInstance($siteTemplateId = null, $body, $isHtml = true)
63 {
64 static::$instance = new static($siteTemplateId, $body, $isHtml);
65
66 return static::$instance;
67 }
68
75 public static function getInstance()
76 {
77 if (!isset(static::$instance))
78 throw new ObjectNotFoundException('createInstance() should be called before getInstance()');
79
80 return static::$instance;
81 }
82
88 public static function unsetInstance()
89 {
90 if (isset(static::$instance))
91 static::$instance = null;
92 }
93
100 {
101 $this->siteTemplateId = $siteTemplateId;
102 }
103
109 public function getSiteTemplateId()
110 {
112 }
113
119 public function setLanguageId($languageId)
120 {
121 $this->languageId = $languageId;
122 }
123
128 public function getLanguageId()
129 {
130 return $this->languageId;
131 }
132
139 public function setSiteId($siteId)
140 {
141 $this->siteId = $siteId;
142 }
143
149 public function getSiteId()
150 {
151 return $this->siteId;
152 }
153
159 public function getResult()
160 {
161 return $this->resultString;
162 }
163
169 public function setParams(array $params)
170 {
171 $this->params = $params;
172 }
173
180 {
181 $this->themeProlog = $themeProlog;
182 }
183
189 public function getThemeProlog()
190 {
191 return $this->themeProlog;
192 }
193
200 {
201 $this->themeEpilog = $themeEpilog;
202 }
203
209 public function getThemeEpilog()
210 {
211 return $this->themeEpilog;
212 }
213
221 public function setStyle($path, $sort = false)
222 {
223 $sort = ($sort === false ? $this->currentResourceOrder : $sort);
224 $this->arStyle[$path] = $sort;
225 }
226
234 public function setStyleArray(array $arPaths, $sort = false)
235 {
236 foreach($arPaths as $path)
237 $this->setStyle($path, $sort);
238 }
239
245 public function getStyles()
246 {
247 return $this->arStyle;
248 }
249
255 public function getStylesString()
256 {
257 $returnStylesString = $this->themeStylesString;
259 asort($arStyle);
260 foreach($arStyle as $path=>$sort)
261 {
263 if(IO\File::isFileExists($pathFull))
264 {
265 $content = "/* $path */ \r\n" . IO\File::getFileContents($pathFull);
266 $returnStylesString .= $content . "\r\n";
267 }
268 }
269
270 if($returnStylesString <> '')
271 {
272 $returnStylesString = '<style type="text/css">'."\r\n".$returnStylesString."\r\n".'</style>';
273 }
274
275 return $returnStylesString;
276 }
277
283 public function showStyles()
284 {
285 if($this->contentTypeHtml)
286 {
287 $identificator = '%BITRIX_MAIL_EVENT_TEMPLATE_THEME_CALLBACK_STYLE%';
288 $this->addReplaceCallback($identificator, array($this, 'getStylesString'));
289 }
290 else
291 {
292 $identificator = '';
293 }
294
295 return $identificator;
296 }
297
298 protected function setTheme($site_template_id)
299 {
300 if($site_template_id <> '')
301 {
302 $result = \CSiteTemplate::GetByID($site_template_id);
303 if($templateFields = $result->Fetch())
304 {
305 $this->themePath = $templateFields['PATH'];
306 $template_path_header = \Bitrix\Main\Application::getDocumentRoot().$templateFields['PATH'].'/header.php';
307 $template_path_footer = \Bitrix\Main\Application::getDocumentRoot().$templateFields['PATH'].'/footer.php';
308 if($templateFields['PATH']!='' && IO\File::isFileExists($template_path_footer) && IO\File::isFileExists($template_path_header))
309 {
310 $this->themeStylesString .= $templateFields['TEMPLATE_STYLES']."\r\n";
311 $this->themeStylesString .= $templateFields['STYLES']."\r\n";
312
313 $this->setThemeProlog(IO\File::getFileContents($template_path_header));
314 $this->setThemeEpilog(IO\File::getFileContents($template_path_footer));
315 }
316 }
317 }
318 }
319
320 protected function setBody($body)
321 {
322 $this->body = $body;
323 }
324
334 final public function includeThemeLang($relativePath = "")
335 {
336 if ($relativePath == "")
337 {
338 $relativePath = ".description.php";
339 }
340
341 $path = $_SERVER["DOCUMENT_ROOT"].$this->themePath."/".$relativePath;
343 }
344
350 public function execute()
351 {
352 $resultThemeProlog = '';
353 $resultThemeEpilog = '';
354
355 if(!$this->themeProlog && $this->contentTypeHtml)
356 $this->body = '<?=$this->showStyles()?>' . $this->body;
357
358 $resultBody = $this->executePhp($this->body, 100);
359 if($this->themeProlog)
360 {
361 $this->includeThemeLang('header.php');
362 $resultThemeProlog = $this->executePhp($this->themeProlog, 50);
363 }
364
365 if($this->themeEpilog)
366 {
367 $this->includeThemeLang('footer.php');
368 $resultThemeEpilog = $this->executePhp($this->themeEpilog, 150);
369 }
370
371 $this->resultString = $resultThemeProlog . $resultBody . $resultThemeEpilog;
372 $this->executeReplaceCallback();
373 }
374
375
376 protected function executePhp($template, $resourceOrder = 100)
377 {
378 $this->currentResourceOrder = $resourceOrder;
379
380 try
381 {
383 $result = eval('use \Bitrix\Main\Mail\EventMessageThemeCompiler; ob_start();?>' . $template . '<? return ob_get_clean();');
384 }
385 catch (\Exception $e)
386 {
388 $exceptionHandler = $application->getExceptionHandler();
389 $exceptionHandler->writeToLog($e);
390 ob_clean();
391 throw $e;
392 }
393
394 return $result;
395 }
396
397 protected function addReplaceCallback($identificator, $callback)
398 {
399 $this->replaceCallback[$identificator] = $callback;
400 }
401
402 protected function executeReplaceCallback()
403 {
404 $arReplaceIdentificators = array();
405 $arReplaceStrings = array();
406 foreach($this->replaceCallback as $identificator => $callback)
407 {
408 $result = call_user_func_array($callback, array());
409 if($result === false)
410 $result = '';
411
412 $arReplaceIdentificators[] = $identificator;
413 $arReplaceStrings[] = $result;
414 }
415
416 $this->resultString = str_replace($arReplaceIdentificators, $arReplaceStrings, $this->resultString);
417 }
418
424 public static function includeComponent($componentName, $componentTemplate, $arParams = array(), $parentComponent = null, $arFunctionParams = array())
425 {
426 $componentRelativePath = \CComponentEngine::MakeComponentPath($componentName);
427 if ($componentRelativePath == '')
428 return false;
429
430 if (is_object($parentComponent))
431 {
432 if (!($parentComponent instanceof \cbitrixcomponent))
433 $parentComponent = null;
434 }
435
436 $result = null;
437 $bComponentEnabled = (!isset($arFunctionParams["ACTIVE_COMPONENT"]) || $arFunctionParams["ACTIVE_COMPONENT"] <> "N");
438
439 $component = new \CBitrixComponent();
440 if($component->InitComponent($componentName))
441 {
442 $obAjax = null;
443 if($bComponentEnabled)
444 {
445 $component->setSiteId(static::getInstance()->getSiteId());
446 $component->setLanguageId(static::getInstance()->getLanguageId());
447 $component->setSiteTemplateId(static::getInstance()->getSiteTemplateId());
448
449 try
450 {
451 $result = $component->IncludeComponent($componentTemplate, $arParams, $parentComponent);
452 }
453 catch(StopException $e)
454 {
455 $component->AbortResultCache();
456 throw $e;
457 }
458
459 $arThemeCss = array(); // TODO: use styles array from $component
460 foreach($arThemeCss as $cssPath)
461 static::getInstance()->setStyle($cssPath);
462 }
463 }
464
465 return $result;
466 }
467
474 public static function stop()
475 {
476 if (static::$instance)
477 {
478 throw new StopException;
479 }
480 }
481}
$arParams
Определения access_dialog.php:21
$path
Определения access_edit.php:21
static getDocumentRoot()
Определения application.php:736
static getInstance()
Определения application.php:98
static loadMessages($file)
Определения loc.php:65
setStyleArray(array $arPaths, $sort=false)
Определения eventmessagethemecompiler.php:234
executePhp($template, $resourceOrder=100)
Определения eventmessagethemecompiler.php:376
static includeComponent($componentName, $componentTemplate, $arParams=array(), $parentComponent=null, $arFunctionParams=array())
Определения eventmessagethemecompiler.php:424
addReplaceCallback($identificator, $callback)
Определения eventmessagethemecompiler.php:397
__construct($siteTemplateId=null, $body, $isHtml=true)
Определения eventmessagethemecompiler.php:46
static createInstance($siteTemplateId=null, $body, $isHtml=true)
Определения eventmessagethemecompiler.php:62
static GetByID($ID)
Определения site_template.php:142
$content
Определения commerceml.php:144
$componentTemplate
Определения component_props2.php:50
$componentName
Определения component_props2.php:49
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$arPaths
Определения options.php:169
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$application
Определения bitrix.php:23
Определения directory.php:3