[-] MO : productComments fixed

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@8220 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
bMancone
2011-08-29 09:19:43 +00:00
parent 16516953fb
commit 53467eb7d3
7 changed files with 397 additions and 201 deletions
+21 -3
View File
@@ -108,8 +108,8 @@ class ProductComment extends ObjectModel
SELECT pc.`id_product_comment`, SELECT pc.`id_product_comment`,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful, (SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '. (SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '.
($id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : ''). ((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'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 `'._DB_PREFIX_.'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').' ((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'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 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 FROM `'._DB_PREFIX_.'product_comment` pc
LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer` LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer`
@@ -151,6 +151,7 @@ class ProductComment extends ObjectModel
die(Tools::displayError()); die(Tools::displayError());
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE'); $validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');
return (Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' return (Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT pc.`id_product_comment`, pcg.`grade`, pccl.`name`, pcc.`id_product_comment_criterion` SELECT pc.`id_product_comment`, pcg.`grade`, pccl.`name`, pcc.`id_product_comment_criterion`
FROM `'._DB_PREFIX_.'product_comment` pc FROM `'._DB_PREFIX_.'product_comment` pc
@@ -162,6 +163,23 @@ class ProductComment extends ObjectModel
($validate == '1' ? ' AND pc.`validate` = 1' : ''))); ($validate == '1' ? ' AND pc.`validate` = 1' : '')));
} }
public static function getAverageGrade($id_product)
{
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT SUM(pc.`grade`) /
(SELECT COUNT(pc.`grade`)
FROM `'._DB_PREFIX_.'product_comment` pc
WHERE pc.`id_product` = '.(int)($id_product).'
AND pc.`deleted` = 0'.
($validate == '1' ? ' AND pc.`validate` = 1' : '').') AS grade
FROM `'._DB_PREFIX_.'product_comment` pc
WHERE pc.`id_product` = '.(int)($id_product).'
AND pc.`deleted` = 0'.
($validate == '1' ? ' AND pc.`validate` = 1' : ''));
}
public static function getAveragesByProduct($id_product, $id_lang) public static function getAveragesByProduct($id_product, $id_lang)
{ {
/* Get all grades */ /* Get all grades */
@@ -305,7 +323,7 @@ class ProductComment extends ObjectModel
{ {
return (Db::getInstance()->Execute(' return (Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'product_comment_report` (`id_product_comment`, `id_customer`) INSERT INTO `'._DB_PREFIX_.'product_comment_report` (`id_product_comment`, `id_customer`)
VALUES ('.(int)$id_product_comment.', '.$id_customer.')')); VALUES ('.(int)$id_product_comment.', '.(int)$id_customer.')'));
} }
/** /**
+2 -2
View File
@@ -2,8 +2,8 @@
<module> <module>
<name>productcomments</name> <name>productcomments</name>
<displayName><![CDATA[Product Comments]]></displayName> <displayName><![CDATA[Product Comments]]></displayName>
<version><![CDATA[2.1]]></version> <version><![CDATA[2.2]]></version>
<description><![CDATA[Allow users to post comment about a product.]]></description> <description><![CDATA[Allows users to post reviews.]]></description>
<author><![CDATA[PrestaShop]]></author> <author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[front_office_features]]></tab> <tab><![CDATA[front_office_features]]></tab>
<is_configurable>1</is_configurable> <is_configurable>1</is_configurable>
+11 -11
View File
@@ -4,16 +4,16 @@ global $_MODULE;
$_MODULE = array(); $_MODULE = array();
$_MODULE['<{productcomments}prestashop>productcomments-extra_7c3b0e9898b88deee7ea75aafd2e37e2'] = 'Note moyenne'; $_MODULE['<{productcomments}prestashop>productcomments-extra_7c3b0e9898b88deee7ea75aafd2e37e2'] = 'Note moyenne';
$_MODULE['<{productcomments}prestashop>productcomments-extra_a71a0229e164fecdcde3c4e0f40473fa'] = 'Lire les avis utilisateurs'; $_MODULE['<{productcomments}prestashop>productcomments-extra_a71a0229e164fecdcde3c4e0f40473fa'] = 'Lire les avis utilisateurs';
$_MODULE['<{productcomments}prestashop>productcomments-extra_4ce7fa08d440a1e0fb369b1273e52ac3'] = 'Donner votre avis'; $_MODULE['<{productcomments}prestashop>productcomments-extra_7966126831926ad29c528b239d69f855'] = 'Donner son avis';
$_MODULE['<{productcomments}prestashop>productcomments-extra_b78a3223503896721cca1303f776159b'] = 'Titre'; $_MODULE['<{productcomments}prestashop>productcomments-extra_b78a3223503896721cca1303f776159b'] = 'Titre';
$_MODULE['<{productcomments}prestashop>productcomments-extra_0be8406951cdfda82f00f79328cf4efc'] = 'Commentaire'; $_MODULE['<{productcomments}prestashop>productcomments-extra_0be8406951cdfda82f00f79328cf4efc'] = 'Commentaire';
$_MODULE['<{productcomments}prestashop>productcomments-extra_a2ed44743411cf8b80e397448fce104c'] = 'Votre nom'; $_MODULE['<{productcomments}prestashop>productcomments-extra_a2ed44743411cf8b80e397448fce104c'] = 'Votre nom';
$_MODULE['<{productcomments}prestashop>productcomments-extra_e699583427bd276f34244d23e6072a41'] = 'Tous les champs sont obligatoires'; $_MODULE['<{productcomments}prestashop>productcomments-extra_70397c4b252a5168c5ec003931cea215'] = 'Champs requis';
$_MODULE['<{productcomments}prestashop>productcomments-extra_94966d90747b97d1f0f206c98a8b1ac3'] = 'Envoyer'; $_MODULE['<{productcomments}prestashop>productcomments-extra_94966d90747b97d1f0f206c98a8b1ac3'] = 'Envoyer';
$_MODULE['<{productcomments}prestashop>productcomments-extra_e81c4e4f2b7b93b481e13a8553c2ae1b'] = 'ou'; $_MODULE['<{productcomments}prestashop>productcomments-extra_e81c4e4f2b7b93b481e13a8553c2ae1b'] = 'ou';
$_MODULE['<{productcomments}prestashop>productcomments-extra_ea4788705e6873b424c65e91c2846b19'] = 'Annuler'; $_MODULE['<{productcomments}prestashop>productcomments-extra_ea4788705e6873b424c65e91c2846b19'] = 'Annuler';
$_MODULE['<{productcomments}prestashop>productcomments_b91c4e8b229a399a3bc911d352524a9b'] = 'Commentaires produits'; $_MODULE['<{productcomments}prestashop>productcomments_b91c4e8b229a399a3bc911d352524a9b'] = 'Commentaires produits';
$_MODULE['<{productcomments}prestashop>productcomments_0c21532dfd3476791e5aab1aa7fa7405'] = 'Autorise les utilisateurs à poster des commentaires sur les produits'; $_MODULE['<{productcomments}prestashop>productcomments_9918811c511f3481c085e46c07ab5da8'] = 'Permet aux client de commenter les produits';
$_MODULE['<{productcomments}prestashop>productcomments_f4d1ea475eaa85102e2b4e6d95da84bd'] = 'Confirmation'; $_MODULE['<{productcomments}prestashop>productcomments_f4d1ea475eaa85102e2b4e6d95da84bd'] = 'Confirmation';
$_MODULE['<{productcomments}prestashop>productcomments_c888438d14855d7d96a2724ee9c306bd'] = 'Configuration mise à jour'; $_MODULE['<{productcomments}prestashop>productcomments_c888438d14855d7d96a2724ee9c306bd'] = 'Configuration mise à jour';
$_MODULE['<{productcomments}prestashop>productcomments_1bb54e382f7dbdb260f0aa6b42bb624b'] = 'Critère supprimé'; $_MODULE['<{productcomments}prestashop>productcomments_1bb54e382f7dbdb260f0aa6b42bb624b'] = 'Critère supprimé';
@@ -59,14 +59,6 @@ $_MODULE['<{productcomments}prestashop>productcomments_af1b98adf7f686b84cd0b443e
$_MODULE['<{productcomments}prestashop>productcomments_b718adec73e04ce3ec720dd11a06a308'] = 'ID'; $_MODULE['<{productcomments}prestashop>productcomments_b718adec73e04ce3ec720dd11a06a308'] = 'ID';
$_MODULE['<{productcomments}prestashop>productcomments_b9aefa40a54680bb258f9f9569290fae'] = 'Nom du produit'; $_MODULE['<{productcomments}prestashop>productcomments_b9aefa40a54680bb258f9f9569290fae'] = 'Nom du produit';
$_MODULE['<{productcomments}prestashop>productcomments_99121ab27aaa7472cfada9071c5ba434'] = 'Nom de la catégorie'; $_MODULE['<{productcomments}prestashop>productcomments_99121ab27aaa7472cfada9071c5ba434'] = 'Nom de la catégorie';
$_MODULE['<{productcomments}prestashop>productcomments_fd4b5401d4d3c7d32d158bfc1e552f3b'] = 'Merci de saisir votre nom';
$_MODULE['<{productcomments}prestashop>productcomments_7b2f2ea0f690ef3c2fc9bba0e4bfbc4c'] = 'Commentaire invalide.';
$_MODULE['<{productcomments}prestashop>productcomments_f88dc17737f7fdd4464b2eb922a8f133'] = 'Une erreur est survenue lors de l\'ajout du commentaire.';
$_MODULE['<{productcomments}prestashop>productcomments_7fa4a3510dafd0eac6435c19861b2bb7'] = 'Commentaire ajouté avec succès.';
$_MODULE['<{productcomments}prestashop>productcomments_f8694a9aae2eb045920f613cfa7f1235'] = 'En attente d\'une validation modérateur';
$_MODULE['<{productcomments}prestashop>productcomments_6bf852d9850445291f5e9d4740ac7b50'] = 'Un texte est nécessaire pour publier un commentaire.';
$_MODULE['<{productcomments}prestashop>productcomments_6d28f2900adb9e500868166f6d04da92'] = 'Vous devez attendre';
$_MODULE['<{productcomments}prestashop>productcomments_ba8d7ae5dcadfba739f28a777378f208'] = 'secondes avant de poster un nouveau commentaire.';
$_MODULE['<{productcomments}prestashop>productcomments_4494d00c901c9e22ff3b953177205cea'] = 'Etes vous sur de vouloir signaler ce commentaire?'; $_MODULE['<{productcomments}prestashop>productcomments_4494d00c901c9e22ff3b953177205cea'] = 'Etes vous sur de vouloir signaler ce commentaire?';
$_MODULE['<{productcomments}prestashop>productcomments_4b3b9db8c9784468094acde0f8bf7071'] = 'Note'; $_MODULE['<{productcomments}prestashop>productcomments_4b3b9db8c9784468094acde0f8bf7071'] = 'Note';
$_MODULE['<{productcomments}prestashop>productcomments_663fc7093256cda156e571631ddb295e'] = 'personne(s) sur'; $_MODULE['<{productcomments}prestashop>productcomments_663fc7093256cda156e571631ddb295e'] = 'personne(s) sur';
@@ -75,6 +67,14 @@ $_MODULE['<{productcomments}prestashop>productcomments_39630ad6ee79b8653ea89194c
$_MODULE['<{productcomments}prestashop>productcomments_a6105c0a611b41b08f1209506350279e'] = 'oui'; $_MODULE['<{productcomments}prestashop>productcomments_a6105c0a611b41b08f1209506350279e'] = 'oui';
$_MODULE['<{productcomments}prestashop>productcomments_7fa3b767c460b54a2be4d49030b349c7'] = 'non'; $_MODULE['<{productcomments}prestashop>productcomments_7fa3b767c460b54a2be4d49030b349c7'] = 'non';
$_MODULE['<{productcomments}prestashop>productcomments_28b3b1e564a00f572c5d4e21da986d49'] = 'Reporter un abus'; $_MODULE['<{productcomments}prestashop>productcomments_28b3b1e564a00f572c5d4e21da986d49'] = 'Reporter un abus';
$_MODULE['<{productcomments}prestashop>productcomments_7966126831926ad29c528b239d69f855'] = 'Donner votre avos';
$_MODULE['<{productcomments}prestashop>productcomments_b78a3223503896721cca1303f776159b'] = 'Titre';
$_MODULE['<{productcomments}prestashop>productcomments_a2ed44743411cf8b80e397448fce104c'] = 'Votre nom:';
$_MODULE['<{productcomments}prestashop>productcomments_70397c4b252a5168c5ec003931cea215'] = 'Champs requis';
$_MODULE['<{productcomments}prestashop>productcomments_94966d90747b97d1f0f206c98a8b1ac3'] = 'Envoyer';
$_MODULE['<{productcomments}prestashop>productcomments_e81c4e4f2b7b93b481e13a8553c2ae1b'] = 'ou';
$_MODULE['<{productcomments}prestashop>productcomments_ea4788705e6873b424c65e91c2846b19'] = 'Annuler';
$_MODULE['<{productcomments}prestashop>productcomments_fbe2625bf3673be380d043a4bf873f28'] = 'Soyez premier à donner votre avis';
$_MODULE['<{productcomments}prestashop>productcomments_08621d00a3a801b9159a11b8bbd69f89'] = 'Aucun commentaire n\'a été publié pour le moment.'; $_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_8413c683b4b27cc3f4dbd4c90329d8ba'] = 'Commentaires';
$_MODULE['<{productcomments}prestashop>products-comparison_b1897515d548a960afe49ecf66a29021'] = 'Moyenne'; $_MODULE['<{productcomments}prestashop>products-comparison_b1897515d548a960afe49ecf66a29021'] = 'Moyenne';
@@ -27,7 +27,11 @@
require_once(dirname(__FILE__).'/../../config/config.inc.php'); require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php'); require_once(dirname(__FILE__).'/../../init.php');
include(dirname(__FILE__).'/ProductComment.php'); require_once(dirname(__FILE__).'/ProductCommentCriterion.php');
include_once(dirname(__FILE__).'/ProductComment.php');
include_once(dirname(__FILE__).'/productcomments.php');
$productCom = new productcomments();
if (Tools::getValue('action') AND Tools::getValue('id_product_comment') AND Context::getContext()->cookie->id_customer) if (Tools::getValue('action') AND Tools::getValue('id_product_comment') AND Context::getContext()->cookie->id_customer)
{ {
@@ -42,6 +46,85 @@ if (Tools::getValue('action') AND Tools::getValue('id_product_comment') AND Cont
die('0'); die('0');
} }
} }
elseif (Tools::getValue('action') AND Tools::getValue('secure_key') == $productCom->secure_key)
{
$review = Tools::jsonDecode(Tools::getValue('review'));
$id_product = 0;
$content = null;
$title = null;
$grades = array();
foreach ($review as $entry)
{
if ($entry->key == "id_product")
$id_product = $entry->value;
elseif ($entry->key == "title")
$title = $entry->value;
elseif ($entry->key == "content")
$content = $entry->value;
elseif (preg_match("/grade/", $entry->key))
{
$id = array(split("_", $entry->key));
$grades[] = array('id' => $id['0']['0'], 'grade' => $entry->value);
}
}
if ($title == "" OR $content == "" OR !$id_product OR count($grades) == 0)
die('0');
$allow_guests = (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS');
if (Context::getContext()->customer->id AND (!Context::getContext()->customer->is_guest OR $allow_guests))
{
$id_guest = (!$id_customer = (int)Context::getContext()->cookie->id_customer) ? (int)Context::getContext()->cookie->id_guest : false;
$customerComment = ProductComment::getByCustomer((int)($id_product), Context::getContext()->cookie->id_customer, true, (int)$id_guest);
if (!$customerComment OR ($customerComment AND (strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) < time()))
{
$errors = array();
$customer_name = false;
if ($id_guest AND (!$customer_name = Context::getContext()->customer->firstname . " " . Context::getContext()->customer->lastname))
$errors[] = $productCom->l('Please fill your name');
if (!sizeof($errors) AND $content)
{
$comment = new ProductComment();
$comment->content = strip_tags($content);
$comment->id_product = (int)$id_product;
$comment->id_customer = (int)Context::getContext()->cookie->id_customer;
$comment->id_guest = (int)$id_guest;
$comment->customer_name = pSQL($customer_name);
$comment->title = pSQL($title);
$comment->grade = 0;
$comment->validate = 0;
if (!$comment->content)
$errors[] = $productCom->l('Invalid comment text posted.');
else
{
$tgrade = 0;
$comment->save();
foreach ($grades as $grade)
{
$tgrade += $grade['grade'];
$productCommentCriterion = new ProductCommentCriterion((int)Tools::getValue('id_product_comment_criterion_'.$grade['id']));
if ($productCommentCriterion->id)
$productCommentCriterion->addGrade($comment->id, $grade['grade']);
}
if ((count($grades) - 1) >= 0)
$comment->grade = (int)($tgrade / ((int)count($grades)));
if (!$comment->save())
$errors[] = $productCom->l('An error occurred while saving your comment.');
else
Context::getContext()->smarty->assign('confirmation', $productCom->l('Comment posted.').((int)(Configuration::get('PRODUCT_COMMENTS_MODERATE')) ? ' '.$productCom->l('Awaiting moderator validation.') : ''));
}
}
else
$errors[] = $productCom->l('Comment text is required.');
}
else
$errors[] = $productCom->l('You should wait').' '.Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME').' '.$productCom->l('seconds before posting a new comment');
}
}
die('1'); die('1');
@@ -53,6 +53,27 @@
$('ul#more_info_tabs a[href^="#idTab"]').removeClass('selected'); $('ul#more_info_tabs a[href^="#idTab"]').removeClass('selected');
$('a[href="#idTab5"]').addClass('selected'); $('a[href="#idTab5"]').addClass('selected');
{literal}}{/literal}); {literal}}{/literal});
{literal}
$('#submitMessage').click(function(){
var datas = [];
$('#fancybox-content').find('input, textarea, select').each(function(index){
var o = {}
o.key = $(this).attr('name');
o.value = $(this).val();
datas.push(o);
});
console.log(datas);
$.ajax({
{/literal}url: "{$module_dir}productcomments-ajax.php",{literal}
post: "POST",
data: {action: 'sendComment', secure_key: '{/literal}{$secure_key}{literal}', review: JSON.stringify(datas)},{/literal}{literal}
dataType: "json",
success: function(result){
$.fancybox.close();
}
});
});
{/literal}
{literal}}{/literal}); {literal}}{/literal});
</script> </script>
@@ -72,14 +93,12 @@
<div class="comments_advices"> <div class="comments_advices">
<a href="#idTab5">{l s='Read user reviews' mod='productcomments'} ({$nbComments})</a><br/> <a href="#idTab5">{l s='Read user reviews' mod='productcomments'} ({$nbComments})</a><br/>
{if ($too_early == false AND ($logged OR $allow_guests))} {if ($too_early == false AND ($logged OR $allow_guests))}
<a id="new_comment_btn" href="#new_comment_form">{l s='Give your advice' mod='productcomments'}</a> <a id="new_comment_btn" href="#new_comment_form">{l s='Write your review' mod='productcomments'}</a>
{/if} {/if}
</div> </div>
<div style="display: none;"> <div style="display: none;">
<div id="new_comment_form"> <div id="new_comment_form">
<form action="{$action_url}" method="post" id="sendComment"> <h2 class="title">{l s='Write your review' mod='productcomments'}</h2>
<h2 class="title">{l s='Give your advice' mod='productcomments'}</h2>
<div class="product clearfix"> <div class="product clearfix">
<img src="{$link->getImageLink($product->link_rewrite, $productcomment_cover, 'home')}" height="{$homeSize.height}" width="{$homeSize.width}" alt="{$product->name|escape:html:'UTF-8'}" /> <img src="{$link->getImageLink($product->link_rewrite, $productcomment_cover, 'home')}" height="{$homeSize.height}" width="{$homeSize.width}" alt="{$product->name|escape:html:'UTF-8'}" />
<div class="product_desc"> <div class="product_desc">
@@ -89,7 +108,7 @@
</div> </div>
<div class="new_comment_form_content"> <div class="new_comment_form_content">
<p class="intro_form">{l s='Give your advice' mod='productcomments'}</p> <p class="intro_form">{l s='Write your review' mod='productcomments'}</p>
{if $criterions|@count > 0} {if $criterions|@count > 0}
<div class="grade_content clearfix"> <div class="grade_content clearfix">
{section loop=$criterions name=i start=0 step=1} {section loop=$criterions name=i start=0 step=1}
@@ -110,26 +129,27 @@
<div class="form_contenair"> <div class="form_contenair">
<p class="text"> <p class="text">
<label for="comment_title">{l s='Title' mod='productcomments'} <sup>*</sup>:</label> <label for="comment_title">{l s='Title' mod='productcomments'} <sup>*</sup>:</label>
<input name="title" type="text" value=""/> <input id="commentTitle" name="title" type="text" value=""/>
</p> </p>
<p class="textarea"> <p class="textarea">
<label for="content">{l s='Comment' mod='productcomments'} <sup>*</sup>:</label> <label for="content">{l s='Comment' mod='productcomments'} <sup>*</sup>:</label>
<textarea name="content"></textarea> <textarea id="commentContent" name="content"></textarea>
</p> </p>
{if $allow_guests == true && $logged == 0} {if $allow_guests == true && $logged == 0}
<p class="text"> <p class="text">
<label>{l s='Your name:' mod='productcomments'} <sup>*</sup>:</label> <label>{l s='Your name:' mod='productcomments'} <sup>*</sup>:</label>
<input name="customer_name" type="text" value=""/> <input id="commentCustomerName" name="customer_name" type="text" value=""/>
</p> </p>
{/if} {/if}
<p class="submit"> <p class="submit">
<span class="txt_required">* {l s='All ths fields are mandatory' mod='productcomments'}</span> <span class="txt_required">* {l s='Required fields' mod='productcomments'}</span>
<button name="submitMessage" type="submit">{l s='Send' mod='productcomments'}</button>&nbsp; <input id="id_product_comment_send" name="id_product" type="hidden" value='{$id_product_comment_form}'></input>
<button id="submitMessage" name="submitMessage" type="submit">{l s='Send' mod='productcomments'}</button>&nbsp;
{l s='or' mod='productcomments'}&nbsp;<a href="#" onclick="$.fancybox.close();">{l s='Cancel' mod='productcomments'}</a> {l s='or' mod='productcomments'}&nbsp;<a href="#" onclick="$.fancybox.close();">{l s='Cancel' mod='productcomments'}</a>
</p> </p>
</div> </div>
</div><!-- /end new_comment_form_content --> </div><!-- /end new_comment_form_content -->
</form>
</div> </div>
</div> </div>
</div> </div>
<!-- /Module ProductComments -->
+48 -71
View File
@@ -45,11 +45,12 @@ class ProductComments extends Module
$this->version = '2.2'; $this->version = '2.2';
$this->author = 'PrestaShop'; $this->author = 'PrestaShop';
$this->need_instance = 0; $this->need_instance = 0;
$this->secure_key = Tools::encrypt($this->name);
parent::__construct(); parent::__construct();
$this->displayName = $this->l('Product Comments'); $this->displayName = $this->l('Product Comments');
$this->description = $this->l('Allow users to post comment about a product.'); $this->description = $this->l('Allows users to post reviews.');
} }
public function install() public function install()
@@ -64,22 +65,42 @@ class ProductComments extends Module
foreach ($sql AS $query) foreach ($sql AS $query)
if (!Db::getInstance()->Execute(trim($query))) if (!Db::getInstance()->Execute(trim($query)))
return false; return false;
if (parent::install() == false OR $this->registerHook('productTab') == false if (parent::install() == false
OR $this->registerHook('extraProductComparison') == false OR $this->registerHook('productTabContent') == false OR !$this->registerHook('productTab')
OR $this->registerHook('header') == false OR $this->registerHook('productOutOfStock') == false OR !Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', 30) OR !$this->registerHook('extraProductComparison') OR !$this->registerHook('productTabContent')
OR !Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', 0) OR !$this->registerHook('header') OR !$this->registerHook('productOutOfStock')
OR !Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', 1)) OR !Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', 30)
return false; OR !Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', 0)
OR !Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', 1))
return false;
return true; return true;
} }
function uninstall() public 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')) if (!parent::uninstall() OR !$this->deleteTables()
return false; OR !Configuration::deleteByName('PRODUCT_COMMENTS_MODERATE')
OR !Configuration::deleteByName('PRODUCT_COMMENTS_ALLOW_GUESTS') OR !Configuration::deleteByName('PRODUCT_COMMENTS_MINIMAL_TIME')
OR !$this->unregisterHook('extraProductComparison') OR !$this->unregisterHook('productOutOfStock') OR !$this->unregisterHook('productTabContent')
OR !$this->unregisterHook('header') OR !$this->unregisterHook('productTab'))
return false;
return true; return true;
} }
public function deleteTables()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->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() protected function _postProcess()
{ {
if (Tools::isSubmit('submitModerate')) if (Tools::isSubmit('submitModerate'))
@@ -589,7 +610,7 @@ class ProductComments extends Module
return ($this->display(__FILE__, '/tab.tpl')); return ($this->display(__FILE__, '/tab.tpl'));
} }
public function hookProductOutOfStock($params) public function hookproductOutOfStock($params)
{ {
require_once(dirname(__FILE__).'/ProductComment.php'); require_once(dirname(__FILE__).'/ProductComment.php');
require_once(dirname(__FILE__).'/ProductCommentCriterion.php'); require_once(dirname(__FILE__).'/ProductCommentCriterion.php');
@@ -597,23 +618,20 @@ class ProductComments extends Module
$id_guest = (!$id_customer = (int)Context::getContext()->cookie->id_customer) ? (int)Context::getContext()->cookie->id_guest : false; $id_guest = (!$id_customer = (int)Context::getContext()->cookie->id_customer) ? (int)Context::getContext()->cookie->id_guest : false;
$customerComment = ProductComment::getByCustomer((int)(Tools::getValue('id_product')), (int)Context::getContext()->cookie->id_customer, true, (int)$id_guest); $customerComment = ProductComment::getByCustomer((int)(Tools::getValue('id_product')), (int)Context::getContext()->cookie->id_customer, true, (int)$id_guest);
$averages = ProductComment::getAveragesByProduct((int)Tools::getValue('id_product'), Context::getContext()->language->id); $average = ProductComment::getAverageGrade((int)Tools::getValue('id_product'));
$averageTotal = 0;
foreach ($averages AS $average)
$averageTotal += (float)($average);
$averageTotal = count($averages) ? ($averageTotal / count($averages)) : 0;
$image = Product::getCover((int)($_GET['id_product'])); $image = Product::getCover((int)($_GET['id_product']));
Context::getContext()->smarty->assign(array( Context::getContext()->smarty->assign(array(
'id_product_comment_form' => (int)Tools::getValue('id_product'),
'secure_key' => $this->secure_key,
'logged' => (int)Context::getContext()->customer->isLogged(true), 'logged' => (int)Context::getContext()->customer->isLogged(true),
'allow_guests' => (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS'), 'allow_guests' => (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS'),
'productcomment_cover' => (int)Tools::getValue('id_product').'-'.(int)$image['id_image'], 'productcomment_cover' => (int)Tools::getValue('id_product').'-'.(int)$image['id_image'],
'mediumSize' => Image::getSize('medium'), 'mediumSize' => Image::getSize('medium'),
'criterions' => ProductCommentCriterion::getByProduct((int)Tools::getValue('id_product'), Context::getContext()->language->id), 'criterions' => ProductCommentCriterion::getByProduct((int)Tools::getValue('id_product'), Context::getContext()->language->id),
'action_url' => '', 'action_url' => '',
'averageTotal' => (int)$averageTotal, 'averageTotal' => (int)$average['grade'],
'too_early' => ($customerComment AND (strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) > time()), 'too_early' => ($customerComment AND (strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) > time()),
'nbComments' => (int)(ProductComment::getCommentNumber((int)($_GET['id_product']))) 'nbComments' => (int)(ProductComment::getCommentNumber((int)($_GET['id_product'])))
)); ));
@@ -623,60 +641,7 @@ class ProductComments extends Module
private function _frontOfficePostProcess() private function _frontOfficePostProcess()
{ {
require_once(dirname(__FILE__).'/ProductComment.php');
require_once(dirname(__FILE__).'/ProductCommentCriterion.php');
$allow_guests = (int)Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS');
if (Tools::isSubmit('submitMessage') AND Context::getContext()->customer->id AND (!Context::getContext()->customer->is_guest OR $allow_guests))
{
$id_guest = (!$id_customer = (int)Context::getContext()->cookie->id_customer) ? (int)Context::getContext()->cookie->id_guest : false;
$customerComment = ProductComment::getByCustomer((int)(Tools::getValue('id_product')), Context::getContext()->cookie->id_customer, true, (int)$id_guest);
if (!$customerComment OR ($customerComment AND (strtotime($customerComment['date_add']) + Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) < time()))
{
$errors = array();
$customer_name = false;
if ($id_guest AND (!$customer_name = Tools::getValue('customer_name')))
$errors[] = $this->l('Please fill your name');
if (!sizeof($errors) AND Tools::getValue('content'))
{
$comment = new ProductComment();
$comment->content = strip_tags(Tools::getValue('content'));
$comment->id_product = (int)$_GET['id_product'];
$comment->id_customer = (int)Context::getContext()->cookie->id_customer;
$comment->id_guest = (int)$id_guest;
$comment->customer_name = pSQL($customer_name);
$comment->title = pSQL(Tools::getValue('title'));
$comment->grade = 0;
$comment->validate = 0;
if (!$comment->content)
$errors[] = $this->l('Invalid comment text posted.');
else
{
$comment->save();
for ($i = 1, $grade = 0; isset($_POST[$i.'_grade']) === true; ++$i)
{
$cgrade = (int)Tools::getValue($i.'_grade');
$grade += $cgrade;
$productCommentCriterion = new ProductCommentCriterion((int)Tools::getValue('id_product_comment_criterion_'.$i));
if ($productCommentCriterion->id)
$productCommentCriterion->addGrade($comment->id, $cgrade);
}
if (($i - 1) > 0)
$comment->grade = ($grade / ($i - 1));
if (!$comment->save())
$errors[] = $this->l('An error occurred while saving your comment.');
else
Context::getContext()->smarty->assign('confirmation', $this->l('Comment posted.').((int)(Configuration::get('PRODUCT_COMMENTS_MODERATE')) ? ' '.$this->l('Awaiting moderator validation.') : ''));
}
}
else
$errors[] = $this->l('Comment text is required.');
}
else
$errors[] = $this->l('You should wait').' '.Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME').' '.$this->l('seconds before posting a new comment');
}
} }
public function hookProductTabContent($params) public function hookProductTabContent($params)
@@ -704,6 +669,18 @@ class ProductComments extends Module
$this->context->controller->pagination((int)ProductComment::getCommentNumber((int)Tools::getValue('id_product'))); $this->context->controller->pagination((int)ProductComment::getCommentNumber((int)Tools::getValue('id_product')));
$image = Product::getCover((int)($_GET['id_product']));
Context::getContext()->smarty->assign(array(
'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)($_GET['id_product'])))
));
return ($this->display(__FILE__, '/productcomments.tpl')); return ($this->display(__FILE__, '/productcomments.tpl'));
} }
@@ -23,9 +23,25 @@
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*} *}
<script type="text/javascript" src="{$module_dir}js/jquery.rating.pack.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function(){literal}{{/literal} $('input[@type=radio].star').rating(); {literal}}{/literal});
$(function(){literal}{{/literal}
$('.auto-submit-star').rating({literal}{{/literal}
callback: function(value, link){literal}{{/literal}
{literal}}{/literal}
{literal}}{/literal});
{literal}}{/literal});
{literal} {literal}
$('document').ready(function(){ $('document').ready(function(){
$('#new_comment_tab_btn').fancybox({
'hideOnContentClick': false,
'onClosed': function(){
},
});
$('button[id^=comment_useful_yes_]').click(function(){ $('button[id^=comment_useful_yes_]').click(function(){
var idProductComment = $(this).attr('id').replace('comment_useful_yes_', ''); var idProductComment = $(this).attr('id').replace('comment_useful_yes_', '');
@@ -79,6 +95,25 @@
}); });
} }
}); });
$('#submitNewMessage').click(function(){
var datas = [];
$('#fancybox-content').find('input, textarea, select').each(function(index){
var o = {}
o.key = $(this).attr('name');
o.value = $(this).val();
datas.push(o);
});
console.log(datas);
$.ajax({
{/literal}url: "{$module_dir}productcomments-ajax.php",{literal}
post: "POST",
data: {action: 'sendComment', secure_key: '{/literal}{$secure_key}{literal}', review: JSON.stringify(datas)},
dataType: "json",
success: function(result){
$.fancybox.close();
}
});
});
}); });
{/literal} {/literal}
</script> </script>
@@ -126,7 +161,70 @@
{/if} {/if}
{/foreach} {/foreach}
{else} {else}
{if ($too_early == false AND ($logged OR $allow_guests))}
<div style="display: none;">
<div id="new_comment_form">
<h2 class="title">{l s='Write your review' mod='productcomments'}</h2>
<div class="product clearfix">
<img src="{$link->getImageLink($product->link_rewrite, $productcomment_cover, 'home')}" height="{$homeSize.height}" width="{$homeSize.width}" alt="{$product->name|escape:html:'UTF-8'}" />
<div class="product_desc">
<p class="product_name"><strong>{$product->name}</strong></p>
{$product->description_short}
</div>
</div>
<div class="new_comment_form_content">
<p class="intro_form">{l s='Write your review' mod='productcomments'}</p>
{if $criterions|@count > 0}
<div class="grade_content clearfix">
{section loop=$criterions name=i start=0 step=1}
<span>
<input type="hidden" name="id_product_comment_criterion_{$smarty.section.i.iteration}" value="{$criterions[i].id_product_comment_criterion|intval}" />
{$criterions[i].name|escape:'html':'UTF-8'}:&nbsp;
</span>
<div class="star_content">
<input class="star" type="radio" name="{$smarty.section.i.iteration}_grade" id="{$smarty.section.i.iteration}_grade" value="1" />
<input class="star" type="radio" name="{$smarty.section.i.iteration}_grade" value="2" />
<input class="star" type="radio" name="{$smarty.section.i.iteration}_grade" value="3" checked="checked" />
<input class="star" type="radio" name="{$smarty.section.i.iteration}_grade" value="4" />
<input class="star" type="radio" name="{$smarty.section.i.iteration}_grade" value="5" />
</div>
{/section}
</div>
{/if}
<div class="form_contenair">
<p class="text">
<label for="comment_title">{l s='Title' mod='productcomments'} <sup>*</sup>:</label>
<input id="commentTitle" name="title" type="text" value=""/>
</p>
<p class="textarea">
<label for="content">{l s='Comment' mod='productcomments'} <sup>*</sup>:</label>
<textarea id="commentContent" name="content"></textarea>
</p>
{if $allow_guests == true && $logged == 0}
<p class="text">
<label>{l s='Your name:' mod='productcomments'} <sup>*</sup>:</label>
<input id="commentCustomerName" name="customer_name" type="text" value=""/>
</p>
{/if}
<p class="submit">
<span class="txt_required">* {l s='Required fields' mod='productcomments'}</span>
<input id="id_product_comment_send" name="id_product" type="hidden" value='{$id_product_comment_form}'></input>
<button id="submitNewMessage" name="submitMessage" type="submit">{l s='Send' mod='productcomments'}</button>&nbsp;
{l s='or' mod='productcomments'}&nbsp;<a href="#" onclick="$.fancybox.close();">{l s='Cancel' mod='productcomments'}</a>
</p>
</div>
</div><!-- /end new_comment_form_content -->
</div>
</div>
<p class="align_center">
<a id="new_comment_tab_btn" href="#new_comment_form">{l s='Be the first to write your review' mod='productcomments'} !</a>
</p>
{else}
<p class="align_center">{l s='No customer comments for the moment.' mod='productcomments'}</p> <p class="align_center">{l s='No customer comments for the moment.' mod='productcomments'}</p>
{/if}
{/if} {/if}
</div> </div>
</div> </div>