/** * Processing object before save data * * @return Mage_Core_Model_Abstract */ protected function _beforeSave() { if ($this->isValueChanged()) { try { $fieldObject = Mage::helper('bronto_common')->getApi()->transferField(); $fieldName = Bronto_Utils::normalize($this->getValue()); if (!array_key_exists($fieldName, self::$_fieldCache)) { $field = $fieldObject->getByName($fieldName); if (!$field) { $field->withName($fieldName)->withLabel($this->getValue())->asText()->asHidden(); $field->withId($fieldObject->add()->addField($field)->first()->getItem()->getId()); } self::$_fieldCache[$fieldName] = $field; } $this->_saveConfigData(str_replace('_new', '', $this->getPath()), $field->getId()); $this->setValue(null); } catch (Exception $e) { Mage::throwException(Mage::helper('adminhtml')->__('Unable to save new field: ') . $e->getMessage()); } } return parent::_beforeSave(); }
/** * Capture "Create New..." attributes, create field in Bronto, and save field id * * @param array $attributesFields * @param string $section * @param string $group * * @return array */ protected function _processDynamicAttributes($attributesFields = array(), $section, $group) { // Create Config Object $config = Mage::getModel('core/config'); // Get Admin Scope Parameters $scopeParams = Mage::helper('bronto_common')->getScopeParams(); // Get Array of Attributes that are hard-coded into system.xml $ignore = Mage::helper('bronto_customer')->getSystemAttributes(); $api = Mage::helper('bronto_common')->getApi(); $fieldCache = array(); // Cycle Through Attribute Fields to Find and Save Dynamic Fields foreach ($attributesFields as $fieldId => $field) { // Save Dynamic 'Create New...' Fields if (preg_match('/_dynamic_new/', $fieldId) || preg_match('/_new/', $fieldId)) { // Strip off '_dynamic_new' or '_new' from Field ID to Get real Field ID $realField = preg_replace('/_dynamic_new|_new$/', '', $fieldId); if (!is_array($field)) { $value = $field; } else { $value = $field['value']; } if (is_null($value)) { continue; } try { /* @var $fieldObject Bronto_Api_Field */ $fieldObject = $api->transferField(); $fieldName = Bronto_Utils::normalize($value); if (!array_key_exists($fieldName, $fieldCache)) { $brontoField = $fieldObject->getByName($fieldName); if (!$brontoField) { $brontoField = $fieldObject->createObject()->withName($fieldName)->withLabel($value)->asText()->asHidden(); foreach ($fieldObject->add()->addField($brontoField) as $result) { $item = $result->getItem(); if ($item->getIsError()) { Mage::throwException("{$item->getErrorCode()}: {$item->getErrorMessage()}"); } $brontoField->withId($item->getId()); } } $fieldCache[$fieldName] = $brontoField; } $scope = $scopeParams['scope']; if ($scope != 'default') { $scope .= 's'; } // Save Field To Config $config->saveConfig($section . '/' . $group . '/' . $realField, $brontoField->getId(), $scope, $scopeParams[$scopeParams['scope'] . '_id']); // Unset Dynamic Fields unset($attributesFields[$realField]); unset($attributesFields[$fieldId]); unset($fieldObject); } catch (Exception $e) { Mage::helper('bronto_customer')->writeError("Unable to save new field: {$value}: {$e->getMessage()}"); } } elseif (array_key_exists('value', $field) && !in_array($fieldId, $ignore[$group])) { $scope = $scopeParams['scope']; if ($scope != 'default') { $scope .= 's'; } // Save Field To Config $config->saveConfig($section . '/' . $group . '/' . $fieldId, array_key_exists('value', $field) ? $field['value'] : '', $scope, $scopeParams[$scopeParams['scope'] . '_id']); // Unset Dynamic Field unset($attributesFields[$fieldId]); } } return $attributesFields; }