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_Review_ProductController extends Mage_Core_Controller_Front_Action
35: {
36:
37: 38: 39: 40: 41:
42: protected $_cookieCheckActions = array('post');
43:
44: public function preDispatch()
45: {
46: parent::preDispatch();
47:
48: $allowGuest = Mage::helper('review')->getIsGuestAllowToWrite();
49: if (!$this->getRequest()->isDispatched()) {
50: return;
51: }
52:
53: $action = $this->getRequest()->getActionName();
54: if (!$allowGuest && $action == 'post' && $this->getRequest()->isPost()) {
55: if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
56: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
57: Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_current' => true)));
58: Mage::getSingleton('review/session')->setFormData($this->getRequest()->getPost())
59: ->setRedirectUrl($this->_getRefererUrl());
60: $this->_redirectUrl(Mage::helper('customer')->getLoginUrl());
61: }
62: }
63:
64: return $this;
65: }
66: 67: 68: 69: 70:
71: protected function _initProduct()
72: {
73: Mage::dispatchEvent('review_controller_product_init_before', array('controller_action'=>$this));
74: $categoryId = (int) $this->getRequest()->getParam('category', false);
75: $productId = (int) $this->getRequest()->getParam('id');
76:
77: $product = $this->_loadProduct($productId);
78: if (!$product) {
79: return false;
80: }
81:
82: if ($categoryId) {
83: $category = Mage::getModel('catalog/category')->load($categoryId);
84: Mage::register('current_category', $category);
85: }
86:
87: try {
88: Mage::dispatchEvent('review_controller_product_init', array('product'=>$product));
89: Mage::dispatchEvent('review_controller_product_init_after', array(
90: 'product' => $product,
91: 'controller_action' => $this
92: ));
93: } catch (Mage_Core_Exception $e) {
94: Mage::logException($e);
95: return false;
96: }
97:
98: return $product;
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: protected function _loadProduct($productId)
109: {
110: if (!$productId) {
111: return false;
112: }
113:
114: $product = Mage::getModel('catalog/product')
115: ->setStoreId(Mage::app()->getStore()->getId())
116: ->load($productId);
117:
118: if (!$product->getId() || !$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
119: return false;
120: }
121:
122: Mage::register('current_product', $product);
123: Mage::register('product', $product);
124:
125: return $product;
126: }
127:
128: 129: 130: 131: 132: 133: 134:
135: protected function _loadReview($reviewId)
136: {
137: if (!$reviewId) {
138: return false;
139: }
140:
141: $review = Mage::getModel('review/review')->load($reviewId);
142:
143: if (!$review->getId() || !$review->isApproved() || !$review->isAvailableOnStore(Mage::app()->getStore())) {
144: return false;
145: }
146:
147: Mage::register('current_review', $review);
148:
149: return $review;
150: }
151:
152: 153: 154: 155:
156: public function postAction()
157: {
158: if ($data = Mage::getSingleton('review/session')->getFormData(true)) {
159: $rating = array();
160: if (isset($data['ratings']) && is_array($data['ratings'])) {
161: $rating = $data['ratings'];
162: }
163: } else {
164: $data = $this->getRequest()->getPost();
165: $rating = $this->getRequest()->getParam('ratings', array());
166: }
167:
168: if (($product = $this->_initProduct()) && !empty($data)) {
169: $session = Mage::getSingleton('core/session');
170:
171: $review = Mage::getModel('review/review')->setData($data);
172:
173:
174: $validate = $review->validate();
175: if ($validate === true) {
176: try {
177: $review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
178: ->setEntityPkValue($product->getId())
179: ->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
180: ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
181: ->setStoreId(Mage::app()->getStore()->getId())
182: ->setStores(array(Mage::app()->getStore()->getId()))
183: ->save();
184:
185: foreach ($rating as $ratingId => $optionId) {
186: Mage::getModel('rating/rating')
187: ->setRatingId($ratingId)
188: ->setReviewId($review->getId())
189: ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
190: ->addOptionVote($optionId, $product->getId());
191: }
192:
193: $review->aggregate();
194: $session->addSuccess($this->__('Your review has been accepted for moderation.'));
195: }
196: catch (Exception $e) {
197: $session->setFormData($data);
198: $session->addError($this->__('Unable to post the review.'));
199: }
200: }
201: else {
202: $session->setFormData($data);
203: if (is_array($validate)) {
204: foreach ($validate as $errorMessage) {
205: $session->addError($errorMessage);
206: }
207: }
208: else {
209: $session->addError($this->__('Unable to post the review.'));
210: }
211: }
212: }
213:
214: if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
215: $this->_redirectUrl($redirectUrl);
216: return;
217: }
218: $this->_redirectReferer();
219: }
220:
221: 222: 223: 224:
225: public function listAction()
226: {
227: if ($product = $this->_initProduct()) {
228: Mage::register('productId', $product->getId());
229:
230: $design = Mage::getSingleton('catalog/design');
231: $settings = $design->getDesignSettings($product);
232: if ($settings->getCustomDesign()) {
233: $design->applyCustomDesign($settings->getCustomDesign());
234: }
235: $this->_initProductLayout($product);
236:
237:
238: if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
239: $breadcrumbsBlock->addCrumb('product', array(
240: 'label' => $product->getName(),
241: 'link' => $product->getProductUrl(),
242: 'readonly' => true,
243: ));
244: $breadcrumbsBlock->addCrumb('reviews', array('label' => Mage::helper('review')->__('Product Reviews')));
245: }
246:
247: $this->renderLayout();
248: } elseif (!$this->getResponse()->isRedirect()) {
249: $this->_forward('noRoute');
250: }
251: }
252:
253: 254: 255: 256:
257: public function viewAction()
258: {
259: $review = $this->_loadReview((int) $this->getRequest()->getParam('id'));
260: if (!$review) {
261: $this->_forward('noroute');
262: return;
263: }
264:
265: $product = $this->_loadProduct($review->getEntityPkValue());
266: if (!$product) {
267: $this->_forward('noroute');
268: return;
269: }
270:
271: $this->loadLayout();
272: $this->_initLayoutMessages('review/session');
273: $this->_initLayoutMessages('catalog/session');
274: $this->renderLayout();
275: }
276:
277: 278: 279: 280:
281: protected function _initProductLayout($product)
282: {
283: $update = $this->getLayout()->getUpdate();
284:
285: $update->addHandle('default');
286: $this->addActionLayoutHandles();
287:
288:
289: $update->addHandle('PRODUCT_TYPE_'.$product->getTypeId());
290:
291: if ($product->getPageLayout()) {
292: $this->getLayout()->helper('page/layout')
293: ->applyHandle($product->getPageLayout());
294: }
295:
296: $this->loadLayoutUpdates();
297: if ($product->getPageLayout()) {
298: $this->getLayout()->helper('page/layout')
299: ->applyTemplate($product->getPageLayout());
300: }
301: $update->addUpdate($product->getCustomLayoutUpdate());
302: $this->generateLayoutXml()->generateLayoutBlocks();
303: }
304: }
305: