PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Pack.php

https://bitbucket.org/enurkov/prestashop
PHP | 276 lines | 176 code | 31 blank | 69 comment | 22 complexity | c30aec8bdb406486e64a34c9de6da73a MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 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/osl-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-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class PackCore extends Product
  27. {
  28. protected static $cachePackItems = array();
  29. protected static $cacheIsPack = array();
  30. protected static $cacheIsPacked = array();
  31. /**
  32. * Is product a pack?
  33. *
  34. * @static
  35. * @param $id_product
  36. * @return bool
  37. */
  38. public static function isPack($id_product)
  39. {
  40. if (!Pack::isFeatureActive())
  41. return false;
  42. if (!$id_product)
  43. return false;
  44. if (!array_key_exists($id_product, self::$cacheIsPack))
  45. {
  46. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)$id_product);
  47. self::$cacheIsPack[$id_product] = ($result > 0);
  48. }
  49. return self::$cacheIsPack[$id_product];
  50. }
  51. /**
  52. * Is product in a pack?
  53. *
  54. * @static
  55. * @param $id_product
  56. * @return bool
  57. */
  58. public static function isPacked($id_product)
  59. {
  60. if (!Pack::isFeatureActive())
  61. return false;
  62. if (!array_key_exists($id_product, self::$cacheIsPacked))
  63. {
  64. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_item = '.(int)$id_product);
  65. self::$cacheIsPacked[$id_product] = ($result > 0);
  66. }
  67. return self::$cacheIsPacked[$id_product];
  68. }
  69. public static function noPackPrice($id_product)
  70. {
  71. $sum = 0;
  72. $price_display_method = !self::$_taxCalculationMethod;
  73. $items = Pack::getItems($id_product, Configuration::get('PS_LANG_DEFAULT'));
  74. foreach ($items as $item)
  75. $sum += $item->getPrice($price_display_method) * $item->pack_quantity;
  76. return $sum;
  77. }
  78. public static function getItems($id_product, $id_lang)
  79. {
  80. if (!Pack::isFeatureActive())
  81. return array();
  82. if (array_key_exists($id_product, self::$cachePackItems))
  83. return self::$cachePackItems[$id_product];
  84. $result = Db::getInstance()->executeS('SELECT id_product_item, quantity FROM '._DB_PREFIX_.'pack where id_product_pack = '.(int)$id_product);
  85. $array_result = array();
  86. foreach ($result as $row)
  87. {
  88. $p = new Product($row['id_product_item'], false, $id_lang);
  89. $p->loadStockData();
  90. $p->pack_quantity = $row['quantity'];
  91. $array_result[] = $p;
  92. }
  93. self::$cachePackItems[$id_product] = $array_result;
  94. return self::$cachePackItems[$id_product];
  95. }
  96. public static function isInStock($id_product)
  97. {
  98. if (!Pack::isFeatureActive())
  99. return true;
  100. $items = Pack::getItems((int)$id_product, Configuration::get('PS_LANG_DEFAULT'));
  101. foreach ($items as $item)
  102. {
  103. // Updated for 1.5.0
  104. if (Product::getQuantity($item->id) < $item->pack_quantity
  105. || (Product::getQuantity($item->id) < $item->pack_quantity && !$item->isAvailableWhenOutOfStock((int)$item->out_of_stock)))
  106. return false;
  107. }
  108. return true;
  109. }
  110. public static function getItemTable($id_product, $id_lang, $full = false)
  111. {
  112. if (!Pack::isFeatureActive())
  113. return array();
  114. $sql = 'SELECT p.*, product_shop.*, pl.*, image_shop.`id_image`, il.`legend`, cl.`name` AS category_default, a.quantity AS pack_quantity, product_shop.`id_category_default`, a.id_product_pack
  115. FROM `'._DB_PREFIX_.'pack` a
  116. LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = a.id_product_item
  117. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  118. ON p.id_product = pl.id_product
  119. AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
  120. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
  121. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  122. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  123. '.Shop::addSqlAssociation('product', 'p').'
  124. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  125. ON product_shop.`id_category_default` = cl.`id_category`
  126. AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
  127. WHERE product_shop.`id_shop` = '.(int)Context::getContext()->shop->id.'
  128. AND ((image_shop.id_image IS NOT NULL OR i.id_image IS NULL) OR (image_shop.id_image IS NULL AND i.cover=1))
  129. AND a.`id_product_pack` = '.(int)$id_product;
  130. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  131. foreach ($result as &$row)
  132. $row = Product::getTaxesInformations($row);
  133. if (!$full)
  134. return $result;
  135. $array_result = array();
  136. foreach ($result as $row)
  137. if (!Pack::isPack($row['id_product']))
  138. $array_result[] = Product::getProductProperties($id_lang, $row);
  139. return $array_result;
  140. }
  141. public static function getPacksTable($id_product, $id_lang, $full = false, $limit = null)
  142. {
  143. if (!Pack::isFeatureActive())
  144. return array();
  145. $packs = Db::getInstance()->getValue('
  146. SELECT GROUP_CONCAT(a.`id_product_pack`)
  147. FROM `'._DB_PREFIX_.'pack` a
  148. WHERE a.`id_product_item` = '.(int)$id_product);
  149. if (!(int)$packs)
  150. return array();
  151. $sql = '
  152. SELECT p.*, product_shop.*, pl.*, image_shop.`id_image`, il.`legend`
  153. FROM `'._DB_PREFIX_.'product` p
  154. NATURAL LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  155. '.Shop::addSqlAssociation('product', 'p').'
  156. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
  157. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  158. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  159. WHERE pl.`id_lang` = '.(int)$id_lang.'
  160. '.Shop::addSqlRestrictionOnLang('pl').'
  161. AND p.`id_product` IN ('.$packs.')
  162. AND ((image_shop.id_image IS NOT NULL OR i.id_image IS NULL) OR (image_shop.id_image IS NULL AND i.cover=1))';
  163. if ($limit)
  164. $sql .= ' LIMIT '.(int)$limit;
  165. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  166. if (!$full)
  167. return $result;
  168. $array_result = array();
  169. foreach ($result as $row)
  170. if (!Pack::isPacked($row['id_product']))
  171. $array_result[] = Product::getProductProperties($id_lang, $row);
  172. return $array_result;
  173. }
  174. public static function deleteItems($id_product)
  175. {
  176. return Db::getInstance()->update('product', array('cache_is_pack' => 0), 'id_product = '.(int)$id_product) &&
  177. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'pack` WHERE `id_product_pack` = '.(int)$id_product) &&
  178. Configuration::updateGlobalValue('PS_PACK_FEATURE_ACTIVE', Pack::isCurrentlyUsed());
  179. }
  180. /**
  181. * Add an item to the pack
  182. *
  183. * @param integer $id_product
  184. * @param integer $id_item
  185. * @param integer $qty
  186. * @return boolean true if everything was fine
  187. */
  188. public static function addItem($id_product, $id_item, $qty)
  189. {
  190. return Db::getInstance()->update('product', array('cache_is_pack' => 1), 'id_product = '.(int)$id_product) &&
  191. Db::getInstance()->insert('pack', array('id_product_pack' => (int)$id_product, 'id_product_item' => (int)$id_item, 'quantity' => (int)$qty)) &&
  192. Configuration::updateGlobalValue('PS_PACK_FEATURE_ACTIVE', '1');
  193. }
  194. public static function duplicate($id_product_old, $id_product_new)
  195. {
  196. Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'pack (id_product_pack, id_product_item, quantity)
  197. (SELECT '.(int)$id_product_new.', id_product_item, quantity FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)$id_product_old.')');
  198. // If return query result, a non-pack product will return false
  199. return true;
  200. }
  201. /**
  202. * This method is allow to know if a feature is used or active
  203. * @since 1.5.0.1
  204. * @return bool
  205. */
  206. public static function isFeatureActive()
  207. {
  208. return Configuration::get('PS_PACK_FEATURE_ACTIVE');
  209. }
  210. /**
  211. * This method is allow to know if a Pack entity is currently used
  212. * @since 1.5.0
  213. * @param $table
  214. * @param $has_active_column
  215. * @return bool
  216. */
  217. public static function isCurrentlyUsed($table = null, $has_active_column = false)
  218. {
  219. // We dont't use the parent method because the identifier isn't id_pack
  220. return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  221. SELECT `id_product_pack`
  222. FROM `'._DB_PREFIX_.'pack`
  223. ');
  224. }
  225. /**
  226. * For a given pack, tells if it has at least one product using the advanced stock management
  227. *
  228. * @param int $id_product id_pack
  229. * @return bool
  230. */
  231. public static function usesAdvancedStockManagement($id_product)
  232. {
  233. if (!Pack::isPack($id_product))
  234. return false;
  235. $products = Pack::getItems($id_product, Configuration::get('PS_LANG_DEFAULT'));
  236. foreach ($products as $product)
  237. {
  238. // if one product uses the advanced stock management
  239. if ($product->advanced_stock_management == 1)
  240. return true;
  241. }
  242. // not used
  243. return false;
  244. }
  245. }