PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Model/Simplexml/Form/Abstract.php

https://bitbucket.org/dnejedly/eaparts
PHP | 524 lines | 225 code | 44 blank | 255 comment | 15 complexity | 81e9b12d7724b37a052943278a18d942 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * XmlConnect simple xml abstract from class
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Model_Simplexml_Form_Abstract extends Varien_Object
  34. {
  35. /**
  36. * Element unique id
  37. *
  38. * @var string
  39. */
  40. protected $_id;
  41. /**
  42. * Form level elements collection
  43. *
  44. * @var Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
  45. */
  46. protected $_elements;
  47. /**
  48. * Element type classes
  49. *
  50. * @var array
  51. */
  52. protected $_types = array();
  53. /**
  54. * From Simplexml object
  55. *
  56. * @var Mage_XmlConnect_Model_Simplexml_Element
  57. */
  58. protected $_xml;
  59. /**
  60. * Main element node
  61. *
  62. * @var string
  63. */
  64. protected $_mainNode = 'form';
  65. /**
  66. * Is name attribute required
  67. *
  68. * @var bool
  69. */
  70. protected $_nameRequired = true;
  71. /**
  72. * Custom attributes array
  73. *
  74. * @var array
  75. */
  76. protected $_customAttributes = array();
  77. /**
  78. * Init form model
  79. *
  80. * @param array $attributes
  81. */
  82. public function __construct($attributes = array())
  83. {
  84. parent::__construct($attributes);
  85. $this->_prepareXmlObject();
  86. }
  87. /**
  88. * Init form parent Simplexml object
  89. *
  90. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  91. */
  92. protected function _prepareXmlObject()
  93. {
  94. $this->setXmlObject(
  95. Mage::getModel('xmlconnect/simplexml_element', '<' . $this->_mainNode . '></' . $this->_mainNode . '>')
  96. );
  97. return $this;
  98. }
  99. /**
  100. * Get base simple xml object
  101. *
  102. * @return Mage_XmlConnect_Model_Simplexml_Element
  103. */
  104. public function getXmlObject()
  105. {
  106. return $this->_xml;
  107. }
  108. /**
  109. * Set simple xml object
  110. *
  111. * @param Mage_XmlConnect_Model_Simplexml_Element $xml
  112. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  113. */
  114. public function setXmlObject(Mage_XmlConnect_Model_Simplexml_Element $xml)
  115. {
  116. $this->_xml = $xml;
  117. return $this;
  118. }
  119. /**
  120. * Get element id
  121. *
  122. * @return string
  123. */
  124. public function getId()
  125. {
  126. return $this->_id;
  127. }
  128. /**
  129. * Set element id
  130. *
  131. * @param $id
  132. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  133. */
  134. public function setId($id)
  135. {
  136. $this->_id = $id;
  137. $this->setData('xml_id', $id);
  138. return $this;
  139. }
  140. /**
  141. * Get element id
  142. *
  143. * @return string
  144. */
  145. public function getXmlId()
  146. {
  147. return $this->getXmlIdPrefix() . $this->getData('xml_id') . $this->getXmlIdSuffix();
  148. }
  149. /**
  150. * Add form element type
  151. *
  152. * @param string $type
  153. * @param string $className
  154. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  155. */
  156. public function addType($type, $className)
  157. {
  158. $this->_types[$type] = $className;
  159. return $this;
  160. }
  161. /**
  162. * Get elements collection
  163. *
  164. * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
  165. */
  166. public function getElements()
  167. {
  168. if (empty($this->_elements)) {
  169. $this->_elements = Mage::getModel('xmlconnect/simplexml_form_element_collection', $this);
  170. }
  171. return $this->_elements;
  172. }
  173. /**
  174. * Add form element
  175. *
  176. * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element
  177. * @param bool|string $after
  178. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  179. */
  180. public function addElement(Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element, $after = false)
  181. {
  182. $element->setForm($this);
  183. $this->getElements()->add($element, $after);
  184. return $this;
  185. }
  186. /**
  187. * Add child element
  188. *
  189. * if $after parameter is false - add element to the end of a collection
  190. * if $after parameter is ^ - prepend element to the beginning of a collection
  191. * if $after parameter is string - add element after the element with some id
  192. *
  193. * @param string $elementId
  194. * @param string $type
  195. * @param array $config
  196. * @param mixed $after
  197. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  198. */
  199. public function addField($elementId, $type, $config, $after = false)
  200. {
  201. if (isset($this->_types[$type])) {
  202. $className = $this->_types[$type];
  203. } else {
  204. $className = 'Mage_XmlConnect_Model_Simplexml_Form_Element_' . uc_words($type);
  205. }
  206. $element = Mage::getModel($className, $config);
  207. $element->setId($elementId);
  208. $this->addElement($element, $after);
  209. return $element;
  210. }
  211. /**
  212. * Remove element from collection
  213. *
  214. * @param string $elementId
  215. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  216. */
  217. public function removeField($elementId)
  218. {
  219. $this->getElements()->remove($elementId);
  220. return $this;
  221. }
  222. /**
  223. * Add fieldset element
  224. *
  225. * @param string $elementId
  226. * @param array $config
  227. * @param bool|string $after
  228. * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset
  229. */
  230. public function addFieldset($elementId, $config = array(), $after = false)
  231. {
  232. $element = Mage::getModel('xmlconnect/simplexml_form_element_fieldset', $config);
  233. $element->setId($elementId);
  234. $this->addElement($element, $after);
  235. return $element;
  236. }
  237. /**
  238. * Add validator element
  239. *
  240. * @param array $config
  241. * @param bool|string $after
  242. * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Validator
  243. */
  244. public function addValidator($config = array(), $after = false)
  245. {
  246. $element = Mage::getModel('xmlconnect/simplexml_form_element_validator', $config);
  247. $element->setId($this->getXmlId());
  248. $this->addElement($element, $after);
  249. return $element;
  250. }
  251. /**
  252. * Get array of existing elements
  253. *
  254. * @param array $arrAttributes
  255. * @return array
  256. */
  257. public function __toArray(array $arrAttributes = array())
  258. {
  259. $res = array();
  260. $res['config'] = $this->getData();
  261. $res['formElements']= array();
  262. foreach ($this->getElements() as $element) {
  263. $res['formElements'][] = $element->toArray();
  264. }
  265. return $res;
  266. }
  267. /**
  268. * Return allowed xml form attributes
  269. *
  270. * @return array
  271. */
  272. public function getXmlAttributes()
  273. {
  274. return array('enctype');
  275. }
  276. /**
  277. * Required form attribute array
  278. *
  279. * @return array
  280. */
  281. public function getRequiredXmlAttributes()
  282. {
  283. return array('action' => null, 'method' => 'post');
  284. }
  285. /**
  286. * Get after element xml
  287. *
  288. * @return array|Mage_XmlConnect_Model_Simplexml_Element
  289. */
  290. public function getAfterElementXml()
  291. {
  292. return $this->getData('after_element_xml');
  293. }
  294. /**
  295. * Get xml object attributes
  296. *
  297. * @param array $attributes
  298. * @return array
  299. */
  300. public function getXmlObjAttributes($attributes = array())
  301. {
  302. $data = array();
  303. if (empty($attributes)) {
  304. $attributes = array_keys($this->_data);
  305. }
  306. foreach ($this->_data as $key => $value) {
  307. if (in_array($key, $attributes)) {
  308. $data[$key] = $value;
  309. }
  310. }
  311. ksort($data);
  312. return $data;
  313. }
  314. /**
  315. * Get object attributes array
  316. *
  317. * @return array
  318. */
  319. public function getAttributes()
  320. {
  321. $attributes = array_merge($this->getXmlAttributes(), $this->getCustomAttributes());
  322. return $this->getXmlObjAttributes($attributes);
  323. }
  324. /**
  325. * Add after element xml to object
  326. *
  327. * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
  328. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  329. */
  330. public function addAfterXmlElementToObj(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
  331. {
  332. if ($this->_checkXmlInstance($this->getAfterElementXml())) {
  333. $xmlObj->appendChild($this->getAfterElementXml());
  334. } elseif (is_array($this->getAfterElementXml())) {
  335. foreach ($this->getAfterElementXml() as $afterElement) {
  336. if (!$this->_checkXmlInstance($afterElement)) {
  337. continue;
  338. }
  339. $xmlObj->appendChild($afterElement);
  340. }
  341. }
  342. return $this;
  343. }
  344. /**
  345. * Add required attributes to element
  346. *
  347. * @throws Mage_Core_Exception
  348. * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
  349. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  350. */
  351. protected function _addRequiredAttributes(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
  352. {
  353. $this->_addId($xmlObj);
  354. $this->_addName($xmlObj);
  355. foreach ($this->getRequiredXmlAttributes() as $attribute => $defValue) {
  356. $data = $this->getData($this->_underscore($attribute));
  357. if ($data) {
  358. $xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($data));
  359. } elseif(null !== $defValue){
  360. $xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($defValue));
  361. } else {
  362. Mage::throwException(Mage::helper('xmlconnect')->__('%s attribute is required.', $attribute));
  363. }
  364. }
  365. return $this;
  366. }
  367. /**
  368. * Add validator to element xml object
  369. *
  370. * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
  371. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  372. */
  373. protected function _addValidator(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
  374. {
  375. return $this;
  376. }
  377. /**
  378. * Add form id to element
  379. *
  380. * @throws Mage_Core_Exception
  381. * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
  382. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  383. */
  384. protected function _addId(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
  385. {
  386. if ($this->getXmlId()) {
  387. $xmlObj->addAttribute('id', $xmlObj->xmlAttribute($this->getXmlId()));
  388. } else {
  389. Mage::throwException(
  390. Mage::helper('xmlconnect')->__('"id" attribute is required for a "%s" field.', $this->getType())
  391. );
  392. }
  393. return $this;
  394. }
  395. /**
  396. * Add form name to element
  397. *
  398. * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
  399. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  400. */
  401. protected function _addName(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
  402. {
  403. if ($this->getName()) {
  404. $name = $this->getName();
  405. } elseif($this->getNameRequired()) {
  406. $name = $this->getXmlId();
  407. }
  408. if (isset($name)) {
  409. $xmlObj->addAttribute('name', $xmlObj->xmlAttribute($name));
  410. }
  411. return $this;
  412. }
  413. /**
  414. * Is object instance of Simplexml object
  415. *
  416. * @param $object
  417. * @return bool
  418. */
  419. protected function _checkXmlInstance($object)
  420. {
  421. return $object instanceof Mage_XmlConnect_Model_Simplexml_Element;
  422. }
  423. /**
  424. * Get is name required attribute
  425. *
  426. * @return boolean
  427. */
  428. public function getNameRequired()
  429. {
  430. return $this->_nameRequired;
  431. }
  432. /**
  433. * Set is name required attribute
  434. *
  435. * @param boolean $nameRequired
  436. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  437. */
  438. public function setNameRequired($nameRequired)
  439. {
  440. $this->_nameRequired = $nameRequired;
  441. return $this;
  442. }
  443. /**
  444. * Get custom attributes
  445. *
  446. * @return array
  447. */
  448. public function getCustomAttributes()
  449. {
  450. return $this->_customAttributes;
  451. }
  452. /**
  453. * Set custom attributes
  454. *
  455. * @param array $customAttributes
  456. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  457. */
  458. public function setCustomAttributes(array $customAttributes)
  459. {
  460. $this->_customAttributes = $customAttributes;
  461. return $this;
  462. }
  463. /**
  464. * Check value and return as array - attribute => value
  465. *
  466. * @param string $attribute
  467. * @param mixed $value
  468. * @return array
  469. */
  470. public function checkAttribute($attribute, $value = null)
  471. {
  472. if (null === $value) {
  473. $value = $this->getData($attribute);
  474. }
  475. if (null !== $value) {
  476. return array($attribute => $value);
  477. }
  478. return array();
  479. }
  480. }