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_ItemsController extends Mage_Adminhtml_Controller_Action
00036 {
00037 protected function _initAction()
00038 {
00039 $this->loadLayout()
00040 ->_setActiveMenu('catalog/googlebase/items')
00041 ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
00042 ->_addBreadcrumb(Mage::helper('adminhtml')->__('Google Base'), Mage::helper('adminhtml')->__('Google Base'));
00043 return $this;
00044 }
00045
00046 public function indexAction()
00047 {
00048 if (0 === (int)$this->getRequest()->getParam('store')) {
00049 $this->_redirect('*/*/', array('store' => Mage::app()->getAnyStoreView()->getId(), '_current' => true));
00050 return;
00051 }
00052 $contentBlock = $this->getLayout()->createBlock('googlebase/adminhtml_items')->setStore($this->_getStore());
00053
00054 if ($this->getRequest()->getParam('captcha_token') && $this->getRequest()->getParam('captcha_url')) {
00055 $contentBlock->setGbaseCaptchaToken(
00056 Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_token'))
00057 );
00058 $contentBlock->setGbaseCaptchaUrl(
00059 Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_url'))
00060 );
00061 }
00062
00063 if (!$this->_getConfig()->isValidBaseCurrencyCode($this->_getStore()->getId())) {
00064 $_countryInfo = $this->_getConfig()->getTargetCountryInfo($this->_getStore()->getId());
00065 $this->_getSession()->addNotice(
00066 $this->__(
00067 "Base Currency should be set to %s for %s in system configuration. Otherwise item prices won't be correct in Google Base.",
00068 $_countryInfo['currency_name'],
00069 $_countryInfo['name']
00070 )
00071 );
00072 }
00073
00074 $this->_initAction()
00075 ->_addBreadcrumb(Mage::helper('googlebase')->__('Items'), Mage::helper('googlebase')->__('Items'))
00076 ->_addContent($contentBlock)
00077 ->renderLayout();
00078 }
00079
00080 public function gridAction()
00081 {
00082 $this->loadLayout();
00083 return $this->getResponse()->setBody(
00084 $this->getLayout()
00085 ->createBlock('googlebase/adminhtml_items_item')
00086 ->setIndex($this->getRequest()->getParam('index'))
00087 ->toHtml()
00088 );
00089 }
00090
00091 public function massAddAction()
00092 {
00093 $storeId = $this->_getStore()->getId();
00094 $productIds = $this->getRequest()->getParam('product');
00095
00096 $totalAdded = 0;
00097
00098 try {
00099 foreach ($productIds as $productId) {
00100 $product = Mage::getSingleton('catalog/product')
00101 ->setStoreId($storeId)
00102 ->load($productId);
00103
00104 if ($product->getId()) {
00105 Mage::getModel('googlebase/item')
00106 ->setProduct($product)
00107 ->insertItem()
00108 ->save();
00109
00110 $totalAdded++;
00111 }
00112 }
00113 if ($totalAdded > 0) {
00114 $this->_getSession()->addSuccess(
00115 $this->__('Total of %d product(s) were successfully added to Google Base', $totalAdded)
00116 );
00117 } else {
00118 $this->_getSession()->addError($this->__('No products were added to Google Base'));
00119 }
00120 } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
00121 $this->_getSession()->addError($e->getMessage());
00122 $this->_redirectToCaptcha($e);
00123 return;
00124 } catch (Zend_Gdata_App_Exception $e) {
00125 $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
00126 } catch (Exception $e) {
00127 $this->_getSession()->addError($e->getMessage());
00128 }
00129
00130 $this->_redirect('*/*/index', array('store'=>$storeId));
00131 }
00132
00133 public function massDeleteAction()
00134 {
00135 $storeId = $this->_getStore()->getId();
00136 $itemIds = $this->getRequest()->getParam('item');
00137
00138 $totalDeleted = 0;
00139
00140 try {
00141 foreach ($itemIds as $itemId) {
00142 $item = Mage::getModel('googlebase/item')->load($itemId);
00143 if ($item->getId()) {
00144 $item->deleteItem();
00145 $item->delete();
00146 $totalDeleted++;
00147 }
00148 }
00149 if ($totalDeleted > 0) {
00150 $this->_getSession()->addSuccess(
00151 $this->__('Total of %d items(s) were successfully removed from Google Base', $totalDeleted)
00152 );
00153 } else {
00154 $this->_getSession()->addError($this->__('No items were deleted from Google Base'));
00155 }
00156 } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
00157 $this->_getSession()->addError($e->getMessage());
00158 $this->_redirectToCaptcha($e);
00159 return;
00160 } catch (Zend_Gdata_App_Exception $e) {
00161 $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
00162 } catch (Exception $e) {
00163 $this->_getSession()->addError($e->getMessage());
00164 }
00165
00166 $this->_redirect('*/*/index', array('store'=>$storeId));
00167 }
00168
00169 public function massPublishAction()
00170 {
00171 $storeId = $this->_getStore()->getId();
00172 $itemIds = $this->getRequest()->getParam('item');
00173
00174 $totalPublished = 0;
00175
00176 try {
00177 foreach ($itemIds as $itemId) {
00178 $item = Mage::getModel('googlebase/item')->load($itemId);
00179 if ($item->getId()) {
00180 $item->activateItem();
00181 $totalPublished++;
00182 }
00183 }
00184 if ($totalPublished > 0) {
00185 $this->_getSession()->addSuccess(
00186 $this->__('Total of %d items(s) were successfully published', $totalPublished)
00187 );
00188 } else {
00189 $this->_getSession()->addError($this->__('No items were published'));
00190 }
00191 } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
00192 $this->_getSession()->addError($e->getMessage());
00193 $this->_redirectToCaptcha($e);
00194 return;
00195 } catch (Zend_Gdata_App_Exception $e) {
00196 $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
00197 } catch (Exception $e) {
00198 $this->_getSession()->addError($e->getMessage());
00199 }
00200
00201 $this->_redirect('*/*/index', array('store'=>$storeId));
00202 }
00203
00204 public function massHideAction()
00205 {
00206 $storeId = $this->_getStore()->getId();
00207 $itemIds = $this->getRequest()->getParam('item');
00208
00209 $totalHidden = 0;
00210
00211 try {
00212 foreach ($itemIds as $itemId) {
00213 $item = Mage::getModel('googlebase/item')->load($itemId);
00214 if ($item->getId()) {
00215 $item->hideItem();
00216 $totalHidden++;
00217 }
00218 }
00219 if ($totalHidden > 0) {
00220 $this->_getSession()->addSuccess(
00221 $this->__('Total of %d items(s) were successfully saved as Inactive items', $totalHidden)
00222 );
00223 } else {
00224 $this->_getSession()->addError($this->__('No items were saved as Inactive items'));
00225 }
00226 } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
00227 $this->_getSession()->addError($e->getMessage());
00228 $this->_redirectToCaptcha($e);
00229 return;
00230 } catch (Zend_Gdata_App_Exception $e) {
00231 $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
00232 } catch (Exception $e) {
00233 $this->_getSession()->addError($e->getMessage());
00234 }
00235
00236 $this->_redirect('*/*/index', array('store'=>$storeId));
00237 }
00238
00239
00240
00241
00242 public function refreshAction()
00243 {
00244 $storeId = $this->_getStore()->getId();
00245 $totalUpdated = 0;
00246 $totalDeleted = 0;
00247
00248 try {
00249 $itemIds = $this->getRequest()->getParam('item');
00250 foreach ($itemIds as $itemId) {
00251 $item = Mage::getModel('googlebase/item')->load($itemId);
00252
00253 $stats = Mage::getSingleton('googlebase/service_feed')->getItemStats($item->getGbaseItemId(), $storeId);
00254 if ($stats === null) {
00255 $item->delete();
00256 $totalDeleted++;
00257 continue;
00258 }
00259
00260 if ($stats['draft'] != $item->getIsHidden()) {
00261 $item->setIsHidden($stats['draft']);
00262 }
00263
00264 if (isset($stats['expires'])) {
00265 $item->setExpires($stats['expires']);
00266 }
00267
00268 $item->save();
00269 $totalUpdated++;
00270 }
00271
00272 $this->_getSession()->addSuccess(
00273 $this->__('Total of %d items(s) were successfully deleted, Total of %d items(s) were successfully updated', $totalDeleted, $totalUpdated)
00274 );
00275
00276 } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
00277 $this->_getSession()->addError($e->getMessage());
00278 $this->_redirectToCaptcha($e);
00279 return;
00280 } catch (Zend_Gdata_App_Exception $e) {
00281 $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
00282 } catch (Exception $e) {
00283 $this->_getSession()->addError($e->getMessage());
00284 }
00285
00286 $this->_redirect('*/*/index', array('store'=>$storeId));
00287 }
00288
00289 public function confirmCaptchaAction()
00290 {
00291 $storeId = $this->_getStore()->getId();
00292 try {
00293 Mage::getModel('googlebase/service')->getClient(
00294 $storeId,
00295 Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_token')),
00296 $this->getRequest()->getParam('user_confirm')
00297 );
00298 $this->_getSession()->addSuccess($this->__('Captcha confirmed successfully'));
00299
00300 } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
00301 $this->_getSession()->addError($this->__('Captcha confirmation error: %s', $e->getMessage()));
00302 $this->_redirectToCaptcha($e);
00303 return;
00304 } catch (Zend_Gdata_App_Exception $e) {
00305 $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
00306 } catch (Exception $e) {
00307 $this->_getSession()->addError($this->__('Captcha confirmation error: %s', $e->getMessage()));
00308 }
00309
00310 $this->_redirect('*/*/index', array('store'=>$storeId));
00311 }
00312
00313
00314
00315
00316
00317
00318 protected function _redirectToCaptcha($e)
00319 {
00320 $this->_redirect('*/*/index',
00321 array('store' => $this->_getStore()->getId(),
00322 'captcha_token' => Mage::helper('core')->urlEncode($e->getCaptchaToken()),
00323 'captcha_url' => Mage::helper('core')->urlEncode($e->getCaptchaUrl())
00324 )
00325 );
00326 }
00327
00328
00329
00330
00331
00332
00333
00334 public function _getStore()
00335 {
00336 $store = Mage::app()->getStore((int)$this->getRequest()->getParam('store', 0));
00337 if ((!$store) || 0 == $store->getId()) {
00338 Mage::throwException($this->__('Unable to select a Store View'));
00339 }
00340 return $store;
00341 }
00342
00343 protected function _getConfig()
00344 {
00345 return Mage::getSingleton('googlebase/config');
00346 }
00347
00348 protected function _isAllowed()
00349 {
00350 return Mage::getSingleton('admin/session')->isAllowed('catalog/googlebase/items');
00351 }
00352
00353
00354
00355
00356
00357
00358
00359 protected function _parseGdataExceptionMessage($message)
00360 {
00361 $result = array();
00362 foreach (explode("\n", $message) as $row) {
00363 if (strip_tags($row) == $row) {
00364 $result[] = $row;
00365 continue;
00366 }
00367 try {
00368 $xml = new Varien_Simplexml_Element($row);
00369 $error = $xml->getAttribute('reason');
00370 $result[] = $error;
00371 } catch (Exception $e) {
00372 continue;
00373 }
00374 }
00375 return implode(" ", $result);
00376 }
00377 }