1<?php 2/** 3 * 2007-2016 PrestaShop 4 * 5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA 6 * Copyright (C) 2017-2018 thirty bees 7 * 8 * NOTICE OF LICENSE 9 * 10 * This source file is subject to the Open Software License (OSL 3.0) 11 * that is bundled with this package in the file LICENSE.txt. 12 * It is also available through the world-wide-web at this URL: 13 * http://opensource.org/licenses/osl-3.0.php 14 * If you did not receive a copy of the license and are unable to 15 * obtain it through the world-wide-web, please send an email 16 * to license@thirtybees.com so we can send you a copy immediately. 17 * 18 * DISCLAIMER 19 * 20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 21 * versions in the future. If you wish to customize PrestaShop for your 22 * needs please refer to https://www.thirtybees.com for more information. 23 * 24 * @author thirty bees <contact@thirtybees.com> 25 * @author PrestaShop SA <contact@prestashop.com> 26 * @copyright 2017-2018 thirty bees 27 * @copyright 2007-2016 PrestaShop SA 28 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 * PrestaShop is an internationally registered trademark & property of PrestaShop SA 30 */ 31 32/** 33 * Class CompareProductCore 34 * 35 * @since 1.0.0 36 */ 37class CompareProductCore extends ObjectModel 38{ 39 // @codingStandardsIgnoreStart 40 /** 41 * @see ObjectModel::$definition 42 */ 43 public static $definition = [ 44 'table' => 'compare', 45 'primary' => 'id_compare', 46 'fields' => [ 47 'id_compare' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true], 48 'id_customer' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true], 49 ], 50 ]; 51 /** @var int $id_compare */ 52 public $id_compare; 53 /** @var int $id_customer */ 54 public $id_customer; 55 /** @var string $date_add */ 56 public $date_add; 57 /** @var string $date_upd */ 58 public $date_upd; 59 // @codingStandardsIgnoreEnd 60 61 /** 62 * Get all compare products of the customer 63 * 64 * @param int $idCompare 65 * 66 * @return array 67 * 68 * @throws PrestaShopDatabaseException 69 * @throws PrestaShopException 70 * @since 1.0.0 71 * @version 1.0.0 Initial version 72 */ 73 public static function getCompareProducts($idCompare) 74 { 75 $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( 76 (new DbQuery()) 77 ->select('DISTINCT `id_product`') 78 ->from('compare', 'c') 79 ->leftJoin('compare_product', 'cp', 'cp.`id_compare` = c.`id_compare`') 80 ->where('cp.`id_compare` = '.(int) $idCompare) 81 ); 82 83 $compareProducts = []; 84 85 if ($results) { 86 foreach ($results as $result) { 87 $compareProducts[] = (int) $result['id_product']; 88 } 89 } 90 91 return $compareProducts; 92 } 93 94 /** 95 * Add a compare product for the customer 96 * 97 * @param int $idCompare 98 * @param int $idProduct 99 * 100 * @return bool 101 * 102 * @since 1.0.0 103 * @version 1.0.0 Initial version 104 * @throws PrestaShopException 105 * @throws PrestaShopException 106 */ 107 public static function addCompareProduct($idCompare, $idProduct) 108 { 109 // Check if compare row exists 110 $idCompare = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue( 111 (new DbQuery()) 112 ->select('`id_compare`') 113 ->from('compare') 114 ->where('`id_compare` = '.(int) $idCompare) 115 ); 116 117 if (!$idCompare) { 118 $idCustomer = false; 119 if (Context::getContext()->customer) { 120 $idCustomer = Context::getContext()->customer->id; 121 } 122 $sql = Db::getInstance()->insert( 123 'compare', 124 [ 125 'id_compare' => ['type' => 'sql', 'value' => 'NULL'], 126 'id_customer' => (int) $idCustomer, 127 ], 128 true 129 ); 130 if ($sql) { 131 $idCompare = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue( 132 (new DbQuery()) 133 ->select('MAX(`id_compare`)') 134 ->from('compare') 135 ); 136 Context::getContext()->cookie->id_compare = $idCompare; 137 } 138 } 139 140 return Db::getInstance()->insert( 141 'compare_product', 142 [ 143 'id_compare' => (int) $idCompare, 144 'id_product' => (int) $idProduct, 145 'date_add' => ['type' => 'sql', 'value' => 'NOW()'], 146 'date_upd' => ['type' => 'sql', 'value' => 'NOW()'], 147 ] 148 ); 149 } 150 151 /** 152 * Remove a compare product for the customer 153 * 154 * @param int $idCompare 155 * @param int $idProduct 156 * 157 * @return bool 158 * 159 * @since 1.0.0 160 * @version 1.0.0 Initial version 161 * @throws PrestaShopException 162 */ 163 public static function removeCompareProduct($idCompare, $idProduct) 164 { 165 return Db::getInstance()->execute(' 166 DELETE cp FROM `'._DB_PREFIX_.'compare_product` cp, `'._DB_PREFIX_.'compare` c 167 WHERE cp.`id_compare`=c.`id_compare` 168 AND cp.`id_product` = '.(int) $idProduct.' 169 AND c.`id_compare` = '.(int) $idCompare); 170 } 171 172 /** 173 * Get the number of compare products of the customer 174 * 175 * @param int $idCompare 176 * 177 * @return int 178 * 179 * @since 1.0.0 180 * @version 1.0.0 Initial version 181 * @throws PrestaShopException 182 */ 183 public static function getNumberProducts($idCompare) 184 { 185 return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue( 186 (new DbQuery()) 187 ->select('COUNT(`id_compare`)') 188 ->from('compare_product') 189 ->where('`id_compare` = '.(int) $idCompare) 190 ); 191 } 192 193 /** 194 * Clean entries which are older than the period 195 * 196 * @param string $period 197 * 198 * @return void 199 * 200 * @since 1.0.0 201 * @version 1.0.0 Initial version 202 * @throws PrestaShopException 203 */ 204 public static function cleanCompareProducts($period = null) 205 { 206 if ($period !== null) { 207 Tools::displayParameterAsDeprecated('period'); 208 } 209 210 Db::getInstance()->execute( 211 ' 212 DELETE cp, c FROM `'._DB_PREFIX_.'compare_product` cp, `'._DB_PREFIX_.'compare` c 213 WHERE cp.date_upd < DATE_SUB(NOW(), INTERVAL 1 WEEK) AND c.`id_compare`=cp.`id_compare`' 214 ); 215 } 216 217 /** 218 * Get the id_compare by id_customer 219 * 220 * @param int $idCustomer 221 * 222 * @return int $id_compare 223 * 224 * @since 1.0.0 225 * @version 1.0.0 Initial version 226 * @throws PrestaShopException 227 */ 228 public static function getIdCompareByIdCustomer($idCustomer) 229 { 230 return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue( 231 (new DbQuery()) 232 ->select('`id_compare`') 233 ->from('compare') 234 ->where('`id_customer` = '.(int) $idCustomer) 235 ); 236 } 237} 238