PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/productcomments/controllers/front/default.php

https://gitlab.com/staging06/myproject
PHP | 171 lines | 121 code | 22 blank | 28 comment | 29 complexity | 1f7db3c3f6386ddd36c890c62f9042be MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. // Include Module
  27. include_once(dirname(__FILE__).'/../../productcomments.php');
  28. // Include Models
  29. include_once(dirname(__FILE__).'/../../ProductComment.php');
  30. include_once(dirname(__FILE__).'/../../ProductCommentCriterion.php');
  31. class ProductCommentsDefaultModuleFrontController extends ModuleFrontController
  32. {
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. $this->context = Context::getContext();
  37. }
  38. public function initContent()
  39. {
  40. parent::initContent();
  41. if (Tools::isSubmit('action'))
  42. {
  43. switch(Tools::getValue('action'))
  44. {
  45. case 'add_comment':
  46. $this->ajaxProcessAddComment();
  47. break;
  48. case 'report_abuse':
  49. $this->ajaxProcessReportAbuse();
  50. break;
  51. case 'comment_is_usefull':
  52. $this->ajaxProcessCommentIsUsefull();
  53. break;
  54. }
  55. }
  56. }
  57. protected function ajaxProcessAddComment()
  58. {
  59. $module_instance = new ProductComments();
  60. $result = true;
  61. $id_guest = 0;
  62. $id_customer = $this->context->customer->id;
  63. if (!$id_customer)
  64. $id_guest = $this->context->cookie->id_guest;
  65. $errors = array();
  66. // Validation
  67. if (!Validate::isInt(Tools::getValue('id_product')))
  68. $errors[] = $module_instance->l('Product ID is incorrect', 'default');
  69. if (!Tools::getValue('title') || !Validate::isGenericName(Tools::getValue('title')))
  70. $errors[] = $module_instance->l('Title is incorrect', 'default');
  71. if (!Tools::getValue('content') || !Validate::isMessage(Tools::getValue('content')))
  72. $errors[] = $module_instance->l('Comment is incorrect', 'default');
  73. if (!$id_customer && (!Tools::isSubmit('customer_name') || !Tools::getValue('customer_name') || !Validate::isGenericName(Tools::getValue('customer_name'))))
  74. $errors[] = $module_instance->l('Customer name is incorrect', 'default');
  75. if (!$this->context->customer->id && !Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS'))
  76. $errors[] = $module_instance->l('You must be connected in order to send a comment', 'default');
  77. if (!count(Tools::getValue('criterion')))
  78. $errors[] = $module_instance->l('You must give a rating', 'default');
  79. $product = new Product(Tools::getValue('id_product'));
  80. if (!$product->id)
  81. $errors[] = $module_instance->l('Product not found', 'default');
  82. if (!count($errors))
  83. {
  84. $customer_comment = ProductComment::getByCustomer(Tools::getValue('id_product'), $id_customer, true, $id_guest);
  85. if (!$customer_comment || ($customer_comment && (strtotime($customer_comment['date_add']) + (int)Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME')) < time()))
  86. {
  87. $comment = new ProductComment();
  88. $comment->content = strip_tags(Tools::getValue('content'));
  89. $comment->id_product = (int)Tools::getValue('id_product');
  90. $comment->id_customer = (int)$id_customer;
  91. $comment->id_guest = $id_guest;
  92. $comment->customer_name = Tools::getValue('customer_name');
  93. if (!$comment->customer_name)
  94. $comment->customer_name = pSQL($this->context->customer->firstname.' '.$this->context->customer->lastname);
  95. $comment->title = Tools::getValue('title');
  96. $comment->grade = 0;
  97. $comment->validate = 0;
  98. $comment->save();
  99. $grade_sum = 0;
  100. foreach(Tools::getValue('criterion') as $id_product_comment_criterion => $grade)
  101. {
  102. $grade_sum += $grade;
  103. $product_comment_criterion = new ProductCommentCriterion($id_product_comment_criterion);
  104. if ($product_comment_criterion->id)
  105. $product_comment_criterion->addGrade($comment->id, $grade);
  106. }
  107. if (count(Tools::getValue('criterion')) >= 1)
  108. {
  109. $comment->grade = $grade_sum / count(Tools::getValue('criterion'));
  110. // Update Grade average of comment
  111. $comment->save();
  112. }
  113. $result = true;
  114. Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('productcomments-reviews.tpl'));
  115. }
  116. else
  117. {
  118. $result = false;
  119. $errors[] = $module_instance->l('Please wait before posting another comment', 'default').' '.Configuration::get('PRODUCT_COMMENTS_MINIMAL_TIME').' '.$module_instance->l('seconds before posting a new comment', 'default');
  120. }
  121. }
  122. else
  123. $result = false;
  124. die(Tools::jsonEncode(array(
  125. 'result' => $result,
  126. 'errors' => $errors
  127. )));
  128. }
  129. protected function ajaxProcessReportAbuse()
  130. {
  131. if (!Tools::isSubmit('id_product_comment'))
  132. die('0');
  133. if (ProductComment::isAlreadyReport(Tools::getValue('id_product_comment'), $this->context->cookie->id_customer))
  134. die('0');
  135. if (ProductComment::reportComment((int)Tools::getValue('id_product_comment'), $this->context->cookie->id_customer))
  136. die('1');
  137. die('0');
  138. }
  139. protected function ajaxProcessCommentIsUsefull()
  140. {
  141. if (!Tools::isSubmit('id_product_comment') || !Tools::isSubmit('value'))
  142. die('0');
  143. if (ProductComment::isAlreadyUsefulness(Tools::getValue('id_product_comment'), $this->context->cookie->id_customer))
  144. die('0');
  145. if (ProductComment::setCommentUsefulness((int)Tools::getValue('id_product_comment'), (bool)Tools::getValue('value'), $this->context->cookie->id_customer))
  146. die('1');
  147. die('0');
  148. }
  149. }