00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Review_ProductController extends Mage_Core_Controller_Front_Action
00035 {
00036
00037
00038
00039
00040
00041
00042 protected $_cookieCheckActions = array('post');
00043
00044 public function preDispatch()
00045 {
00046 parent::preDispatch();
00047
00048 $allowGuest = Mage::helper('review')->getIsGuestAllowToWrite();
00049 if (!$this->getRequest()->isDispatched()) {
00050 return;
00051 }
00052
00053 $action = $this->getRequest()->getActionName();
00054 if (!$allowGuest && $action == 'post' && $this->getRequest()->isPost()) {
00055 if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
00056 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00057 Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_current' => true)));
00058 Mage::getSingleton('review/session')->setFormData($this->getRequest()->getPost())
00059 ->setRedirectUrl($this->_getRefererUrl());
00060 $this->_redirectUrl(Mage::helper('customer')->getLoginUrl());
00061 }
00062 }
00063
00064 return $this;
00065 }
00066
00067
00068
00069
00070
00071 protected function _initProduct()
00072 {
00073 Mage::dispatchEvent('review_controller_product_init_before', array('controller_action'=>$this));
00074 $categoryId = (int) $this->getRequest()->getParam('category', false);
00075 $productId = (int) $this->getRequest()->getParam('id');
00076
00077 if (!$productId) {
00078 return false;
00079 }
00080
00081 $product = Mage::getModel('catalog/product')
00082 ->setStoreId(Mage::app()->getStore()->getId())
00083 ->load($productId);
00084
00085 if (!$product->getId() || !$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
00086 return false;
00087 }
00088 if ($categoryId) {
00089 $category = Mage::getModel('catalog/category')->load($categoryId);
00090 Mage::register('current_category', $category);
00091 }
00092 Mage::register('current_product', $product);
00093 Mage::register('product', $product);
00094
00095 try {
00096 Mage::dispatchEvent('review_controller_product_init', array('product'=>$product));
00097 Mage::dispatchEvent('review_controller_product_init_after', array('product'=>$product, 'controller_action' => $this));
00098 } catch (Mage_Core_Exception $e) {
00099 Mage::logException($e);
00100 return false;
00101 }
00102
00103 return $product;
00104 }
00105
00106 public function postAction()
00107 {
00108 if ($data = Mage::getSingleton('review/session')->getFormData(true)) {
00109 $rating = array();
00110 if (isset($data['ratings']) && is_array($data['ratings'])) {
00111 $rating = $data['ratings'];
00112 }
00113 } else {
00114 $data = $this->getRequest()->getPost();
00115 $rating = $this->getRequest()->getParam('ratings', array());
00116 }
00117
00118 if (($product = $this->_initProduct()) && !empty($data)) {
00119 $session = Mage::getSingleton('core/session');
00120
00121 $review = Mage::getModel('review/review')->setData($data);
00122
00123
00124 $validate = $review->validate();
00125 if ($validate === true) {
00126 try {
00127 $review->setEntityId(Mage_Review_Model_Review::ENTITY_PRODUCT)
00128 ->setEntityPkValue($product->getId())
00129 ->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
00130 ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
00131 ->setStoreId(Mage::app()->getStore()->getId())
00132 ->setStores(array(Mage::app()->getStore()->getId()))
00133 ->save();
00134
00135 foreach ($rating as $ratingId => $optionId) {
00136 Mage::getModel('rating/rating')
00137 ->setRatingId($ratingId)
00138 ->setReviewId($review->getId())
00139 ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
00140 ->addOptionVote($optionId, $product->getId());
00141 }
00142
00143 $review->aggregate();
00144 $session->addSuccess($this->__('Your review has been accepted for moderation'));
00145 }
00146 catch (Exception $e) {
00147 $session->setFormData($data);
00148 $session->addError($this->__('Unable to post review. Please, try again later.'));
00149 }
00150 }
00151 else {
00152 $session->setFormData($data);
00153 if (is_array($validate)) {
00154 foreach ($validate as $errorMessage) {
00155 $session->addError($errorMessage);
00156 }
00157 }
00158 else {
00159 $session->addError($this->__('Unable to post review. Please, try again later.'));
00160 }
00161 }
00162 }
00163
00164 if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
00165 $this->_redirectUrl($redirectUrl);
00166 return;
00167 }
00168 $this->_redirectReferer();
00169 }
00170
00171 public function listAction()
00172 {
00173 if ($product = $this->_initProduct()) {
00174 Mage::register('productId', $product->getId());
00175 Mage::getModel('catalog/design')->applyDesign($product, Mage_Catalog_Model_Design::APPLY_FOR_PRODUCT);
00176 $this->_initProductLayout($product);
00177
00178
00179 if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
00180 $breadcrumbsBlock->addCrumb('product', array(
00181 'label' => $product->getName(),
00182 'link' => $product->getProductUrl(),
00183 'readonly' => true,
00184 ));
00185 $breadcrumbsBlock->addCrumb('reviews', array('label' => Mage::helper('review')->__('Product Reviews')));
00186 }
00187
00188 $this->renderLayout();
00189 } elseif (!$this->getResponse()->isRedirect()) {
00190 $this->_forward('noRoute');
00191 }
00192 }
00193
00194 public function viewAction()
00195 {
00196 $this->loadLayout();
00197 $this->_initLayoutMessages('review/session');
00198 $this->renderLayout();
00199 }
00200
00201 protected function _initProductLayout($product)
00202 {
00203 $update = $this->getLayout()->getUpdate();
00204
00205 $update->addHandle('default');
00206 $this->addActionLayoutHandles();
00207
00208
00209 $update->addHandle('PRODUCT_TYPE_'.$product->getTypeId());
00210
00211 if ($product->getPageLayout()) {
00212 $this->getLayout()->helper('page/layout')
00213 ->applyHandle($product->getPageLayout());
00214 }
00215
00216 $this->loadLayoutUpdates();
00217 if ($product->getPageLayout()) {
00218 $this->getLayout()->helper('page/layout')
00219 ->applyTemplate($product->getPageLayout());
00220 }
00221 $update->addUpdate($product->getCustomLayoutUpdate());
00222 $this->generateLayoutXml()->generateLayoutBlocks();
00223 }
00224 }