* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6900 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_CAN_LOAD_FILES_'))
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.1';
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Product Comments');
$this->description = $this->l('Allow users to post comment about a product.');
}
public function install()
{
if (!file_exists(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE))
return false;
elseif (!$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 OR $this->registerHook('productTab') == false
OR $this->registerHook('extraProductComparison') == false OR $this->registerHook('productTabContent') == false
OR $this->registerHook('header') == false OR !Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', 30)
OR !Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', 0)
OR !Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', 1))
return false;
return true;
}
function uninstall()
{
if (!parent::uninstall() OR !Configuration::deleteByName('PRODUCT_COMMENTS_MODERATE') OR !Configuration::deleteByName('PRODUCT_COMMENTS_ALLOW_GUESTS') OR !Configuration::deleteByName('PRODUCT_COMMENTS_MINIMAL_TIME'))
return false;
return true;
}
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->_checkCriterion();
$this->_updateApplicationCriterion();
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 (sizeof($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 _checkCriterion()
{
$action_criterion = Tools::getValue('criterion_action');
$name = Tools::getValue('criterion');
if (Tools::isSubmit('submitAddCriterion'))
{
global $cookie;
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.'