diff --git a/app/code/core/Mage/Adminhtml/Model/Customer/Observer.php b/app/code/core/Mage/Adminhtml/Model/Customer/Observer.php new file mode 100644 index 00000000000..c7ddc851b9a --- /dev/null +++ b/app/code/core/Mage/Adminhtml/Model/Customer/Observer.php @@ -0,0 +1,148 @@ + + */ +class Mage_Adminhtml_Model_Customer_Observer +{ + + /** + * Add frontend properties to customer attribute edit form + * + * @param Varien_Event_Observer $observer + * @return $this + */ + public function customerAttributeEditPrepareForm($observer) + { + /* @var $attribute Mage_Customer_Model_Attribute */ + $attribute = $observer->getAttribute(); + + /* @var $form Varien_Data_Form */ + $form = $observer->getForm(); + + /* @var $fieldset Varien_Data_Form_Element_Fieldset */ + $fieldset = $form->getElement('base_fieldset'); + + // frontend properties fieldset + $fieldset = $form->addFieldset('front_fieldset', array('legend'=>Mage::helper('adminhtml')->__('Extra Properties'))); + + $fieldset->addField('use_in_forms', 'multiselect', array( + 'name' => 'use_in_forms', + 'label' => Mage::helper('adminhtml')->__('Use in Forms'), + 'title' => Mage::helper('adminhtml')->__('Use in Forms'), + 'values' => Mage::getModel('customer/config_forms')->toOptionArray(), + 'value' => Mage::getResourceModel('customer/form_attribute')->getFormTypesByAttribute($attribute) + )); + + return $this; + } + + /** + * Save frontend properties from customer attribute edit form + * + * @param Varien_Event_Observer $observer + * @return $this + */ + public function customerAttributeEditPrepareSave($observer) + { + /** @var Mage_Core_Controller_Request_Http $request */ + $request = $observer->getRequest(); + + $data = $request->getPost(); + if ($data) { + + /** @var Mage_Eav_Model_Entity_Attribute $object */ + $model = $observer->getObject(); + + if (!isset($data['use_in_forms'])) { + $data['use_in_forms'] = array(); + } + + $model->setData('used_in_forms', $data['use_in_forms']); + + } + return $this; + } + + /** + * Add frontend properties to customer address attribute edit form + * + * @param Varien_Event_Observer $observer + * @return $this + */ + public function customerAddressAttributeEditPrepareForm($observer) + { + /* @var $attribute Mage_Customer_Model_Attribute */ + $attribute = $observer->getAttribute(); + + /* @var $form Varien_Data_Form */ + $form = $observer->getForm(); + + /* @var $fieldset Varien_Data_Form_Element_Fieldset */ + $fieldset = $form->getElement('base_fieldset'); + + // frontend properties fieldset + $fieldset = $form->addFieldset('front_fieldset', array('legend'=>Mage::helper('adminhtml')->__('Extra Properties'))); + + $fieldset->addField('use_in_forms', 'multiselect', array( + 'name' => 'use_in_forms', + 'label' => Mage::helper('adminhtml')->__('Use in Forms'), + 'title' => Mage::helper('adminhtml')->__('Use in Forms'), + 'values' => Mage::getModel('customer/config_address_forms')->toOptionArray(), + 'value' => Mage::getResourceModel('customer/form_attribute')->getFormTypesByAttribute($attribute) + )); + + return $this; + } + + /** + * Save frontend properties from customer address attribute edit form + * + * @param Varien_Event_Observer $observer + * @return $this + */ + public function customerAddressAttributeEditPrepareSave($observer) + { + /** @var Mage_Core_Controller_Request_Http $request */ + $request = $observer->getRequest(); + + $data = $request->getPost(); + if ($data) { + + /** @var Mage_Eav_Model_Entity_Attribute $object */ + $model = $observer->getObject(); + + if (!isset($data['use_in_forms'])) { + $data['use_in_forms'] = array(); + } + + $model->setData('used_in_forms', $data['use_in_forms']); + + } + return $this; + } + +} diff --git a/app/code/core/Mage/Adminhtml/etc/config.xml b/app/code/core/Mage/Adminhtml/etc/config.xml index 4125484d17f..50f91ec241b 100644 --- a/app/code/core/Mage/Adminhtml/etc/config.xml +++ b/app/code/core/Mage/Adminhtml/etc/config.xml @@ -126,6 +126,38 @@ + + + + adminhtml/customer_observer + customerAttributeEditPrepareForm + + + + + + + adminhtml/customer_observer + customerAttributeEditPrepareSave + + + + + + + adminhtml/customer_observer + customerAddressAttributeEditPrepareForm + + + + + + + adminhtml/customer_observer + customerAddressAttributeEditPrepareSave + + + diff --git a/app/code/core/Mage/Customer/Model/Config/Address/Forms.php b/app/code/core/Mage/Customer/Model/Config/Address/Forms.php new file mode 100644 index 00000000000..aa7e568e0ad --- /dev/null +++ b/app/code/core/Mage/Customer/Model/Config/Address/Forms.php @@ -0,0 +1,64 @@ + 'adminhtml_customer_address', + 'label' => Mage::helper('customer')->__('Adminhtml Customer Address') + ), + array( + 'value' => 'customer_address_edit', + 'label' => Mage::helper('customer')->__('Customer Address Edit') + ), + array( + 'value' => 'customer_register_address', + 'label' => Mage::helper('customer')->__('Customer Register Address') + ), + ); + } + + /** + * Get options in "key-value" format + * + * @return array + */ + public function toArray() + { + return array_combine( + array_column($this->toOptionArray(), 'value'), + array_column($this->toOptionArray(), 'label') + ); + } + +} diff --git a/app/code/core/Mage/Customer/Model/Config/Forms.php b/app/code/core/Mage/Customer/Model/Config/Forms.php new file mode 100644 index 00000000000..ba250eb04c7 --- /dev/null +++ b/app/code/core/Mage/Customer/Model/Config/Forms.php @@ -0,0 +1,72 @@ + 'adminhtml_checkout', + 'label' => Mage::helper('customer')->__('Adminhtml Checkout') + ), + array( + 'value' => 'adminhtml_customer', + 'label' => Mage::helper('customer')->__('Adminhtml Customer') + ), + array( + 'value' => 'checkout_register', + 'label' => Mage::helper('customer')->__('Checkout Register') + ), + array( + 'value' => 'customer_account_create', + 'label' => Mage::helper('customer')->__('Customer Account Create') + ), + array( + 'value' => 'customer_account_edit', + 'label' => Mage::helper('customer')->__('Customer Account Edit') + ), + ); + } + + /** + * Get options in "key-value" format + * + * @return array + */ + public function toArray() + { + return array_combine( + array_column($this->toOptionArray(), 'value'), + array_column($this->toOptionArray(), 'label') + ); + } + +} diff --git a/app/code/core/Mage/Eav/Model/Resource/Form/Attribute.php b/app/code/core/Mage/Eav/Model/Resource/Form/Attribute.php index 2098189474e..aeac189a0f9 100644 --- a/app/code/core/Mage/Eav/Model/Resource/Form/Attribute.php +++ b/app/code/core/Mage/Eav/Model/Resource/Form/Attribute.php @@ -49,4 +49,27 @@ public function getFormAttributeIds($formCode) return $this->_getReadAdapter()->fetchCol($select, $bind); } + + /** + * Retrieve form type filtered by given attribute + * + * @param Mage_Eav_Model_Entity_Attribute_Abstract|int $attribute + * @return array + */ + public function getFormTypesByAttribute($attribute) + { + if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) { + $attribute = $attribute->getId(); + } + if (!$attribute) { + return array(); + } + + $bind = array('attribute_id' => $attribute); + $select = $this->_getReadAdapter()->select() + ->from($this->getMainTable(), 'form_code') + ->where('attribute_id = :attribute_id'); + + return $this->_getReadAdapter()->fetchCol($select, $bind); + } }