/app/code/core/Enterprise/Customer/controllers/Adminhtml/Customer/Address/AttributeController.php
PHP | 374 lines | 245 code | 33 blank | 96 comment | 28 complexity | ca0c3c0c569dacc622881d1878f1eb1b MD5 | raw file
- <?php
- /**
- * Magento Enterprise Edition
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Magento Enterprise Edition License
- * that is bundled with this package in the file LICENSE_EE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://www.magentocommerce.com/license/enterprise-edition
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@magentocommerce.com so we can send you a copy immediately.
- *
- * DISCLAIMER
- *
- * Do not edit or add to this file if you wish to upgrade Magento to newer
- * versions in the future. If you wish to customize Magento for your
- * needs please refer to http://www.magentocommerce.com for more information.
- *
- * @category Enterprise
- * @package Enterprise_Customer
- * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
- * @license http://www.magentocommerce.com/license/enterprise-edition
- */
- /**
- * Manage Customer Address Attributes Controller
- *
- * @category Enterprise
- * @package Enterprise_Customer
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Enterprise_Customer_Adminhtml_Customer_Address_AttributeController
- extends Mage_Adminhtml_Controller_Action
- {
- /**
- * Customer Address Entity Type instance
- *
- * @var Mage_Eav_Model_Entity_Type
- */
- protected $_entityType;
- /**
- * Return Customer Address Entity Type instance
- *
- * @return Mage_Eav_Model_Entity_Type
- */
- protected function _getEntityType()
- {
- if (is_null($this->_entityType)) {
- $this->_entityType = Mage::getSingleton('eav/config')->getEntityType('customer_address');
- }
- return $this->_entityType;
- }
- /**
- * Load layout, set breadcrumbs
- *
- * @return Enterprise_Customer_Adminhtml_Customer_Address_AttributeController
- */
- protected function _initAction()
- {
- $this->loadLayout()
- ->_setActiveMenu('customer/address_attributes')
- ->_addBreadcrumb(
- Mage::helper('enterprise_customer')->__('Customer'),
- Mage::helper('enterprise_customer')->__('Customer'))
- ->_addBreadcrumb(
- Mage::helper('enterprise_customer')->__('Manage Customer Address Attributes'),
- Mage::helper('enterprise_customer')->__('Manage Customer Address Attributes'));
- return $this;
- }
- /**
- * Retrieve customer attribute object
- *
- * @return Mage_Customer_Model_Attribute
- */
- protected function _initAttribute()
- {
- $attribute = Mage::getModel('customer/attribute');
- $websiteId = $this->getRequest()->getParam('website');
- if ($websiteId) {
- $attribute->setWebsite($websiteId);
- }
- return $attribute;
- }
- /**
- * Attributes grid
- *
- */
- public function indexAction()
- {
- $this->_title($this->__('Manage Customer Address Attributes'));
- $this->_initAction()
- ->renderLayout();
- }
- /**
- * Create new attribute action
- *
- */
- public function newAction()
- {
- $this->addActionLayoutHandles();
- $this->_forward('edit');
- }
- /**
- * Edit attribute action
- *
- */
- public function editAction()
- {
- $attributeId = $this->getRequest()->getParam('attribute_id');
- /* @var $attributeObject Mage_Customer_Model_Attribute */
- $attributeObject = $this->_initAttribute()
- ->setEntityTypeId($this->_getEntityType()->getId());
- $this->_title($this->__('Manage Customer Address Attributes'));
- if ($attributeId) {
- $attributeObject->load($attributeId);
- if (!$attributeObject->getId()) {
- $this->_getSession()->addError(
- Mage::helper('enterprise_customer')->__('Attribute is no longer exists.')
- );
- $this->_redirect('*/*/');
- return;
- }
- if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()) {
- $this->_getSession()->addError(
- Mage::helper('enterprise_customer')->__('You cannot edit this attribute.')
- );
- $this->_redirect('*/*/');
- return;
- }
- $this->_title($attributeObject->getFrontendLabel());
- } else {
- $this->_title($this->__('New Attribute'));
- }
- // restore attribute data
- $attributeData = $this->_getSession()->getAttributeData(true);
- if (!empty($attributeData)) {
- $attributeObject->setData($attributeData);
- }
- // register attribute object
- Mage::register('entity_attribute', $attributeObject);
- $label = $attributeObject->getId()
- ? Mage::helper('enterprise_customer')->__('Edit Customer Address Attribute')
- : Mage::helper('enterprise_customer')->__('New Customer Address Attribute');
- $this->_initAction()
- ->_addBreadcrumb($label, $label)
- ->renderLayout();
- }
- /**
- * Validate attribute action
- *
- */
- public function validateAction()
- {
- $response = new Varien_Object();
- $response->setError(false);
- $attributeId = $this->getRequest()->getParam('attribute_id');
- if (!$attributeId) {
- $attributeCode = $this->getRequest()->getParam('attribute_code');
- $attributeObject = $this->_initAttribute()
- ->loadByCode($this->_getEntityType()->getId(), $attributeCode);
- if ($attributeObject->getId()) {
- $this->_getSession()->addError(
- Mage::helper('enterprise_customer')->__('Attribute with the same code already exists')
- );
- $this->_initLayoutMessages('adminhtml/session');
- $response->setError(true);
- $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
- }
- }
- $this->getResponse()->setBody($response->toJson());
- }
- /**
- * Filter post data
- *
- * @param array $data
- * @return array
- */
- protected function _filterPostData($data)
- {
- return Mage::helper('enterprise_customer/address')->filterPostData($data);
- }
- /**
- * Save attribute action
- *
- */
- public function saveAction()
- {
- $data = $this->getRequest()->getPost();
- if ($this->getRequest()->isPost() && $data) {
- /* @var $attributeObject Mage_Customer_Model_Attribute */
- $attributeObject = $this->_initAttribute();
- /* @var $helper Enterprise_Customer_Helper_Data */
- $helper = Mage::helper('enterprise_customer');
- //filtering
- try {
- $data = $this->_filterPostData($data);
- } catch (Mage_Core_Exception $e) {
- $this->_getSession()->addError($e->getMessage());
- if (isset($data['attribute_id'])) {
- $this->_redirect('*/*/edit', array('_current' => true));
- } else {
- $this->_redirect('*/*/new', array('_current' => true));
- }
- return;
- }
- $attributeId = $this->getRequest()->getParam('attribute_id');
- if ($attributeId) {
- $attributeObject->load($attributeId);
- if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()) {
- $this->_getSession()->addError(
- Mage::helper('enterprise_customer')->__('You cannot edit this attribute.')
- );
- $this->_getSession()->addAttributeData($data);
- $this->_redirect('*/*/');
- return;
- }
- $data['attribute_code'] = $attributeObject->getAttributeCode();
- $data['is_user_defined'] = $attributeObject->getIsUserDefined();
- $data['frontend_input'] = $attributeObject->getFrontendInput();
- $data['is_user_defined'] = $attributeObject->getIsUserDefined();
- $data['is_system'] = $attributeObject->getIsSystem();
- } else {
- $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
- $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
- $data['backend_type'] = $helper->getAttributeBackendTypeByInputType($data['frontend_input']);
- $data['is_user_defined'] = 1;
- $data['is_system'] = 0;
- // add set and group info
- $data['attribute_set_id'] = $this->_getEntityType()->getDefaultAttributeSetId();
- $data['attribute_group_id'] = Mage::getModel('eav/entity_attribute_set')
- ->getDefaultGroupId($data['attribute_set_id']);
- }
- if (isset($data['used_in_forms']) && is_array($data['used_in_forms'])) {
- $data['used_in_forms'][] = 'adminhtml_customer_address';
- }
- $defaultValueField = $helper->getAttributeDefaultValueByInput($data['frontend_input']);
- if ($defaultValueField) {
- $scopeKeyPrefix = ($this->getRequest()->getParam('website') ? 'scope_' : '');
- $data[$scopeKeyPrefix . 'default_value'] = $helper->stripTags(
- $this->getRequest()->getParam($scopeKeyPrefix . $defaultValueField));
- }
- $data['entity_type_id'] = $this->_getEntityType()->getId();
- $data['validate_rules'] = $helper->getAttributeValidateRules($data['frontend_input'], $data);
- $attributeObject->addData($data);
- /**
- * Check "Use Default Value" checkboxes values
- */
- if ($useDefaults = $this->getRequest()->getPost('use_default')) {
- foreach ($useDefaults as $key) {
- $attributeObject->setData('scope_' . $key, null);
- }
- }
- try {
- $attributeObject->save();
- Mage::dispatchEvent('enterprise_customer_address_attribute_save', array(
- 'attribute' => $attributeObject
- ));
- $this->_getSession()->addSuccess(
- Mage::helper('enterprise_customer')->__('The customer address attribute has been saved.')
- );
- $this->_getSession()->setAttributeData(false);
- if ($this->getRequest()->getParam('back', false)) {
- $this->_redirect('*/*/edit', array(
- 'attribute_id' => $attributeObject->getId(),
- '_current' => true
- ));
- } else {
- $this->_redirect('*/*/');
- }
- return;
- } catch (Mage_Core_Exception $e) {
- $this->_getSession()->addError($e->getMessage());
- $this->_getSession()->setAttributeData($data);
- $this->_redirect('*/*/edit', array('_current' => true));
- return;
- } catch (Exception $e) {
- $this->_getSession()->addException($e,
- Mage::helper('enterprise_customer')->__('An error occurred while saving the customer address attribute.')
- );
- $this->_getSession()->setAttributeData($data);
- $this->_redirect('*/*/edit', array('_current' => true));
- return;
- }
- }
- $this->_redirect('*/*/');
- return;
- }
- /**
- * Delete attribute action
- *
- */
- public function deleteAction()
- {
- $attributeId = $this->getRequest()->getParam('attribute_id');
- if ($attributeId) {
- $attributeObject = $this->_initAttribute()->load($attributeId);
- if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()
- || !$attributeObject->getIsUserDefined())
- {
- $this->_getSession()->addError(
- Mage::helper('enterprise_customer')->__('You cannot delete this attribute.')
- );
- $this->_redirect('*/*/');
- return;
- }
- try {
- $attributeObject->delete();
- Mage::dispatchEvent('enterprise_customer_address_attribute_delete', array(
- 'attribute' => $attributeObject
- ));
- $this->_getSession()->addSuccess(
- Mage::helper('enterprise_customer')->__('The customer address attribute has been deleted.')
- );
- $this->_redirect('*/*/');
- return;
- } catch (Mage_Core_Exception $e) {
- $this->_getSession()->addError($e->getMessage());
- $this->_redirect('*/*/edit', array('attribute_id' => $attributeId, '_current' => true));
- return;
- } catch (Exception $e) {
- $this->_getSession()->addException($e,
- Mage::helper('enterprise_customer')->__('An error occurred while deleting the customer address attribute.')
- );
- $this->_redirect('*/*/edit', array('attribute_id' => $attributeId, '_current' => true));
- return;
- }
- }
- $this->_redirect('*/*/');
- return;
- }
- /**
- * Check whether attributes management functionality is allowed
- *
- * @return bool
- */
- protected function _isAllowed()
- {
- return Mage::getSingleton('admin/session')->isAllowed('admin/customer/attributes/customer_address_attributes');
- }
- }