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_TypesController extends Mage_Adminhtml_Controller_Action
00036 {
00037 protected function _initItemType()
00038 {
00039 Mage::register('current_item_type', Mage::getModel('googlebase/type'));
00040 $typeId = $this->getRequest()->getParam('id');
00041 if (!is_null($typeId)) {
00042 Mage::registry('current_item_type')->load($typeId);
00043 }
00044 }
00045
00046 protected function _initAction()
00047 {
00048 $this->loadLayout()
00049 ->_setActiveMenu('catalog/googlebase/types')
00050 ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
00051 ->_addBreadcrumb(Mage::helper('adminhtml')->__('Google Base'), Mage::helper('adminhtml')->__('Google Base'));
00052 return $this;
00053 }
00054
00055 public function indexAction()
00056 {
00057 $this->_initAction()
00058 ->_addBreadcrumb(Mage::helper('googlebase')->__('Item Types'), Mage::helper('googlebase')->__('Item Types'))
00059 ->_addContent($this->getLayout()->createBlock('googlebase/adminhtml_types'))
00060 ->renderLayout();
00061 }
00062
00063
00064
00065
00066 public function gridAction()
00067 {
00068 $this->getResponse()->setBody(
00069 $this->getLayout()->createBlock('googlebase/adminhtml_types_grid')->toHtml()
00070 );
00071 }
00072
00073 public function newAction()
00074 {
00075 try {
00076 $this->_initItemType();
00077 $this->_initAction()
00078 ->_addBreadcrumb(Mage::helper('googlebase')->__('New Item Type'), Mage::helper('adminhtml')->__('New Item Type'))
00079 ->_addContent($this->getLayout()->createBlock('googlebase/adminhtml_types_edit'))
00080 ->renderLayout();
00081 } catch (Exception $e) {
00082 $this->_getSession()->addError($e->getMessage());
00083 $this->_redirect('*/*/index', array('store' => $this->_getStore()->getId()));
00084 }
00085 }
00086
00087 public function editAction()
00088 {
00089 $id = $this->getRequest()->getParam('id');
00090 $model = Mage::getModel('googlebase/type');
00091
00092 try {
00093 $result = array();
00094 if ($id) {
00095 $model->load($id);
00096 $collection = Mage::getResourceModel('googlebase/attribute_collection')
00097 ->addTypeFilter($model->getTypeId())
00098 ->load();
00099 foreach ($collection as $attribute) {
00100 $result[] = $attribute->getData();
00101 }
00102 }
00103
00104 Mage::register('current_item_type', $model);
00105 Mage::register('attributes', $result);
00106
00107 $this->_initAction()
00108 ->_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'))
00109 ->_addContent($this->getLayout()->createBlock('googlebase/adminhtml_types_edit'))
00110 ->renderLayout();
00111 } catch (Exception $e) {
00112 $this->_getSession()->addError($e->getMessage());
00113 $this->_redirect('*/*/index');
00114 }
00115 }
00116
00117 public function saveAction()
00118 {
00119 $typeModel = Mage::getModel('googlebase/type');
00120 $id = $this->getRequest()->getParam('type_id');
00121 if (!is_null($id)) {
00122 $typeModel->load($id);
00123 }
00124
00125 try {
00126 if ($typeModel->getId()) {
00127 $collection = Mage::getResourceModel('googlebase/attribute_collection')
00128 ->addTypeFilter($typeModel->getId())
00129 ->load();
00130 foreach ($collection as $attribute) {
00131 $attribute->delete();
00132 }
00133 }
00134 $typeModel->setAttributeSetId($this->getRequest()->getParam('attribute_set_id'))
00135 ->setGbaseItemtype($this->getRequest()->getParam('gbase_itemtype'))
00136 ->setTargetCountry($this->getRequest()->getParam('target_country'))
00137 ->save();
00138
00139
00140 $attributes = $this->getRequest()->getParam('attributes');
00141 if (is_array($attributes)) {
00142 $typeId = $typeModel->getId();
00143 foreach ($attributes as $attrInfo) {
00144 if (isset($attrInfo['delete']) && $attrInfo['delete'] == 1) {
00145 continue;
00146 }
00147 Mage::getModel('googlebase/attribute')
00148 ->setAttributeId($attrInfo['attribute_id'])
00149 ->setGbaseAttribute($attrInfo['gbase_attribute'])
00150 ->setTypeId($typeId)
00151 ->save();
00152 }
00153 }
00154
00155 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('googlebase')->__('Item type was successfully saved'));
00156 } catch (Exception $e) {
00157 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00158 }
00159 $this->_redirect('*/*/index', array('store' => $this->_getStore()->getId()));
00160 }
00161
00162 public function deleteAction ()
00163 {
00164 try {
00165 $id = $this->getRequest()->getParam('id');
00166 $model = Mage::getModel('googlebase/type');
00167 $model->load($id);
00168 if ($model->getTypeId()) {
00169 $model->delete();
00170 }
00171 $this->_getSession()->addSuccess($this->__('Item Type was deleted'));
00172 } catch (Exception $e) {
00173 $this->_getSession()->addError($e->getMessage());
00174 }
00175 $this->_redirect('*/*/index', array('store' => $this->_getStore()->getId()));
00176 }
00177
00178 public function loadAttributesAction ()
00179 {
00180 try {
00181 $this->getResponse()->setBody(
00182 $this->getLayout()->createBlock('googlebase/adminhtml_types_edit_attributes')
00183 ->setAttributeSetId($this->getRequest()->getParam('attribute_set_id'))
00184 ->setGbaseItemtype($this->getRequest()->getParam('gbase_itemtype'))
00185 ->setTargetCountry($this->getRequest()->getParam('target_country'))
00186 ->setAttributeSetSelected(true)
00187 ->toHtml()
00188 );
00189 } catch (Exception $e) {
00190
00191 $this->_getSession()->addError($e->getMessage());
00192 }
00193 }
00194
00195 public function loadItemTypesAction()
00196 {
00197 try {
00198 $this->getResponse()->setBody(
00199 $this->getLayout()->getBlockSingleton('googlebase/adminhtml_types_edit_form')
00200 ->getItemTypesSelectElement($this->getRequest()->getParam('target_country'))
00201 ->toHtml()
00202 );
00203 } catch (Exception $e) {
00204
00205 $this->_getSession()->addError($e->getMessage());
00206 }
00207 }
00208
00209 protected function loadAttributeSetsAction()
00210 {
00211 try {
00212 $this->getResponse()->setBody(
00213 $this->getLayout()->getBlockSingleton('googlebase/adminhtml_types_edit_form')
00214 ->getAttributeSetsSelectElement($this->getRequest()->getParam('target_country'))
00215 ->toHtml()
00216 );
00217 } catch (Exception $e) {
00218
00219 $this->_getSession()->addError($e->getMessage());
00220 }
00221 }
00222
00223 public function _getStore()
00224 {
00225 $storeId = (int) $this->getRequest()->getParam('store', 0);
00226 if ($storeId == 0) {
00227 return Mage::app()->getDefaultStoreView();
00228 }
00229 return Mage::app()->getStore($storeId);
00230 }
00231
00232 protected function _isAllowed()
00233 {
00234 return Mage::getSingleton('admin/session')->isAllowed('catalog/googlebase/types');
00235 }
00236 }