예제 #1
0
 public function testProductRatingTypes()
 {
     $subCategory = Category::getNewInstance(Category::getRootNode());
     $subCategory->save();
     $product = Product::getNewInstance($subCategory, 'test');
     $product->save();
     $rootType = ProductRatingType::getNewInstance(Category::getRootNode());
     $rootType->save();
     $subType = ProductRatingType::getNewInstance($subCategory);
     $subType->save();
     $types = ProductRatingType::getProductRatingTypes($product);
     $this->assertEqual($types->size(), 2);
     // parent category types should go first
     $this->assertSame($types->get(0), $rootType);
     $this->assertSame($types->get(1), $subType);
 }
예제 #2
0
 public function rate()
 {
     $product = Product::getInstanceByID($this->request->get('id'), Product::LOAD_DATA);
     $ratingTypes = ProductRatingType::getProductRatingTypes($product);
     $validator = $this->buildRatingValidator($ratingTypes->toArray(), $product, true);
     $redirect = new ActionRedirectResponse('product', 'index', array('id' => $product->getID()));
     if ($validator->isValid()) {
         $msg = $this->translate('_msg_rating_added');
         if ($this->isAddingReview()) {
             $review = ProductReview::getNewInstance($product, $this->user);
             $review->loadRequestData($this->request);
             $review->ip->set($this->request->getIpLong());
             // approval status
             $approval = $this->config->get('APPROVE_REVIEWS');
             $review->isEnabled->set('APPROVE_REVIEWS_AUTO' == $approval || 'APPROVE_REVIEWS_USER' == $approval && !$this->user->isAnonymous());
             $review->save();
             $msg = $this->translate('_msg_review_added');
         }
         foreach ($ratingTypes as $type) {
             $rating = ProductRating::getNewInstance($product, $type, $this->user);
             $rating->rating->set($this->request->get('rating_' . $type->getID()));
             if (isset($review)) {
                 $rating->review->set($review);
             }
             $rating->ip->set($this->request->getIpLong());
             $rating->save();
         }
         if ($this->isAjax()) {
             $response = new JSONResponse(array('message' => $msg), 'success');
         } else {
             $this->setMessage($msg);
             $response = $redirect;
         }
         $response->setCookie('rating_' . $product->getID(), true, strtotime('+' . $this->config->get('RATING_SAME_IP_TIME') . ' hours'), $this->router->getBaseDirFromUrl());
         return $response;
     } else {
         if ($this->isAjax()) {
             return new JSONResponse(array('errors' => $validator->getErrorList()));
         } else {
             return $redirect;
         }
     }
 }
예제 #3
0
 private function buildValidator(ProductReview $review)
 {
     $validator = $this->getValidator("productRating", $this->getRequest());
     // option validation
     foreach (ProductRatingType::getProductRatingTypes($review->product->get())->toArray() as $type) {
         $validator->addCheck('rating_' . $type['ID'], new IsNotEmptyCheck($this->translate('_err_no_rating_selected')));
     }
     $validator->addCheck('nickname', new IsNotEmptyCheck($this->translate('_err_no_review_nickname')));
     $validator->addCheck('title', new IsNotEmptyCheck($this->translate('_err_no_review_summary')));
     $validator->addCheck('text', new IsNotEmptyCheck($this->translate('_err_no_review_text')));
     return $validator;
 }