Skip to content

Commit

Permalink
use in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbeaty committed Aug 1, 2022
1 parent ce254ce commit 2293a10
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 0 deletions.
148 changes: 148 additions & 0 deletions app/code/core/Mage/Adminhtml/Model/Customer/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* OpenMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* 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@magento.com so we can send you a copy immediately.
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/


/**
* Customer EAV Observer
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/
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;
}

}
32 changes: 32 additions & 0 deletions app/code/core/Mage/Adminhtml/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@
</bind_locale>
</observers>
</admin_system_config_changed_section_admin>
<adminhtml_customer_attribute_edit_prepare_form>
<observers>
<eav>
<class>adminhtml/customer_observer</class>
<method>customerAttributeEditPrepareForm</method>
</eav>
</observers>
</adminhtml_customer_attribute_edit_prepare_form>
<adminhtml_customer_attribute_edit_prepare_save>
<observers>
<eav>
<class>adminhtml/customer_observer</class>
<method>customerAttributeEditPrepareSave</method>
</eav>
</observers>
</adminhtml_customer_attribute_edit_prepare_save>
<adminhtml_customer_address_attribute_edit_prepare_form>
<observers>
<eav>
<class>adminhtml/customer_observer</class>
<method>customerAddressAttributeEditPrepareForm</method>
</eav>
</observers>
</adminhtml_customer_address_attribute_edit_prepare_form>
<adminhtml_customer_address_attribute_edit_prepare_save>
<observers>
<eav>
<class>adminhtml/customer_observer</class>
<method>customerAddressAttributeEditPrepareSave</method>
</eav>
</observers>
</adminhtml_customer_address_attribute_edit_prepare_save>
</events>
<global_search>
<products>
Expand Down
64 changes: 64 additions & 0 deletions app/code/core/Mage/Customer/Model/Config/Address/Forms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* OpenMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* 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@magento.com so we can send you a copy immediately.
*
* @category Mage
* @package Mage_Customer
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Used in creating options for use_in_forms selection
*
*/
class Mage_Customer_Model_Config_Address_Forms
{

/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
return array(
array(
'value' => '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')
);
}

}
72 changes: 72 additions & 0 deletions app/code/core/Mage/Customer/Model/Config/Forms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* OpenMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* 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@magento.com so we can send you a copy immediately.
*
* @category Mage
* @package Mage_Customer
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Used in creating options for use_in_forms selection
*
*/
class Mage_Customer_Model_Config_Forms
{

/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
return array(
array(
'value' => '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')
);
}

}
23 changes: 23 additions & 0 deletions app/code/core/Mage/Eav/Model/Resource/Form/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 2293a10

Please sign in to comment.