PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/GoogleBase/controllers/Adminhtml/Googlebase/ItemsController.php

https://github.com/rgranadino/magento-mirror
PHP | 399 lines | 245 code | 10 blank | 144 comment | 18 complexity | 9c680e13d2073f2c149d06ab57707166 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_GoogleBase
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * GoogleBase Admin Items Controller
  28. *
  29. * @category Mage
  30. * @package Mage_GoogleBase
  31. * @name Mage_GoogleBase_Adminhtml_Googlebase_ItemsController
  32. * @author Magento Core Team <core@magentocommerce.com>
  33. */
  34. class Mage_GoogleBase_Adminhtml_Googlebase_ItemsController extends Mage_Adminhtml_Controller_Action
  35. {
  36. protected function _initAction()
  37. {
  38. $this->loadLayout()
  39. ->_setActiveMenu('catalog/googlebase/items')
  40. ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
  41. ->_addBreadcrumb(Mage::helper('adminhtml')->__('Google Base'), Mage::helper('adminhtml')->__('Google Base'));
  42. return $this;
  43. }
  44. public function indexAction()
  45. {
  46. $this->_title($this->__('Catalog'))
  47. ->_title($this->__('Google base'))
  48. ->_title($this->__('Manage Items'));
  49. if (0 === (int)$this->getRequest()->getParam('store')) {
  50. $this->_redirect('*/*/', array('store' => Mage::app()->getAnyStoreView()->getId(), '_current' => true));
  51. return;
  52. }
  53. $contentBlock = $this->getLayout()->createBlock('googlebase/adminhtml_items')->setStore($this->_getStore());
  54. if ($this->getRequest()->getParam('captcha_token') && $this->getRequest()->getParam('captcha_url')) {
  55. $contentBlock->setGbaseCaptchaToken(
  56. Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_token'))
  57. );
  58. $contentBlock->setGbaseCaptchaUrl(
  59. Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_url'))
  60. );
  61. }
  62. if (!$this->_getConfig()->isValidBaseCurrencyCode($this->_getStore()->getId())) {
  63. $_countryInfo = $this->_getConfig()->getTargetCountryInfo($this->_getStore()->getId());
  64. $this->_getSession()->addNotice(
  65. $this->__("Base Currency should be set to %s for %s in system configuration. Otherwise item prices won't be correct in Google Base.",$_countryInfo['currency_name'],$_countryInfo['name'])
  66. );
  67. }
  68. $this->_initAction()
  69. ->_addBreadcrumb(Mage::helper('googlebase')->__('Items'), Mage::helper('googlebase')->__('Items'))
  70. ->_addContent($contentBlock)
  71. ->renderLayout();
  72. }
  73. public function gridAction()
  74. {
  75. $this->loadLayout();
  76. return $this->getResponse()->setBody(
  77. $this->getLayout()
  78. ->createBlock('googlebase/adminhtml_items_item')
  79. ->setIndex($this->getRequest()->getParam('index'))
  80. ->toHtml()
  81. );
  82. }
  83. public function massAddAction()
  84. {
  85. $storeId = $this->_getStore()->getId();
  86. $productIds = $this->getRequest()->getParam('product', null);
  87. $totalAdded = 0;
  88. try {
  89. if (is_array($productIds)) {
  90. foreach ($productIds as $productId) {
  91. $product = Mage::getSingleton('catalog/product')
  92. ->setStoreId($storeId)
  93. ->load($productId);
  94. if ($product->getId()) {
  95. Mage::getModel('googlebase/item')
  96. ->setProduct($product)
  97. ->insertItem()
  98. ->save();
  99. $totalAdded++;
  100. }
  101. }
  102. }
  103. if ($totalAdded > 0) {
  104. $this->_getSession()->addSuccess(
  105. $this->__('Total of %d product(s) have been added to Google Base.', $totalAdded)
  106. );
  107. } elseif (is_null($productIds)) {
  108. $this->_getSession()->addError($this->__('Session expired during export. Please revise exported products and repeat the process if necessary.'));
  109. } else {
  110. $this->_getSession()->addError($this->__('No products were added to Google Base'));
  111. }
  112. } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
  113. $this->_getSession()->addError($e->getMessage());
  114. $this->_redirectToCaptcha($e);
  115. return;
  116. } catch (Zend_Gdata_App_Exception $e) {
  117. $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
  118. } catch (Exception $e) {
  119. $this->_getSession()->addError($e->getMessage());
  120. }
  121. $this->_redirect('*/*/index', array('store'=>$storeId));
  122. }
  123. public function massDeleteAction()
  124. {
  125. $storeId = $this->_getStore()->getId();
  126. $itemIds = $this->getRequest()->getParam('item');
  127. $totalDeleted = 0;
  128. try {
  129. foreach ($itemIds as $itemId) {
  130. $item = Mage::getModel('googlebase/item')->load($itemId);
  131. if ($item->getId()) {
  132. $item->deleteItem();
  133. $item->delete();
  134. $totalDeleted++;
  135. }
  136. }
  137. if ($totalDeleted > 0) {
  138. $this->_getSession()->addSuccess(
  139. $this->__('Total of %d items(s) have been removed from Google Base.', $totalDeleted)
  140. );
  141. } else {
  142. $this->_getSession()->addError($this->__('No items were deleted from Google Base'));
  143. }
  144. } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
  145. $this->_getSession()->addError($e->getMessage());
  146. $this->_redirectToCaptcha($e);
  147. return;
  148. } catch (Zend_Gdata_App_Exception $e) {
  149. $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
  150. } catch (Exception $e) {
  151. $this->_getSession()->addError($e->getMessage());
  152. }
  153. $this->_redirect('*/*/index', array('store'=>$storeId));
  154. }
  155. public function massPublishAction()
  156. {
  157. $storeId = $this->_getStore()->getId();
  158. $itemIds = $this->getRequest()->getParam('item');
  159. $totalPublished = 0;
  160. try {
  161. if (!empty($itemIds) && is_array($itemIds)) {
  162. foreach ($itemIds as $itemId) {
  163. $item = Mage::getModel('googlebase/item')->load($itemId);
  164. if ($item->getId()) {
  165. $item->activateItem();
  166. $totalPublished++;
  167. }
  168. }
  169. }
  170. if ($totalPublished > 0) {
  171. $this->_getSession()->addSuccess(
  172. $this->__('Total of %d items(s) have been published.', $totalPublished)
  173. );
  174. } else {
  175. $this->_getSession()->addError($this->__('No items were published'));
  176. }
  177. } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
  178. $this->_getSession()->addError($e->getMessage());
  179. $this->_redirectToCaptcha($e);
  180. return;
  181. } catch (Zend_Gdata_App_Exception $e) {
  182. $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
  183. } catch (Exception $e) {
  184. $this->_getSession()->addError($e->getMessage());
  185. }
  186. $this->_redirect('*/*/index', array('store'=>$storeId));
  187. }
  188. public function massHideAction()
  189. {
  190. $storeId = $this->_getStore()->getId();
  191. $itemIds = $this->getRequest()->getParam('item');
  192. $totalHidden = 0;
  193. try {
  194. foreach ($itemIds as $itemId) {
  195. $item = Mage::getModel('googlebase/item')->load($itemId);
  196. if ($item->getId()) {
  197. $item->hideItem();
  198. $totalHidden++;
  199. }
  200. }
  201. if ($totalHidden > 0) {
  202. $this->_getSession()->addSuccess(
  203. $this->__('Total of %d items(s) have been saved as inactive items.', $totalHidden)
  204. );
  205. } else {
  206. $this->_getSession()->addError($this->__('No items were saved as inactive items'));
  207. }
  208. } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
  209. $this->_getSession()->addError($e->getMessage());
  210. $this->_redirectToCaptcha($e);
  211. return;
  212. } catch (Zend_Gdata_App_Exception $e) {
  213. $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
  214. } catch (Exception $e) {
  215. $this->_getSession()->addError($e->getMessage());
  216. }
  217. $this->_redirect('*/*/index', array('store'=>$storeId));
  218. }
  219. /**
  220. * Update items statistics and remove the items which are not available in Google Base
  221. */
  222. public function refreshAction()
  223. {
  224. $storeId = $this->_getStore()->getId();
  225. $totalUpdated = 0;
  226. $totalDeleted = 0;
  227. try {
  228. $itemIds = $this->getRequest()->getParam('item');
  229. foreach ($itemIds as $itemId) {
  230. $item = Mage::getModel('googlebase/item')->load($itemId);
  231. $stats = Mage::getSingleton('googlebase/service_feed')->getItemStats($item->getGbaseItemId(), $storeId);
  232. if ($stats === null) {
  233. $item->delete();
  234. $totalDeleted++;
  235. continue;
  236. }
  237. if ($stats['draft'] != $item->getIsHidden()) {
  238. $item->setIsHidden($stats['draft']);
  239. }
  240. if (isset($stats['clicks'])) {
  241. $item->setClicks($stats['clicks']);
  242. }
  243. if (isset($stats['impressions'])) {
  244. $item->setImpr($stats['impressions']);
  245. }
  246. if (isset($stats['expires'])) {
  247. $item->setExpires($stats['expires']);
  248. }
  249. $item->save();
  250. $totalUpdated++;
  251. }
  252. $this->_getSession()->addSuccess(
  253. $this->__('Total of %d items(s) have been deleted; total of %d items(s) have been updated.', $totalDeleted, $totalUpdated)
  254. );
  255. } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
  256. $this->_getSession()->addError($e->getMessage());
  257. $this->_redirectToCaptcha($e);
  258. return;
  259. } catch (Zend_Gdata_App_Exception $e) {
  260. $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
  261. } catch (Exception $e) {
  262. $this->_getSession()->addError($e->getMessage());
  263. }
  264. $this->_redirect('*/*/index', array('store'=>$storeId));
  265. }
  266. public function confirmCaptchaAction()
  267. {
  268. $storeId = $this->_getStore()->getId();
  269. try {
  270. Mage::getModel('googlebase/service')->getClient(
  271. $storeId,
  272. Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_token')),
  273. $this->getRequest()->getParam('user_confirm')
  274. );
  275. $this->_getSession()->addSuccess($this->__('Captcha has been confirmed.'));
  276. } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
  277. $this->_getSession()->addError($this->__('Captcha confirmation error: %s', $e->getMessage()));
  278. $this->_redirectToCaptcha($e);
  279. return;
  280. } catch (Zend_Gdata_App_Exception $e) {
  281. $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
  282. } catch (Exception $e) {
  283. $this->_getSession()->addError($this->__('Captcha confirmation error: %s', $e->getMessage()));
  284. }
  285. $this->_redirect('*/*/index', array('store'=>$storeId));
  286. }
  287. /**
  288. * Redirect user to Google Captcha challenge
  289. *
  290. * @param Zend_Gdata_App_CaptchaRequiredException $e
  291. */
  292. protected function _redirectToCaptcha($e)
  293. {
  294. $this->_redirect('*/*/index',
  295. array('store' => $this->_getStore()->getId(),
  296. 'captcha_token' => Mage::helper('core')->urlEncode($e->getCaptchaToken()),
  297. 'captcha_url' => Mage::helper('core')->urlEncode($e->getCaptchaUrl())
  298. )
  299. );
  300. }
  301. /**
  302. * Get store object, basing on request
  303. *
  304. * @return Mage_Core_Model_Store
  305. * @throws Mage_Core_Exception
  306. */
  307. public function _getStore()
  308. {
  309. $store = Mage::app()->getStore((int)$this->getRequest()->getParam('store', 0));
  310. if ((!$store) || 0 == $store->getId()) {
  311. Mage::throwException($this->__('Unable to select a Store View.'));
  312. }
  313. return $store;
  314. }
  315. protected function _getConfig()
  316. {
  317. return Mage::getSingleton('googlebase/config');
  318. }
  319. protected function _isAllowed()
  320. {
  321. return Mage::getSingleton('admin/session')->isAllowed('catalog/googlebase/items');
  322. }
  323. /**
  324. * Parse Exception Response Body
  325. *
  326. * @param string $message Exception message to parse
  327. * @return string
  328. */
  329. protected function _parseGdataExceptionMessage($message)
  330. {
  331. $result = array();
  332. foreach (explode("\n", $message) as $row) {
  333. if (strip_tags($row) == $row) {
  334. $result[] = $row;
  335. continue;
  336. }
  337. // parse not well-formatted xml
  338. preg_match_all('/(reason|field|type)=\"([^\"]+)\"/', $row, $matches);
  339. if (is_array($matches) && count($matches) == 3) {
  340. if (is_array($matches[1]) && count($matches[1]) > 0) {
  341. $c = count($matches[1]);
  342. for ($i = 0; $i < $c; $i++) {
  343. if (isset($matches[2][$i])) {
  344. $result[] = ucfirst($matches[1][$i]) . ': ' . $matches[2][$i];
  345. }
  346. }
  347. }
  348. }
  349. }
  350. return implode(". ", $result);
  351. }
  352. }