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_Adminhtml_Googlebase_TypesController extends Mage_Adminhtml_Controller_Action
36: {
37: 38: 39:
40: public function postDispatch()
41: {
42: parent::postDispatch();
43: if ($this->getFlag('', self::FLAG_NO_POST_DISPATCH)) {
44: return;
45: }
46: Mage::dispatchEvent('controller_action_postdispatch_adminhtml', array('controller_action' => $this));
47: }
48:
49: protected function _initItemType()
50: {
51: $this->_title($this->__('Catalog'))
52: ->_title($this->__('Google Base'))
53: ->_title($this->__('Manage Attributes'));
54:
55: Mage::register('current_item_type', Mage::getModel('googlebase/type'));
56: $typeId = $this->getRequest()->getParam('id');
57: if (!is_null($typeId)) {
58: Mage::registry('current_item_type')->load($typeId);
59: }
60: }
61:
62: protected function _initAction()
63: {
64: $this->loadLayout()
65: ->_setActiveMenu('catalog/googlebase/types')
66: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
67: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Google Base'), Mage::helper('adminhtml')->__('Google Base'));
68: return $this;
69: }
70:
71: public function indexAction()
72: {
73: $this->_title($this->__('Catalog'))
74: ->_title($this->__('Google base'))
75: ->_title($this->__('Manage Attributes'));
76:
77: $this->_initAction()
78: ->_addBreadcrumb(Mage::helper('googlebase')->__('Item Types'), Mage::helper('googlebase')->__('Item Types'))
79: ->_addContent($this->getLayout()->createBlock('googlebase/adminhtml_types'))
80: ->renderLayout();
81: }
82:
83: 84: 85:
86: public function gridAction()
87: {
88: $this->getResponse()->setBody(
89: $this->getLayout()->createBlock('googlebase/adminhtml_types_grid')->toHtml()
90: );
91: }
92:
93: public function newAction()
94: {
95: try {
96: $this->_initItemType();
97:
98: $this->_title($this->__('New ItemType'));
99:
100: $this->_initAction()
101: ->_addBreadcrumb(Mage::helper('googlebase')->__('New Item Type'), Mage::helper('adminhtml')->__('New Item Type'))
102: ->_addContent($this->getLayout()->createBlock('googlebase/adminhtml_types_edit'))
103: ->renderLayout();
104: } catch (Exception $e) {
105: $this->_getSession()->addError($e->getMessage());
106: $this->_redirect('*/*/index', array('store' => $this->_getStore()->getId()));
107: }
108: }
109:
110: public function editAction()
111: {
112: $this->_title($this->__('Catalog'))
113: ->_title($this->__('Google base'))
114: ->_title($this->__('Manage Attributes'));
115:
116: $id = $this->getRequest()->getParam('id');
117: $model = Mage::getModel('googlebase/type');
118:
119: try {
120: $result = array();
121: if ($id) {
122: $model->load($id);
123: $collection = Mage::getResourceModel('googlebase/attribute_collection')
124: ->addTypeFilter($model->getTypeId())
125: ->load();
126: foreach ($collection as $attribute) {
127: $result[] = $attribute->getData();
128: }
129: }
130:
131: $this->_title($this->__('Edit Item Type'));
132:
133: Mage::register('current_item_type', $model);
134: Mage::register('attributes', $result);
135:
136: $this->_initAction()
137: ->_addBreadcrumb($id ? Mage::helper('googlebase')->__('Edit Item Type') : Mage::helper('googlebase')->__('New Item Type'), $id ? Mage::helper('googlebase')->__('Edit Item Type') : Mage::helper('googlebase')->__('New Item Type'))
138: ->_addContent($this->getLayout()->createBlock('googlebase/adminhtml_types_edit'))
139: ->renderLayout();
140: } catch (Exception $e) {
141: $this->_getSession()->addError($e->getMessage());
142: $this->_redirect('*/*/index');
143: }
144: }
145:
146: public function saveAction()
147: {
148: $typeModel = Mage::getModel('googlebase/type');
149: $id = $this->getRequest()->getParam('type_id');
150: if (!is_null($id)) {
151: $typeModel->load($id);
152: }
153:
154: try {
155: if ($typeModel->getId()) {
156: $collection = Mage::getResourceModel('googlebase/attribute_collection')
157: ->addTypeFilter($typeModel->getId())
158: ->load();
159: foreach ($collection as $attribute) {
160: $attribute->delete();
161: }
162: }
163: $typeModel->setAttributeSetId($this->getRequest()->getParam('attribute_set_id'))
164: ->setGbaseItemtype($this->getRequest()->getParam('gbase_itemtype'))
165: ->setTargetCountry($this->getRequest()->getParam('target_country'))
166: ->save();
167:
168:
169: $attributes = $this->getRequest()->getParam('attributes');
170: if (is_array($attributes)) {
171: $typeId = $typeModel->getId();
172: foreach ($attributes as $attrInfo) {
173: if (isset($attrInfo['delete']) && $attrInfo['delete'] == 1) {
174: continue;
175: }
176: Mage::getModel('googlebase/attribute')
177: ->setAttributeId($attrInfo['attribute_id'])
178: ->setGbaseAttribute($attrInfo['gbase_attribute'])
179: ->setTypeId($typeId)
180: ->save();
181: }
182: }
183:
184: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('googlebase')->__('The item type has been saved.'));
185: } catch (Exception $e) {
186: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
187: }
188: $this->_redirect('*/*/index', array('store' => $this->_getStore()->getId()));
189: }
190:
191: public function deleteAction ()
192: {
193: try {
194: $id = $this->getRequest()->getParam('id');
195: $model = Mage::getModel('googlebase/type');
196: $model->load($id);
197: if ($model->getTypeId()) {
198: $model->delete();
199: }
200: $this->_getSession()->addSuccess($this->__('Item Type was deleted'));
201: } catch (Exception $e) {
202: $this->_getSession()->addError($e->getMessage());
203: }
204: $this->_redirect('*/*/index', array('store' => $this->_getStore()->getId()));
205: }
206:
207: public function loadAttributesAction ()
208: {
209: try {
210: $this->getResponse()->setBody(
211: $this->getLayout()->createBlock('googlebase/adminhtml_types_edit_attributes')
212: ->setAttributeSetId($this->getRequest()->getParam('attribute_set_id'))
213: ->setGbaseItemtype($this->getRequest()->getParam('gbase_itemtype'))
214: ->setTargetCountry($this->getRequest()->getParam('target_country'))
215: ->setAttributeSetSelected(true)
216: ->toHtml()
217: );
218: } catch (Exception $e) {
219:
220: $this->_getSession()->addError($e->getMessage());
221: }
222: }
223:
224: public function loadItemTypesAction()
225: {
226: try {
227: $this->getResponse()->setBody(
228: $this->getLayout()->getBlockSingleton('googlebase/adminhtml_types_edit_form')
229: ->getItemTypesSelectElement($this->getRequest()->getParam('target_country'))
230: ->toHtml()
231: );
232: } catch (Exception $e) {
233:
234: $this->_getSession()->addError($e->getMessage());
235: }
236: }
237:
238: protected function loadAttributeSetsAction()
239: {
240: try {
241: $this->getResponse()->setBody(
242: $this->getLayout()->getBlockSingleton('googlebase/adminhtml_types_edit_form')
243: ->getAttributeSetsSelectElement($this->getRequest()->getParam('target_country'))
244: ->toHtml()
245: );
246: } catch (Exception $e) {
247:
248: $this->_getSession()->addError($e->getMessage());
249: }
250: }
251:
252: public function _getStore()
253: {
254: $storeId = (int) $this->getRequest()->getParam('store', 0);
255: if ($storeId == 0) {
256: return Mage::app()->getDefaultStoreView();
257: }
258: return Mage::app()->getStore($storeId);
259: }
260:
261: protected function _isAllowed()
262: {
263: return Mage::getSingleton('admin/session')->isAllowed('catalog/googlebase/types');
264: }
265: }
266: