PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/src/Enterprise/Customer/controllers/Adminhtml/Customer/Address/AttributeController.php

https://bitbucket.org/kdms/sh-magento
PHP | 374 lines | 245 code | 33 blank | 96 comment | 28 complexity | ca0c3c0c569dacc622881d1878f1eb1b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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 Enterprise
  22. * @package Enterprise_Customer
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Manage Customer Address Attributes Controller
  28. *
  29. * @category Enterprise
  30. * @package Enterprise_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Enterprise_Customer_Adminhtml_Customer_Address_AttributeController
  34. extends Mage_Adminhtml_Controller_Action
  35. {
  36. /**
  37. * Customer Address Entity Type instance
  38. *
  39. * @var Mage_Eav_Model_Entity_Type
  40. */
  41. protected $_entityType;
  42. /**
  43. * Return Customer Address Entity Type instance
  44. *
  45. * @return Mage_Eav_Model_Entity_Type
  46. */
  47. protected function _getEntityType()
  48. {
  49. if (is_null($this->_entityType)) {
  50. $this->_entityType = Mage::getSingleton('eav/config')->getEntityType('customer_address');
  51. }
  52. return $this->_entityType;
  53. }
  54. /**
  55. * Load layout, set breadcrumbs
  56. *
  57. * @return Enterprise_Customer_Adminhtml_Customer_Address_AttributeController
  58. */
  59. protected function _initAction()
  60. {
  61. $this->loadLayout()
  62. ->_setActiveMenu('customer/address_attributes')
  63. ->_addBreadcrumb(
  64. Mage::helper('enterprise_customer')->__('Customer'),
  65. Mage::helper('enterprise_customer')->__('Customer'))
  66. ->_addBreadcrumb(
  67. Mage::helper('enterprise_customer')->__('Manage Customer Address Attributes'),
  68. Mage::helper('enterprise_customer')->__('Manage Customer Address Attributes'));
  69. return $this;
  70. }
  71. /**
  72. * Retrieve customer attribute object
  73. *
  74. * @return Mage_Customer_Model_Attribute
  75. */
  76. protected function _initAttribute()
  77. {
  78. $attribute = Mage::getModel('customer/attribute');
  79. $websiteId = $this->getRequest()->getParam('website');
  80. if ($websiteId) {
  81. $attribute->setWebsite($websiteId);
  82. }
  83. return $attribute;
  84. }
  85. /**
  86. * Attributes grid
  87. *
  88. */
  89. public function indexAction()
  90. {
  91. $this->_title($this->__('Manage Customer Address Attributes'));
  92. $this->_initAction()
  93. ->renderLayout();
  94. }
  95. /**
  96. * Create new attribute action
  97. *
  98. */
  99. public function newAction()
  100. {
  101. $this->addActionLayoutHandles();
  102. $this->_forward('edit');
  103. }
  104. /**
  105. * Edit attribute action
  106. *
  107. */
  108. public function editAction()
  109. {
  110. $attributeId = $this->getRequest()->getParam('attribute_id');
  111. /* @var $attributeObject Mage_Customer_Model_Attribute */
  112. $attributeObject = $this->_initAttribute()
  113. ->setEntityTypeId($this->_getEntityType()->getId());
  114. $this->_title($this->__('Manage Customer Address Attributes'));
  115. if ($attributeId) {
  116. $attributeObject->load($attributeId);
  117. if (!$attributeObject->getId()) {
  118. $this->_getSession()->addError(
  119. Mage::helper('enterprise_customer')->__('Attribute is no longer exists.')
  120. );
  121. $this->_redirect('*/*/');
  122. return;
  123. }
  124. if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()) {
  125. $this->_getSession()->addError(
  126. Mage::helper('enterprise_customer')->__('You cannot edit this attribute.')
  127. );
  128. $this->_redirect('*/*/');
  129. return;
  130. }
  131. $this->_title($attributeObject->getFrontendLabel());
  132. } else {
  133. $this->_title($this->__('New Attribute'));
  134. }
  135. // restore attribute data
  136. $attributeData = $this->_getSession()->getAttributeData(true);
  137. if (!empty($attributeData)) {
  138. $attributeObject->setData($attributeData);
  139. }
  140. // register attribute object
  141. Mage::register('entity_attribute', $attributeObject);
  142. $label = $attributeObject->getId()
  143. ? Mage::helper('enterprise_customer')->__('Edit Customer Address Attribute')
  144. : Mage::helper('enterprise_customer')->__('New Customer Address Attribute');
  145. $this->_initAction()
  146. ->_addBreadcrumb($label, $label)
  147. ->renderLayout();
  148. }
  149. /**
  150. * Validate attribute action
  151. *
  152. */
  153. public function validateAction()
  154. {
  155. $response = new Varien_Object();
  156. $response->setError(false);
  157. $attributeId = $this->getRequest()->getParam('attribute_id');
  158. if (!$attributeId) {
  159. $attributeCode = $this->getRequest()->getParam('attribute_code');
  160. $attributeObject = $this->_initAttribute()
  161. ->loadByCode($this->_getEntityType()->getId(), $attributeCode);
  162. if ($attributeObject->getId()) {
  163. $this->_getSession()->addError(
  164. Mage::helper('enterprise_customer')->__('Attribute with the same code already exists')
  165. );
  166. $this->_initLayoutMessages('adminhtml/session');
  167. $response->setError(true);
  168. $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
  169. }
  170. }
  171. $this->getResponse()->setBody($response->toJson());
  172. }
  173. /**
  174. * Filter post data
  175. *
  176. * @param array $data
  177. * @return array
  178. */
  179. protected function _filterPostData($data)
  180. {
  181. return Mage::helper('enterprise_customer/address')->filterPostData($data);
  182. }
  183. /**
  184. * Save attribute action
  185. *
  186. */
  187. public function saveAction()
  188. {
  189. $data = $this->getRequest()->getPost();
  190. if ($this->getRequest()->isPost() && $data) {
  191. /* @var $attributeObject Mage_Customer_Model_Attribute */
  192. $attributeObject = $this->_initAttribute();
  193. /* @var $helper Enterprise_Customer_Helper_Data */
  194. $helper = Mage::helper('enterprise_customer');
  195. //filtering
  196. try {
  197. $data = $this->_filterPostData($data);
  198. } catch (Mage_Core_Exception $e) {
  199. $this->_getSession()->addError($e->getMessage());
  200. if (isset($data['attribute_id'])) {
  201. $this->_redirect('*/*/edit', array('_current' => true));
  202. } else {
  203. $this->_redirect('*/*/new', array('_current' => true));
  204. }
  205. return;
  206. }
  207. $attributeId = $this->getRequest()->getParam('attribute_id');
  208. if ($attributeId) {
  209. $attributeObject->load($attributeId);
  210. if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()) {
  211. $this->_getSession()->addError(
  212. Mage::helper('enterprise_customer')->__('You cannot edit this attribute.')
  213. );
  214. $this->_getSession()->addAttributeData($data);
  215. $this->_redirect('*/*/');
  216. return;
  217. }
  218. $data['attribute_code'] = $attributeObject->getAttributeCode();
  219. $data['is_user_defined'] = $attributeObject->getIsUserDefined();
  220. $data['frontend_input'] = $attributeObject->getFrontendInput();
  221. $data['is_user_defined'] = $attributeObject->getIsUserDefined();
  222. $data['is_system'] = $attributeObject->getIsSystem();
  223. } else {
  224. $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
  225. $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
  226. $data['backend_type'] = $helper->getAttributeBackendTypeByInputType($data['frontend_input']);
  227. $data['is_user_defined'] = 1;
  228. $data['is_system'] = 0;
  229. // add set and group info
  230. $data['attribute_set_id'] = $this->_getEntityType()->getDefaultAttributeSetId();
  231. $data['attribute_group_id'] = Mage::getModel('eav/entity_attribute_set')
  232. ->getDefaultGroupId($data['attribute_set_id']);
  233. }
  234. if (isset($data['used_in_forms']) && is_array($data['used_in_forms'])) {
  235. $data['used_in_forms'][] = 'adminhtml_customer_address';
  236. }
  237. $defaultValueField = $helper->getAttributeDefaultValueByInput($data['frontend_input']);
  238. if ($defaultValueField) {
  239. $scopeKeyPrefix = ($this->getRequest()->getParam('website') ? 'scope_' : '');
  240. $data[$scopeKeyPrefix . 'default_value'] = $helper->stripTags(
  241. $this->getRequest()->getParam($scopeKeyPrefix . $defaultValueField));
  242. }
  243. $data['entity_type_id'] = $this->_getEntityType()->getId();
  244. $data['validate_rules'] = $helper->getAttributeValidateRules($data['frontend_input'], $data);
  245. $attributeObject->addData($data);
  246. /**
  247. * Check "Use Default Value" checkboxes values
  248. */
  249. if ($useDefaults = $this->getRequest()->getPost('use_default')) {
  250. foreach ($useDefaults as $key) {
  251. $attributeObject->setData('scope_' . $key, null);
  252. }
  253. }
  254. try {
  255. $attributeObject->save();
  256. Mage::dispatchEvent('enterprise_customer_address_attribute_save', array(
  257. 'attribute' => $attributeObject
  258. ));
  259. $this->_getSession()->addSuccess(
  260. Mage::helper('enterprise_customer')->__('The customer address attribute has been saved.')
  261. );
  262. $this->_getSession()->setAttributeData(false);
  263. if ($this->getRequest()->getParam('back', false)) {
  264. $this->_redirect('*/*/edit', array(
  265. 'attribute_id' => $attributeObject->getId(),
  266. '_current' => true
  267. ));
  268. } else {
  269. $this->_redirect('*/*/');
  270. }
  271. return;
  272. } catch (Mage_Core_Exception $e) {
  273. $this->_getSession()->addError($e->getMessage());
  274. $this->_getSession()->setAttributeData($data);
  275. $this->_redirect('*/*/edit', array('_current' => true));
  276. return;
  277. } catch (Exception $e) {
  278. $this->_getSession()->addException($e,
  279. Mage::helper('enterprise_customer')->__('An error occurred while saving the customer address attribute.')
  280. );
  281. $this->_getSession()->setAttributeData($data);
  282. $this->_redirect('*/*/edit', array('_current' => true));
  283. return;
  284. }
  285. }
  286. $this->_redirect('*/*/');
  287. return;
  288. }
  289. /**
  290. * Delete attribute action
  291. *
  292. */
  293. public function deleteAction()
  294. {
  295. $attributeId = $this->getRequest()->getParam('attribute_id');
  296. if ($attributeId) {
  297. $attributeObject = $this->_initAttribute()->load($attributeId);
  298. if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()
  299. || !$attributeObject->getIsUserDefined())
  300. {
  301. $this->_getSession()->addError(
  302. Mage::helper('enterprise_customer')->__('You cannot delete this attribute.')
  303. );
  304. $this->_redirect('*/*/');
  305. return;
  306. }
  307. try {
  308. $attributeObject->delete();
  309. Mage::dispatchEvent('enterprise_customer_address_attribute_delete', array(
  310. 'attribute' => $attributeObject
  311. ));
  312. $this->_getSession()->addSuccess(
  313. Mage::helper('enterprise_customer')->__('The customer address attribute has been deleted.')
  314. );
  315. $this->_redirect('*/*/');
  316. return;
  317. } catch (Mage_Core_Exception $e) {
  318. $this->_getSession()->addError($e->getMessage());
  319. $this->_redirect('*/*/edit', array('attribute_id' => $attributeId, '_current' => true));
  320. return;
  321. } catch (Exception $e) {
  322. $this->_getSession()->addException($e,
  323. Mage::helper('enterprise_customer')->__('An error occurred while deleting the customer address attribute.')
  324. );
  325. $this->_redirect('*/*/edit', array('attribute_id' => $attributeId, '_current' => true));
  326. return;
  327. }
  328. }
  329. $this->_redirect('*/*/');
  330. return;
  331. }
  332. /**
  333. * Check whether attributes management functionality is allowed
  334. *
  335. * @return bool
  336. */
  337. protected function _isAllowed()
  338. {
  339. return Mage::getSingleton('admin/session')->isAllowed('admin/customer/attributes/customer_address_attributes');
  340. }
  341. }