Container.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Form;
  7. /**
  8. * Backend form container block
  9. *
  10. * @api
  11. * @deprecated 100.2.0 in favour of UI component implementation
  12. * @SuppressWarnings(PHPMD.NumberOfChildren)
  13. * @since 100.0.2
  14. */
  15. class Container extends \Magento\Backend\Block\Widget\Container
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_objectId = 'id';
  21. /**
  22. * @var string[]
  23. */
  24. protected $_formScripts = [];
  25. /**
  26. * @var string[]
  27. */
  28. protected $_formInitScripts = [];
  29. /**
  30. * @var string
  31. */
  32. protected $_mode = 'edit';
  33. /**
  34. * @var string
  35. */
  36. protected $_blockGroup = 'Magento_Backend';
  37. /**
  38. * @var string
  39. */
  40. const PARAM_BLOCK_GROUP = 'block_group';
  41. /**
  42. * @var string
  43. */
  44. const PARAM_MODE = 'mode';
  45. /**
  46. * @var string
  47. */
  48. protected $_template = 'Magento_Backend::widget/form/container.phtml';
  49. /**
  50. * Initialize form.
  51. *
  52. * @return void
  53. */
  54. protected function _construct()
  55. {
  56. parent::_construct();
  57. if ($this->hasData(self::PARAM_BLOCK_GROUP)) {
  58. $this->_blockGroup = $this->_getData(self::PARAM_BLOCK_GROUP);
  59. }
  60. if ($this->hasData(self::PARAM_MODE)) {
  61. $this->_mode = $this->_getData(self::PARAM_MODE);
  62. }
  63. $this->addButton(
  64. 'back',
  65. [
  66. 'label' => __('Back'),
  67. 'onclick' => 'setLocation(\'' . $this->getBackUrl() . '\')',
  68. 'class' => 'back'
  69. ],
  70. -1
  71. );
  72. $this->addButton(
  73. 'reset',
  74. ['label' => __('Reset'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
  75. -1
  76. );
  77. $objId = (int)$this->getRequest()->getParam($this->_objectId);
  78. if (!empty($objId)) {
  79. $this->addButton(
  80. 'delete',
  81. [
  82. 'label' => __('Delete'),
  83. 'class' => 'delete',
  84. 'onclick' => 'deleteConfirm(\'' . __(
  85. 'Are you sure you want to do this?'
  86. ) . '\', \'' . $this->getDeleteUrl() . '\', {data: {}})'
  87. ]
  88. );
  89. }
  90. $this->addButton(
  91. 'save',
  92. [
  93. 'label' => __('Save'),
  94. 'class' => 'save primary',
  95. 'data_attribute' => [
  96. 'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
  97. ]
  98. ],
  99. 1
  100. );
  101. }
  102. /**
  103. * Create form block
  104. *
  105. * @return $this
  106. */
  107. protected function _prepareLayout()
  108. {
  109. if ($this->_blockGroup && $this->_controller && $this->_mode && !$this->_layout->getChildName(
  110. $this->_nameInLayout,
  111. 'form'
  112. )
  113. ) {
  114. $this->addChild('form', $this->_buildFormClassName());
  115. }
  116. return parent::_prepareLayout();
  117. }
  118. /**
  119. * Build child form class name
  120. *
  121. * @return string
  122. */
  123. protected function _buildFormClassName()
  124. {
  125. return $this->nameBuilder->buildClassName(
  126. [$this->_blockGroup, 'Block', $this->_controller, $this->_mode, 'Form']
  127. );
  128. }
  129. /**
  130. * Get URL for back (reset) button
  131. *
  132. * @return string
  133. */
  134. public function getBackUrl()
  135. {
  136. return $this->getUrl('*/*/');
  137. }
  138. /**
  139. * Get URL for delete button.
  140. *
  141. * @return string
  142. */
  143. public function getDeleteUrl()
  144. {
  145. return $this->getUrl('*/*/delete', [$this->_objectId => (int)$this->getRequest()->getParam($this->_objectId)]);
  146. }
  147. /**
  148. * Get form save URL
  149. *
  150. * @see getFormActionUrl()
  151. * @return string
  152. */
  153. public function getSaveUrl()
  154. {
  155. return $this->getFormActionUrl();
  156. }
  157. /**
  158. * Get form action URL
  159. *
  160. * @return string
  161. */
  162. public function getFormActionUrl()
  163. {
  164. if ($this->hasFormActionUrl()) {
  165. return $this->getData('form_action_url');
  166. }
  167. return $this->getUrl('*/*/save');
  168. }
  169. /**
  170. * Get form HTML.
  171. *
  172. * @return string
  173. */
  174. public function getFormHtml()
  175. {
  176. $this->getChildBlock('form')->setData('action', $this->getSaveUrl());
  177. return $this->getChildHtml('form');
  178. }
  179. /**
  180. * Get form init scripts.
  181. *
  182. * @return string
  183. */
  184. public function getFormInitScripts()
  185. {
  186. if (!empty($this->_formInitScripts) && is_array($this->_formInitScripts)) {
  187. return '<script>' . implode("\n", $this->_formInitScripts) . '</script>';
  188. }
  189. return '';
  190. }
  191. /**
  192. * Get form scripts.
  193. *
  194. * @return string
  195. */
  196. public function getFormScripts()
  197. {
  198. if (!empty($this->_formScripts) && is_array($this->_formScripts)) {
  199. return '<script>' . implode("\n", $this->_formScripts) . '</script>';
  200. }
  201. return '';
  202. }
  203. /**
  204. * Get header width.
  205. *
  206. * @return string
  207. */
  208. public function getHeaderWidth()
  209. {
  210. return '';
  211. }
  212. /**
  213. * Get header css class.
  214. *
  215. * @return string
  216. */
  217. public function getHeaderCssClass()
  218. {
  219. return 'icon-head head-' . strtr($this->_controller, '_', '-');
  220. }
  221. /**
  222. * Get header HTML.
  223. *
  224. * @return string
  225. */
  226. public function getHeaderHtml()
  227. {
  228. return '<h3 class="' . $this->getHeaderCssClass() . '">' . $this->getHeaderText() . '</h3>';
  229. }
  230. /**
  231. * Set data object and pass it to form
  232. *
  233. * @param \Magento\Framework\DataObject $object
  234. * @return $this
  235. */
  236. public function setDataObject($object)
  237. {
  238. $this->getChildBlock('form')->setDataObject($object);
  239. return $this->setData('data_object', $object);
  240. }
  241. }