public function actionCommentCreate()
 {
     if (isset($_POST['productCommentJSON']) && isJSON($_POST['productCommentJSON'])) {
         $productCommentModel = new ProductComment();
         $errorMessage = null;
         $commentCreatePost = json_decode($_POST['productCommentJSON']);
         $productCommentModel->text = $commentCreatePost['text'];
         $productCommentModel->create_time = date("Y-m-d H:i:s");
         $productCommentModel->product_id = $commentCreatePost['product_id'];
         $productCommentModel->contact_method = $commentCreatePost['contact_method'];
         $productCommentModel->amazing_level = $commentCreatePost['amazing_level'];
         if (!$productCommentModel->validateProductId()) {
             $errorMessage = '该产品不存在,请确认后重新提交';
         }
         if (!$productCommentModel->validateContactMethod()) {
             $errorMessage = '联系方式超出长度,请简略填写联系信息(小于50个字)';
         }
         if (!$productCommentModel->validateSiteMark()) {
             $errorMessage = '评分不在范围之内,请重新输入或与管理员联系';
         }
         $this->layout = false;
         if ($errorMessage === null) {
             $productModel = Product::model()->findByPk($productCommentModel->product_id);
             $productModel->product_mark_sum = $productModel->product_mark * $productModel->product_mark_times + $productCommentModel->amazing_level;
             ++$productModel->product_mark_times;
             $productModel->product_mark = $productModel->product_mark_sum / $productModel->product_mark_times;
             if ($productCommentModel->save() && $productModel->save()) {
                 $this->render('_customerCreate', array('response' => 'success', 'message' => $productCommentModel->create_time));
             }
         } else {
             $this->render('_customerCreate', array('response' => 'failure', 'message' => $errorMessage));
         }
     }
 }
 public function actionCommentCreate()
 {
     if (isset($_POST['commentJSON']) && isJSON($_POST['commentJSON'])) {
         $productCommentModel = new ProductComment();
         $errorMessage = null;
         $commentCreatePost = json_decode($_POST['commentJSON'], true);
         $productCommentModel->text = isset($commentCreatePost['text']) ? $commentCreatePost['text'] : '';
         $productCommentModel->create_time = date("Y-m-d H:i:s");
         $productCommentModel->product_id = isset($commentCreatePost['product_id']) ? $commentCreatePost['product_id'] : '';
         $productCommentModel->contact_method = isset($commentCreatePost['contact_method']) ? $commentCreatePost['contact_method'] : '';
         $productCommentModel->amazing_level = isset($commentCreatePost['amazing_level']) ? $commentCreatePost['amazing_level'] : 5;
         if (!$productCommentModel->validateProductId($productCommentModel->product_id)) {
             $errorMessage = Yii::t('productComment', 'The product is not exist, please try again!');
         }
         if (!$productCommentModel->validateContactMethod()) {
             $errorMessage = Yii::t('productComment', 'Contact method is too long, please shorten it!');
         }
         if (!$productCommentModel->validateSiteMark()) {
             $errorMessage = Yii::t('productComment', 'The mark is out of range, please try again!');
         }
         $this->layout = false;
         if ($errorMessage === null) {
             $productModel = Product::model()->findByAttributes(array('product_id' => $productCommentModel->product_id));
             $productModel->product_mark_sum = $productModel->product_mark * $productModel->product_marked_times + $productCommentModel->amazing_level;
             ++$productModel->product_marked_times;
             $productModel->product_mark = $productModel->product_mark_sum / $productModel->product_marked_times;
             if ($productCommentModel->save() && $productModel->save()) {
                 $this->render('_customerCreate', array('response' => 'success', 'message' => $productCommentModel->create_time));
             }
         } else {
             $this->render('_customerCreate', array('response' => 'failure', 'message' => $errorMessage));
         }
     }
 }
示例#3
0
 protected function ajaxProcessAddComment()
 {
     $module_instance = new ProductComments();
     $result = true;
     $id_guest = 0;
     $id_customer = $this->context->customer->id;
     if (!$id_customer) {
         $id_guest = $this->context->cookie->id_guest;
     }
     $errors = array();
     // Validation
     if (!Validate::isInt(Tools::getValue('id_product'))) {
         $errors[] = $module_instance->l('ID product is incorrect', 'default');
     }
     if (!Tools::getValue('title') || !Validate::isGenericName(Tools::getValue('title'))) {
         $errors[] = $module_instance->l('Title is incorrect', 'default');
     }
     if (!Tools::getValue('content') || !Validate::isMessage(Tools::getValue('content'))) {
         $errors[] = $module_instance->l('Comment is incorrect', 'default');
     }
     if (!$id_customer && (!Tools::isSubmit('customer_name') || !Tools::getValue('customer_name') || !Validate::isGenericName(Tools::getValue('customer_name')))) {
         $errors[] = $module_instance->l('Customer name is incorrect', 'default');
     }
     if (!$this->context->customer->id && !Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS')) {
         $errors[] = $module_instance->l('You must be logged in order to send a comment', 'default');
     }
     if (!count(Tools::getValue('criterion'))) {
         $errors[] = $module_instance->l('You must give a rating', 'default');
     }
     $product = new Product(Tools::getValue('id_product'));
     if (!$product->id) {
         $errors[] = $module_instance->l('Product not found', 'default');
     }
     if (!count($errors)) {
         $customer_comment = ProductComment::getByCustomer(Tools::getValue('id_product'), $id_customer, true, $id_guest);
         if (!$customer_comment || $customer_comment && strtotime($customer_comment['date_add']) + (int) Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME') < time()) {
             $comment = new ProductComment();
             $comment->content = strip_tags(Tools::getValue('content'));
             $comment->id_product = (int) Tools::getValue('id_product');
             $comment->id_customer = (int) $id_customer;
             $comment->id_guest = $id_guest;
             $comment->customer_name = Tools::getValue('customer_name');
             if (!$comment->customer_name) {
                 $comment->customer_name = pSQL($this->context->customer->firstname . ' ' . $this->context->customer->lastname);
             }
             $comment->title = Tools::getValue('title');
             $comment->grade = 0;
             $comment->validate = 0;
             $comment->save();
             $grade_sum = 0;
             foreach (Tools::getValue('criterion') as $id_product_comment_criterion => $grade) {
                 $grade_sum += $grade;
                 $product_comment_criterion = new ProductCommentCriterion($id_product_comment_criterion);
                 if ($product_comment_criterion->id) {
                     $product_comment_criterion->addGrade($comment->id, $grade);
                 }
             }
             if (count(Tools::getValue('criterion')) >= 1) {
                 $comment->grade = $grade_sum / count(Tools::getValue('criterion'));
                 // Update Grade average of comment
                 $comment->save();
             }
             $result = true;
         } else {
             $result = false;
             $errors[] = $module_instance->l('You should wait') . ' ' . Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME') . ' ' . $module_instance->l('seconds before posting a new comment');
         }
     } else {
         $result = false;
     }
     die(Tools::jsonEncode(array('result' => $result, 'errors' => $errors)));
 }
示例#4
0
 }
 if (!count($errors) && $content) {
     $comment = new ProductComment();
     $comment->content = strip_tags($content);
     $comment->id_product = (int) $id_product;
     $comment->id_customer = (int) Context::getContext()->cookie->id_customer;
     $comment->id_guest = (int) $id_guest;
     $comment->customer_name = pSQL($customer_name);
     if (!$comment->id_customer) {
         $comment->customer_name = pSQL($name);
     }
     $comment->title = pSQL($title);
     $comment->grade = 0;
     $comment->validate = 0;
     $tgrade = 0;
     $comment->save();
     foreach ($grades as $grade) {
         $tgrade += $grade['grade'];
         $productCommentCriterion = new ProductCommentCriterion((int) Tools::getValue('id_product_comment_criterion_' . $grade['id']));
         if ($productCommentCriterion->id) {
             $productCommentCriterion->addGrade($comment->id, $grade['grade']);
         }
     }
     if (count($grades) - 1 >= 0) {
         $comment->grade = (int) ($tgrade / (int) count($grades));
     }
     if (!$comment->save()) {
         $errors[] = $productCom->l('An error occurred while saving your comment.');
     } else {
         Context::getContext()->smarty->assign('confirmation', $productCom->l('Comment posted.') . ((int) Configuration::get('PRODUCT_COMMENTS_MODERATE') ? ' ' . $productCom->l('Awaiting moderator validation.') : ''));
     }
 /**
  * Action: Show page post action
  * @return Response
  */
 public function postAction($slug)
 {
     $postComment = e(Input::get('postComment'));
     if ($postComment) {
         // Get comment
         $content = e(Input::get('content'));
         // Check word
         if (mb_strlen($content) < 3) {
             return Redirect::back()->withInput()->withErrors($this->messages->add('content', '评论不得少于3个字符。'));
         }
         // Find article
         $product = $this->model->where('slug', $slug)->first();
         // Create comment
         $comment = new ProductComment();
         $comment->content = $content;
         $comment->product_id = $product->id;
         $comment->user_id = Auth::user()->id;
         if ($comment->save()) {
             // Create success
             // Updated comments
             $product->comments_count = $product->comments->count();
             $product->save();
             // Return success
             return Redirect::back()->with('success', '评论成功。');
         } else {
             // Create fail
             return Redirect::back()->withInput()->with('error', '评论失败。');
         }
     } else {
         $data = Input::all();
         $rules = array('quantity' => 'required|integer', 'product_id' => 'required', 'price' => 'required', 'seller_id' => 'required', 'inventory' => 'required');
         if (e($data['inventory']) < e($data['quantity'])) {
             return Redirect::back()->with('error', '<strong>请输入正确的' . $this->resourceName . '购买数量</strong>');
         } elseif (Auth::user()->id == e($data['seller_id'])) {
             return Redirect::back()->with('error', '<strong>您不能购买自己出售的商品</strong>');
         } else {
             // Custom validation message
             $messages = $this->validatorMessages;
             // Begin verification
             $validator = Validator::make($data, $rules, $messages);
             if ($validator->passes()) {
                 // Verification success
                 // Add recource
                 $model = new ShoppingCart();
                 $model->buyer_id = Auth::user()->id;
                 $model->quantity = e($data['quantity']);
                 $model->product_id = e($data['product_id']);
                 $model->price = e($data['price']);
                 $model->payment = e($data['quantity']) * e($data['price']);
                 $model->seller_id = e($data['seller_id']);
                 if ($model->save()) {
                     // Add success
                     return Redirect::back()->with('success', '<strong>' . $this->resourceName . '已添加到购物车:</strong>您可以继续选购' . $this->resourceName . ',或立即结算。');
                 } else {
                     // Add fail
                     return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '添加到购物车失败。</strong>');
                 }
             } else {
                 // Verification fail
                 return Redirect::back()->withInput()->withErrors($validator);
             }
         }
     }
 }
示例#6
0
 private function _frontOfficePostProcess()
 {
     global $smarty, $cookie, $errors;
     require_once dirname(__FILE__) . '/ProductComment.php';
     require_once dirname(__FILE__) . '/ProductCommentCriterion.php';
     if (Tools::isSubmit('submitMessage') and empty($cookie->id_customer) === false) {
         if (Tools::getValue('content')) {
             $comment = new ProductComment();
             $comment->content = strip_tags(Tools::getValue('content'));
             $comment->id_product = intval($_GET['id_product']);
             $comment->id_customer = intval($cookie->id_customer);
             $comment->grade = 0;
             $comment->validate = 0;
             if (!$comment->content) {
                 $errors[] = $this->l('Invalid comment text posted.');
             } else {
                 $comment->save();
                 for ($i = 1, $grade = 0; isset($_POST[$i . '_grade']) === true; ++$i) {
                     $cgrade = intval(Tools::getValue($i . '_grade'));
                     $grade += $cgrade;
                     $cid_product_comment_criterion = Tools::getValue('id_product_comment_criterion_' . $i);
                     ProductCommentCriterion::addGrade($comment->id, $cid_product_comment_criterion, $cgrade);
                 }
                 if ($i - 1 > 0) {
                     $comment->grade = $grade / ($i - 1);
                 }
                 if (!$comment->save()) {
                     $errors[] = $this->l('An error occured while saving your comment.');
                 } else {
                     $smarty->assign('confirmation', $this->l('Comment posted successfully.') . (intval(Configuration::get('PRODUCT_COMMENTS_MODERATE')) ? $this->l(' Awaiting moderator validation.') : ''));
                 }
             }
         } else {
             $errors[] = $this->l('Comment text is required.');
         }
     }
 }
 private function _frontOfficePostProcess()
 {
     global $smarty, $cookie, $errors;
     require_once dirname(__FILE__) . '/ProductComment.php';
     require_once dirname(__FILE__) . '/ProductCommentCriterion.php';
     $allow_guests = (int) Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS');
     if (Tools::isSubmit('submitMessage') and (empty($cookie->id_customer) === false or $cookie->id_guest and $allow_guests)) {
         $id_guest = !($id_customer = (int) $cookie->id_customer) ? (int) $cookie->id_guest : false;
         $customerComment = ProductComment::getByCustomer((int) Tools::getValue('id_product'), (int) $cookie->id_customer, true, (int) $id_guest);
         if (!$customerComment or $customerComment and strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME') < time()) {
             $customer_name = false;
             if ($id_guest and !($customer_name = Tools::getValue('customer_name'))) {
                 $errors[] = $this->l('Please fill your name');
             }
             if (!sizeof($errors) and Tools::getValue('content')) {
                 $product = new Product((int) $_GET['id_product'], true, $cookie->id_lang);
                 $comment = new ProductComment();
                 $comment->content = strip_tags(Tools::getValue('content'));
                 $comment->id_product = (int) $_GET['id_product'];
                 $comment->id_customer = (int) $cookie->id_customer;
                 $comment->id_guest = (int) $id_guest;
                 $comment->customer_name = pSQL($customer_name);
                 $comment->title = pSQL(Tools::getValue('title'));
                 $comment->grade = 0;
                 $comment->validate = 0;
                 if (!$comment->content) {
                     $errors[] = $this->l('Invalid comment text posted.');
                 } else {
                     $comment->save();
                     for ($i = 1, $grade = 0; isset($_POST[$i . '_grade']) === true; ++$i) {
                         $cgrade = (int) Tools::getValue($i . '_grade');
                         $grade += $cgrade;
                         $productCommentCriterion = new ProductCommentCriterion((int) Tools::getValue('id_product_comment_criterion_' . $i));
                         if ($productCommentCriterion->id) {
                             $productCommentCriterion->addGrade($comment->id, $cgrade);
                         }
                     }
                     if ($i - 1 > 0) {
                         $comment->grade = $grade / ($i - 1);
                     }
                     if (!$comment->save()) {
                         $errors[] = $this->l('An error occurred while saving your review.');
                     } else {
                         $smarty->assign('submit_confirmation', $this->l('Review posted.') . ((int) Configuration::get('PRODUCT_COMMENTS_MODERATE') ? ' ' . $this->l('Awaiting moderator approval.') : ''));
                     }
                     //send mail notifications
                     Mail::Send((int) $cookie->id_lang, 'review', Mail::l('New Product Review'), array('{product}' => $product->name, '{rw_rating}' => $comment->grade, '{title}' => $comment->title, '{rw_content}' => $comment->content), '*****@*****.**', 'Product Reviews');
                 }
             } else {
                 $errors[] = $this->l('Comment text is required.');
             }
         } else {
             $errors[] = $this->l('You should wait') . ' ' . Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME') . ' ' . $this->l('seconds before posting a new comment');
         }
     }
 }
 private function _frontOfficePostProcess()
 {
     global $smarty, $cookie, $errors;
     require_once dirname(__FILE__) . '/ProductComment.php';
     require_once dirname(__FILE__) . '/ProductCommentCriterion.php';
     if (empty($cookie->id_customer) && !isset($cookie->id_guest)) {
         Guest::setNewGuest($cookie);
     }
     $allow_guests = (int) Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS');
     if (Tools::isSubmit('submitMessage') && (!empty($cookie->id_customer) || $cookie->id_guest && $allow_guests)) {
         $id_guest = !($id_customer = (int) $cookie->id_customer) ? (int) $cookie->id_guest : false;
         $customerComment = ProductComment::getByCustomer((int) Tools::getValue('id_product'), (int) $cookie->id_customer, true, (int) $id_guest);
         if (!$customerComment || $customerComment && strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME') < time()) {
             $customer_name = false;
             if ($id_guest && !($customer_name = Tools::getValue('customer_name'))) {
                 $errors[] = $this->l('Please fill your name');
             }
             if (!count($errors) && Tools::getValue('content')) {
                 $comment = new ProductComment();
                 $comment->content = strip_tags(Tools::getValue('content'));
                 $comment->id_product = (int) $_GET['id_product'];
                 $comment->id_customer = (int) $cookie->id_customer;
                 $comment->id_guest = (int) $id_guest;
                 $comment->customer_name = pSQL($customer_name);
                 $comment->title = pSQL(Tools::getValue('title'));
                 $comment->grade = 0;
                 $comment->validate = 0;
                 if (!$comment->content) {
                     $errors[] = $this->l('Invalid comment text posted.');
                 } else {
                     $comment->save();
                     for ($i = 1, $grade = 0; isset($_POST[$i . '_grade']) === true; ++$i) {
                         $cgrade = (int) Tools::getValue($i . '_grade');
                         $grade += $cgrade;
                         $productCommentCriterion = new ProductCommentCriterion((int) Tools::getValue('id_product_comment_criterion_' . $i));
                         if ($productCommentCriterion->id) {
                             $productCommentCriterion->addGrade($comment->id, $cgrade);
                         }
                     }
                     if ($i - 1 > 0) {
                         $comment->grade = $grade / ($i - 1);
                     }
                     if (!$comment->save()) {
                         $errors[] = $this->l('An error occurred while saving your comment.');
                     } else {
                         $smarty->assign('confirmation', $this->l('Comment posted.') . ((int) Configuration::get('PRODUCT_COMMENTS_MODERATE') ? ' ' . $this->l('Awaiting moderator validation.') : ''));
                     }
                 }
             } else {
                 $errors[] = $this->l('Comment text is required.');
             }
         } else {
             $errors[] = $this->l('You should wait') . ' ' . Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME') . ' ' . $this->l('seconds before posting a new comment');
         }
     }
 }