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_ItemsController extends Mage_Adminhtml_Controller_Action
36: {
37: protected function _initAction()
38: {
39: $this->loadLayout()
40: ->_setActiveMenu('catalog/googlebase/items')
41: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
42: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Google Base'), Mage::helper('adminhtml')->__('Google Base'));
43: return $this;
44: }
45:
46: public function indexAction()
47: {
48: $this->_title($this->__('Catalog'))
49: ->_title($this->__('Google base'))
50: ->_title($this->__('Manage Items'));
51:
52: if (0 === (int)$this->getRequest()->getParam('store')) {
53: $this->_redirect('*/*/', array('store' => Mage::app()->getAnyStoreView()->getId(), '_current' => true));
54: return;
55: }
56: $contentBlock = $this->getLayout()->createBlock('googlebase/adminhtml_items')->setStore($this->_getStore());
57:
58: if ($this->getRequest()->getParam('captcha_token') && $this->getRequest()->getParam('captcha_url')) {
59: $contentBlock->setGbaseCaptchaToken(
60: Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_token'))
61: );
62: $contentBlock->setGbaseCaptchaUrl(
63: Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_url'))
64: );
65: }
66:
67: if (!$this->_getConfig()->isValidBaseCurrencyCode($this->_getStore()->getId())) {
68: $_countryInfo = $this->_getConfig()->getTargetCountryInfo($this->_getStore()->getId());
69: $this->_getSession()->addNotice(
70: $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'])
71: );
72: }
73:
74: $this->_initAction()
75: ->_addBreadcrumb(Mage::helper('googlebase')->__('Items'), Mage::helper('googlebase')->__('Items'))
76: ->_addContent($contentBlock)
77: ->renderLayout();
78: }
79:
80: public function gridAction()
81: {
82: $this->loadLayout();
83: return $this->getResponse()->setBody(
84: $this->getLayout()
85: ->createBlock('googlebase/adminhtml_items_item')
86: ->setIndex($this->getRequest()->getParam('index'))
87: ->toHtml()
88: );
89: }
90:
91: public function massAddAction()
92: {
93: $storeId = $this->_getStore()->getId();
94: $productIds = $this->getRequest()->getParam('product', null);
95:
96: $totalAdded = 0;
97:
98: try {
99: if (is_array($productIds)) {
100: foreach ($productIds as $productId) {
101: $product = Mage::getSingleton('catalog/product')
102: ->setStoreId($storeId)
103: ->load($productId);
104:
105: if ($product->getId()) {
106: Mage::getModel('googlebase/item')
107: ->setProduct($product)
108: ->insertItem()
109: ->save();
110:
111: $totalAdded++;
112: }
113: }
114: }
115:
116: if ($totalAdded > 0) {
117: $this->_getSession()->addSuccess(
118: $this->__('Total of %d product(s) have been added to Google Base.', $totalAdded)
119: );
120: } elseif (is_null($productIds)) {
121: $this->_getSession()->addError($this->__('Session expired during export. Please revise exported products and repeat the process if necessary.'));
122: } else {
123: $this->_getSession()->addError($this->__('No products were added to Google Base'));
124: }
125: } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
126: $this->_getSession()->addError($e->getMessage());
127: $this->_redirectToCaptcha($e);
128: return;
129: } catch (Zend_Gdata_App_Exception $e) {
130: $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
131: } catch (Exception $e) {
132: $this->_getSession()->addError($e->getMessage());
133: }
134:
135: $this->_redirect('*/*/index', array('store'=>$storeId));
136: }
137:
138: public function massDeleteAction()
139: {
140: $storeId = $this->_getStore()->getId();
141: $itemIds = $this->getRequest()->getParam('item');
142:
143: $totalDeleted = 0;
144:
145: try {
146: foreach ($itemIds as $itemId) {
147: $item = Mage::getModel('googlebase/item')->load($itemId);
148: if ($item->getId()) {
149: $item->deleteItem();
150: $item->delete();
151: $totalDeleted++;
152: }
153: }
154: if ($totalDeleted > 0) {
155: $this->_getSession()->addSuccess(
156: $this->__('Total of %d items(s) have been removed from Google Base.', $totalDeleted)
157: );
158: } else {
159: $this->_getSession()->addError($this->__('No items were deleted from Google Base'));
160: }
161: } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
162: $this->_getSession()->addError($e->getMessage());
163: $this->_redirectToCaptcha($e);
164: return;
165: } catch (Zend_Gdata_App_Exception $e) {
166: $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
167: } catch (Exception $e) {
168: $this->_getSession()->addError($e->getMessage());
169: }
170:
171: $this->_redirect('*/*/index', array('store'=>$storeId));
172: }
173:
174: public function massPublishAction()
175: {
176: $storeId = $this->_getStore()->getId();
177: $itemIds = $this->getRequest()->getParam('item');
178:
179: $totalPublished = 0;
180:
181: try {
182: if (!empty($itemIds) && is_array($itemIds)) {
183: foreach ($itemIds as $itemId) {
184: $item = Mage::getModel('googlebase/item')->load($itemId);
185: if ($item->getId()) {
186: $item->activateItem();
187: $totalPublished++;
188: }
189: }
190: }
191: if ($totalPublished > 0) {
192: $this->_getSession()->addSuccess(
193: $this->__('Total of %d items(s) have been published.', $totalPublished)
194: );
195: } else {
196: $this->_getSession()->addError($this->__('No items were published'));
197: }
198: } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
199: $this->_getSession()->addError($e->getMessage());
200: $this->_redirectToCaptcha($e);
201: return;
202: } catch (Zend_Gdata_App_Exception $e) {
203: $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
204: } catch (Exception $e) {
205: $this->_getSession()->addError($e->getMessage());
206: }
207:
208: $this->_redirect('*/*/index', array('store'=>$storeId));
209: }
210:
211: public function massHideAction()
212: {
213: $storeId = $this->_getStore()->getId();
214: $itemIds = $this->getRequest()->getParam('item');
215:
216: $totalHidden = 0;
217:
218: try {
219: foreach ($itemIds as $itemId) {
220: $item = Mage::getModel('googlebase/item')->load($itemId);
221: if ($item->getId()) {
222: $item->hideItem();
223: $totalHidden++;
224: }
225: }
226: if ($totalHidden > 0) {
227: $this->_getSession()->addSuccess(
228: $this->__('Total of %d items(s) have been saved as inactive items.', $totalHidden)
229: );
230: } else {
231: $this->_getSession()->addError($this->__('No items were saved as inactive items'));
232: }
233: } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
234: $this->_getSession()->addError($e->getMessage());
235: $this->_redirectToCaptcha($e);
236: return;
237: } catch (Zend_Gdata_App_Exception $e) {
238: $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
239: } catch (Exception $e) {
240: $this->_getSession()->addError($e->getMessage());
241: }
242:
243: $this->_redirect('*/*/index', array('store'=>$storeId));
244: }
245:
246: 247: 248:
249: public function refreshAction()
250: {
251: $storeId = $this->_getStore()->getId();
252: $totalUpdated = 0;
253: $totalDeleted = 0;
254:
255: try {
256: $itemIds = $this->getRequest()->getParam('item');
257: foreach ($itemIds as $itemId) {
258: $item = Mage::getModel('googlebase/item')->load($itemId);
259:
260: $stats = Mage::getSingleton('googlebase/service_feed')->getItemStats($item->getGbaseItemId(), $storeId);
261: if ($stats === null) {
262: $item->delete();
263: $totalDeleted++;
264: continue;
265: }
266:
267: if ($stats['draft'] != $item->getIsHidden()) {
268: $item->setIsHidden($stats['draft']);
269: }
270:
271: if (isset($stats['clicks'])) {
272: $item->setClicks($stats['clicks']);
273: }
274:
275: if (isset($stats['impressions'])) {
276: $item->setImpr($stats['impressions']);
277: }
278:
279: if (isset($stats['expires'])) {
280: $item->setExpires($stats['expires']);
281: }
282:
283: $item->save();
284: $totalUpdated++;
285: }
286:
287: $this->_getSession()->addSuccess(
288: $this->__('Total of %d items(s) have been deleted; total of %d items(s) have been updated.', $totalDeleted, $totalUpdated)
289: );
290:
291: } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
292: $this->_getSession()->addError($e->getMessage());
293: $this->_redirectToCaptcha($e);
294: return;
295: } catch (Zend_Gdata_App_Exception $e) {
296: $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
297: } catch (Exception $e) {
298: $this->_getSession()->addError($e->getMessage());
299: }
300:
301: $this->_redirect('*/*/index', array('store'=>$storeId));
302: }
303:
304: public function confirmCaptchaAction()
305: {
306: $storeId = $this->_getStore()->getId();
307: try {
308: Mage::getModel('googlebase/service')->getClient(
309: $storeId,
310: Mage::helper('core')->urlDecode($this->getRequest()->getParam('captcha_token')),
311: $this->getRequest()->getParam('user_confirm')
312: );
313: $this->_getSession()->addSuccess($this->__('Captcha has been confirmed.'));
314:
315: } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
316: $this->_getSession()->addError($this->__('Captcha confirmation error: %s', $e->getMessage()));
317: $this->_redirectToCaptcha($e);
318: return;
319: } catch (Zend_Gdata_App_Exception $e) {
320: $this->_getSession()->addError( $this->_parseGdataExceptionMessage($e->getMessage()) );
321: } catch (Exception $e) {
322: $this->_getSession()->addError($this->__('Captcha confirmation error: %s', $e->getMessage()));
323: }
324:
325: $this->_redirect('*/*/index', array('store'=>$storeId));
326: }
327:
328: 329: 330: 331: 332:
333: protected function _redirectToCaptcha($e)
334: {
335: $this->_redirect('*/*/index',
336: array('store' => $this->_getStore()->getId(),
337: 'captcha_token' => Mage::helper('core')->urlEncode($e->getCaptchaToken()),
338: 'captcha_url' => Mage::helper('core')->urlEncode($e->getCaptchaUrl())
339: )
340: );
341: }
342:
343: 344: 345: 346: 347: 348:
349: public function _getStore()
350: {
351: $store = Mage::app()->getStore((int)$this->getRequest()->getParam('store', 0));
352: if ((!$store) || 0 == $store->getId()) {
353: Mage::throwException($this->__('Unable to select a Store View.'));
354: }
355: return $store;
356: }
357:
358: protected function _getConfig()
359: {
360: return Mage::getSingleton('googlebase/config');
361: }
362:
363: protected function _isAllowed()
364: {
365: return Mage::getSingleton('admin/session')->isAllowed('catalog/googlebase/items');
366: }
367:
368: 369: 370: 371: 372: 373:
374: protected function _parseGdataExceptionMessage($message)
375: {
376: $result = array();
377: foreach (explode("\n", $message) as $row) {
378: if (strip_tags($row) == $row) {
379: $result[] = $row;
380: continue;
381: }
382:
383:
384: preg_match_all('/(reason|field|type)=\"([^\"]+)\"/', $row, $matches);
385:
386: if (is_array($matches) && count($matches) == 3) {
387: if (is_array($matches[1]) && count($matches[1]) > 0) {
388: $c = count($matches[1]);
389: for ($i = 0; $i < $c; $i++) {
390: if (isset($matches[2][$i])) {
391: $result[] = ucfirst($matches[1][$i]) . ': ' . $matches[2][$i];
392: }
393: }
394: }
395: }
396: }
397: return implode(". ", $result);
398: }
399: }
400: