00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_GoogleBase_Block_Adminhtml_Types_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
00036 {
00037 protected function _prepareForm()
00038 {
00039 $form = new Varien_Data_Form();
00040
00041 $itemType = $this->getItemType();
00042
00043 $fieldset = $form->addFieldset('base_fieldset', array(
00044 'legend' => $this->__('Attribute Set and Item Type')
00045 ));
00046
00047 if ( !($targetCountry = $itemType->getTargetCountry()) ) {
00048 $isoKeys = array_keys($this->_getCountriesArray());
00049 $targetCountry = isset($isoKeys[0]) ? $isoKeys[0] : null;
00050 }
00051 $countrySelect = $fieldset->addField('select_target_country', 'select', array(
00052 'label' => $this->__('Target Country'),
00053 'title' => $this->__('Target Country'),
00054 'name' => 'target_country',
00055 'required' => true,
00056 'options' => $this->_getCountriesArray(),
00057 'value' => $targetCountry,
00058 ));
00059 if ($itemType->getTargetCountry()) {
00060 $countrySelect->setDisabled(true);
00061 }
00062
00063 $attributeSetsSelect = $this->getAttributeSetsSelectElement($targetCountry)->setValue($itemType->getAttributeSetId());
00064 if ($itemType->getAttributeSetId()) {
00065 $attributeSetsSelect->setDisabled(true);
00066 }
00067
00068 $fieldset->addField('attribute_set', 'note', array(
00069 'label' => $this->__('Attribute Set'),
00070 'title' => $this->__('Attribute Set'),
00071 'required' => true,
00072 'text' => '<div id="attribute_set_select">' . $attributeSetsSelect->toHtml() . '</div>',
00073 ));
00074
00075 $itemTypesSelect = $this->getItemTypesSelectElement($targetCountry)->setValue($itemType->getGbaseItemtype());
00076 if ($itemType->getGbaseItemtype()) {
00077 $itemTypesSelect->setDisabled(true);
00078 }
00079
00080 $fieldset->addField('itemtype', 'note', array(
00081 'label' => $this->__('Google Base Item Type'),
00082 'title' => $this->__('Google Base Item Type'),
00083 'required' => true,
00084 'text' => '<div id="gbase_itemtype_select">' . $itemTypesSelect->toHtml() . '</div>',
00085 ));
00086
00087 $attributesBlock = $this->getLayout()
00088 ->createBlock('googlebase/adminhtml_types_edit_attributes')
00089 ->setTargetCountry($targetCountry);
00090 if ($itemType->getId()) {
00091 $attributesBlock->setAttributeSetId($itemType->getAttributeSetId())
00092 ->setGbaseItemtype($itemType->getGbaseItemtype())
00093 ->setAttributeSetSelected(true);
00094 }
00095
00096 $attributes = Mage::registry('attributes');
00097 if (is_array($attributes) && count($attributes) > 0) {
00098 $attributesBlock->setAttributesData($attributes);
00099 }
00100
00101 $fieldset->addField('attributes_box', 'note', array(
00102 'label' => $this->__('Attributes mapping'),
00103 'text' => '<div id="attributes_details">' . $attributesBlock->toHtml() . '</div>',
00104 ));
00105
00106 $form->addValues($itemType->getData());
00107 $form->setUseContainer(true);
00108 $form->setId('edit_form');
00109 $form->setMethod('post');
00110 $form->setAction($this->getSaveUrl());
00111 $this->setForm($form);
00112 }
00113
00114 public function getAttributeSetsSelectElement($targetCountry)
00115 {
00116 $field = new Varien_Data_Form_Element_Select();
00117 $field->setName('attribute_set_id')
00118 ->setId('select_attribute_set')
00119 ->setForm(new Varien_Data_Form())
00120 ->addClass('required-entry')
00121 ->setValues($this->_getAttributeSetsArray($targetCountry));
00122 return $field;
00123 }
00124
00125 public function getItemTypesSelectElement($targetCountry)
00126 {
00127 $field = new Varien_Data_Form_Element_Select();
00128 $field->setName('gbase_itemtype')
00129 ->setId('select_itemtype')
00130 ->setForm(new Varien_Data_Form())
00131 ->addClass('required-entry')
00132 ->setValues($this->_getGbaseItemTypesArray($targetCountry));
00133 return $field;
00134 }
00135
00136 protected function _getCountriesArray()
00137 {
00138 $_allowed = Mage::getSingleton('googlebase/config')->getAllowedCountries();
00139 $result = array();
00140 foreach ($_allowed as $iso => $info) {
00141 $result[$iso] = $info['name'];
00142 }
00143 return $result;
00144 }
00145
00146 protected function _getAttributeSetsArray($targetCountry)
00147 {
00148 $entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
00149 $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
00150 ->setEntityTypeFilter($entityType->getId());
00151
00152 $ids = array();
00153 $itemType = $this->getItemType();
00154 if ( !($itemType instanceof Varien_Object && $itemType->getId()) ) {
00155 $typesCollection = Mage::getResourceModel('googlebase/type_collection')
00156 ->addCountryFilter($targetCountry)
00157 ->load();
00158 foreach ($typesCollection as $type) {
00159 $ids[] = $type->getAttributeSetId();
00160 }
00161 }
00162
00163 $result = array('' => '');
00164 foreach ($collection as $attributeSet) {
00165 if (!in_array($attributeSet->getId(), $ids)) {
00166 $result[$attributeSet->getId()] = $attributeSet->getAttributeSetName();
00167 }
00168 }
00169 return $result;
00170 }
00171
00172 protected function _getGbaseItemTypesArray($targetCountry)
00173 {
00174 $itemTypes = Mage::getModel('googlebase/service_feed')->getItemTypes($targetCountry);
00175 $result = array('' => '');
00176 foreach ($itemTypes as $type) {
00177 $result[$type->getId()] = $type->getName();
00178 }
00179 return $result;
00180 }
00181
00182 public function getItemType()
00183 {
00184 return Mage::registry('current_item_type');
00185 }
00186
00187 public function getSaveUrl()
00188 {
00189 return $this->getUrl('*/*/save', array('type_id' => $this->getItemType()->getId()));
00190 }
00191 }