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_Adminhtml_Catalog_Product_ReviewController extends Mage_Adminhtml_Controller_Action
36: {
37: 38: 39: 40: 41:
42: protected $_publicActions = array('edit');
43:
44: public function indexAction()
45: {
46: $this->_title($this->__('Catalog'))
47: ->_title($this->__('Reviews and Ratings'))
48: ->_title($this->__('Customer Reviews'));
49:
50: $this->_title($this->__('All Reviews'));
51:
52: if ($this->getRequest()->getParam('ajax')) {
53: return $this->_forward('reviewGrid');
54: }
55:
56: $this->loadLayout();
57: $this->_setActiveMenu('catalog/review');
58:
59: $this->_addContent($this->getLayout()->createBlock('adminhtml/review_main'));
60:
61: $this->renderLayout();
62: }
63:
64: public function pendingAction()
65: {
66: $this->_title($this->__('Catalog'))
67: ->_title($this->__('Reviews and Ratings'))
68: ->_title($this->__('Customer Reviews'));
69:
70: $this->_title($this->__('Pending Reviews'));
71:
72: if ($this->getRequest()->getParam('ajax')) {
73: Mage::register('usePendingFilter', true);
74: return $this->_forward('reviewGrid');
75: }
76:
77: $this->loadLayout();
78: $this->_setActiveMenu('catalog/review');
79:
80: Mage::register('usePendingFilter', true);
81: $this->_addContent($this->getLayout()->createBlock('adminhtml/review_main'));
82:
83: $this->renderLayout();
84: }
85:
86: public function editAction()
87: {
88: $this->_title($this->__('Catalog'))
89: ->_title($this->__('Reviews and Ratings'))
90: ->_title($this->__('Customer Reviews'));
91:
92: $this->_title($this->__('Edit Review'));
93:
94: $this->loadLayout();
95: $this->_setActiveMenu('catalog/review');
96:
97: $this->_addContent($this->getLayout()->createBlock('adminhtml/review_edit'));
98:
99: $this->renderLayout();
100: }
101:
102: public function newAction()
103: {
104: $this->_title($this->__('Catalog'))
105: ->_title($this->__('Reviews and Ratings'))
106: ->_title($this->__('Customer Reviews'));
107:
108: $this->_title($this->__('New Review'));
109:
110: $this->loadLayout();
111: $this->_setActiveMenu('catalog/review');
112:
113: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
114:
115: $this->_addContent($this->getLayout()->createBlock('adminhtml/review_add'));
116: $this->_addContent($this->getLayout()->createBlock('adminhtml/review_product_grid'));
117:
118: $this->renderLayout();
119: }
120:
121: public function saveAction()
122: {
123: if (($data = $this->getRequest()->getPost()) && ($reviewId = $this->getRequest()->getParam('id'))) {
124: $review = Mage::getModel('review/review')->load($reviewId);
125: $session = Mage::getSingleton('adminhtml/session');
126: if (! $review->getId()) {
127: $session->addError(Mage::helper('catalog')->__('The review was removed by another user or does not exist.'));
128: } else {
129: try {
130: $review->addData($data)->save();
131:
132: $arrRatingId = $this->getRequest()->getParam('ratings', array());
133: $votes = Mage::getModel('rating/rating_option_vote')
134: ->getResourceCollection()
135: ->setReviewFilter($reviewId)
136: ->addOptionInfo()
137: ->load()
138: ->addRatingOptions();
139: foreach ($arrRatingId as $ratingId=>$optionId) {
140: if($vote = $votes->getItemByColumnValue('rating_id', $ratingId)) {
141: Mage::getModel('rating/rating')
142: ->setVoteId($vote->getId())
143: ->setReviewId($review->getId())
144: ->updateOptionVote($optionId);
145: } else {
146: Mage::getModel('rating/rating')
147: ->setRatingId($ratingId)
148: ->setReviewId($review->getId())
149: ->addOptionVote($optionId, $review->getEntityPkValue());
150: }
151: }
152:
153: $review->aggregate();
154:
155: $session->addSuccess(Mage::helper('catalog')->__('The review has been saved.'));
156: } catch (Mage_Core_Exception $e) {
157: $session->addError($e->getMessage());
158: } catch (Exception $e){
159: $session->addException($e, Mage::helper('catalog')->__('An error occurred while saving this review.'));
160: }
161: }
162:
163: return $this->getResponse()->setRedirect($this->getUrl($this->getRequest()->getParam('ret') == 'pending' ? '*/*/pending' : '*/*/'));
164: }
165: $this->_redirect('*/*/');
166: }
167:
168: public function deleteAction()
169: {
170: $reviewId = $this->getRequest()->getParam('id', false);
171: $session = Mage::getSingleton('adminhtml/session');
172:
173: try {
174: Mage::getModel('review/review')->setId($reviewId)
175: ->aggregate()
176: ->delete();
177:
178: $session->addSuccess(Mage::helper('catalog')->__('The review has been deleted'));
179: if( $this->getRequest()->getParam('ret') == 'pending' ) {
180: $this->getResponse()->setRedirect($this->getUrl('*/*/pending'));
181: } else {
182: $this->getResponse()->setRedirect($this->getUrl('*/*/'));
183: }
184: return;
185: } catch (Mage_Core_Exception $e) {
186: $session->addError($e->getMessage());
187: } catch (Exception $e){
188: $session->addException($e, Mage::helper('catalog')->__('An error occurred while deleting this review.'));
189: }
190:
191: $this->_redirect('*/*/edit/',array('id'=>$reviewId));
192: }
193:
194: public function massDeleteAction()
195: {
196: $reviewsIds = $this->getRequest()->getParam('reviews');
197: $session = Mage::getSingleton('adminhtml/session');
198:
199: if(!is_array($reviewsIds)) {
200: $session->addError(Mage::helper('adminhtml')->__('Please select review(s).'));
201: } else {
202: try {
203: foreach ($reviewsIds as $reviewId) {
204: $model = Mage::getModel('review/review')->load($reviewId);
205: $model->delete();
206: }
207: Mage::getSingleton('adminhtml/session')->addSuccess(
208: Mage::helper('adminhtml')->__('Total of %d record(s) have been deleted.', count($reviewsIds))
209: );
210: } catch (Mage_Core_Exception $e) {
211: $session->addError($e->getMessage());
212: } catch (Exception $e){
213: $session->addException($e, Mage::helper('adminhtml')->__('An error occurred while deleting record(s).'));
214: }
215: }
216:
217: $this->_redirect('*/*/' . $this->getRequest()->getParam('ret', 'index'));
218: }
219:
220: public function massUpdateStatusAction()
221: {
222: $reviewsIds = $this->getRequest()->getParam('reviews');
223: $session = Mage::getSingleton('adminhtml/session');
224:
225: if(!is_array($reviewsIds)) {
226: $session->addError(Mage::helper('adminhtml')->__('Please select review(s).'));
227: } else {
228:
229: try {
230: $status = $this->getRequest()->getParam('status');
231: foreach ($reviewsIds as $reviewId) {
232: $model = Mage::getModel('review/review')->load($reviewId);
233: $model->setStatusId($status)
234: ->save()
235: ->aggregate();
236: }
237: $session->addSuccess(
238: Mage::helper('adminhtml')->__('Total of %d record(s) have been updated.', count($reviewsIds))
239: );
240: } catch (Mage_Core_Exception $e) {
241: $session->addError($e->getMessage());
242: } catch (Exception $e) {
243: $session->addException($e, Mage::helper('adminhtml')->__('An error occurred while updating the selected review(s).'));
244: }
245: }
246:
247: $this->_redirect('*/*/' . $this->getRequest()->getParam('ret', 'index'));
248: }
249:
250: public function massVisibleInAction()
251: {
252: $reviewsIds = $this->getRequest()->getParam('reviews');
253: $session = Mage::getSingleton('adminhtml/session');
254:
255: if(!is_array($reviewsIds)) {
256: $session->addError(Mage::helper('adminhtml')->__('Please select review(s).'));
257: } else {
258: $session = Mage::getSingleton('adminhtml/session');
259:
260: try {
261: $stores = $this->getRequest()->getParam('stores');
262: foreach ($reviewsIds as $reviewId) {
263: $model = Mage::getModel('review/review')->load($reviewId);
264: $model->setSelectStores($stores);
265: $model->save();
266: }
267: $session->addSuccess(
268: Mage::helper('adminhtml')->__('Total of %d record(s) have been updated.', count($reviewsIds))
269: );
270: } catch (Mage_Core_Exception $e) {
271: $session->addError($e->getMessage());
272: } catch (Exception $e) {
273: $session->addException($e, Mage::helper('adminhtml')->__('An error occurred while updating the selected review(s).'));
274: }
275: }
276:
277: $this->_redirect('*/*/pending');
278: }
279:
280: public function productGridAction()
281: {
282: $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/review_product_grid')->toHtml());
283: }
284:
285: public function reviewGridAction()
286: {
287: $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/review_grid')->toHtml());
288: }
289:
290: public function jsonProductInfoAction()
291: {
292: $response = new Varien_Object();
293: $id = $this->getRequest()->getParam('id');
294: if( intval($id) > 0 ) {
295: $product = Mage::getModel('catalog/product')
296: ->load($id);
297:
298: $response->setId($id);
299: $response->addData($product->getData());
300: $response->setError(0);
301: } else {
302: $response->setError(1);
303: $response->setMessage(Mage::helper('catalog')->__('Unable to get the product ID.'));
304: }
305: $this->getResponse()->setBody($response->toJSON());
306: }
307:
308: public function postAction()
309: {
310: $productId = $this->getRequest()->getParam('product_id', false);
311: $session = Mage::getSingleton('adminhtml/session');
312:
313: if ($data = $this->getRequest()->getPost()) {
314: if (Mage::app()->isSingleStoreMode()) {
315: $data['stores'] = array(Mage::app()->getStore(true)->getId());
316: } else if (isset($data['select_stores'])) {
317: $data['stores'] = $data['select_stores'];
318: }
319:
320: $review = Mage::getModel('review/review')->setData($data);
321:
322: $product = Mage::getModel('catalog/product')
323: ->load($productId);
324:
325: try {
326: $review->setEntityId(1)
327: ->setEntityPkValue($productId)
328: ->setStoreId($product->getStoreId())
329: ->setStatusId($data['status_id'])
330: ->setCustomerId(null)
331: ->save();
332:
333: $arrRatingId = $this->getRequest()->getParam('ratings', array());
334: foreach ($arrRatingId as $ratingId=>$optionId) {
335: Mage::getModel('rating/rating')
336: ->setRatingId($ratingId)
337: ->setReviewId($review->getId())
338: ->addOptionVote($optionId, $productId);
339: }
340:
341: $review->aggregate();
342:
343: $session->addSuccess(Mage::helper('catalog')->__('The review has been saved.'));
344: if( $this->getRequest()->getParam('ret') == 'pending' ) {
345: $this->getResponse()->setRedirect($this->getUrl('*/*/pending'));
346: } else {
347: $this->getResponse()->setRedirect($this->getUrl('*/*/'));
348: }
349:
350: return;
351: } catch (Mage_Core_Exception $e) {
352: $session->addError($e->getMessage());
353: } catch (Exception $e) {
354: $session->addException($e, Mage::helper('adminhtml')->__('An error occurred while saving review.'));
355: }
356: }
357: $this->getResponse()->setRedirect($this->getUrl('*/*/'));
358: return;
359: }
360:
361: public function ratingItemsAction()
362: {
363: $this->getResponse()->setBody(
364: $this->getLayout()->createBlock('adminhtml/review_rating_detailed')->setIndependentMode()->toHtml()
365: );
366: }
367:
368: protected function _isAllowed()
369: {
370: switch ($this->getRequest()->getActionName()) {
371: case 'pending':
372: return Mage::getSingleton('admin/session')->isAllowed('catalog/reviews_ratings/reviews/pending');
373: break;
374: default:
375: return Mage::getSingleton('admin/session')->isAllowed('catalog/reviews_ratings/reviews/all');
376: break;
377: }
378: }
379: }
380: