Edit.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Block;
  7. /**
  8. * Block for URL rewrites edit page
  9. */
  10. class Edit extends \Magento\Backend\Block\Widget\Container
  11. {
  12. /**
  13. * @var \Magento\UrlRewrite\Block\Selector
  14. */
  15. private $_selectorBlock;
  16. /**
  17. * Part for building some blocks names
  18. *
  19. * @var string
  20. */
  21. protected $_controller = 'url_rewrite';
  22. /**
  23. * Generated buttons html cache
  24. *
  25. * @var string
  26. */
  27. protected $_buttonsHtml;
  28. /**
  29. * Adminhtml data
  30. *
  31. * @var \Magento\Backend\Helper\Data
  32. */
  33. protected $_adminhtmlData = null;
  34. /**
  35. * @var \Magento\UrlRewrite\Model\UrlRewriteFactory
  36. */
  37. protected $_rewriteFactory;
  38. /**
  39. * @param \Magento\Backend\Block\Widget\Context $context
  40. * @param \Magento\UrlRewrite\Model\UrlRewriteFactory $rewriteFactory
  41. * @param \Magento\Backend\Helper\Data $adminhtmlData
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Backend\Block\Widget\Context $context,
  46. \Magento\UrlRewrite\Model\UrlRewriteFactory $rewriteFactory,
  47. \Magento\Backend\Helper\Data $adminhtmlData,
  48. array $data = []
  49. ) {
  50. $this->_rewriteFactory = $rewriteFactory;
  51. $this->_adminhtmlData = $adminhtmlData;
  52. parent::__construct($context, $data);
  53. }
  54. /**
  55. * Prepare URL rewrite editing layout
  56. *
  57. * @return $this
  58. */
  59. protected function _prepareLayout()
  60. {
  61. $this->setTemplate('Magento_UrlRewrite::edit.phtml');
  62. $this->_addBackButton();
  63. $this->_prepareLayoutFeatures();
  64. return parent::_prepareLayout();
  65. }
  66. /**
  67. * Prepare featured blocks for layout of URL rewrite editing
  68. *
  69. * @return void
  70. */
  71. protected function _prepareLayoutFeatures()
  72. {
  73. if ($this->_getUrlRewrite()->getId()) {
  74. $this->_headerText = __('Edit URL Rewrite');
  75. } else {
  76. $this->_headerText = __('Add New URL Rewrite');
  77. }
  78. $this->_addUrlRewriteSelectorBlock();
  79. $this->_addEditFormBlock();
  80. }
  81. /**
  82. * Add child edit form block
  83. *
  84. * @return void
  85. */
  86. protected function _addEditFormBlock()
  87. {
  88. $this->setChild('form', $this->_createEditFormBlock());
  89. if ($this->_getUrlRewrite()->getId()) {
  90. $this->_addResetButton();
  91. $this->_addDeleteButton();
  92. }
  93. $this->_addSaveButton();
  94. }
  95. /**
  96. * Add reset button
  97. *
  98. * @return void
  99. */
  100. protected function _addResetButton()
  101. {
  102. $this->addButton(
  103. 'reset',
  104. [
  105. 'label' => __('Reset'),
  106. 'onclick' => 'location.reload();',
  107. 'class' => 'scalable',
  108. 'level' => -1
  109. ]
  110. );
  111. }
  112. /**
  113. * Add back button
  114. *
  115. * @return void
  116. */
  117. protected function _addBackButton()
  118. {
  119. $this->addButton(
  120. 'back',
  121. [
  122. 'label' => __('Back'),
  123. 'onclick' => 'setLocation(\'' . $this->_adminhtmlData->getUrl('adminhtml/*/') . '\')',
  124. 'class' => 'back',
  125. 'level' => -1
  126. ]
  127. );
  128. }
  129. /**
  130. * Update Back button location link
  131. *
  132. * @param string $link
  133. * @return void
  134. */
  135. protected function _updateBackButtonLink($link)
  136. {
  137. $this->updateButton('back', 'onclick', 'setLocation(\'' . $link . '\')');
  138. }
  139. /**
  140. * Add delete button
  141. *
  142. * @return void
  143. */
  144. protected function _addDeleteButton()
  145. {
  146. $this->addButton(
  147. 'delete',
  148. [
  149. 'label' => __('Delete'),
  150. 'onclick' => 'deleteConfirm(' . json_encode(__('Are you sure you want to do this?'))
  151. . ','
  152. . json_encode(
  153. $this->_adminhtmlData->getUrl(
  154. 'adminhtml/*/delete',
  155. ['id' => $this->getUrlRewrite()->getId()]
  156. )
  157. )
  158. . ', {data: {}})',
  159. 'class' => 'scalable delete',
  160. 'level' => -1
  161. ]
  162. );
  163. }
  164. /**
  165. * Add save button
  166. *
  167. * @return void
  168. */
  169. protected function _addSaveButton()
  170. {
  171. $this->addButton(
  172. 'save',
  173. [
  174. 'label' => __('Save'),
  175. 'class' => 'save primary save-url-rewrite',
  176. 'level' => -1,
  177. 'data_attribute' => [
  178. 'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
  179. ]
  180. ]
  181. );
  182. }
  183. /**
  184. * Creates edit form block
  185. *
  186. * @return \Magento\UrlRewrite\Block\Edit\Form
  187. */
  188. protected function _createEditFormBlock()
  189. {
  190. return $this->getLayout()->createBlock(
  191. \Magento\UrlRewrite\Block\Edit\Form::class,
  192. '',
  193. ['data' => ['url_rewrite' => $this->_getUrlRewrite()]]
  194. );
  195. }
  196. /**
  197. * Add child URL rewrite selector block
  198. *
  199. * @return void
  200. */
  201. protected function _addUrlRewriteSelectorBlock()
  202. {
  203. $this->setChild('selector', $this->_getSelectorBlock());
  204. }
  205. /**
  206. * Get selector block
  207. *
  208. * @return \Magento\UrlRewrite\Block\Selector
  209. */
  210. private function _getSelectorBlock()
  211. {
  212. if (!$this->_selectorBlock) {
  213. $this->_selectorBlock = $this->getLayout()->createBlock(\Magento\UrlRewrite\Block\Selector::class);
  214. }
  215. return $this->_selectorBlock;
  216. }
  217. /**
  218. * Get container buttons HTML
  219. *
  220. * Since buttons are set as children, we remove them as children after generating them
  221. * not to duplicate them in future
  222. *
  223. * @param string|null $area
  224. * @return string
  225. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  226. */
  227. public function getButtonsHtml($area = null)
  228. {
  229. if (null === $this->_buttonsHtml) {
  230. $this->_buttonsHtml = parent::getButtonsHtml();
  231. $layout = $this->getLayout();
  232. foreach ($this->getChildNames() as $name) {
  233. $alias = $layout->getElementAlias($name);
  234. if (false !== strpos($alias, '_button')) {
  235. $layout->unsetChild($this->getNameInLayout(), $alias);
  236. }
  237. }
  238. }
  239. return $this->_buttonsHtml;
  240. }
  241. /**
  242. * Get or create new instance of URL rewrite
  243. *
  244. * @return \Magento\UrlRewrite\Model\UrlRewrite
  245. */
  246. protected function _getUrlRewrite()
  247. {
  248. if (!$this->hasData('url_rewrite')) {
  249. $this->setUrlRewrite($this->_rewriteFactory->create());
  250. }
  251. return $this->getUrlRewrite();
  252. }
  253. }