PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sender/lib/templates/selector.php

https://gitlab.com/alexprowars/bitrix
PHP | 435 lines | 273 code | 57 blank | 105 comment | 48 complexity | 69972e93f9067041e2817678d29af9ee MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage sender
  6. * @copyright 2001-2012 Bitrix
  7. */
  8. namespace Bitrix\Sender\Templates;
  9. use Bitrix\Main\Event;
  10. use Bitrix\Main\EventResult;
  11. use Bitrix\Main\Localization\Loc;
  12. use Bitrix\Sender\Message;
  13. use Bitrix\Sender\TemplateTable;
  14. Loc::loadMessages(__FILE__);
  15. /**
  16. * Class Selector
  17. * @package Bitrix\Sender\Templates
  18. */
  19. class Selector
  20. {
  21. /** @var string $messageCode Message code. */
  22. private $messageCode;
  23. /** @var integer $version Version. */
  24. private $version;
  25. /** @var integer $typeId Type ID. */
  26. private $typeId = null;
  27. /** @var bool $includeTriggers Include triggers. */
  28. private $includeTriggers = true;
  29. /** @var integer $id Template ID. */
  30. private $id = null;
  31. /** @var integer $templateCounter Template counter. */
  32. private $templateCounter = 0;
  33. /**
  34. * Create selector instance.
  35. *
  36. * @return static;
  37. */
  38. public static function create()
  39. {
  40. return new static();
  41. }
  42. /**
  43. * With default message code.
  44. *
  45. * @return $this
  46. */
  47. public function withDefaultMessageCode()
  48. {
  49. $this->messageCode = Message\iBase::CODE_MAIL;
  50. return $this;
  51. }
  52. /**
  53. * With message code.
  54. *
  55. * @param string $messageCode Message code.
  56. * @return $this
  57. */
  58. public function withMessageCode($messageCode)
  59. {
  60. $this->messageCode = $messageCode;
  61. return $this;
  62. }
  63. /**
  64. * With minimal version.
  65. *
  66. * @param integer $version Version.
  67. * @return $this
  68. */
  69. public function withVersion($version)
  70. {
  71. $this->version = $version;
  72. return $this;
  73. }
  74. /**
  75. * With type ID.
  76. *
  77. * @param integer $typeId Type ID.
  78. * @return $this
  79. */
  80. public function withTypeId($typeId)
  81. {
  82. $this->typeId = $typeId;
  83. return $this;
  84. }
  85. /**
  86. * With id.
  87. *
  88. * @param integer $id Template ID.
  89. * @return $this
  90. */
  91. public function withId($id)
  92. {
  93. $this->id = $id;
  94. return $this;
  95. }
  96. /**
  97. * With triggers.
  98. *
  99. * @param bool $include Include triggers.
  100. * @return $this
  101. */
  102. public function withTriggers($include)
  103. {
  104. $this->includeTriggers = $include;
  105. return $this;
  106. }
  107. /**
  108. * Get list.
  109. *
  110. * @return array
  111. */
  112. public function get()
  113. {
  114. $list = $this->getTemplates(1);
  115. return count($list) > 0 ? $list[0] : null;
  116. }
  117. /**
  118. * Get list.
  119. *
  120. * @return array
  121. */
  122. public function getList()
  123. {
  124. return $this->getTemplates();
  125. }
  126. /**
  127. * Return true if it has templates.
  128. *
  129. * @return bool
  130. */
  131. public function hasAny()
  132. {
  133. return count($this->getList()) > 0;
  134. }
  135. /**
  136. * Get categories of list.
  137. *
  138. * @return array
  139. */
  140. public function getCategories()
  141. {
  142. $result = array();
  143. foreach($this->getList() as $template)
  144. {
  145. $categoryCode = $template['CATEGORY'];
  146. if (isset($result[$categoryCode]))
  147. {
  148. continue;
  149. }
  150. $result[$categoryCode] = array(
  151. 'id' => $categoryCode,
  152. 'name' => Category::getName(Category::getId($categoryCode))
  153. );
  154. }
  155. $result = array_values($result);
  156. usort(
  157. $result,
  158. function ($a, $b)
  159. {
  160. return Category::sortByCode($a['id'], $b['id']);
  161. }
  162. );
  163. return $result;
  164. }
  165. /**
  166. * Get categorized list.
  167. *
  168. * @return array
  169. */
  170. public function getCategorized()
  171. {
  172. $list = array();
  173. foreach($this->getList() as $template)
  174. {
  175. $list[$template['CATEGORY']][] = $template;
  176. }
  177. return $list;
  178. }
  179. /**
  180. * Get templates
  181. * @param int $limit Limit.
  182. * @return array
  183. */
  184. private function getTemplates($limit = 0)
  185. {
  186. $result = array();
  187. $parameters = array(
  188. Type::getCode($this->typeId),
  189. $this->id,
  190. $this->messageCode
  191. );
  192. $event = new Event(
  193. 'sender',
  194. 'OnPresetTemplateList',
  195. $parameters
  196. );
  197. $event->send();
  198. foreach ($event->getResults() as $eventResult)
  199. {
  200. if ($eventResult->getType() == EventResult::ERROR)
  201. {
  202. continue;
  203. }
  204. $eventResultParameters = $eventResult->getParameters();
  205. if (!is_array($eventResultParameters))
  206. {
  207. continue;
  208. }
  209. foreach ($eventResultParameters as $template)
  210. {
  211. $template = $this->prepareTemplate($template);
  212. if (!$this->checkTemplate($template))
  213. {
  214. continue;
  215. }
  216. $result[] = $template;
  217. if ($limit && count($result) >= $limit)
  218. {
  219. return $result;
  220. }
  221. }
  222. }
  223. $providers = array(
  224. array('\Bitrix\Sender\Templates\Recent', 'onPresetTemplateList'),
  225. array('\Bitrix\Sender\Preset\Templates\Mail', 'onPresetTemplateList'),
  226. array('\Bitrix\Sender\Preset\Templates\Sms', 'onPresetTemplateList'),
  227. array('\Bitrix\Sender\Preset\Templates\Rc', 'onPresetTemplateList'),
  228. array('\Bitrix\Sender\Preset\Templates\Toloka', 'onPresetTemplateList'),
  229. array('\Bitrix\Sender\Preset\Templates\AudioCall', 'onPresetTemplateList')
  230. );
  231. foreach ($providers as $provider)
  232. {
  233. if (!is_callable($provider))
  234. {
  235. continue;
  236. }
  237. $eventResult = call_user_func_array($provider, $parameters);
  238. if (!is_array($eventResult))
  239. {
  240. continue;
  241. }
  242. foreach ($eventResult as $template)
  243. {
  244. $template = $this->prepareTemplate($template);
  245. if (!$this->checkTemplate($template))
  246. {
  247. continue;
  248. }
  249. $result[] = $template;
  250. if ($limit && count($result) >= $limit)
  251. {
  252. return $result;
  253. }
  254. }
  255. }
  256. return $result;
  257. }
  258. /**
  259. * Check template
  260. * @param array $template Template.
  261. * @return bool
  262. */
  263. private function checkTemplate($template)
  264. {
  265. if (count($template['FIELDS']) === 0)
  266. {
  267. return false;
  268. }
  269. if ($this->messageCode)
  270. {
  271. $messageCodes = $template['MESSAGE_CODE'];
  272. if (!is_array($messageCodes))
  273. {
  274. $messageCodes = array($messageCodes);
  275. }
  276. if (!in_array($this->messageCode, $messageCodes))
  277. {
  278. return false;
  279. }
  280. }
  281. if ($this->version && $template['VERSION'] < $this->version)
  282. {
  283. return false;
  284. }
  285. if (!$this->includeTriggers && $template['IS_TRIGGER'])
  286. {
  287. return false;
  288. }
  289. if (!in_array($template['TYPE'], Type::getCodes()))
  290. {
  291. return false;
  292. }
  293. if ($this->id && $this->id != $template['ID'])
  294. {
  295. return false;
  296. }
  297. return true;
  298. }
  299. /**
  300. * Prepare template
  301. * @param array $template Template.
  302. * @return array
  303. */
  304. private function prepareTemplate($template)
  305. {
  306. if (!is_array($template))
  307. {
  308. $template = array();
  309. }
  310. // type
  311. if (!isset($template['TYPE']))
  312. {
  313. $template['TYPE'] = Type::getCode(Type::ADDITIONAL);
  314. }
  315. // type
  316. if (!isset($template['IS_TRIGGER']))
  317. {
  318. $template['IS_TRIGGER'] = false;
  319. }
  320. // type
  321. if (!isset($template['ID']))
  322. {
  323. $template['ID'] = ++$this->templateCounter;
  324. }
  325. // fields of template
  326. if (!isset($template['FIELDS']) || !is_array($template['FIELDS']))
  327. {
  328. $template['FIELDS'] = [];
  329. }
  330. // segments of template
  331. if (!isset($template['SEGMENTS']) || !is_array($template['SEGMENTS']))
  332. {
  333. $template['SEGMENTS'] = [];
  334. }
  335. // dispatch of template
  336. if (!isset($template['DISPATCH']) || !is_array($template['DISPATCH']))
  337. {
  338. $template['DISPATCH'] = [];
  339. }
  340. // compatibility for mail templates
  341. if (isset($template['HTML']) && $template['HTML'] && count($template['FIELDS']) === 0)
  342. {
  343. $template['FIELDS']['MESSAGE'] = array(
  344. 'CODE' => 'MESSAGE',
  345. 'VALUE' => $template['HTML'],
  346. 'ON_DEMAND' => TemplateTable::isContentForBlockEditor($template['HTML'])
  347. );
  348. }
  349. if (!isset($template['CATEGORY']) || !$template['CATEGORY'])
  350. {
  351. $template['CATEGORY'] = $template['TYPE'];
  352. }
  353. if (!isset($template['VERSION']) || !$template['VERSION'])
  354. {
  355. $template['VERSION'] = 2;
  356. }
  357. if (!isset($template['HINT']) || !$template['HINT'])
  358. {
  359. $template['HINT'] = '';
  360. }
  361. if (!isset($template['HOT']) || !$template['HOT'])
  362. {
  363. $template['HOT'] = false;
  364. }
  365. $template['HOT'] = (bool) $template['HOT'];
  366. // default message code is MAIL
  367. if (!isset($template['MESSAGE_CODE']) || !$template['MESSAGE_CODE'])
  368. {
  369. $template['MESSAGE_CODE'] = Message\iBase::CODE_MAIL;
  370. }
  371. // compatibility
  372. if ($template['MESSAGE_CODE'] === Message\iBase::CODE_MAIL)
  373. {
  374. $template['IS_SUPPORT_BLOCK_EDITOR'] = $template['FIELDS']['MESSAGE']['ON_DEMAND'];
  375. }
  376. return $template;
  377. }
  378. }