1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34:
35: class Mage_GoogleBase_Block_Adminhtml_Types_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
36: {
37: protected function _prepareForm()
38: {
39: $form = new Varien_Data_Form();
40:
41: $itemType = $this->getItemType();
42:
43: $fieldset = $form->addFieldset('base_fieldset', array(
44: 'legend' => $this->__('Attribute Set and Item Type')
45: ));
46:
47: if ( !($targetCountry = $itemType->getTargetCountry()) ) {
48: $isoKeys = array_keys($this->_getCountriesArray());
49: $targetCountry = isset($isoKeys[0]) ? $isoKeys[0] : null;
50: }
51: $countrySelect = $fieldset->addField('select_target_country', 'select', array(
52: 'label' => $this->__('Target Country'),
53: 'title' => $this->__('Target Country'),
54: 'name' => 'target_country',
55: 'required' => true,
56: 'options' => $this->_getCountriesArray(),
57: 'value' => $targetCountry,
58: ));
59: if ($itemType->getTargetCountry()) {
60: $countrySelect->setDisabled(true);
61: }
62:
63: $attributeSetsSelect = $this->getAttributeSetsSelectElement($targetCountry)->setValue($itemType->getAttributeSetId());
64: if ($itemType->getAttributeSetId()) {
65: $attributeSetsSelect->setDisabled(true);
66: }
67:
68: $fieldset->addField('attribute_set', 'note', array(
69: 'label' => $this->__('Attribute Set'),
70: 'title' => $this->__('Attribute Set'),
71: 'required' => true,
72: 'text' => '<div id="attribute_set_select">' . $attributeSetsSelect->toHtml() . '</div>',
73: ));
74:
75: $itemTypesSelect = $this->getItemTypesSelectElement($targetCountry)->setValue($itemType->getGbaseItemtype());
76: if ($itemType->getGbaseItemtype()) {
77: $itemTypesSelect->setDisabled(true);
78: }
79:
80: $fieldset->addField('itemtype', 'note', array(
81: 'label' => $this->__('Google Base Item Type'),
82: 'title' => $this->__('Google Base Item Type'),
83: 'required' => true,
84: 'text' => '<div id="gbase_itemtype_select">' . $itemTypesSelect->toHtml() . '</div>',
85: ));
86:
87: $attributesBlock = $this->getLayout()
88: ->createBlock('googlebase/adminhtml_types_edit_attributes')
89: ->setTargetCountry($targetCountry);
90: if ($itemType->getId()) {
91: $attributesBlock->setAttributeSetId($itemType->getAttributeSetId())
92: ->setGbaseItemtype($itemType->getGbaseItemtype())
93: ->setAttributeSetSelected(true);
94: }
95:
96: $attributes = Mage::registry('attributes');
97: if (is_array($attributes) && count($attributes) > 0) {
98: $attributesBlock->setAttributesData($attributes);
99: }
100:
101: $fieldset->addField('attributes_box', 'note', array(
102: 'label' => $this->__('Attributes Mapping'),
103: 'text' => '<div id="attributes_details">' . $attributesBlock->toHtml() . '</div>',
104: ));
105:
106: $form->addValues($itemType->getData());
107: $form->setUseContainer(true);
108: $form->setId('edit_form');
109: $form->setMethod('post');
110: $form->setAction($this->getSaveUrl());
111: $this->setForm($form);
112: }
113:
114: public function getAttributeSetsSelectElement($targetCountry)
115: {
116: $field = new Varien_Data_Form_Element_Select();
117: $field->setName('attribute_set_id')
118: ->setId('select_attribute_set')
119: ->setForm(new Varien_Data_Form())
120: ->addClass('required-entry')
121: ->setValues($this->_getAttributeSetsArray($targetCountry));
122: return $field;
123: }
124:
125: public function getItemTypesSelectElement($targetCountry)
126: {
127: $field = new Varien_Data_Form_Element_Select();
128: $field->setName('gbase_itemtype')
129: ->setId('select_itemtype')
130: ->setForm(new Varien_Data_Form())
131: ->addClass('required-entry')
132: ->setValues($this->_getGbaseItemTypesArray($targetCountry));
133: return $field;
134: }
135:
136: protected function _getCountriesArray()
137: {
138: $_allowed = Mage::getSingleton('googlebase/config')->getAllowedCountries();
139: $result = array();
140: foreach ($_allowed as $iso => $info) {
141: $result[$iso] = $info['name'];
142: }
143: return $result;
144: }
145:
146: protected function _getAttributeSetsArray($targetCountry)
147: {
148: $entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
149: $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
150: ->setEntityTypeFilter($entityType->getId());
151:
152: $ids = array();
153: $itemType = $this->getItemType();
154: if ( !($itemType instanceof Varien_Object && $itemType->getId()) ) {
155: $typesCollection = Mage::getResourceModel('googlebase/type_collection')
156: ->addCountryFilter($targetCountry)
157: ->load();
158: foreach ($typesCollection as $type) {
159: $ids[] = $type->getAttributeSetId();
160: }
161: }
162:
163: $result = array('' => '');
164: foreach ($collection as $attributeSet) {
165: if (!in_array($attributeSet->getId(), $ids)) {
166: $result[$attributeSet->getId()] = $attributeSet->getAttributeSetName();
167: }
168: }
169: return $result;
170: }
171:
172: protected function _getGbaseItemTypesArray($targetCountry)
173: {
174: $itemTypes = Mage::getModel('googlebase/service_feed')->getItemTypes($targetCountry);
175: $result = array('' => '');
176: foreach ($itemTypes as $type) {
177: $result[$type->getId()] = $type->getName();
178: }
179: return $result;
180: }
181:
182: public function getItemType()
183: {
184: return Mage::registry('current_item_type');
185: }
186:
187: public function getSaveUrl()
188: {
189: return $this->getUrl('*/*/save', array('type_id' => $this->getItemType()->getId()));
190: }
191: }
192: