PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/main/lib/mail/eventmessagethemecompiler.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 336 lines | 231 code | 55 blank | 50 comment | 21 complexity | 0e571d22aa13ef8fe0fc5788e1fefb5c MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2012 Bitrix
  7. */
  8. namespace Bitrix\Main\Mail;
  9. use Bitrix\Main\Mail\Internal as MailInternal;
  10. use Bitrix\Main\Config as Config;
  11. use Bitrix\Main\IO as IO;
  12. use Bitrix\Main\ObjectNotFoundException as ObjectNotFoundException;
  13. class EventMessageThemeCompiler
  14. {
  15. /**
  16. * @var EventMessageThemeCompiler
  17. */
  18. protected static $instance = null;
  19. protected $siteTemplateId;
  20. protected $siteId;
  21. protected $languageId;
  22. protected $themeProlog;
  23. protected $themeEpilog;
  24. protected $themeStylesString = '';
  25. protected $resultString = '';
  26. protected $body;
  27. protected $contentTypeHtml = false;
  28. protected $arStyle = array();
  29. protected $replaceCallback = array();
  30. protected $currentResourceOrder = 100;
  31. public function __construct($siteTemplateId = null, $body, $isHtml = true)
  32. {
  33. $this->contentTypeHtml = $isHtml;
  34. $this->siteTemplateId = $siteTemplateId;
  35. $this->setTheme($siteTemplateId);
  36. $this->setBody($body);
  37. }
  38. /**
  39. * @return EventMessageThemeCompiler
  40. */
  41. public static function createInstance($siteTemplateId = null, $body, $isHtml = true)
  42. {
  43. static::$instance = new static($siteTemplateId, $body, $isHtml);
  44. return static::$instance;
  45. }
  46. /**
  47. * Returns current instance of the EventMessageThemeCompiler.
  48. *
  49. * @return EventMessageThemeCompiler
  50. */
  51. public static function getInstance()
  52. {
  53. if (!isset(static::$instance))
  54. throw new ObjectNotFoundException('createInstance() should be called before getInstance()');
  55. return static::$instance;
  56. }
  57. public static function unsetInstance()
  58. {
  59. if (isset(static::$instance))
  60. static::$instance = null;
  61. }
  62. /**
  63. * @param mixed $siteTemplateId
  64. */
  65. public function setSiteTemplateId($siteTemplateId)
  66. {
  67. $this->siteTemplateId = $siteTemplateId;
  68. }
  69. /**
  70. * @return mixed
  71. */
  72. public function getSiteTemplateId()
  73. {
  74. return $this->siteTemplateId;
  75. }
  76. /**
  77. * @param mixed $languageId
  78. */
  79. public function setLanguageId($languageId)
  80. {
  81. $this->languageId = $languageId;
  82. }
  83. /**
  84. * @return mixed
  85. */
  86. public function getLanguageId()
  87. {
  88. return $this->languageId;
  89. }
  90. /**
  91. * @param mixed $siteId
  92. */
  93. public function setSiteId($siteId)
  94. {
  95. $this->siteId = $siteId;
  96. }
  97. /**
  98. * @return mixed
  99. */
  100. public function getSiteId()
  101. {
  102. return $this->siteId;
  103. }
  104. public function getResult()
  105. {
  106. return $this->resultString;
  107. }
  108. public function setParams(array $arParams)
  109. {
  110. $this->params = $arParams;
  111. }
  112. /**
  113. * @param mixed $themeProlog
  114. */
  115. public function setThemeProlog($themeProlog)
  116. {
  117. $this->themeProlog = $themeProlog;
  118. }
  119. /**
  120. * @return mixed
  121. */
  122. public function getThemeProlog()
  123. {
  124. return $this->themeProlog;
  125. }
  126. /**
  127. * @param mixed $themeEpilog
  128. */
  129. public function setThemeEpilog($themeEpilog)
  130. {
  131. $this->themeEpilog = $themeEpilog;
  132. }
  133. /**
  134. * @return mixed
  135. */
  136. public function getThemeEpilog()
  137. {
  138. return $this->themeEpilog;
  139. }
  140. public function setStyle($path, $sort = false)
  141. {
  142. $sort = ($sort === false ? $this->currentResourceOrder : $sort);
  143. $this->arStyle[$path] = $sort;
  144. }
  145. public function setStyleArray(array $arPaths, $sort = false)
  146. {
  147. foreach($arPaths as $path)
  148. $this->setStyle($path, $sort);
  149. }
  150. public function getStyles()
  151. {
  152. return $this->arStyle;
  153. }
  154. public function getStylesString()
  155. {
  156. $returnStylesString = $this->themeStylesString;
  157. $arStyle = $this->arStyle;
  158. asort($arStyle);
  159. foreach($arStyle as $path=>$sort)
  160. {
  161. $pathFull = \Bitrix\Main\Application::getDocumentRoot().$path;
  162. if(IO\File::isFileExists($pathFull))
  163. {
  164. $content = "/* $path */ \r\n" . IO\File::getFileContents($pathFull);
  165. $returnStylesString .= $content . "\r\n";
  166. }
  167. }
  168. if(strlen($returnStylesString)>0)
  169. {
  170. $returnStylesString = '<style type="text/css">'."\r\n".$returnStylesString."\r\n".'</style>';
  171. }
  172. return $returnStylesString;
  173. }
  174. public function showStyles()
  175. {
  176. if($this->contentTypeHtml)
  177. {
  178. $identificator = '%BITRIX_MAIL_EVENT_TEMPLATE_THEME_CALLBACK_STYLE%';
  179. $this->addReplaceCallback($identificator, array($this, 'getStylesString'));
  180. }
  181. else
  182. {
  183. $identificator = '';
  184. }
  185. return $identificator;
  186. }
  187. protected function setTheme($site_template_id)
  188. {
  189. if(strlen($site_template_id)>0)
  190. {
  191. $result = \CSiteTemplate::GetByID($site_template_id);
  192. if($templateFields = $result->Fetch())
  193. {
  194. $template_path_header = \Bitrix\Main\Application::getDocumentRoot().$templateFields['PATH'].'/header.php';
  195. $template_path_footer = \Bitrix\Main\Application::getDocumentRoot().$templateFields['PATH'].'/footer.php';
  196. if($templateFields['PATH']!='' && IO\File::isFileExists($template_path_footer) && IO\File::isFileExists($template_path_header))
  197. {
  198. $this->themeStylesString .= $templateFields['TEMPLATE_STYLES']."\r\n";
  199. $this->themeStylesString .= $templateFields['STYLES']."\r\n";
  200. $this->setThemeProlog(IO\File::getFileContents($template_path_header));
  201. $this->setThemeEpilog(IO\File::getFileContents($template_path_footer));
  202. }
  203. }
  204. }
  205. }
  206. protected function setBody($body)
  207. {
  208. $this->body = $body;
  209. }
  210. /**
  211. * @param
  212. */
  213. public function execute()
  214. {
  215. $resultThemeProlog = '';
  216. $resultThemeEpilog = '';
  217. if(!$this->themeProlog && $this->contentTypeHtml)
  218. $this->body = '<?=$this->showStyles()?>' . $this->body;
  219. $resultBody = $this->executePhp($this->body, 100);
  220. if($this->themeProlog)
  221. $resultThemeProlog = $this->executePhp($this->themeProlog, 50);
  222. if($this->themeEpilog)
  223. $resultThemeEpilog = $this->executePhp($this->themeEpilog, 150);
  224. $this->resultString = $resultThemeProlog . $resultBody . $resultThemeEpilog;
  225. $this->executeReplaceCallback();
  226. }
  227. protected function executePhp($template, $resourceOrder = 100)
  228. {
  229. $this->currentResourceOrder = $resourceOrder;
  230. $arParams = $this->params;
  231. $result = eval('use \Bitrix\Main\Mail\EventMessageThemeCompiler; ob_start();?>' . $template . '<? return ob_get_clean();');
  232. return $result;
  233. }
  234. protected function addReplaceCallback($identificator, $callback)
  235. {
  236. $this->replaceCallback[$identificator] = $callback;
  237. }
  238. protected function executeReplaceCallback()
  239. {
  240. $arReplaceIdentificators = array();
  241. $arReplaceStrings = array();
  242. foreach($this->replaceCallback as $identificator => $callback)
  243. {
  244. $result = call_user_func_array($callback, array());
  245. if($result === false)
  246. $result = '';
  247. $arReplaceIdentificators[] = $identificator;
  248. $arReplaceStrings[] = $result;
  249. }
  250. $this->resultString = str_replace($arReplaceIdentificators, $arReplaceStrings, $this->resultString);
  251. }
  252. public static function includeComponent($componentName, $componentTemplate, $arParams = array(), $parentComponent = null, $arFunctionParams = array())
  253. {
  254. $componentRelativePath = \CComponentEngine::MakeComponentPath($componentName);
  255. if (StrLen($componentRelativePath) <= 0)
  256. return False;
  257. if (is_object($parentComponent))
  258. {
  259. if (!($parentComponent instanceof \cbitrixcomponent))
  260. $parentComponent = null;
  261. }
  262. $result = null;
  263. $bComponentEnabled = (!isset($arFunctionParams["ACTIVE_COMPONENT"]) || $arFunctionParams["ACTIVE_COMPONENT"] <> "N");
  264. $component = new \CBitrixComponent();
  265. if($component->InitComponent($componentName))
  266. {
  267. $obAjax = null;
  268. if($bComponentEnabled)
  269. {
  270. $component->setSiteId(static::getInstance()->getSiteId());
  271. $component->setLanguageId(static::getInstance()->getLanguageId());
  272. $component->setSiteTemplateId(static::getInstance()->getSiteTemplateId());
  273. $result = $component->IncludeComponent($componentTemplate, $arParams, $parentComponent);
  274. $arThemeCss = array(); // TODO: use styles array from $component
  275. foreach($arThemeCss as $cssPath)
  276. static::getInstance()->setStyle($cssPath);
  277. }
  278. }
  279. return $result;
  280. }
  281. }