PageRenderTime 2976ms CodeModel.GetById 33ms RepoModel.GetById 5ms app.codeStats 1ms

/app/code/core/Mage/Eav/Model/Form.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 553 lines | 262 code | 46 blank | 245 comment | 31 complexity | b2277c94324ca2850e7685f2b3dacf72 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_Eav
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * EAV Entity Form Model
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Eav_Model_Form
  34. {
  35. /**
  36. * Current module pathname
  37. *
  38. * @var string
  39. */
  40. protected $_moduleName = '';
  41. /**
  42. * Current EAV entity type code
  43. *
  44. * @var string
  45. */
  46. protected $_entityTypeCode = '';
  47. /**
  48. * Current store instance
  49. *
  50. * @var Mage_Core_Model_Store
  51. */
  52. protected $_store;
  53. /**
  54. * Current entity type instance
  55. *
  56. * @var Mage_Eav_Model_Entity_Type
  57. */
  58. protected $_entityType;
  59. /**
  60. * Current entity instance
  61. *
  62. * @var Mage_Core_Model_Abstract
  63. */
  64. protected $_entity;
  65. /**
  66. * Current form code
  67. *
  68. * @var string
  69. */
  70. protected $_formCode;
  71. /**
  72. * Array of form attributes
  73. *
  74. * @var array
  75. */
  76. protected $_attributes;
  77. /**
  78. * Array of form system attributes
  79. *
  80. * @var array
  81. */
  82. protected $_systemAttributes;
  83. /**
  84. * Array of form user defined attributes
  85. *
  86. * @var array
  87. */
  88. protected $_userAttributes;
  89. /**
  90. * Is AJAX request flag
  91. *
  92. * @var boolean
  93. */
  94. protected $_isAjax = false;
  95. /**
  96. * Whether the invisible form fields need to be filtered/ignored
  97. *
  98. * @var bool
  99. */
  100. protected $_ignoreInvisible = true;
  101. /**
  102. * Checks correct module choice
  103. *
  104. * @throws Mage_Core_Exception
  105. */
  106. public function __construct()
  107. {
  108. if (empty($this->_moduleName)) {
  109. Mage::throwException(Mage::helper('Mage_Eav_Helper_Data')->__('Current module pathname is undefined'));
  110. }
  111. if (empty($this->_entityTypeCode)) {
  112. Mage::throwException(Mage::helper('Mage_Eav_Helper_Data')->__('Current module EAV entity is undefined'));
  113. }
  114. }
  115. /**
  116. * Get EAV Entity Form Attribute Collection
  117. *
  118. * @return mixed
  119. */
  120. protected function _getFormAttributeCollection()
  121. {
  122. return Mage::getResourceModel($this->_moduleName . '_Model_Resource_Form_Attribute_Collection');
  123. }
  124. /**
  125. * Set current store
  126. *
  127. * @param Mage_Core_Model_Store|string|int $store
  128. * @return Mage_Eav_Model_Form
  129. */
  130. public function setStore($store)
  131. {
  132. $this->_store = Mage::app()->getStore($store);
  133. return $this;
  134. }
  135. /**
  136. * Set entity instance
  137. *
  138. * @param Mage_Core_Model_Abstract $entity
  139. * @return Mage_Eav_Model_Form
  140. */
  141. public function setEntity(Mage_Core_Model_Abstract $entity)
  142. {
  143. $this->_entity = $entity;
  144. if ($entity->getEntityTypeId()) {
  145. $this->setEntityType($entity->getEntityTypeId());
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Set entity type instance
  151. *
  152. * @param Mage_Eav_Model_Entity_Type|string|int $entityType
  153. * @return Mage_Eav_Model_Form
  154. */
  155. public function setEntityType($entityType)
  156. {
  157. $this->_entityType = Mage::getSingleton('Mage_Eav_Model_Config')->getEntityType($entityType);
  158. return $this;
  159. }
  160. /**
  161. * Set form code
  162. *
  163. * @param string $formCode
  164. * @return Mage_Eav_Model_Form
  165. */
  166. public function setFormCode($formCode)
  167. {
  168. $this->_formCode = $formCode;
  169. return $this;
  170. }
  171. /**
  172. * Return current store instance
  173. *
  174. * @return Mage_Core_Model_Store
  175. */
  176. public function getStore()
  177. {
  178. if (is_null($this->_store)) {
  179. $this->_store = Mage::app()->getStore();
  180. }
  181. return $this->_store;
  182. }
  183. /**
  184. * Return current form code
  185. *
  186. * @throws Mage_Core_Exception
  187. * @return string
  188. */
  189. public function getFormCode()
  190. {
  191. if (empty($this->_formCode)) {
  192. Mage::throwException(Mage::helper('Mage_Eav_Helper_Data')->__('Form code is not defined'));
  193. }
  194. return $this->_formCode;
  195. }
  196. /**
  197. * Return entity type instance
  198. * Return EAV entity type if entity type is not defined
  199. *
  200. * @return Mage_Eav_Model_Entity_Type
  201. */
  202. public function getEntityType()
  203. {
  204. if (is_null($this->_entityType)) {
  205. $this->setEntityType($this->_entityTypeCode);
  206. }
  207. return $this->_entityType;
  208. }
  209. /**
  210. * Return current entity instance
  211. *
  212. * @throws Mage_Core_Exception
  213. * @return Mage_Core_Model_Abstract
  214. */
  215. public function getEntity()
  216. {
  217. if (is_null($this->_entity)) {
  218. Mage::throwException(Mage::helper('Mage_Eav_Helper_Data')->__('Entity instance is not defined'));
  219. }
  220. return $this->_entity;
  221. }
  222. /**
  223. * Return array of form attributes
  224. *
  225. * @return array
  226. */
  227. public function getAttributes()
  228. {
  229. if (is_null($this->_attributes)) {
  230. /* @var $collection Mage_Eav_Model_Resource_Form_Attribute_Collection */
  231. $collection = $this->_getFormAttributeCollection();
  232. $collection->setStore($this->getStore())
  233. ->setEntityType($this->getEntityType())
  234. ->addFormCodeFilter($this->getFormCode())
  235. ->setSortOrder();
  236. $this->_attributes = array();
  237. $this->_userAttributes = array();
  238. foreach ($collection as $attribute) {
  239. /* @var $attribute Mage_Eav_Model_Entity_Attribute */
  240. $this->_attributes[$attribute->getAttributeCode()] = $attribute;
  241. if ($attribute->getIsUserDefined()) {
  242. $this->_userAttributes[$attribute->getAttributeCode()] = $attribute;
  243. } else {
  244. $this->_systemAttributes[$attribute->getAttributeCode()] = $attribute;
  245. }
  246. }
  247. }
  248. return $this->_attributes;
  249. }
  250. /**
  251. * Return attribute instance by code or false
  252. *
  253. * @param string $attributeCode
  254. * @return Mage_Eav_Model_Entity_Attribute|false
  255. */
  256. public function getAttribute($attributeCode)
  257. {
  258. $attributes = $this->getAttributes();
  259. if (isset($attributes[$attributeCode])) {
  260. return $attributes[$attributeCode];
  261. }
  262. return false;
  263. }
  264. /**
  265. * Return array of form user defined attributes
  266. *
  267. * @return array
  268. */
  269. public function getUserAttributes()
  270. {
  271. if (is_null($this->_userAttributes)) {
  272. // load attributes
  273. $this->getAttributes();
  274. }
  275. return $this->_userAttributes;
  276. }
  277. /**
  278. * Return array of form system attributes
  279. *
  280. * @return array
  281. */
  282. public function getSystemAttributes()
  283. {
  284. if (is_null($this->_systemAttributes)) {
  285. // load attributes
  286. $this->getAttributes();
  287. }
  288. return $this->_systemAttributes;
  289. }
  290. /**
  291. * Return attribute data model by attribute
  292. *
  293. * @param Mage_Eav_Model_Entity_Attribute $attribute
  294. * @return Mage_Eav_Model_Attribute_Data_Abstract
  295. */
  296. protected function _getAttributeDataModel(Mage_Eav_Model_Entity_Attribute $attribute)
  297. {
  298. $dataModel = Mage_Eav_Model_Attribute_Data::factory($attribute, $this->getEntity());
  299. $dataModel->setIsAjaxRequest($this->getIsAjaxRequest());
  300. return $dataModel;
  301. }
  302. /**
  303. * Prepare request with data and returns it
  304. *
  305. * @param array $data
  306. * @return Zend_Controller_Request_Http
  307. */
  308. public function prepareRequest(array $data)
  309. {
  310. $request = clone Mage::app()->getRequest();
  311. $request->setParamSources();
  312. $request->clearParams();
  313. $request->setParams($data);
  314. return $request;
  315. }
  316. /**
  317. * Extract data from request and return associative data array
  318. *
  319. * @param Zend_Controller_Request_Http $request
  320. * @param string $scope the request scope
  321. * @param boolean $scopeOnly search value only in scope or search value in global too
  322. * @return array
  323. */
  324. public function extractData(Zend_Controller_Request_Http $request, $scope = null, $scopeOnly = true)
  325. {
  326. $data = array();
  327. foreach ($this->getAttributes() as $attribute) {
  328. if ($this->_isAttributeOmitted($attribute)) {
  329. continue;
  330. }
  331. $dataModel = $this->_getAttributeDataModel($attribute);
  332. $dataModel->setRequestScope($scope);
  333. $dataModel->setRequestScopeOnly($scopeOnly);
  334. $data[$attribute->getAttributeCode()] = $dataModel->extractValue($request);
  335. }
  336. return $data;
  337. }
  338. /**
  339. * Validate data array and return true or array of errors
  340. *
  341. * @param array $data
  342. * @return boolean|array
  343. */
  344. public function validateData(array $data)
  345. {
  346. $errors = array();
  347. foreach ($this->getAttributes() as $attribute) {
  348. if ($this->_isAttributeOmitted($attribute)) {
  349. continue;
  350. }
  351. $dataModel = $this->_getAttributeDataModel($attribute);
  352. $dataModel->setExtractedData($data);
  353. if (!isset($data[$attribute->getAttributeCode()])) {
  354. $data[$attribute->getAttributeCode()] = null;
  355. }
  356. $result = $dataModel->validateValue($data[$attribute->getAttributeCode()]);
  357. if ($result !== true) {
  358. $errors = array_merge($errors, $result);
  359. }
  360. }
  361. if (count($errors) == 0) {
  362. return true;
  363. }
  364. return $errors;
  365. }
  366. /**
  367. * Compact data array to current entity
  368. *
  369. * @param array $data
  370. * @return Mage_Eav_Model_Form
  371. */
  372. public function compactData(array $data)
  373. {
  374. foreach ($this->getAttributes() as $attribute) {
  375. if ($this->_isAttributeOmitted($attribute)) {
  376. continue;
  377. }
  378. $dataModel = $this->_getAttributeDataModel($attribute);
  379. $dataModel->setExtractedData($data);
  380. if (!isset($data[$attribute->getAttributeCode()])) {
  381. $data[$attribute->getAttributeCode()] = false;
  382. }
  383. $dataModel->compactValue($data[$attribute->getAttributeCode()]);
  384. }
  385. return $this;
  386. }
  387. /**
  388. * Restore data array from SESSION to current entity
  389. *
  390. * @param array $data
  391. * @return Mage_Eav_Model_Form
  392. */
  393. public function restoreData(array $data)
  394. {
  395. foreach ($this->getAttributes() as $attribute) {
  396. if ($this->_isAttributeOmitted($attribute)) {
  397. continue;
  398. }
  399. $dataModel = $this->_getAttributeDataModel($attribute);
  400. $dataModel->setExtractedData($data);
  401. if (!isset($data[$attribute->getAttributeCode()])) {
  402. $data[$attribute->getAttributeCode()] = false;
  403. }
  404. $dataModel->restoreValue($data[$attribute->getAttributeCode()]);
  405. }
  406. return $this;
  407. }
  408. /**
  409. * Return array of entity formated values
  410. *
  411. * @param string $format
  412. * @return array
  413. */
  414. public function outputData($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
  415. {
  416. $data = array();
  417. foreach ($this->getAttributes() as $attribute) {
  418. if ($this->_isAttributeOmitted($attribute)) {
  419. continue;
  420. }
  421. $dataModel = $this->_getAttributeDataModel($attribute);
  422. $dataModel->setExtractedData($data);
  423. $data[$attribute->getAttributeCode()] = $dataModel->outputValue($format);
  424. }
  425. return $data;
  426. }
  427. /**
  428. * Restore entity original data
  429. *
  430. * @return Mage_Eav_Model_Form
  431. */
  432. public function resetEntityData()
  433. {
  434. foreach ($this->getAttributes() as $attribute) {
  435. if ($this->_isAttributeOmitted($attribute)) {
  436. continue;
  437. }
  438. $value = $this->getEntity()->getOrigData($attribute->getAttributeCode());
  439. $this->getEntity()->setData($attribute->getAttributeCode(), $value);
  440. }
  441. return $this;
  442. }
  443. /**
  444. * Set is AJAX Request flag
  445. *
  446. * @param boolean $flag
  447. * @return Mage_Eav_Model_Form
  448. */
  449. public function setIsAjaxRequest($flag = true)
  450. {
  451. $this->_isAjax = (bool)$flag;
  452. return $this;
  453. }
  454. /**
  455. * Return is AJAX Request
  456. *
  457. * @return boolean
  458. */
  459. public function getIsAjaxRequest()
  460. {
  461. return $this->_isAjax;
  462. }
  463. /**
  464. * Set default attribute values for new entity
  465. *
  466. * @return Mage_Eav_Model_Form
  467. */
  468. public function initDefaultValues()
  469. {
  470. if (!$this->getEntity()->getId()) {
  471. foreach ($this->getAttributes() as $attribute) {
  472. $default = $attribute->getDefaultValue();
  473. if ($default != '') {
  474. $this->getEntity()->setData($attribute->getAttributeCode(), $default);
  475. }
  476. }
  477. }
  478. return $this;
  479. }
  480. /**
  481. * Combined getter/setter whether to omit invisible attributes during rendering/validation
  482. *
  483. * @param mixed $setValue
  484. * @return bool|Mage_Eav_Model_Form
  485. */
  486. public function ignoreInvisible($setValue = null)
  487. {
  488. if (null !== $setValue) {
  489. $this->_ignoreInvisible = (bool)$setValue;
  490. return $this;
  491. }
  492. return $this->_ignoreInvisible;
  493. }
  494. /**
  495. * Whether the specified attribute needs to skip rendering/validation
  496. *
  497. * @param Mage_Eav_Model_Attribute $attribute
  498. * @return bool
  499. */
  500. protected function _isAttributeOmitted($attribute)
  501. {
  502. if ($this->_ignoreInvisible && !$attribute->getIsVisible()) {
  503. return true;
  504. }
  505. return false;
  506. }
  507. }