PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/XmlConnect/controllers/ReviewController.php

https://bitbucket.org/jit_bec/shopifine
PHP | 194 lines | 107 code | 19 blank | 68 comment | 15 complexity | 270163ed4a299a4ec47d2f1aa94d7e26 MD5 | raw file
Possible License(s): LGPL-3.0
  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_XmlConnect
  23. * @copyright Copyright (c) 2012 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. * XmlConnect review controller
  28. *
  29. * @category Mage
  30. * @package Mage_Xmlconnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_ReviewController extends Mage_XmlConnect_Controller_Action
  34. {
  35. /**
  36. * Initialize and check product
  37. *
  38. * @return Mage_Catalog_Model_Product
  39. */
  40. protected function _initProduct()
  41. {
  42. Mage::dispatchEvent('review_controller_product_init_before', array('controller_action' => $this));
  43. $productId = (int) $this->getRequest()->getParam('id');
  44. $product = $this->_loadProduct($productId);
  45. try {
  46. Mage::dispatchEvent('review_controller_product_init', array('product' => $product));
  47. Mage::dispatchEvent('review_controller_product_init_after', array(
  48. 'product' => $product,
  49. 'controller_action' => $this
  50. ));
  51. } catch (Mage_Core_Exception $e) {
  52. Mage::logException($e);
  53. return false;
  54. }
  55. return $product;
  56. }
  57. /**
  58. * Load product model with data by passed id.
  59. * Return false if product was not loaded or has incorrect status.
  60. *
  61. * @param int $productId
  62. * @return bool | Mage_Catalog_Model_Product
  63. */
  64. protected function _loadProduct($productId)
  65. {
  66. if (!$productId) {
  67. return false;
  68. }
  69. $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
  70. /** @var $product Mage_Catalog_Model_Product */
  71. if (!$product->getId() || !$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
  72. return false;
  73. }
  74. Mage::register('current_product', $product);
  75. Mage::register('product', $product);
  76. return $product;
  77. }
  78. /**
  79. * Check if guest is allowed to write review
  80. *
  81. * Do check the customer is logged in or guest is allowed to write review
  82. *
  83. * @return bool
  84. */
  85. protected function _checkGuestAllowed()
  86. {
  87. if (Mage::getSingleton('customer/session')->isLoggedIn() || Mage::helper('review')->getIsGuestAllowToWrite()) {
  88. return true;
  89. }
  90. $this->_message(
  91. $this->__('Only registered users can write reviews. Please, log in or register.'),
  92. self::MESSAGE_STATUS_ERROR
  93. );
  94. return false;
  95. }
  96. /**
  97. * Get review form
  98. *
  99. * @return null
  100. */
  101. public function formAction()
  102. {
  103. if (!$this->_checkGuestAllowed()) {
  104. return;
  105. }
  106. try {
  107. $this->loadLayout(false);
  108. $this->renderLayout();
  109. } catch (Mage_Core_Exception $e) {
  110. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  111. } catch (Exception $e) {
  112. $this->_message($this->__('Unable to load review form.'), self::MESSAGE_STATUS_ERROR);
  113. Mage::logException($e);
  114. }
  115. }
  116. /**
  117. * Save product review
  118. *
  119. * @return null
  120. */
  121. public function saveAction()
  122. {
  123. if (!$this->_checkGuestAllowed()) {
  124. return;
  125. }
  126. $data = $this->getRequest()->getPost();
  127. $rating = $this->getRequest()->getPost('ratings', array());
  128. $product = $this->_initProduct();
  129. if ($product && !empty($data)) {
  130. /** @var $review Mage_Review_Model_Review */
  131. $review = Mage::getModel('review/review')->setData($data);
  132. $validate = $review->validate();
  133. if ($validate === true) {
  134. try {
  135. $review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
  136. ->setEntityPkValue($product->getId())->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
  137. ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
  138. ->setStoreId(Mage::app()->getStore()->getId())
  139. ->setStores(array(Mage::app()->getStore()->getId()))->save();
  140. foreach ($rating as $ratingId => $optionId) {
  141. Mage::getModel('rating/rating')->setRatingId($ratingId)->setReviewId($review->getId())
  142. ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
  143. ->addOptionVote($optionId, $product->getId());
  144. }
  145. $review->aggregate();
  146. $this->_message(
  147. $this->__('Your review has been accepted for moderation.'), self::MESSAGE_STATUS_SUCCESS
  148. );
  149. } catch (Exception $e) {
  150. $this->_message($this->__('Unable to post the review.'), self::MESSAGE_STATUS_ERROR);
  151. Mage::logException($e);
  152. }
  153. } else {
  154. if (is_array($validate)) {
  155. $validate = array_map(array($this, '_trimDot'), $validate);
  156. $this->_message(implode('. ', $validate) . '.', self::MESSAGE_STATUS_ERROR);
  157. } else {
  158. $this->_message($this->__('Unable to post the review.'), self::MESSAGE_STATUS_ERROR);
  159. }
  160. }
  161. } else {
  162. $this->_message($this->__('Unable to post the review.'), self::MESSAGE_STATUS_ERROR);
  163. }
  164. }
  165. /**
  166. * Trim ending dot (the ".") symbol from string
  167. *
  168. * @param string $text
  169. * @return string
  170. */
  171. private function _trimDot($text)
  172. {
  173. return trim($text, " \n\r\t.");
  174. }
  175. }