PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/productcomments/ProductCommentCriterion.php

https://gitlab.com/staging06/myproject
PHP | 266 lines | 191 code | 20 blank | 55 comment | 22 complexity | 356205e96d0b0a293a8be7d2e7502226 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. class ProductCommentCriterion extends ObjectModel
  27. {
  28. public $id;
  29. public $id_product_comment_criterion_type;
  30. public $name;
  31. public $active = true;
  32. /**
  33. * @see ObjectModel::$definition
  34. */
  35. public static $definition = array(
  36. 'table' => 'product_comment_criterion',
  37. 'primary' => 'id_product_comment_criterion',
  38. 'multilang' => true,
  39. 'fields' => array(
  40. 'id_product_comment_criterion_type' => array('type' => self::TYPE_INT),
  41. 'active' => array('type' => self::TYPE_BOOL),
  42. // Lang fields
  43. 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
  44. )
  45. );
  46. public function delete()
  47. {
  48. if (!parent::delete())
  49. return false;
  50. if ($this->id_product_comment_criterion_type == 2)
  51. {
  52. if (!Db::getInstance()->execute('
  53. DELETE FROM '._DB_PREFIX_.'product_comment_criterion_category
  54. WHERE id_product_comment_criterion='.(int)$this->id))
  55. return false;
  56. }
  57. elseif ($this->id_product_comment_criterion_type == 3)
  58. {
  59. if (!Db::getInstance()->execute('
  60. DELETE FROM '._DB_PREFIX_.'product_comment_criterion_product
  61. WHERE id_product_comment_criterion='.(int)$this->id))
  62. return false;
  63. }
  64. return Db::getInstance()->execute('
  65. DELETE FROM `'._DB_PREFIX_.'product_comment_grade`
  66. WHERE `id_product_comment_criterion` = '.(int)$this->id);
  67. }
  68. public function update($nullValues = false)
  69. {
  70. $previousUpdate = new self((int)$this->id);
  71. if (!parent::update($nullValues))
  72. return false;
  73. if ($previousUpdate->id_product_comment_criterion_type != $this->id_product_comment_criterion_type)
  74. {
  75. if ($previousUpdate->id_product_comment_criterion_type == 2)
  76. return Db::getInstance()->execute('
  77. DELETE FROM '._DB_PREFIX_.'product_comment_criterion_category
  78. WHERE id_product_comment_criterion = '.(int)$previousUpdate->id);
  79. elseif ($previousUpdate->id_product_comment_criterion_type == 3)
  80. return Db::getInstance()->execute('
  81. DELETE FROM '._DB_PREFIX_.'product_comment_criterion_product
  82. WHERE id_product_comment_criterion = '.(int)$previousUpdate->id);
  83. }
  84. return true;
  85. }
  86. /**
  87. * Link a Comment Criterion to a product
  88. *
  89. * @return boolean succeed
  90. */
  91. public function addProduct($id_product)
  92. {
  93. if (!Validate::isUnsignedId($id_product))
  94. die(Tools::displayError());
  95. return Db::getInstance()->execute('
  96. INSERT INTO `'._DB_PREFIX_.'product_comment_criterion_product` (`id_product_comment_criterion`, `id_product`)
  97. VALUES('.(int)$this->id.','.(int)$id_product.')
  98. ');
  99. }
  100. /**
  101. * Link a Comment Criterion to a category
  102. *
  103. * @return boolean succeed
  104. */
  105. public function addCategory($id_category)
  106. {
  107. if (!Validate::isUnsignedId($id_category))
  108. die(Tools::displayError());
  109. return Db::getInstance()->execute('
  110. INSERT INTO `'._DB_PREFIX_.'product_comment_criterion_category` (`id_product_comment_criterion`, `id_category`)
  111. VALUES('.(int)$this->id.','.(int)$id_category.')
  112. ');
  113. }
  114. /**
  115. * Add grade to a criterion
  116. *
  117. * @return boolean succeed
  118. */
  119. public function addGrade($id_product_comment, $grade)
  120. {
  121. if (!Validate::isUnsignedId($id_product_comment))
  122. die(Tools::displayError());
  123. if ($grade < 0)
  124. $grade = 0;
  125. elseif ($grade > 10)
  126. $grade = 10;
  127. return (Db::getInstance()->execute('
  128. INSERT INTO `'._DB_PREFIX_.'product_comment_grade`
  129. (`id_product_comment`, `id_product_comment_criterion`, `grade`) VALUES(
  130. '.(int)($id_product_comment).',
  131. '.(int)$this->id.',
  132. '.(int)($grade).')'));
  133. }
  134. /**
  135. * Get criterion by Product
  136. *
  137. * @return array Criterion
  138. */
  139. public static function getByProduct($id_product, $id_lang)
  140. {
  141. if (!Validate::isUnsignedId($id_product) ||
  142. !Validate::isUnsignedId($id_lang))
  143. die(Tools::displayError());
  144. $alias = 'p';
  145. $table = '';
  146. // check if version > 1.5 to add shop association
  147. if (version_compare(_PS_VERSION_, '1.5', '>'))
  148. {
  149. $table = '_shop';
  150. $alias = 'ps';
  151. }
  152. $cache_id = 'ProductCommentCriterion::getByProduct_'.(int)$id_product.'-'.(int)$id_lang;
  153. if (!Cache::isStored($cache_id))
  154. {
  155. $result = Db::getInstance()->executeS('
  156. SELECT pcc.`id_product_comment_criterion`, pccl.`name`
  157. FROM `'._DB_PREFIX_.'product_comment_criterion` pcc
  158. LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_lang` pccl
  159. ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion)
  160. LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_product` pccp
  161. ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = '.(int)$id_product.')
  162. LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_category` pccc
  163. ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`)
  164. LEFT JOIN `'._DB_PREFIX_.'product'.$table.'` '.$alias.'
  165. ON ('.$alias.'.id_category_default = pccc.id_category AND '.$alias.'.id_product = '.(int)$id_product.')
  166. WHERE pccl.`id_lang` = '.(int)($id_lang).'
  167. AND (
  168. pccp.id_product IS NOT NULL
  169. OR ps.id_product IS NOT NULL
  170. OR pcc.id_product_comment_criterion_type = 1
  171. )
  172. AND pcc.active = 1
  173. GROUP BY pcc.id_product_comment_criterion
  174. ');
  175. Cache::store($cache_id, $result);
  176. }
  177. return Cache::retrieve($cache_id);
  178. }
  179. /**
  180. * Get Criterions
  181. *
  182. * @return array Criterions
  183. */
  184. public static function getCriterions($id_lang, $type = false, $active = false)
  185. {
  186. if (!Validate::isUnsignedId($id_lang))
  187. die(Tools::displayError());
  188. $sql = '
  189. SELECT pcc.`id_product_comment_criterion`, pcc.id_product_comment_criterion_type, pccl.`name`, pcc.active
  190. FROM `'._DB_PREFIX_.'product_comment_criterion` pcc
  191. JOIN `'._DB_PREFIX_.'product_comment_criterion_lang` pccl ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion)
  192. WHERE pccl.`id_lang` = '.(int)$id_lang.($active ? ' AND active = 1' : '').($type ? ' AND id_product_comment_criterion_type = '.(int)$type : '').'
  193. ORDER BY pccl.`name` ASC';
  194. $criterions = Db::getInstance()->executeS($sql);
  195. $types = self::getTypes();
  196. foreach ($criterions as $key => $data)
  197. $criterions[$key]['type_name'] = $types[$data['id_product_comment_criterion_type']];
  198. return $criterions;
  199. }
  200. public function getProducts()
  201. {
  202. $res = Db::getInstance()->executeS('
  203. SELECT pccp.id_product, pccp.id_product_comment_criterion
  204. FROM `'._DB_PREFIX_.'product_comment_criterion_product` pccp
  205. WHERE pccp.id_product_comment_criterion = '.(int)$this->id);
  206. $products = array();
  207. if ($res)
  208. foreach ($res AS $row)
  209. $products[] = (int)$row['id_product'];
  210. return $products;
  211. }
  212. public function getCategories()
  213. {
  214. $res = Db::getInstance()->executeS('
  215. SELECT pccc.id_category, pccc.id_product_comment_criterion
  216. FROM `'._DB_PREFIX_.'product_comment_criterion_category` pccc
  217. WHERE pccc.id_product_comment_criterion = '.(int)$this->id);
  218. $criterions = array();
  219. if ($res)
  220. foreach ($res AS $row)
  221. $criterions[] = (int)$row['id_category'];
  222. return $criterions;
  223. }
  224. public function deleteCategories()
  225. {
  226. return Db::getInstance()->execute('
  227. DELETE FROM `'._DB_PREFIX_.'product_comment_criterion_category`
  228. WHERE `id_product_comment_criterion` = '.(int)$this->id);
  229. }
  230. public function deleteProducts()
  231. {
  232. return Db::getInstance()->execute('
  233. DELETE FROM `'._DB_PREFIX_.'product_comment_criterion_product`
  234. WHERE `id_product_comment_criterion` = '.(int)$this->id);
  235. }
  236. public static function getTypes()
  237. {
  238. // Instance of module class for translations
  239. $module = new ProductComments();
  240. return array(
  241. 1 => $module->l('Valid for the entire catalog', 'ProductCommentCriterion'),
  242. 2 => $module->l('Restricted to some categories', 'ProductCommentCriterion'),
  243. 3 => $module->l('Restricted to some products', 'ProductCommentCriterion')
  244. );
  245. }
  246. }