Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventmessagethemecompiler.php
1<?php
8namespace Bitrix\Main\Mail;
9
13use Bitrix\Main\IO as IO;
14use Bitrix\Main\ObjectNotFoundException as ObjectNotFoundException;
15
17{
21 protected static $instance = null;
22
23 protected $siteTemplateId;
24 protected $siteId;
25 protected $languageId;
26
27 protected $themePath = '';
28 protected $themeProlog;
29 protected $themeEpilog;
30 protected $themeStylesString = '';
31 protected $resultString = '';
32 protected $body;
33 protected $contentTypeHtml = false;
34
35 protected $params = array();
36 protected $arStyle = array();
37 protected $replaceCallback = array();
38 protected $currentResourceOrder = 100;
39
48 public function __construct($siteTemplateId = null, $body, $isHtml = true)
49 {
50 $this->contentTypeHtml = $isHtml;
51 $this->siteTemplateId = $siteTemplateId;
53 $this->setBody($body);
54 }
55
64 public static function createInstance($siteTemplateId = null, $body, $isHtml = true)
65 {
66 static::$instance = new static($siteTemplateId, $body, $isHtml);
67
68 return static::$instance;
69 }
70
77 public static function getInstance()
78 {
79 if (!isset(static::$instance))
80 throw new ObjectNotFoundException('createInstance() should be called before getInstance()');
81
82 return static::$instance;
83 }
84
90 public static function unsetInstance()
91 {
92 if (isset(static::$instance))
93 static::$instance = null;
94 }
95
102 {
103 $this->siteTemplateId = $siteTemplateId;
104 }
105
111 public function getSiteTemplateId()
112 {
114 }
115
121 public function setLanguageId($languageId)
122 {
123 $this->languageId = $languageId;
124 }
125
130 public function getLanguageId()
131 {
132 return $this->languageId;
133 }
134
141 public function setSiteId($siteId)
142 {
143 $this->siteId = $siteId;
144 }
145
151 public function getSiteId()
152 {
153 return $this->siteId;
154 }
155
161 public function getResult()
162 {
163 return $this->resultString;
164 }
165
171 public function setParams(array $params)
172 {
173 $this->params = $params;
174 }
175
182 {
183 $this->themeProlog = $themeProlog;
184 }
185
191 public function getThemeProlog()
192 {
193 return $this->themeProlog;
194 }
195
202 {
203 $this->themeEpilog = $themeEpilog;
204 }
205
211 public function getThemeEpilog()
212 {
213 return $this->themeEpilog;
214 }
215
223 public function setStyle($path, $sort = false)
224 {
225 $sort = ($sort === false ? $this->currentResourceOrder : $sort);
226 $this->arStyle[$path] = $sort;
227 }
228
236 public function setStyleArray(array $arPaths, $sort = false)
237 {
238 foreach($arPaths as $path)
239 $this->setStyle($path, $sort);
240 }
241
247 public function getStyles()
248 {
249 return $this->arStyle;
250 }
251
257 public function getStylesString()
258 {
259 $returnStylesString = $this->themeStylesString;
261 asort($arStyle);
262 foreach($arStyle as $path=>$sort)
263 {
264 $pathFull = \Bitrix\Main\Application::getDocumentRoot().$path;
265 if(IO\File::isFileExists($pathFull))
266 {
267 $content = "/* $path */ \r\n" . IO\File::getFileContents($pathFull);
268 $returnStylesString .= $content . "\r\n";
269 }
270 }
271
272 if($returnStylesString <> '')
273 {
274 $returnStylesString = '<style type="text/css">'."\r\n".$returnStylesString."\r\n".'</style>';
275 }
276
277 return $returnStylesString;
278 }
279
285 public function showStyles()
286 {
287 if($this->contentTypeHtml)
288 {
289 $identificator = '%BITRIX_MAIL_EVENT_TEMPLATE_THEME_CALLBACK_STYLE%';
290 $this->addReplaceCallback($identificator, array($this, 'getStylesString'));
291 }
292 else
293 {
294 $identificator = '';
295 }
296
297 return $identificator;
298 }
299
300 protected function setTheme($site_template_id)
301 {
302 if($site_template_id <> '')
303 {
304 $result = \CSiteTemplate::GetByID($site_template_id);
305 if($templateFields = $result->Fetch())
306 {
307 $this->themePath = $templateFields['PATH'];
308 $template_path_header = \Bitrix\Main\Application::getDocumentRoot().$templateFields['PATH'].'/header.php';
309 $template_path_footer = \Bitrix\Main\Application::getDocumentRoot().$templateFields['PATH'].'/footer.php';
310 if($templateFields['PATH']!='' && IO\File::isFileExists($template_path_footer) && IO\File::isFileExists($template_path_header))
311 {
312 $this->themeStylesString .= $templateFields['TEMPLATE_STYLES']."\r\n";
313 $this->themeStylesString .= $templateFields['STYLES']."\r\n";
314
315 $this->setThemeProlog(IO\File::getFileContents($template_path_header));
316 $this->setThemeEpilog(IO\File::getFileContents($template_path_footer));
317 }
318 }
319 }
320 }
321
322 protected function setBody($body)
323 {
324 $this->body = $body;
325 }
326
336 final public function includeThemeLang($relativePath = "")
337 {
338 if ($relativePath == "")
339 {
340 $relativePath = ".description.php";
341 }
342
343 $path = $_SERVER["DOCUMENT_ROOT"].$this->themePath."/".$relativePath;
344 \Bitrix\Main\Localization\Loc::loadMessages($path);
345 }
346
352 public function execute()
353 {
354 $resultThemeProlog = '';
355 $resultThemeEpilog = '';
356
357 if(!$this->themeProlog && $this->contentTypeHtml)
358 $this->body = '<?=$this->showStyles()?>' . $this->body;
359
360 $resultBody = $this->executePhp($this->body, 100);
361 if($this->themeProlog)
362 {
363 $this->includeThemeLang('header.php');
364 $resultThemeProlog = $this->executePhp($this->themeProlog, 50);
365 }
366
367 if($this->themeEpilog)
368 {
369 $this->includeThemeLang('footer.php');
370 $resultThemeEpilog = $this->executePhp($this->themeEpilog, 150);
371 }
372
373 $this->resultString = $resultThemeProlog . $resultBody . $resultThemeEpilog;
374 $this->executeReplaceCallback();
375 }
376
377
378 protected function executePhp($template, $resourceOrder = 100)
379 {
380 $this->currentResourceOrder = $resourceOrder;
381
382 try
383 {
384 $arParams = $this->params;
385 $result = eval('use \Bitrix\Main\Mail\EventMessageThemeCompiler; ob_start();?>' . $template . '<? return ob_get_clean();');
386 }
387 catch (\Exception $e)
388 {
389 $application = Application::getInstance();
390 $exceptionHandler = $application->getExceptionHandler();
391 $exceptionHandler->writeToLog($e);
392 ob_clean();
393 throw $e;
394 }
395
396 return $result;
397 }
398
399 protected function addReplaceCallback($identificator, $callback)
400 {
401 $this->replaceCallback[$identificator] = $callback;
402 }
403
404 protected function executeReplaceCallback()
405 {
406 $arReplaceIdentificators = array();
407 $arReplaceStrings = array();
408 foreach($this->replaceCallback as $identificator => $callback)
409 {
410 $result = call_user_func_array($callback, array());
411 if($result === false)
412 $result = '';
413
414 $arReplaceIdentificators[] = $identificator;
415 $arReplaceStrings[] = $result;
416 }
417
418 $this->resultString = str_replace($arReplaceIdentificators, $arReplaceStrings, $this->resultString);
419 }
420
426 public static function includeComponent($componentName, $componentTemplate, $arParams = array(), $parentComponent = null, $arFunctionParams = array())
427 {
428 $componentRelativePath = \CComponentEngine::MakeComponentPath($componentName);
429 if ($componentRelativePath == '')
430 return false;
431
432 if (is_object($parentComponent))
433 {
434 if (!($parentComponent instanceof \cbitrixcomponent))
435 $parentComponent = null;
436 }
437
438 $result = null;
439 $bComponentEnabled = (!isset($arFunctionParams["ACTIVE_COMPONENT"]) || $arFunctionParams["ACTIVE_COMPONENT"] <> "N");
440
441 $component = new \CBitrixComponent();
442 if($component->InitComponent($componentName))
443 {
444 $obAjax = null;
445 if($bComponentEnabled)
446 {
447 $component->setSiteId(static::getInstance()->getSiteId());
448 $component->setLanguageId(static::getInstance()->getLanguageId());
449 $component->setSiteTemplateId(static::getInstance()->getSiteTemplateId());
450
451 try
452 {
453 $result = $component->IncludeComponent($componentTemplate, $arParams, $parentComponent);
454 }
455 catch(StopException $e)
456 {
457 $component->AbortResultCache();
458 throw $e;
459 }
460
461 $arThemeCss = array(); // TODO: use styles array from $component
462 foreach($arThemeCss as $cssPath)
463 static::getInstance()->setStyle($cssPath);
464 }
465 }
466
467 return $result;
468 }
469
476 public static function stop()
477 {
478 if (static::$instance)
479 {
480 throw new StopException;
481 }
482 }
483}
static includeComponent($componentName, $componentTemplate, $arParams=array(), $parentComponent=null, $arFunctionParams=array())
__construct($siteTemplateId=null, $body, $isHtml=true)
static createInstance($siteTemplateId=null, $body, $isHtml=true)