* @copyright 2007-2014 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ include_once(dirname(__FILE__).'/../../stblogcomments.php'); include_once(dirname(__FILE__).'/../../classes/StBlogCommentClass.php'); class StBlogCommentsDefaultModuleFrontController extends ModuleFrontController { public function __construct() { parent::__construct(); $this->context = Context::getContext(); } public function initContent() { parent::initContent(); if (Tools::isSubmit('action')) { switch(Tools::getValue('action')) { case 'add_comment': $this->ajaxProcessAddComment(); break; } } } protected function ajaxProcessAddComment() { $module_instance = new StBlogComments(); $result = array( 'r' => false, 'm' => '', ); $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_st_blog'))) $errors[] = $this->trans('Invalid blog ID ', array(), 'Shop.Theme.Panda'); if (!Tools::getValue('content') || !Validate::isMessage(Tools::getValue('content'))) $errors[] = $this->trans('Your comment is too short. Please try again', array(), 'Shop.Theme.Panda'); if (!$id_customer && (!Tools::isSubmit('customer_name') || !Tools::getValue('customer_name') || !Validate::isGenericName(Tools::getValue('customer_name')))) $errors[] = $this->trans('Customer name is empty', array(), 'Shop.Theme.Panda'); if (!$id_customer && Tools::getValue('customer_email') && !Validate::isEmail(Tools::getValue('customer_email'))) $errors[] = $this->trans('Customer email is invalid', array(), 'Shop.Theme.Panda'); if (!$this->context->customer->id && !Configuration::get('ST_BLOG_C_ALLOW_GUESTS')) $errors[] = $this->trans('Please login to post a comment', array(), 'Shop.Theme.Panda'); $blog = new StBlogClass(Tools::getValue('id_st_blog')); if (!$blog->id) $errors[] = $this->trans('Blog not found', array(), 'Shop.Theme.Panda'); if (!count($errors)) { $customer_comment = StBlogCommentClass::getByCustomer((int)(Tools::getValue('id_st_blog','0')), $id_customer, true, (int)$id_guest, $this->context->shop->id); if (!$customer_comment || ($customer_comment && (strtotime($customer_comment['date_add']) + Configuration::get('ST_BLOG_C_MINIMAL_TIME')) < time())) { $comment = new StBlogCommentClass(); $comment->content = strip_tags(Tools::getValue('content')); $comment->id_st_blog = (int)Tools::getValue('id_st_blog'); $comment->id_parent = (int)Tools::getValue('id_parent'); $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->customer_email = Tools::getValue('customer_email'); if (!$comment->customer_email) $comment->customer_email = $this->context->customer->email; $comment->id_shop = $this->context->shop->id;; $comment->active = 0; $comment->save(); $result['r'] = true; $result['thank'] = $this->trans('Thank you for your comment.', array(), 'Shop.Theme.Panda'); $result['moderation'] = $this->trans('Your comment may be awaiting moderation before being published.', array(), 'Shop.Theme.Panda'); } else { $minimal_time = Configuration::get('ST_BLOG_C_MINIMAL_TIME'); $result['m'] = $this->trans('You should wait %seconds% seconds before posting a new comment.', array('%seconds%'=>$minimal_time), 'Shop.Theme.Panda'); } } else $result['m'] = implode('
',$errors); die(Tools::jsonEncode($result)); } }