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))); }
public static function getTypes() { // Instance of module class for translations $module = new ProductComments(); return array(1 => $module->l('Valid for the entire catalog', 'ProductCommentCriterion'), 2 => $module->l('Restricted to some categories', 'ProductCommentCriterion'), 3 => $module->l('Restricted to some products', 'ProductCommentCriterion')); }