PageRenderTime 57ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Pack.php

http://marocmall.googlecode.com/
PHP | 201 lines | 143 code | 21 blank | 37 comment | 11 complexity | 1694370ce9b8b0fccf91bd8c31fd1837 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 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-2011 PrestaShop SA
  23. * @version Release: $Revision: 6594 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class PackCore extends Product
  28. {
  29. protected static $cachePackItems = array();
  30. protected static $cacheIsPack = array();
  31. protected static $cacheIsPacked = array();
  32. public static function isPack($id_product)
  33. {
  34. if (!array_key_exists($id_product, self::$cacheIsPack))
  35. {
  36. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)($id_product));
  37. self::$cacheIsPack[$id_product] = ($result > 0);
  38. }
  39. return self::$cacheIsPack[$id_product];
  40. }
  41. public static function isPacked($id_product)
  42. {
  43. if (!array_key_exists($id_product, self::$cacheIsPacked))
  44. {
  45. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_item = '.(int)($id_product));
  46. self::$cacheIsPacked[$id_product] = ($result > 0);
  47. }
  48. return self::$cacheIsPacked[$id_product];
  49. }
  50. public static function noPackPrice($id_product)
  51. {
  52. global $cookie;
  53. $sum = 0;
  54. $price_display_method = !self::$_taxCalculationMethod;
  55. $items = self::getItems($id_product, Configuration::get('PS_LANG_DEFAULT'));
  56. foreach ($items as $item)
  57. $sum += $item->getPrice($price_display_method) * $item->pack_quantity;
  58. return $sum;
  59. }
  60. public static function getItems($id_product, $id_lang)
  61. {
  62. if (array_key_exists($id_product, self::$cachePackItems))
  63. return self::$cachePackItems[$id_product];
  64. $result = Db::getInstance()->ExecuteS('SELECT id_product_item, quantity FROM '._DB_PREFIX_.'pack where id_product_pack = '.(int)($id_product));
  65. $arrayResult = array();
  66. foreach ($result AS $row)
  67. {
  68. $p = new Product($row['id_product_item'], false, (int)($id_lang));
  69. $p->pack_quantity = $row['quantity'];
  70. $arrayResult[] = $p;
  71. }
  72. self::$cachePackItems[$id_product] = $arrayResult;
  73. return self::$cachePackItems[$id_product];
  74. }
  75. public static function isInStock($id_product)
  76. {
  77. $items = self::getItems((int)($id_product), Configuration::get('PS_LANG_DEFAULT'));
  78. foreach ($items AS $item)
  79. if ($item->quantity < $item->pack_quantity AND !$item->isAvailableWhenOutOfStock((int)($item->out_of_stock)))
  80. return false;
  81. return true;
  82. }
  83. public static function getItemTable($id_product, $id_lang, $full = false)
  84. {
  85. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
  86. SELECT p.*, pl.*, i.`id_image`, il.`legend`, t.`rate`, cl.`name` AS category_default, a.quantity AS pack_quantity
  87. FROM `'._DB_PREFIX_.'pack` a
  88. LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = a.id_product_item
  89. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.id_product = pl.id_product AND pl.`id_lang` = '.(int)($id_lang).')
  90. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
  91. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
  92. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).')
  93. LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
  94. AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
  95. AND tr.`id_state` = 0)
  96. LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
  97. LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')
  98. WHERE a.`id_product_pack` = '.(int)($id_product)
  99. );
  100. if (!$full)
  101. return $result;
  102. $arrayResult = array();
  103. foreach ($result as $row)
  104. if (!Pack::isPack($row['id_product']))
  105. $arrayResult[] = Product::getProductProperties($id_lang, $row);
  106. return $arrayResult;
  107. }
  108. public static function getPacksTable($id_product, $id_lang, $full = false, $limit = NULL)
  109. {
  110. $packs = Db::getInstance()->getValue('
  111. SELECT GROUP_CONCAT(a.`id_product_pack`)
  112. FROM `'._DB_PREFIX_.'pack` a
  113. WHERE a.`id_product_item` = '.(int)$id_product);
  114. if (!(int)$packs)
  115. return array();
  116. $sql = '
  117. SELECT p.*, pl.*, i.`id_image`, il.`legend`, t.`rate`
  118. FROM `'._DB_PREFIX_.'product` p
  119. NATURAL LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  120. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
  121. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  122. LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
  123. AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
  124. AND tr.`id_state` = 0)
  125. LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
  126. LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$id_lang.')
  127. WHERE pl.`id_lang` = '.(int)$id_lang.'
  128. AND p.`id_product` IN ('.$packs.')';
  129. if ($limit)
  130. $sql .= ' LIMIT '.(int)$limit;
  131. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
  132. if (!$full)
  133. return $result;
  134. $arrayResult = array();
  135. foreach ($result as $row)
  136. if (!Pack::isPacked($row['id_product']))
  137. $arrayResult[] = Product::getProductProperties($id_lang, $row);
  138. return $arrayResult;
  139. }
  140. public static function deleteItems($id_product)
  141. {
  142. Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'product SET cache_is_pack = 0 WHERE id_product = '.(int)($id_product).' LIMIT 1');
  143. return Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'pack` WHERE `id_product_pack` = '.(int)($id_product));
  144. }
  145. /**
  146. * @deprecated
  147. */
  148. public static function addItems($id_product, $ids)
  149. {
  150. Tools::displayAsDeprecated();
  151. array_pop($ids);
  152. foreach ($ids as $id_product_item)
  153. {
  154. $idQty = explode('x', $id_product_item);
  155. if (!self::addItem($id_product, $idQty[1], $idQty[0]))
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * Add an item to the pack
  162. *
  163. * @param integer $id_product
  164. * @param integer $id_item
  165. * @param integer $qty
  166. * @return boolean true if everything was fine
  167. */
  168. public static function addItem($id_product, $id_item, $qty)
  169. {
  170. Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'product SET cache_is_pack = 1 WHERE id_product = '.(int)($id_product).' LIMIT 1');
  171. return Db::getInstance()->AutoExecute(_DB_PREFIX_.'pack', array('id_product_pack' => (int)($id_product), 'id_product_item' => (int)($id_item), 'quantity' => (int)($qty)), 'INSERT');
  172. }
  173. public static function duplicate($id_product_old, $id_product_new)
  174. {
  175. Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'pack (id_product_pack, id_product_item, quantity)
  176. (SELECT '.(int)($id_product_new).', id_product_item, quantity FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)($id_product_old).')');
  177. // If return query result, a non-pack product will return false
  178. return true;
  179. }
  180. }