Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Edit.php
Go to the documentation of this file.
1 <?php
7 
11 
19 class Edit extends Widget implements ContainerInterface
20 {
25  protected $_registryManager;
26 
30  protected $_menuConfig;
31 
35  protected $_configStructure;
36 
40  private $_emailConfig;
41 
47  protected $_template = 'Magento_Email::template/edit.phtml';
48 
52  protected $_jsonEncoder;
53 
57  protected $jsonHelper;
58 
62  protected $buttonList;
63 
67  protected $toolbar;
68 
83  public function __construct(
84  \Magento\Backend\Block\Template\Context $context,
85  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
86  \Magento\Framework\Registry $registry,
87  \Magento\Backend\Model\Menu\Config $menuConfig,
88  \Magento\Config\Model\Config\Structure $configStructure,
89  \Magento\Email\Model\Template\Config $emailConfig,
90  \Magento\Framework\Json\Helper\Data $jsonHelper,
91  \Magento\Backend\Block\Widget\Button\ButtonList $buttonList,
92  \Magento\Backend\Block\Widget\Button\ToolbarInterface $toolbar,
93  array $data = []
94  ) {
95  $this->jsonHelper = $jsonHelper;
96  $this->_jsonEncoder = $jsonEncoder;
97  $this->_registryManager = $registry;
98  $this->_menuConfig = $menuConfig;
99  $this->_configStructure = $configStructure;
100  $this->_emailConfig = $emailConfig;
101  $this->buttonList = $buttonList;
102  $this->toolbar = $toolbar;
103  parent::__construct($context, $data);
104  }
105 
109  public function updateButton($buttonId, $key, $data)
110  {
111  $this->buttonList->update($buttonId, $key, $data);
112  return $this;
113  }
114 
118  public function canRender(\Magento\Backend\Block\Widget\Button\Item $item)
119  {
120  return !$item->isDeleted();
121  }
122 
126  public function removeButton($buttonId)
127  {
128  $this->buttonList->remove($buttonId);
129  return $this;
130  }
131 
138  protected function _prepareLayout()
139  {
140  $this->buttonList->add(
141  'back',
142  [
143  'label' => __('Back'),
144  'onclick' => "window.location.href = '" . $this->getUrl('adminhtml/*') . "'",
145  'class' => 'back'
146  ]
147  );
148  $this->buttonList->add(
149  'reset',
150  ['label' => __('Reset'), 'onclick' => 'window.location.href = window.location.href']
151  );
152 
153  if ($this->getEditMode()) {
154  $this->buttonList->add(
155  'delete',
156  [
157  'label' => __('Delete Template'),
158  'data_attribute' => [
159  'role' => 'template-delete',
160  ],
161  'class' => 'delete'
162  ]
163  );
164  }
165  if (!$this->isTextType()) {
166  $this->buttonList->add(
167  'to_plain',
168  [
169  'label' => __('Convert to Plain Text'),
170  'data_attribute' => [
171  'role' => 'template-strip',
172  ],
173  'id' => 'convert_button'
174  ]
175  );
176  $this->buttonList->add(
177  'to_html',
178  [
179  'label' => __('Return Html Version'),
180  'data_attribute' => [
181  'role' => 'template-unstrip',
182  ],
183  'id' => 'convert_button_back',
184  'style' => 'display:none'
185  ]
186  );
187  }
188  $this->buttonList->add(
189  'preview',
190  [
191  'label' => __('Preview Template'),
192  'data_attribute' => [
193  'role' => 'template-preview',
194  ]
195  ]
196  );
197  $this->buttonList->add(
198  'save',
199  [
200  'label' => __('Save Template'),
201  'data_attribute' => [
202  'role' => 'template-save',
203  ],
204  'class' => 'save primary save-template'
205  ]
206  );
207  $this->buttonList->add(
208  'load',
209  [
210  'label' => __('Load Template'),
211  'data_attribute' => [
212  'role' => 'template-load',
213  ],
214  'type' => 'button',
215  'class' => 'save'
216  ],
217  0,
218  0,
219  null
220  );
221  $this->toolbar->pushButtons($this, $this->buttonList);
222  $this->addChild(
223  'form',
224  \Magento\Email\Block\Adminhtml\Template\Edit\Form::class,
225  [
226  'email_template' => $this->getEmailTemplate()
227  ]
228  );
229  return parent::_prepareLayout();
230  }
231 
235  public function addButton($buttonId, $data, $level = 0, $sortOrder = 0, $region = 'toolbar')
236  {
237  $this->buttonList->add($buttonId, $data, $level, $sortOrder, $region);
238  return $this;
239  }
240 
246  protected function _beforeToHtml()
247  {
248  $groupedOptions = [];
249  foreach ($this->_getDefaultTemplatesAsOptionsArray() as $option) {
250  $groupedOptions[$option['group']][] = $option;
251  }
252  ksort($groupedOptions);
253  $this->setData('template_options', $groupedOptions);
254 
255  return parent::_beforeToHtml();
256  }
257 
264  {
265  $options = array_merge(
266  [['value' => '', 'label' => '', 'group' => '']],
267  $this->_emailConfig->getAvailableTemplates()
268  );
269  uasort(
270  $options,
271  function (array $firstElement, array $secondElement) {
272  return strcmp($firstElement['label'], $secondElement['label']);
273  }
274  );
275  return $options;
276  }
277 
283  public function getLoadButtonHtml()
284  {
285  return $this->getChildHtml('load_button');
286  }
287 
293  public function getEditMode()
294  {
295  return $this->getEmailTemplate()->getId();
296  }
297 
303  public function getHeaderText()
304  {
305  if ($this->getEditMode()) {
306  return __('Edit Email Template');
307  }
308  return __('New Email Template');
309  }
310 
316  public function getFormHtml()
317  {
318  return $this->getChildHtml('form');
319  }
320 
326  public function getSaveUrl()
327  {
328  return $this->getUrl('adminhtml/*/save', ['_current' => true]);
329  }
330 
336  public function getPreviewUrl()
337  {
338  return $this->getUrl('adminhtml/*/preview');
339  }
340 
346  public function isTextType()
347  {
348  return $this->getEmailTemplate()->isPlain();
349  }
350 
356  public function getTemplateType()
357  {
358  return $this->getEmailTemplate()->getType();
359  }
360 
366  public function getDeleteUrl()
367  {
368  return $this->getUrl('adminhtml/*/delete', ['_current' => true]);
369  }
370 
376  public function getEmailTemplate()
377  {
378  return $this->getData('email_template');
379  }
380 
386  public function getLoadUrl()
387  {
388  return $this->getUrl('adminhtml/*/defaultTemplate');
389  }
390 
397  public function getCurrentlyUsedForPaths($asJSON = true)
398  {
400  $template = $this->getEmailTemplate();
401  $paths = $template->getSystemConfigPathsWhereCurrentlyUsed();
402  $pathsParts = $this->_getSystemConfigPathsParts($paths);
403  if ($asJSON) {
404  return $this->_jsonEncoder->encode($pathsParts);
405  }
406  return $pathsParts;
407  }
408 
416  protected function _getSystemConfigPathsParts($paths)
417  {
418  $result = $urlParams = $prefixParts = [];
419  $scopeLabel = __('Default Config');
420  if ($paths) {
422  $menu = $this->_menuConfig->getMenu();
423  $item = $menu->get('Magento_Backend::stores');
424  // create prefix path parts
425  $prefixParts[] = ['title' => __($item->getTitle())];
426  $item = $menu->get('Magento_Config::system_config');
427  $prefixParts[] = [
428  'title' => __($item->getTitle()),
429  'url' => $this->getUrl('adminhtml/system_config/'),
430  ];
431 
432  $pathParts = $prefixParts;
433  foreach ($paths as $pathData) {
434  $pathDataParts = explode('/', $pathData['path']);
435  $sectionName = array_shift($pathDataParts);
436 
437  $urlParams = ['section' => $sectionName];
438  if (isset($pathData['scope']) && isset($pathData['scope_id'])) {
439  switch ($pathData['scope']) {
440  case 'stores':
441  $store = $this->_storeManager->getStore($pathData['scope_id']);
442  if ($store) {
443  $urlParams['website'] = $store->getWebsite()->getCode();
444  $urlParams['store'] = $store->getCode();
445  $scopeLabel = $store->getWebsite()->getName() . '/' . $store->getName();
446  }
447  break;
448  case 'websites':
449  $website = $this->_storeManager->getWebsite($pathData['scope_id']);
450  if ($website) {
451  $urlParams['website'] = $website->getCode();
452  $scopeLabel = $website->getName();
453  }
454  break;
455  default:
456  break;
457  }
458  }
459  $pathParts[] = [
460  'title' => $this->_configStructure->getElement($sectionName)->getLabel(),
461  'url' => $this->getUrl('adminhtml/system_config/edit', $urlParams),
462  ];
463  $elementPathParts = [$sectionName];
464  while (count($pathDataParts) != 1) {
465  $elementPathParts[] = array_shift($pathDataParts);
466  $pathParts[] = [
467  'title' => $this->_configStructure->getElementByPathParts($elementPathParts)->getLabel(),
468  ];
469  }
470  $elementPathParts[] = array_shift($pathDataParts);
471  $pathParts[] = [
472  'title' => $this->_configStructure->getElementByPathParts($elementPathParts)->getLabel(),
473  'scope' => $scopeLabel,
474  ];
475  $result[] = $pathParts;
476  $pathParts = $prefixParts;
477  }
478  }
479  return $result;
480  }
481 
487  public function getOrigTemplateCode()
488  {
489  return $this->getEmailTemplate()->getOrigTemplateCode();
490  }
491 }
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\Registry $registry, \Magento\Backend\Model\Menu\Config $menuConfig, \Magento\Config\Model\Config\Structure $configStructure, \Magento\Email\Model\Template\Config $emailConfig, \Magento\Framework\Json\Helper\Data $jsonHelper, \Magento\Backend\Block\Widget\Button\ButtonList $buttonList, \Magento\Backend\Block\Widget\Button\ToolbarInterface $toolbar, array $data=[])
Definition: Edit.php:83
__()
Definition: __.php:13
updateButton($buttonId, $key, $data)
Definition: Edit.php:109
addButton($buttonId, $data, $level=0, $sortOrder=0, $region='toolbar')
Definition: Edit.php:235
setData($key, $value=null)
Definition: DataObject.php:72
canRender(\Magento\Backend\Block\Widget\Button\Item $item)
Definition: Edit.php:118
$paths
Definition: _bootstrap.php:83
$template
Definition: export.php:12