Пример #1
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)));
 }
Пример #2
0
         $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.') : ''));
         }
     } else {
         $errors[] = $productCom->l('Comment text is required.');
     }
 } else {
     $errors[] = sprintf($productCom->l('You should wait %d seconds before posting a new comment.'), Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME'));
Пример #3
0
 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');
         }
     }
 }
Пример #4
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.');
         }
     }
 }
Пример #5
0
 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');
         }
     }
 }