* @copyright 2007-2012 PrestaShop SA * @version Release: $Revision: 7040 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; class ProductComments extends Module { const INSTALL_SQL_FILE = 'install.sql'; private $_html = ''; private $_postErrors = array(); private $_productCommentsCriterionTypes = array(); private $_baseUrl; public function __construct() { $this->name = 'productcomments'; $this->tab = 'front_office_features'; $this->version = '2.3'; $this->author = 'PrestaShop'; $this->need_instance = 0; $this->secure_key = Tools::encrypt($this->name); parent::__construct(); $this->displayName = $this->l('Product Comments'); $this->description = $this->l('Allows users to post reviews.'); } public function install() { if (!file_exists(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE)) return false; else if (!$sql = file_get_contents(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE)) return false; $sql = str_replace(array('PREFIX_', 'ENGINE_TYPE'), array(_DB_PREFIX_, _MYSQL_ENGINE_), $sql); $sql = preg_split("/;\s*[\r\n]+/", trim($sql)); foreach ($sql as $query) if (!Db::getInstance()->execute(trim($query))) return false; if (parent::install() == false || !$this->registerHook('productTab') || !$this->registerHook('extraProductComparison') || !$this->registerHook('productTabContent') || !$this->registerHook('header') || !$this->registerHook('productOutOfStock') || !Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', 30) || !Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', 0) || !Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', 1)) return false; return true; } public function uninstall() { if (!parent::uninstall() || !$this->deleteTables() || !Configuration::deleteByName('PRODUCT_COMMENTS_MODERATE') || !Configuration::deleteByName('PRODUCT_COMMENTS_ALLOW_GUESTS') || !Configuration::deleteByName('PRODUCT_COMMENTS_MINIMAL_TIME') || !$this->unregisterHook('extraProductComparison') || !$this->unregisterHook('productOutOfStock') || !$this->unregisterHook('productTabContent') || !$this->unregisterHook('header') || !$this->unregisterHook('productTab')) return false; return true; } public function deleteTables() { return Db::getInstance()->execute(' DROP TABLE IF EXISTS `'._DB_PREFIX_.'product_comment`, `'._DB_PREFIX_.'product_comment_criterion`, `'._DB_PREFIX_.'product_comment_criterion_product`, `'._DB_PREFIX_.'product_comment_criterion_lang`, `'._DB_PREFIX_.'product_comment_criterion_category`, `'._DB_PREFIX_.'product_comment_grade`, `'._DB_PREFIX_.'product_comment_usefulness`, `'._DB_PREFIX_.'product_comment_report`'); } protected function _postProcess() { if (Tools::isSubmit('submitModerate')) { Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', (int)Tools::getValue('moderate')); Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', (int)Tools::getValue('allow_guest')); Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', (int)Tools::getValue('product_comments_minimal_time')); $this->_html .= '
'.$this->l('Settings updated').'
'; } if ($id_criterion = (int)Tools::getValue('deleteCriterion')) { $productCommentCriterion = new ProductCommentCriterion((int)$id_criterion); if ($productCommentCriterion->id) if ($productCommentCriterion->delete()) $this->_html .= '
'.$this->l('Criterion deleted').'
'; } } public function getContent() { include_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $this->_setBaseUrl(); $this->_productCommentsCriterionTypes = ProductCommentCriterion::getTypes(); $this->_html = '

'.$this->displayName.'

'; $this->_postProcess(); $this->_checkModerateComment(); $this->_checkReportedComment(); $this->_checkCriterion(); $this->_updateApplicationCriterion(); $this->_checkDeleteComment(); return $this->_html.$this->_displayForm(); } private function _setBaseUrl() { $this->_baseUrl = 'index.php?'; foreach ($_GET as $k => $value) if (!in_array($k, array('deleteCriterion', 'editCriterion'))) $this->_baseUrl .= $k.'='.$value.'&'; $this->_baseUrl = rtrim($this->_baseUrl, '&'); } private function _checkModerateComment() { $action = Tools::getValue('action'); if (empty($action) === false && (int)Configuration::get('PRODUCT_COMMENTS_MODERATE')) { $product_comments = Tools::getValue('id_product_comment'); if (count($product_comments)) { require_once(dirname(__FILE__).'/ProductComment.php'); switch ($action) { case 'accept': foreach ($product_comments as $id_product_comment) { if (!$id_product_comment) continue; $comment = new ProductComment((int)$id_product_comment); $comment->validate(); } break; case 'delete': foreach ($product_comments as $id_product_comment) { if (!$id_product_comment) continue; $comment = new ProductComment((int)$id_product_comment); $comment->delete(); ProductComment::deleteGrades((int)$id_product_comment); } break; default: ; } } } } private function _checkReportedComment() { $action = Tools::getValue('action'); if (empty($action) === false) { $product_comments = Tools::getValue('id_product_comment'); if (count($product_comments)) { require_once(dirname(__FILE__).'/ProductComment.php'); switch ($action) { case 'accept': foreach ($product_comments as $id_product_comment) { if (!$id_product_comment) continue; $comment = new ProductComment((int)$id_product_comment); $comment->validate(); ProductComment::deleteReports((int)$id_product_comment); } break; case 'delete': foreach ($product_comments as $id_product_comment) { if (!$id_product_comment) continue; $comment = new ProductComment((int)$id_product_comment); $comment->delete(); ProductComment::deleteGrades((int)$id_product_comment); ProductComment::deleteReports((int)$id_product_comment); ProductComment::deleteUsefulness((int)$id_product_comment); } break; default: ; } } } } private function _checkCriterion() { $action_criterion = Tools::getValue('criterion_action'); $name = Tools::getValue('criterion'); if (Tools::isSubmit('submitAddCriterion')) { require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $languages = Language::getLanguages(); $id_criterion = (int)Tools::getValue('id_product_comment_criterion'); $productCommentCriterion = new ProductCommentCriterion((int)$id_criterion); foreach ($languages as $lang) $productCommentCriterion->name[(int)$lang['id_lang']] = Tools::getValue('criterion_'.(int)$lang['id_lang']); // Check default language criterion name $defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT'))); if (!Tools::getValue('criterion_'.$defaultLanguage->id)) { $this->_html .= '
'.$this->l('The field Name is required at least in').' '.$defaultLanguage->name.'
'; return; } $productCommentCriterion->id_product_comment_criterion_type = (int)Tools::getValue('criterion_type'); $productCommentCriterion->active = (int)Tools::getValue('criterion_active'); if ($productCommentCriterion->save()) $this->_html .= '
'.(Tools::getValue('editCriterion') ? $this->l('Criterion updated') : $this->l('Criterion added')).'
'; } else if (!empty($action_criterion) && empty($name)) { $id_product_comment_criterion = Tools::getValue('id_product_comment_criterion'); require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); switch ($action_criterion) { case 'edit': ProductCommentCriterion::update($id_product_comment_criterion, Tools::getValue('criterion_id_lang'), Tools::getValue('criterion_name')); break; case 'delete': ProductCommentCriterion::delete($id_product_comment_criterion); break; default: ; } } } private function _updateApplicationCriterion() { if (Tools::isSubmit('submitApplicationCriterion')) { include_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $id_criterion = (int)Tools::getValue('id_criterion'); $productCommentCriterion = new ProductCommentCriterion((int)$id_criterion); if ($productCommentCriterion->id) { if ($productCommentCriterion->id_product_comment_criterion_type == 2) { $productCommentCriterion->deleteCategories(); if ($categories = Tools::getValue('id_product')) if (count($categories)) foreach ($categories as $id_category) $productCommentCriterion->addCategory((int)$id_category); } else if ($productCommentCriterion->id_product_comment_criterion_type == 3) { $productCommentCriterion->deleteProducts(); if ($products = Tools::getValue('id_product')) if (count($products)) foreach ($products as $product) $productCommentCriterion->addProduct((int)$product); } } $this->_html .= '
'.$this->l('Settings updated').'
'; } } private function _displayForm() { $this->_displayFormModerate(); $this->_displayFormReported(); $this->_displayFormConfigurationCriterion(); $this->_displayFormApplicationCriterion(); $this->_displayFormDelete(); return $this->_html; } private function _displayFormModerate() { $this->_html = '
'.$this->l('Configuration').'
'.$this->l('seconds').'

'.$this->l('Moderate Comments').''; if (Configuration::get('PRODUCT_COMMENTS_MODERATE')) { require_once(dirname(__FILE__).'/ProductComment.php'); $comments = ProductComment::getByValidate(); if (count($comments)) { $this->_html .= '

'; foreach ($comments as $comment) $this->_html .= ''; $this->_html .= '
'.$this->l('Author').' '.$this->l('Comment').' '.$this->l('Product name').' '.$this->l('Actions').'
'.htmlspecialchars($comment['customer_name'], ENT_COMPAT, 'UTF-8').'. '.htmlspecialchars($comment['content'], ENT_COMPAT, 'UTF-8').' '.$comment['id_product'].' - '.htmlspecialchars($comment['name'], ENT_COMPAT, 'UTF-8').' '.$this->l('Accept').' '.$this->l('Delete').'
'.$this->l('Selection:').' '.$this->l('Accept').' '.$this->l('Delete').'
'; } else $this->_html .= $this->l('No comments to validate at this time.'); } $this->_html .= '

'; } private function _displayFormReported() { $this->_html .= '
'.$this->l('Reported Comments').''; require_once(dirname(__FILE__).'/ProductComment.php'); $comments = ProductComment::getReportedComments(); if (count($comments)) { $this->_html .= '

'; foreach ($comments as $comment) $this->_html .= ''; $this->_html .= '
'.$this->l('Author').' '.$this->l('Comment').' '.$this->l('Product name').' '.$this->l('Actions').'
'.htmlspecialchars($comment['customer_name'], ENT_COMPAT, 'UTF-8').'. '.htmlspecialchars($comment['content'], ENT_COMPAT, 'UTF-8').' '.$comment['id_product'].' - '.htmlspecialchars($comment['name'], ENT_COMPAT, 'UTF-8').' '.$this->l('Accept').' '.$this->l('Delete').'
'.$this->l('Selection:').' '.$this->l('Accept').' '.$this->l('Delete').'
'; } else $this->_html .= $this->l('No reported comment at this time.'); $this->_html .= '

'; } private function _displayFormConfigurationCriterion() { $langs = Language::getLanguages(false); $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT'); $id_criterion = (int)Tools::getValue('editCriterion'); $criterion = new ProductCommentCriterion((int)$id_criterion); $languageIds = 'criterion'; $this->_html .= '
'.$this->l('Add a new comment criterion').'

'.$this->l('You can define several criterions to help your customers during their review. For instance: efficiency, lightness, design.').'

'.$this->l('You can add a new criterion below:').'

'; foreach ($langs as $lang) $this->_html .= '
'; $this->_html .= $this->displayFlags($langs, (int)$id_lang_default, $languageIds, 'criterion', true); $this->_html .= '
 
active ? 'checked="checked" ' : '').'/> active ? 'checked="checked" ' : '').'/>
'; require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $criterions = ProductCommentCriterion::getCriterions($this->context->language->id); if (count($criterions)) { $this->_html .= '
'; foreach ($criterions as $criterion) { $this->_html .= ''; } $this->_html .= '
'.$this->l('Criterion').' '.$this->l('Type').' '.$this->l('Status').' '.$this->l('Actions').'
'.$criterion['name'].' '.$this->_productCommentsCriterionTypes[(int)$criterion['id_product_comment_criterion_type']].' '.$this->l('Edit').' '.$this->l('Delete').'
'; } $this->_html .= '

'; } private function _displayFormApplicationCriterion() { include_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $criterions = ProductCommentCriterion::getCriterions($this->context->language->id, false, true); $id_criterion = (int)Tools::getValue('updateCriterion'); if ($id_criterion) { $criterion = new ProductCommentCriterion((int)$id_criterion); if ($criterion->id_product_comment_criterion_type == 2) { $categories = Category::getSimpleCategories($this->context->language->id); $criterion_categories = $criterion->getCategories(); } else if ($criterion->id_product_comment_criterion_type == 3) { $criterion_products = $criterion->getProducts(); $products = Product::getSimpleProducts($this->context->language->id); } } foreach ($criterions as $key => $foo) if ($foo['id_product_comment_criterion_type'] == 1) unset($criterions[$key]); if (count($criterions)) { $this->_html .= '
'.$this->l('Manage criterions scope').'

'.$this->l('Only criterions restricted to categories or products can be configured below:').'

">
'; if ($id_criterion && $criterion->id_product_comment_criterion_type != 1) { $this->_html .='

'; if ($criterion->id_product_comment_criterion_type == 3) foreach ($products as $product) $this->_html .=''; else if ($criterion->id_product_comment_criterion_type == 2) foreach ($categories as $category) $this->_html .=''; $this->_html .='
'.$this->l('ID').' '.($criterion->id_product_comment_criterion_type == 3 ? $this->l('Product Name') : $this->l('Category Name')).'
'.(int)$product['id_product'].''.$product['name'].'
'.(int)$category['id_category'].''.$category['name'].'
'; } $this->_html .= '
'; } } private function _displayFormDelete() { $this->_html .= '
'.$this->l('Manage Comments').''; require_once(dirname(__FILE__).'/ProductComment.php'); $comments = ProductComment::getAll(); if (count($comments)) { $this->_html .= '

'; foreach ($comments as $comment) $this->_html .= ''; $this->_html .= '
'.$this->l('Author').' '.$this->l('Comment').' '.$this->l('Actions').'
'.htmlspecialchars($comment['customer_name'], ENT_COMPAT, 'UTF-8').'. '.htmlspecialchars($comment['content'], ENT_COMPAT, 'UTF-8').' '.$this->l('Delete').'
'.$this->l('Selection:').' '.$this->l('Delete').'
'; } else $this->_html .= $this->l('No comments to manage at this time.'); $this->_html .= '

'; } private function _checkDeleteComment() { $action = Tools::getValue('delete_action'); if (empty($action) === false) { $product_comments = Tools::getValue('delete_id_product_comment'); if (count($product_comments)) { require_once(dirname(__FILE__).'/ProductComment.php'); if ($action == 'delete') { foreach ($product_comments as $id_product_comment) { if (!$id_product_comment) continue; $comment = new ProductComment((int)$id_product_comment); $comment->delete(); ProductComment::deleteGrades((int)$id_product_comment); } } } } } public function hookProductTab($params) { require_once(dirname(__FILE__).'/ProductComment.php'); require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $this->context->smarty->assign(array( 'allow_guests' => (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS'), 'comments' => ProductComment::getByProduct((int)(Tools::getValue('id_product'))), 'criterions' => ProductCommentCriterion::getByProduct((int)(Tools::getValue('id_product')), $this->context->language->id), 'nbComments' => (int)(ProductComment::getCommentNumber((int)(Tools::getValue('id_product')))) )); return ($this->display(__FILE__, '/tab.tpl')); } public function hookproductOutOfStock($params) { require_once(dirname(__FILE__).'/ProductComment.php'); require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $id_guest = (!$id_customer = (int)$this->context->cookie->id_customer) ? (int)$this->context->cookie->id_guest : false; $customerComment = ProductComment::getByCustomer((int)(Tools::getValue('id_product')), (int)$this->context->cookie->id_customer, true, (int)$id_guest); $average = ProductComment::getAverageGrade((int)Tools::getValue('id_product')); $image = Product::getCover((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'id_product_comment_form' => (int)Tools::getValue('id_product'), 'product' => new Product((int)Tools::getValue('id_product'), false, $this->context->language->id), 'secure_key' => $this->secure_key, 'logged' => (int)$this->context->customer->isLogged(true), 'allow_guests' => (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS'), 'productcomment_cover' => (int)Tools::getValue('id_product').'-'.(int)$image['id_image'], 'mediumSize' => Image::getSize('medium'), 'criterions' => ProductCommentCriterion::getByProduct((int)Tools::getValue('id_product'), $this->context->language->id), 'action_url' => '', 'averageTotal' => (int)$average['grade'], 'too_early' => ($customerComment && (strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) > time()), 'nbComments' => (int)(ProductComment::getCommentNumber((int)Tools::getValue('id_product'))) )); return ($this->display(__FILE__, '/productcomments-extra.tpl')); } public function hookProductTabContent($params) { $this->context->controller->addJS($this->_path.'js/jquery.rating.pack.js'); $this->context->controller->addJS($this->_path.'js/jquery.textareaCounter.plugin.js'); $this->context->controller->addJS($this->_path.'js/productcomments.js'); $id_guest = (!$id_customer = (int)$this->context->cookie->id_customer) ? (int)$this->context->cookie->id_guest : false; $customerComment = ProductComment::getByCustomer((int)(Tools::getValue('id_product')), (int)$this->context->cookie->id_customer, true, (int)$id_guest); $averages = ProductComment::getAveragesByProduct((int)Tools::getValue('id_product'), $this->context->language->id); $averageTotal = 0; foreach ($averages as $average) $averageTotal += (float)($average); $averageTotal = count($averages) ? ($averageTotal / count($averages)) : 0; $image = Product::getCover((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'logged' => (int)$this->context->customer->isLogged(true), 'action_url' => '', 'comments' => ProductComment::getByProduct((int)Tools::getValue('id_product'), 1, null, $this->context->cookie->id_customer), 'criterions' => ProductCommentCriterion::getByProduct((int)Tools::getValue('id_product'), $this->context->language->id), 'averages' => $averages, 'product_comment_path' => $this->_path, 'averageTotal' => $averageTotal, 'allow_guests' => (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS'), 'too_early' => ($customerComment && (strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) > time()), 'delay' => Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME'), 'id_product_comment_form' => (int)Tools::getValue('id_product'), 'secure_key' => $this->secure_key, 'productcomment_cover' => (int)Tools::getValue('id_product').'-'.(int)$image['id_image'], 'mediumSize' => Image::getSize('medium'), 'nbComments' => (int)ProductComment::getCommentNumber((int)Tools::getValue('id_product')), 'productcomments_controller_url' => $this->context->link->getModuleLink('productcomments'), 'productcomments_url_rewriting_activated' => Configuration::get('PS_REWRITING_SETTINGS', 0) )); $this->context->controller->pagination((int)ProductComment::getCommentNumber((int)Tools::getValue('id_product'))); return ($this->display(__FILE__, '/productcomments.tpl')); } public function hookHeader() { $this->context->controller->addCSS($this->_path.'productcomments.css', 'all'); } public function hookExtraProductComparison($params) { require_once(dirname(__FILE__).'/ProductComment.php'); require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); $list_grades = array(); $list_product_grades = array(); $list_product_average = array(); $list_product_comment = array(); foreach ($params['list_ids_product'] as $id_product) { $grades = ProductComment::getAveragesByProduct($id_product, $this->context->language->id); $criterions = ProductCommentCriterion::getByProduct($id_product, $this->context->language->id); $grade_total = 0; if (count($grades) > 0) { foreach ($criterions as $criterion) { if (isset($grades[$criterion['id_product_comment_criterion']])) { $list_product_grades[$criterion['id_product_comment_criterion']][$id_product] = $grades[$criterion['id_product_comment_criterion']]; $grade_total += (float)($grades[$criterion['id_product_comment_criterion']]); } else $list_product_grades[$criterion['id_product_comment_criterion']][$id_product] = 0; if (!array_key_exists($criterion['id_product_comment_criterion'], $list_grades)) $list_grades[$criterion['id_product_comment_criterion']] = $criterion['name']; } $list_product_average[$id_product] = $grade_total / count($criterions); $list_product_comment[$id_product] = ProductComment::getByProduct($id_product, 0, 3); } } if (count($list_grades) < 1) return false; $this->context->smarty->assign(array('grades' => $list_grades, 'product_grades' => $list_product_grades, 'list_ids_product' => $params['list_ids_product'], 'list_product_average' => $list_product_average, 'product_comments' => $list_product_comment)); return $this->display(__FILE__, '/products-comparison.tpl'); } }