Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Template.php
Go to the documentation of this file.
1 <?php
7 
39 {
46  protected $_mail;
47 
53  protected $_storeManager;
54 
60  protected $_request;
61 
67  protected $_filterFactory;
68 
88  public function __construct(
89  \Magento\Framework\Model\Context $context,
90  \Magento\Framework\View\DesignInterface $design,
91  \Magento\Framework\Registry $registry,
92  \Magento\Store\Model\App\Emulation $appEmulation,
93  \Magento\Store\Model\StoreManagerInterface $storeManager,
94  \Magento\Framework\View\Asset\Repository $assetRepo,
95  \Magento\Framework\Filesystem $filesystem,
96  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
97  \Magento\Email\Model\Template\Config $emailConfig,
98  \Magento\Email\Model\TemplateFactory $templateFactory,
99  \Magento\Framework\Filter\FilterManager $filterManager,
100  \Magento\Framework\UrlInterface $urlModel,
101  \Magento\Framework\App\RequestInterface $request,
102  \Magento\Newsletter\Model\Template\FilterFactory $filterFactory,
103  array $data = []
104  ) {
105  parent::__construct(
106  $context,
107  $design,
108  $registry,
111  $assetRepo,
112  $filesystem,
113  $scopeConfig,
114  $emailConfig,
117  $urlModel,
118  $data
119  );
120  $this->_storeManager = $storeManager;
121  $this->_request = $request;
122  $this->_filterFactory = $filterFactory;
123  }
124 
130  protected function _construct()
131  {
132  $this->_init(\Magento\Newsletter\Model\ResourceModel\Template::class);
133  }
134 
141  public function validate()
142  {
143  $validators = [
144  'template_code' => [\Zend_Filter_Input::ALLOW_EMPTY => false],
145  'template_type' => 'Int',
146  'template_sender_email' => 'EmailAddress',
147  'template_sender_name' => [\Zend_Filter_Input::ALLOW_EMPTY => false],
148  ];
149  $data = [];
150  foreach (array_keys($validators) as $validateField) {
151  $data[$validateField] = $this->getDataUsingMethod($validateField);
152  }
153 
154  $validateInput = new \Zend_Filter_Input([], $validators, $data);
155  if (!$validateInput->isValid()) {
156  $errorMessages = [];
157  foreach ($validateInput->getMessages() as $messages) {
158  if (is_array($messages)) {
159  foreach ($messages as $message) {
160  $errorMessages[] = $message;
161  }
162  } else {
163  $errorMessages[] = $messages;
164  }
165  }
166 
167  throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $errorMessages)));
168  }
169  }
170 
176  public function beforeSave()
177  {
178  $this->validate();
179  parent::beforeSave();
180  return $this;
181  }
182 
188  public function getType()
189  {
190  return $this->getTemplateType();
191  }
192 
199  public function getProcessedTemplateSubject(array $variables)
200  {
201  $variables['this'] = $this;
202 
203  return $this->getTemplateFilter()
204  ->setVariables($variables)
205  ->filter($this->getTemplateSubject());
206  }
207 
213  public function getTemplateText()
214  {
215  if (!$this->getData('template_text') && !$this->getId()) {
216  $this->setData(
217  'template_text',
218  __(
219  'Follow this link to unsubscribe <!-- This tag is for unsubscribe link -->' .
220  '<a href="{{var subscriber.getUnsubscriptionLink()}}">{{var subscriber.getUnsubscriptionLink()}}' .
221  '</a>'
222  )
223  );
224  }
225 
226  return $this->getData('template_text');
227  }
228 
232  protected function getFilterFactory()
233  {
234  return $this->_filterFactory;
235  }
236 
242  public function isValidForSend()
243  {
244  return $this->getTemplateSenderName() && $this->getTemplateSenderEmail() && $this->getTemplateSubject();
245  }
246 }
getData($key='', $index=null)
Definition: DataObject.php:119
getDataUsingMethod($key, $args=null)
Definition: DataObject.php:218
__()
Definition: __.php:13
$message
getProcessedTemplateSubject(array $variables)
Definition: Template.php:199
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\View\DesignInterface $design, \Magento\Framework\Registry $registry, \Magento\Store\Model\App\Emulation $appEmulation, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Email\Model\Template\Config $emailConfig, \Magento\Email\Model\TemplateFactory $templateFactory, \Magento\Framework\Filter\FilterManager $filterManager, \Magento\Framework\UrlInterface $urlModel, \Magento\Framework\App\RequestInterface $request, \Magento\Newsletter\Model\Template\FilterFactory $filterFactory, array $data=[])
Definition: Template.php:88
const ALLOW_EMPTY
Definition: Input.php:46