Config.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * High-level interface for email templates data that hides format from the client code
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Email\Model\Template;
  9. use Magento\Framework\Filesystem\Directory\ReadFactory;
  10. use Magento\Framework\View\Design\Theme\ThemePackageList;
  11. class Config implements \Magento\Framework\Mail\Template\ConfigInterface
  12. {
  13. /**
  14. * @var \Magento\Email\Model\Template\Config\Data
  15. */
  16. protected $_dataStorage;
  17. /**
  18. * @var \Magento\Framework\Module\Dir\Reader
  19. */
  20. protected $_moduleReader;
  21. /**
  22. * @var \Magento\Framework\Filesystem
  23. */
  24. protected $fileSystem;
  25. /**
  26. * @var \Magento\Framework\View\FileSystem
  27. */
  28. protected $viewFileSystem;
  29. /**
  30. * @var ReadFactory
  31. */
  32. private $readDirFactory;
  33. /**
  34. * @var ThemePackageList
  35. */
  36. private $themePackages;
  37. /**
  38. * @param \Magento\Email\Model\Template\Config\Data $dataStorage
  39. * @param \Magento\Framework\Module\Dir\Reader $moduleReader
  40. * @param \Magento\Framework\View\FileSystem $viewFileSystem
  41. * @param ThemePackageList $themePackages
  42. * @param ReadFactory $readDirFactory
  43. */
  44. public function __construct(
  45. \Magento\Email\Model\Template\Config\Data $dataStorage,
  46. \Magento\Framework\Module\Dir\Reader $moduleReader,
  47. \Magento\Framework\View\FileSystem $viewFileSystem,
  48. ThemePackageList $themePackages,
  49. ReadFactory $readDirFactory
  50. ) {
  51. $this->_dataStorage = $dataStorage;
  52. $this->_moduleReader = $moduleReader;
  53. $this->viewFileSystem = $viewFileSystem;
  54. $this->themePackages = $themePackages;
  55. $this->readDirFactory = $readDirFactory;
  56. }
  57. /**
  58. * Return list of all email templates, both default module and theme-specific templates
  59. *
  60. * @return array[]
  61. */
  62. public function getAvailableTemplates()
  63. {
  64. $templates = [];
  65. foreach (array_keys($this->_dataStorage->get()) as $templateId) {
  66. $templates[] = [
  67. 'value' => $templateId,
  68. 'label' => $this->getTemplateLabel($templateId),
  69. 'group' => $this->getTemplateModule($templateId),
  70. ];
  71. $themeTemplates = $this->getThemeTemplates($templateId);
  72. $templates = array_merge($templates, $themeTemplates);
  73. }
  74. return $templates;
  75. }
  76. /**
  77. * Find all theme-based email templates for a given template ID
  78. *
  79. * @param string $templateId
  80. * @return array[]
  81. */
  82. public function getThemeTemplates($templateId)
  83. {
  84. $templates = [];
  85. $area = $this->getTemplateArea($templateId);
  86. $module = $this->getTemplateModule($templateId);
  87. $filename = $this->_getInfo($templateId, 'file');
  88. foreach ($this->themePackages->getThemes() as $theme) {
  89. if ($theme->getArea() == $area) {
  90. $themeDir = $this->readDirFactory->create($theme->getPath());
  91. $file = "$module/email/$filename";
  92. if ($themeDir->isExist($file)) {
  93. $templates[] = [
  94. 'value' => sprintf(
  95. '%s/%s/%s',
  96. $templateId,
  97. $theme->getVendor(),
  98. $theme->getName()
  99. ),
  100. 'label' => sprintf(
  101. '%s (%s/%s)',
  102. $this->getTemplateLabel($templateId),
  103. $theme->getVendor(),
  104. $theme->getName()
  105. ),
  106. 'group' => $this->getTemplateModule($templateId),
  107. ];
  108. }
  109. }
  110. }
  111. return $templates;
  112. }
  113. /**
  114. * Parses a template ID and returns an array of templateId and theme
  115. *
  116. * @param string $templateId
  117. * @return array an array of array('templateId' => '...', 'theme' => '...')
  118. */
  119. public function parseTemplateIdParts($templateId)
  120. {
  121. $parts = [
  122. 'templateId' => $templateId,
  123. 'theme' => null
  124. ];
  125. $pattern = "#^(?<templateId>[^/]+)/(?<themeVendor>[^/]+)/(?<themeName>[^/]+)#i";
  126. if (preg_match($pattern, $templateId, $matches)) {
  127. $parts['templateId'] = $matches['templateId'];
  128. $parts['theme'] = $matches['themeVendor'] . '/' . $matches['themeName'];
  129. }
  130. return $parts;
  131. }
  132. /**
  133. * Retrieve translated label of an email template
  134. *
  135. * @param string $templateId
  136. * @return \Magento\Framework\Phrase
  137. */
  138. public function getTemplateLabel($templateId)
  139. {
  140. return __($this->_getInfo($templateId, 'label'));
  141. }
  142. /**
  143. * Retrieve type of an email template
  144. *
  145. * @param string $templateId
  146. * @return string
  147. */
  148. public function getTemplateType($templateId)
  149. {
  150. return $this->_getInfo($templateId, 'type');
  151. }
  152. /**
  153. * Retrieve fully-qualified name of a module an email template belongs to
  154. *
  155. * @param string $templateId
  156. * @return string
  157. */
  158. public function getTemplateModule($templateId)
  159. {
  160. return $this->_getInfo($templateId, 'module');
  161. }
  162. /**
  163. * Retrieve the area an email template belongs to
  164. *
  165. * @param string $templateId
  166. * @return string
  167. */
  168. public function getTemplateArea($templateId)
  169. {
  170. return $this->_getInfo($templateId, 'area');
  171. }
  172. /**
  173. * Retrieve full path to an email template file
  174. *
  175. * @param string $templateId
  176. * @param array|null $designParams
  177. * @return string
  178. */
  179. public function getTemplateFilename($templateId, $designParams = [])
  180. {
  181. // If design params aren't passed, then use area/module defined in email_templates.xml
  182. if (!isset($designParams['area'])) {
  183. $designParams['area'] = $this->getTemplateArea($templateId);
  184. }
  185. $module = $this->getTemplateModule($templateId);
  186. $designParams['module'] = $module;
  187. $file = $this->_getInfo($templateId, 'file');
  188. $filename = $this->getFilename($file, $designParams, $module);
  189. return $filename;
  190. }
  191. /**
  192. * Retrieve value of a field of an email template
  193. *
  194. * @param string $templateId Name of an email template
  195. * @param string $fieldName Name of a field value of which to return
  196. * @return string
  197. * @throws \UnexpectedValueException
  198. */
  199. protected function _getInfo($templateId, $fieldName)
  200. {
  201. $data = $this->_dataStorage->get();
  202. if (!isset($data[$templateId])) {
  203. throw new \UnexpectedValueException("Email template '{$templateId}' is not defined.");
  204. }
  205. if (!isset($data[$templateId][$fieldName])) {
  206. throw new \UnexpectedValueException(
  207. "Field '{$fieldName}' is not defined for email template '{$templateId}'."
  208. );
  209. }
  210. return $data[$templateId][$fieldName];
  211. }
  212. /**
  213. * Retrieve template file path.
  214. *
  215. * @param string $file
  216. * @param array $designParams
  217. * @param string $module
  218. *
  219. * @return string
  220. *
  221. * @throws \UnexpectedValueException
  222. */
  223. private function getFilename(string $file, array $designParams, string $module): string
  224. {
  225. $filename = $this->viewFileSystem->getEmailTemplateFileName($file, $designParams, $module);
  226. if ($filename === false) {
  227. throw new \UnexpectedValueException("Template file '{$file}' is not found.");
  228. }
  229. return $filename;
  230. }
  231. }