public function create() { // load the product // is it valid? // is the user logged in? // can the user review this product? // try/catch the save try { $slug = $this->inputfilter->clean($this->app->get('PARAMS.slug'), 'cmd'); $item = $this->model('products')->setState('filter.slug', $slug)->getItem(); if (empty($item->id)) { throw new \Exception(); } } catch (\Exception $e) { if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false, 'error' => true, 'message' => 'Invalid Product'))); } else { $this->app->error('404', 'Invalid Product'); return; } } $redirect = '/shop/product/' . $item->slug; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('shop.product_review.redirect')) { $redirect = $custom_redirect; } try { $user = $this->getIdentity(); if (empty($user->id)) { throw new \Exception('Must be logged in to post a review'); } $canReview = \Shop\Models\ProductReviews::canUserReview($user, $item); if ($canReview !== true) { throw new \Exception($canReview); } $post = $this->app->get('POST'); $post['description'] = !empty($post['description']) ? nl2br($post['description']) : null; $review = (new \Shop\Models\ProductReviews($post))->set('product_id', $item->id)->set('user_id', $user->id)->set('user_name', $user->first_name)->set('publication.status', 'draft')->save(); // Add images, using a model method $review->addImages($this->app->get('FILES')); $successMessage = 'Thanks for the review! It will be published following review by our moderators.'; if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => true, 'message' => $successMessage))); } else { \Dsc\System::addMessage($successMessage, 'success'); $this->app->reroute($redirect); return; } } catch (\Exception $e) { if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false, 'error' => true, 'message' => $e->getMessage()))); } else { \Dsc\System::addMessage($e->getMessage(), 'error'); $this->app->reroute($redirect); return; } } }
}); </script> <?php } ?> </div> </div> </div> </div> <hr /> <?php // can this user make the review? ?> <?php if (\Shop\Models\ProductReviews::canUserReview($this->auth->getIdentity(), $item) === true) { $this->app->set('review_key', 0); echo $this->renderView('Shop/Site/Views::product/fragment_reviews_create.php'); ?> <hr /><?php } ?> <?php // are there reviews to display? ?> <?php if ($paginated = \Shop\Models\ProductReviews::forProduct($item)) { ?> <div class="reviews-content">