From 9cf09cbc7ce2cebf7bae013cb7f7ff932dafac07 Mon Sep 17 00:00:00 2001 From: gBrunier Date: Thu, 11 Aug 2011 09:48:49 +0000 Subject: [PATCH] [*] MO : Review of the productcomments module - Part2 git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@8016 b9a71923-0436-4b27-9f14-aed3839534dd --- modules/productcomments/ProductComment.php | 107 +++++++++++++++++- modules/productcomments/fr.php | 8 ++ modules/productcomments/install.sql | 13 +++ .../productcomments/productcomments-ajax.php | 47 ++++++++ modules/productcomments/productcomments.css | 9 +- modules/productcomments/productcomments.php | 56 ++++++++- modules/productcomments/productcomments.tpl | 69 +++++++++++ 7 files changed, 299 insertions(+), 10 deletions(-) create mode 100644 modules/productcomments/productcomments-ajax.php diff --git a/modules/productcomments/ProductComment.php b/modules/productcomments/ProductComment.php index 6a6987bcc..f55c85810 100644 --- a/modules/productcomments/ProductComment.php +++ b/modules/productcomments/ProductComment.php @@ -18,7 +18,8 @@ * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * -* @author PrestaShop SA +* @author Presta + SA * @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7040 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) @@ -91,7 +92,7 @@ class ProductComment extends ObjectModel * * @return array Comments */ - public static function getByProduct($id_product, $p = 1, $n = null) + public static function getByProduct($id_product, $p = 1, $n = null, $id_customer = null) { if (!Validate::isUnsignedId($id_product)) die(Tools::displayError()); @@ -102,8 +103,14 @@ class ProductComment extends ObjectModel $p = 1; if ($n != null AND $n <= 0) $n = 5; + return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT pc.`id_product_comment`, IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title + SELECT pc.`id_product_comment`, + (SELECT count(*) FROM `ps_product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful, + (SELECT count(*) FROM `ps_product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '. + ($id_customer ? '(SELECT count(*) FROM `ps_product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : ''). + ($id_customer ? '(SELECT count(*) FROM `ps_product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').' + IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title FROM `'._DB_PREFIX_.'product_comment` pc LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer` WHERE pc.`id_product` = '.(int)($id_product).($validate == '1' ? ' AND pc.`validate` = 1' : '').' @@ -111,6 +118,11 @@ class ProductComment extends ObjectModel '.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : '')); } + /** + * Return customer's comment + * + * @return arrayComments + */ public static function getByCustomer($id_product, $id_customer, $last = false, $id_guest = false) { $results = Db::getInstance()->ExecuteS(' @@ -221,9 +233,8 @@ class ProductComment extends ObjectModel SELECT pc.`id_product_comment`, pc.`id_product`, IF(c.id_customer, CONCAT(c.`firstname`, \' \', c.`lastname`), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pl.`name` FROM `'._DB_PREFIX_.'product_comment` pc LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = pc.`id_customer`) - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = pc.`id_product`) - WHERE pc.`validate` = '.(int)($validate).' - AND pl.`id_lang` = '.(int)Context::getContext()->language->id.' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = pc.`id_product` AND pl.`id_lang` = '.(int)Context::getContext()->language->id.Context::getContext()->shop->sqlLang('pl').') + WHERE pc.`validate` = '.(int)($validate).' ORDER BY pc.`date_add` DESC')); } @@ -255,4 +266,88 @@ class ProductComment extends ObjectModel DELETE FROM `'._DB_PREFIX_.'product_comment_grade` WHERE `id_product_comment` = '.(int)($id_product_comment))); } + + /** + * Delete Reports + * + * @return boolean succeed + */ + public static function deleteReports($id_product_comment) + { + if (!Validate::isUnsignedId($id_product_comment)) + die(Tools::displayError()); + return (Db::getInstance()->Execute(' + DELETE FROM `'._DB_PREFIX_.'product_comment_report` + WHERE `id_product_comment` = '.(int)($id_product_comment))); + } + + /** + * Report comment + * + * @return boolean + */ + public static function reportComment($id_product_comment, $id_customer) + { + return (Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'product_comment_report` (`id_product_comment`, `id_customer`) + VALUES ('.(int)$id_product_comment.', '.$id_customer.')')); + } + + /** + * Comment already report + * + * @return boolean + */ + public static function isAlreadyReport($id_product_comment, $id_customer) + { + return (bool)Db::getInstance()->getValue(' + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'product_comment_report` + WHERE `id_customer` = '.(int)($id_customer).' + AND `id_product_comment` = '.(int)($id_product_comment)); + } + + /** + * Set comment usefulness + * + * @return boolean + */ + public static function setCommentUsefulness($id_product_comment, $usefulness, $id_customer) + { + return (Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'product_comment_usefulness` (`id_product_comment`, `usefulness`, `id_customer`) + VALUES ('.(int)$id_product_comment.', '.(int)$usefulness.', '.(int)$id_customer.')')); + } + + /** + * Usefulness already set + * + * @return boolean + */ + public static function isAlreadyUsefulness($id_product_comment, $id_customer) + { + return (bool)Db::getInstance()->getValue(' + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'product_comment_usefulness` + WHERE `id_customer` = '.(int)($id_customer).' + AND `id_product_comment` = '.(int)($id_product_comment)); + } + + /** + * Get reported comments + * + * @return array Comments + */ + public static function getReportedComments() + { + return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' + SELECT DISTINCT(pc.`id_product_comment`), pc.`id_product`, IF(c.id_customer, CONCAT(c.`firstname`, \' \', c.`lastname`), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pl.`name` + FROM `'._DB_PREFIX_.'product_comment_report` pcr + LEFT JOIN `'._DB_PREFIX_.'product_comment` pc + ON pcr.id_product_comment = pc.id_product_comment + LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = pc.`id_customer`) + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = pc.`id_product` AND pl.`id_lang` = '.(int)Context::getContext()->language->id.' AND pl.`id_lang` = '.(int)Context::getContext()->language->id.Context::getContext()->shop->sqlLang('pl').') + ORDER BY pc.`date_add` DESC'); + } + }; diff --git a/modules/productcomments/fr.php b/modules/productcomments/fr.php index 3be648027..70145adf7 100644 --- a/modules/productcomments/fr.php +++ b/modules/productcomments/fr.php @@ -36,6 +36,8 @@ $_MODULE['<{productcomments}prestashop>productcomments_c4408d335012a56ff58937d78 $_MODULE['<{productcomments}prestashop>productcomments_f2a6c498fb90ee345d997f888fce3b18'] = 'Supprimer'; $_MODULE['<{productcomments}prestashop>productcomments_8bf0c707232d63bf83b4b2467d2df41a'] = 'Sélection :'; $_MODULE['<{productcomments}prestashop>productcomments_d00b7f656273a495f555bead0248d6f5'] = 'Aucun commentaire à valider.'; +$_MODULE['<{productcomments}prestashop>productcomments_82bd3dd5eb07bcc37a9649cbb1a5fd33'] = 'Commentaires signalés'; +$_MODULE['<{productcomments}prestashop>productcomments_66c4dd257983350f8cc2f8b71ade47ba'] = 'Aucun commentaire signalé'; $_MODULE['<{productcomments}prestashop>productcomments_7799b301b44c329fc9ec6a3a9c1905e0'] = 'Ajouter un nouveau critère'; $_MODULE['<{productcomments}prestashop>productcomments_7753ab38f8e113e8af65ab1241331625'] = 'Vous pouvez définir plusieurs critères afin de guider vos clients dans leur commentaire. Par exemple: performance, design etc ...'; $_MODULE['<{productcomments}prestashop>productcomments_bc58d00e1e42de31a8e58f8dc7d9bdc7'] = 'Vous pouvez ajouter un nouveau critère ci-dessous :'; @@ -65,6 +67,12 @@ $_MODULE['<{productcomments}prestashop>productcomments_6bf852d9850445291f5e9d474 $_MODULE['<{productcomments}prestashop>productcomments_6d28f2900adb9e500868166f6d04da92'] = 'Vous devez attendre'; $_MODULE['<{productcomments}prestashop>productcomments_ba8d7ae5dcadfba739f28a777378f208'] = 'secondes avant de poster un nouveau commentaire.'; $_MODULE['<{productcomments}prestashop>productcomments_7c3b0e9898b88deee7ea75aafd2e37e2'] = 'Note moyenne'; +$_MODULE['<{productcomments}prestashop>productcomments_663fc7093256cda156e571631ddb295e'] = 'personne(s) sur'; +$_MODULE['<{productcomments}prestashop>productcomments_e759c67f646fedbee26e1e6732588a98'] = 'ont trouvé ce commentaire utile'; +$_MODULE['<{productcomments}prestashop>productcomments_39630ad6ee79b8653ea89194cdb45bec'] = 'Ce commentaire vous a-t-il été utile?'; +$_MODULE['<{productcomments}prestashop>productcomments_a6105c0a611b41b08f1209506350279e'] = 'oui'; +$_MODULE['<{productcomments}prestashop>productcomments_7fa3b767c460b54a2be4d49030b349c7'] = 'non'; +$_MODULE['<{productcomments}prestashop>productcomments_28b3b1e564a00f572c5d4e21da986d49'] = 'Reporter un abus'; $_MODULE['<{productcomments}prestashop>productcomments_08621d00a3a801b9159a11b8bbd69f89'] = 'Aucun commentaire n\'a été publié pour le moment.'; $_MODULE['<{productcomments}prestashop>products-comparison_8413c683b4b27cc3f4dbd4c90329d8ba'] = 'Commentaires'; $_MODULE['<{productcomments}prestashop>products-comparison_b1897515d548a960afe49ecf66a29021'] = 'Moyenne'; diff --git a/modules/productcomments/install.sql b/modules/productcomments/install.sql index d03361cd1..10a15e4de 100644 --- a/modules/productcomments/install.sql +++ b/modules/productcomments/install.sql @@ -50,4 +50,17 @@ CREATE TABLE IF NOT EXISTS `PREFIX_product_comment_grade` ( `grade` int(10) unsigned NOT NULL, PRIMARY KEY (`id_product_comment`, `id_product_comment_criterion`), KEY `id_product_comment_criterion` (`id_product_comment_criterion`) +) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS `PREFIX_product_comment_usefulness` ( + `id_product_comment` int(10) unsigned NOT NULL, + `id_customer` int(10) unsigned NOT NULL, + `usefulness` tinyint(1) unsigned NOT NULL, + PRIMARY KEY (`id_product_comment`, `id_customer`) +) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS `PREFIX_product_comment_report` ( + `id_product_comment` int(10) unsigned NOT NULL, + `id_customer` int(10) unsigned NOT NULL, + PRIMARY KEY (`id_product_comment`, `id_customer`) ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/modules/productcomments/productcomments-ajax.php b/modules/productcomments/productcomments-ajax.php new file mode 100644 index 000000000..5e39a647b --- /dev/null +++ b/modules/productcomments/productcomments-ajax.php @@ -0,0 +1,47 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +require_once(dirname(__FILE__).'/../../config/config.inc.php'); +require_once(dirname(__FILE__).'/../../init.php'); +include(dirname(__FILE__).'/ProductComment.php'); + +if (Tools::getValue('action') AND Tools::getValue('id_product_comment') AND Context::getContext()->cookie->id_customer) +{ + if (Tools::getValue('action') == 'report') + { + if (!ProductComment::isAlreadyReport(Tools::getValue('id_product_comment'), Context::getContext()->cookie->id_customer) AND ProductComment::reportComment((int)Tools::getValue('id_product_comment'), (int)Context::getContext()->cookie->id_customer)) + die('0'); + } + elseif (Tools::getValue('action') == 'usefulness' AND Tools::getValue('value') AND Tools::getValue('value')) + { + if (!ProductComment::isAlreadyUsefulness(Tools::getValue('id_product_comment'), Context::getContext()->cookie->id_customer) AND ProductComment::setCommentUsefulness((int)Tools::getValue('id_product_comment'), (bool)((int)Tools::getValue('value')), Context::getContext()->cookie->id_customer)) + die('0'); + } +} + +die('1'); + diff --git a/modules/productcomments/productcomments.css b/modules/productcomments/productcomments.css index fe6b8b848..a9d7cd8d1 100644 --- a/modules/productcomments/productcomments.css +++ b/modules/productcomments/productcomments.css @@ -114,11 +114,16 @@ background-position: 0 -16px } -#product_comments_block_tab button { +#product_comments_block_tab button.usefulness_btn { background-color: #bbb; border: solid 1px #999; padding: 1px; cursor: pointer; color: #fff; - border-radius: 3px + margin: 3px +} + +#product_comments_block_tab span.report_btn { + cursor: pointer; + text-decoration: underline } \ No newline at end of file diff --git a/modules/productcomments/productcomments.php b/modules/productcomments/productcomments.php index bfeacb61a..21d2e4b97 100644 --- a/modules/productcomments/productcomments.php +++ b/modules/productcomments/productcomments.php @@ -141,6 +141,7 @@ class ProductComments extends Module continue; $comment = new ProductComment((int)$id_product_comment); $comment->validate(); + ProductComment::deleteReports((int)$id_product_comment); } break; case 'delete': @@ -151,6 +152,7 @@ class ProductComments extends Module $comment = new ProductComment((int)$id_product_comment); $comment->delete(); ProductComment::deleteGrades((int)$id_product_comment); + ProductComment::deleteReports((int)$id_product_comment); } break; default: @@ -159,6 +161,7 @@ class ProductComments extends Module } } } + private function _checkCriterion() { $action_criterion = Tools::getValue('criterion_action'); @@ -241,6 +244,7 @@ class ProductComments extends Module private function _displayForm() { $this->_displayFormModerate(); + $this->_displayFormReported(); $this->_displayFormConfigurationCriterion(); $this->_displayFormApplicationCriterion(); return $this->_html; @@ -327,6 +331,54 @@ class ProductComments extends Module $this->_html .= '
'; } + private function _displayFormReported() + { + $this->_html .= '
+ '.$this->l('Reported Comments').''; + + require_once(dirname(__FILE__).'/ProductComment.php'); + $comments = ProductComment::getReportedComments(); + if (sizeof($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); @@ -595,11 +647,11 @@ class ProductComments extends Module foreach ($averages AS $average) $averageTotal += (float)($average); $averageTotal = count($averages) ? ($averageTotal / count($averages)) : 0; - + Context::getContext()->smarty->assign(array( 'logged' => (int)Context::getContext()->cookie->id_customer, 'action_url' => '', - 'comments' => ProductComment::getByProduct((int)Tools::getValue('id_product')), + 'comments' => ProductComment::getByProduct((int)Tools::getValue('id_product'), 1, null, Context::getContext()->cookie->id_customer), 'criterions' => ProductCommentCriterion::getByProduct((int)Tools::getValue('id_product'), Context::getContext()->language->id), 'averages' => $averages, 'product_comment_path' => $this->_path, diff --git a/modules/productcomments/productcomments.tpl b/modules/productcomments/productcomments.tpl index 862155536..fcac10223 100644 --- a/modules/productcomments/productcomments.tpl +++ b/modules/productcomments/productcomments.tpl @@ -23,6 +23,62 @@ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA *} +
@@ -48,6 +104,19 @@

{$comment.title}

{$comment.content|escape:'html':'UTF-8'|nl2br}

+
    + {if $comment.total_advice > 0} +
  • {$comment.total_useful} {l s='out of' mod='productcomments'} {$comment.total_advice} {l s='people found this review useful' mod='productcomments'}
  • + {/if} + {if $cookie->isLogged() == true} + {if !$comment.customer_advice} +
  • {l s='Was this comment useful to you?' mod='productcomments'}
  • + {/if} + {if !$comment.customer_report} +
  • {l s='Report abuse' mod='productcomments'}
  • + {/if} + {/if} +
{/if}