AbstractForm.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Form;
  7. use Magento\Framework\Data\Form\Element\AbstractElement;
  8. use Magento\Framework\Data\Form\Element\Collection;
  9. use Magento\Framework\Data\Form\Element\CollectionFactory;
  10. use Magento\Framework\Data\Form\Element\Column;
  11. use Magento\Framework\Data\Form\Element\Factory;
  12. use Magento\Framework\Data\Form\Element\Fieldset;
  13. /**
  14. * Abstract class for form, column and fieldset
  15. *
  16. * @author Magento Core Team <core@magentocommerce.com>
  17. */
  18. class AbstractForm extends \Magento\Framework\DataObject
  19. {
  20. /**
  21. * Form level elements collection
  22. *
  23. * @var Collection
  24. */
  25. protected $_elements;
  26. /**
  27. * Element type classes
  28. *
  29. * @var array
  30. */
  31. protected $_types = [];
  32. /**
  33. * @var Factory
  34. */
  35. protected $_factoryElement;
  36. /**
  37. * @var CollectionFactory
  38. */
  39. protected $_factoryCollection;
  40. /**
  41. * @var array
  42. */
  43. protected $customAttributes = [];
  44. /**
  45. * @param Factory $factoryElement
  46. * @param CollectionFactory $factoryCollection
  47. * @param array $data
  48. */
  49. public function __construct(Factory $factoryElement, CollectionFactory $factoryCollection, $data = [])
  50. {
  51. $this->_factoryElement = $factoryElement;
  52. $this->_factoryCollection = $factoryCollection;
  53. parent::__construct($data);
  54. $this->_construct();
  55. }
  56. /**
  57. * Internal constructor, that is called from real constructor
  58. *
  59. * Please override this one instead of overriding real __construct constructor
  60. *
  61. * @return void
  62. */
  63. protected function _construct()
  64. {
  65. }
  66. /**
  67. * Add element type
  68. *
  69. * @param string $type
  70. * @param string $className
  71. * @return $this
  72. */
  73. public function addType($type, $className)
  74. {
  75. $this->_types[$type] = $className;
  76. return $this;
  77. }
  78. /**
  79. * Get elements collection
  80. *
  81. * @return Collection
  82. */
  83. public function getElements()
  84. {
  85. if (empty($this->_elements)) {
  86. $this->_elements = $this->_factoryCollection->create(['container' => $this]);
  87. }
  88. return $this->_elements;
  89. }
  90. /**
  91. * Disable elements
  92. *
  93. * @param boolean $readonly
  94. * @param boolean $useDisabled
  95. * @return $this
  96. */
  97. public function setReadonly($readonly, $useDisabled = false)
  98. {
  99. if ($useDisabled) {
  100. $this->setDisabled($readonly);
  101. $this->setData('readonly_disabled', $readonly);
  102. } else {
  103. $this->setData('readonly', $readonly);
  104. }
  105. foreach ($this->getElements() as $element) {
  106. $element->setReadonly($readonly, $useDisabled);
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Add form element
  112. *
  113. * @param AbstractElement $element
  114. * @param bool|string|null $after
  115. * @return $this
  116. */
  117. public function addElement(AbstractElement $element, $after = null)
  118. {
  119. $element->setForm($this);
  120. $this->getElements()->add($element, $after);
  121. return $this;
  122. }
  123. /**
  124. * Add child element
  125. *
  126. * if $after parameter is false - then element adds to end of collection
  127. * if $after parameter is null - then element adds to befin of collection
  128. * if $after parameter is string - then element adds after of the element with some id
  129. *
  130. * @param string $elementId
  131. * @param string $type
  132. * @param array $config
  133. * @param bool|string|null $after
  134. * @return AbstractElement
  135. */
  136. public function addField($elementId, $type, $config, $after = false)
  137. {
  138. if (isset($this->_types[$type])) {
  139. $type = $this->_types[$type];
  140. }
  141. $element = $this->_factoryElement->create($type, ['data' => $config]);
  142. $element->setId($elementId);
  143. $this->addElement($element, $after);
  144. return $element;
  145. }
  146. /**
  147. * Enter description here...
  148. *
  149. * @param string $elementId
  150. * @return $this
  151. */
  152. public function removeField($elementId)
  153. {
  154. $this->getElements()->remove($elementId);
  155. return $this;
  156. }
  157. /**
  158. * Add fieldset
  159. *
  160. * @param string $elementId
  161. * @param array $config
  162. * @param bool|string|null $after
  163. * @param bool $isAdvanced
  164. * @return Fieldset
  165. */
  166. public function addFieldset($elementId, $config, $after = false, $isAdvanced = false)
  167. {
  168. $element = $this->_factoryElement->create('fieldset', ['data' => $config]);
  169. $element->setId($elementId);
  170. $element->setAdvanced($isAdvanced);
  171. $this->addElement($element, $after);
  172. return $element;
  173. }
  174. /**
  175. * Add column element
  176. *
  177. * @param string $elementId
  178. * @param array $config
  179. * @return Column
  180. */
  181. public function addColumn($elementId, $config)
  182. {
  183. $element = $this->_factoryElement->create('column', ['data' => $config]);
  184. $element->setForm($this)->setId($elementId);
  185. $this->addElement($element);
  186. return $element;
  187. }
  188. /**
  189. * Convert elements to array
  190. *
  191. * @param array $arrAttributes
  192. * @return array
  193. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  194. */
  195. public function convertToArray(array $arrAttributes = [])
  196. {
  197. $res = [];
  198. $res['config'] = $this->getData();
  199. $res['formElements'] = [];
  200. foreach ($this->getElements() as $element) {
  201. $res['formElements'][] = $element->toArray();
  202. }
  203. return $res;
  204. }
  205. /**
  206. * Add custom attribute
  207. *
  208. * @param string $key
  209. * @param mixed $value
  210. * @return $this
  211. */
  212. public function addCustomAttribute($key, $value)
  213. {
  214. $this->customAttributes[$key] = $value;
  215. return $this;
  216. }
  217. /**
  218. * Convert data into string with defined keys and values
  219. *
  220. * @param array $keys
  221. * @param string $valueSeparator
  222. * @param string $fieldSeparator
  223. * @param string $quote
  224. * @return string
  225. */
  226. public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
  227. {
  228. $data = [];
  229. if (empty($keys)) {
  230. $keys = array_keys($this->_data);
  231. }
  232. $customAttributes = array_filter($this->customAttributes);
  233. $keys = array_merge($keys, array_keys(array_diff($this->customAttributes, $customAttributes)));
  234. foreach ($this->_data as $key => $value) {
  235. if (in_array($key, $keys)) {
  236. $data[] = $key . $valueSeparator . $quote . $value . $quote;
  237. }
  238. }
  239. foreach ($customAttributes as $key => $value) {
  240. $data[] = $key . $valueSeparator . $quote . $value . $quote;
  241. }
  242. return implode($fieldSeparator, $data);
  243. }
  244. }