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: class Mage_XmlConnect_ReviewController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _initProduct()
42: {
43: Mage::dispatchEvent('review_controller_product_init_before', array('controller_action' => $this));
44:
45: $productId = (int) $this->getRequest()->getParam('id');
46: $product = $this->_loadProduct($productId);
47:
48: try {
49: Mage::dispatchEvent('review_controller_product_init', array('product' => $product));
50: Mage::dispatchEvent('review_controller_product_init_after', array(
51: 'product' => $product,
52: 'controller_action' => $this
53: ));
54: } catch (Mage_Core_Exception $e) {
55: Mage::logException($e);
56: return false;
57: }
58:
59: return $product;
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: protected function _loadProduct($productId)
70: {
71: if (!$productId) {
72: return false;
73: }
74:
75: $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
76:
77: if (!$product->getId() || !$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
78: return false;
79: }
80:
81: Mage::register('current_product', $product);
82: Mage::register('product', $product);
83:
84: return $product;
85: }
86:
87: 88: 89: 90: 91: 92: 93:
94: protected function _checkGuestAllowed()
95: {
96: if (Mage::getSingleton('customer/session')->isLoggedIn() || Mage::helper('review')->getIsGuestAllowToWrite()) {
97: return true;
98: }
99:
100: $this->_message(
101: $this->__('Only registered users can write reviews. Please, log in or register.'),
102: self::MESSAGE_STATUS_ERROR
103: );
104: return false;
105: }
106:
107: 108: 109: 110: 111:
112: public function formAction()
113: {
114: if (!$this->_checkGuestAllowed()) {
115: return;
116: }
117:
118: try {
119: $this->loadLayout(false);
120: $this->renderLayout();
121: } catch (Mage_Core_Exception $e) {
122: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
123: } catch (Exception $e) {
124: $this->_message($this->__('Unable to load review form.'), self::MESSAGE_STATUS_ERROR);
125: Mage::logException($e);
126: }
127: }
128:
129: 130: 131: 132: 133:
134: public function saveAction()
135: {
136: if (!$this->_checkGuestAllowed()) {
137: return;
138: }
139:
140: $data = $this->getRequest()->getPost();
141: $rating = $this->getRequest()->getPost('ratings', array());
142:
143: $product = $this->_initProduct();
144: if ($product && !empty($data)) {
145:
146: $review = Mage::getModel('review/review')->setData($data);
147: $validate = $review->validate();
148:
149: if ($validate === true) {
150: try {
151: $review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
152: ->setEntityPkValue($product->getId())->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
153: ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
154: ->setStoreId(Mage::app()->getStore()->getId())
155: ->setStores(array(Mage::app()->getStore()->getId()))->save();
156:
157: foreach ($rating as $ratingId => $optionId) {
158: Mage::getModel('rating/rating')->setRatingId($ratingId)->setReviewId($review->getId())
159: ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
160: ->addOptionVote($optionId, $product->getId());
161: }
162:
163: $review->aggregate();
164: $this->_message(
165: $this->__('Your review has been accepted for moderation.'), self::MESSAGE_STATUS_SUCCESS
166: );
167: } catch (Exception $e) {
168: $this->_message($this->__('Unable to post the review.'), self::MESSAGE_STATUS_ERROR);
169: Mage::logException($e);
170: }
171: } else {
172: if (is_array($validate)) {
173: $validate = array_map(array($this, '_trimDot'), $validate);
174: $this->_message(implode('. ', $validate) . '.', self::MESSAGE_STATUS_ERROR);
175: } else {
176: $this->_message($this->__('Unable to post the review.'), self::MESSAGE_STATUS_ERROR);
177: }
178: }
179: } else {
180: $this->_message($this->__('Unable to post the review.'), self::MESSAGE_STATUS_ERROR);
181: }
182: }
183:
184: 185: 186: 187: 188: 189:
190: private function _trimDot($text)
191: {
192: return trim($text, " \n\r\t.");
193: }
194: }
195: