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

/magento/app/code/core/Mage/Catalog/Model/Resource/Product/Compare/Item.php

https://bitbucket.org/jit_bec/shopifine
PHP | 247 lines | 146 code | 25 blank | 76 comment | 17 complexity | c907ce8eef441cd95f7e53902d0534a4 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  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@magentocommerce.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog compare item resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Compare_Item extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Initialize connection
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('catalog/compare_item', 'catalog_compare_item_id');
  42. }
  43. /**
  44. * Load object by product
  45. *
  46. * @param Mage_Core_Model_Abstract $object
  47. * @param mixed $product
  48. * @return bool
  49. */
  50. public function loadByProduct(Mage_Catalog_Model_Product_Compare_Item $object, $product)
  51. {
  52. $read = $this->_getReadAdapter();
  53. if ($product instanceof Mage_Catalog_Model_Product) {
  54. $productId = $product->getId();
  55. } else {
  56. $productId = $product;
  57. }
  58. $select = $read->select()->from($this->getMainTable())
  59. ->where('product_id = ?', (int)$productId);
  60. if ($object->getCustomerId()) {
  61. $select->where('customer_id = ?', (int)$object->getCustomerId());
  62. } else {
  63. $select->where('visitor_id = ?', (int)$object->getVisitorId());
  64. }
  65. $data = $read->fetchRow($select);
  66. if (!$data) {
  67. return false;
  68. }
  69. $object->setData($data);
  70. $this->_afterLoad($object);
  71. return true;
  72. }
  73. /**
  74. * Resource retrieve count compare items
  75. *
  76. * @param int $customerId
  77. * @param int $visitorId
  78. * @return int
  79. */
  80. public function getCount($customerId, $visitorId)
  81. {
  82. $bind = array('visitore_id' => (int)$visitorId);
  83. $select = $this->_getReadAdapter()->select()->from($this->getMainTable(), 'COUNT(*)')
  84. ->where('visitor_id = :visitore_id');
  85. if ($customerId) {
  86. $bind['customer_id'] = (int)$customerId;
  87. $select->where('customer_id = :customer_id');
  88. }
  89. return $this->_getReadAdapter()->fetchOne($select, $bind);
  90. }
  91. /**
  92. * Clean compare table
  93. *
  94. * @return Mage_Catalog_Model_Resource_Product_Compare_Item
  95. */
  96. public function clean()
  97. {
  98. while (true) {
  99. $select = $this->_getReadAdapter()->select()
  100. ->from(array('compare_table' => $this->getMainTable()), array('catalog_compare_item_id'))
  101. ->joinLeft(
  102. array('visitor_table' => $this->getTable('log/visitor')),
  103. 'visitor_table.visitor_id=compare_table.visitor_id AND compare_table.customer_id IS NULL',
  104. array())
  105. ->where('compare_table.visitor_id > ?', 0)
  106. ->where('visitor_table.visitor_id IS NULL')
  107. ->limit(100);
  108. $itemIds = $this->_getReadAdapter()->fetchCol($select);
  109. if (!$itemIds) {
  110. break;
  111. }
  112. $this->_getWriteAdapter()->delete(
  113. $this->getMainTable(),
  114. $this->_getWriteAdapter()->quoteInto('catalog_compare_item_id IN(?)', $itemIds)
  115. );
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Purge visitor data after customer logout
  121. *
  122. * @param Mage_Catalog_Model_Product_Compare_Item $object
  123. * @return Mage_Catalog_Model_Resource_Product_Compare_Item
  124. */
  125. public function purgeVisitorByCustomer($object)
  126. {
  127. if (!$object->getCustomerId()) {
  128. return $this;
  129. }
  130. $where = $this->_getWriteAdapter()->quoteInto('customer_id=?', $object->getCustomerId());
  131. $bind = array(
  132. 'visitor_id' => 0,
  133. );
  134. $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $where);
  135. return $this;
  136. }
  137. /**
  138. * Update (Merge) customer data from visitor
  139. * After Login process
  140. *
  141. * @param Mage_Catalog_Model_Product_Compare_Item $object
  142. * @return Mage_Catalog_Model_Resource_Product_Compare_Item
  143. */
  144. public function updateCustomerFromVisitor($object)
  145. {
  146. if (!$object->getCustomerId()) {
  147. return $this;
  148. }
  149. // collect visitor compared items
  150. $select = $this->_getWriteAdapter()->select()
  151. ->from($this->getMainTable())
  152. ->where('visitor_id=?', $object->getVisitorId());
  153. $visitor = $this->_getWriteAdapter()->fetchAll($select);
  154. // collect customer compared items
  155. $select = $this->_getWriteAdapter()->select()
  156. ->from($this->getMainTable())
  157. ->where('customer_id = ?', $object->getCustomerId())
  158. ->where('visitor_id != ?', $object->getVisitorId());
  159. $customer = $this->_getWriteAdapter()->fetchAll($select);
  160. $products = array();
  161. $delete = array();
  162. $update = array();
  163. foreach ($visitor as $row) {
  164. $products[$row['product_id']] = array(
  165. 'store_id' => $row['store_id'],
  166. 'customer_id' => $object->getCustomerId(),
  167. 'visitor_id' => $object->getVisitorId(),
  168. 'product_id' => $row['product_id']
  169. );
  170. $update[$row[$this->getIdFieldName()]] = $row['product_id'];
  171. }
  172. foreach ($customer as $row) {
  173. if (isset($products[$row['product_id']])) {
  174. $delete[] = $row[$this->getIdFieldName()];
  175. }
  176. else {
  177. $products[$row['product_id']] = array(
  178. 'store_id' => $row['store_id'],
  179. 'customer_id' => $object->getCustomerId(),
  180. 'visitor_id' => $object->getVisitorId(),
  181. 'product_id' => $row['product_id']
  182. );
  183. }
  184. }
  185. if ($delete) {
  186. $this->_getWriteAdapter()->delete($this->getMainTable(),
  187. $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . ' IN(?)', $delete));
  188. }
  189. if ($update) {
  190. foreach ($update as $itemId => $productId) {
  191. $bind = $products[$productId];
  192. $this->_getWriteAdapter()->update($this->getMainTable(), $bind,
  193. $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $itemId));
  194. }
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Clear compare items by visitor and/or customer
  200. *
  201. * @param int $visitorId
  202. * @param int $customerId
  203. * @return Mage_Catalog_Model_Resource_Product_Compare_Item
  204. */
  205. public function clearItems($visitorId = null, $customerId = null)
  206. {
  207. $where = array();
  208. if ($customerId) {
  209. $customerId = (int)$customerId;
  210. $where[] = $this->_getWriteAdapter()->quoteInto('customer_id = ?', $customerId);
  211. }
  212. if ($visitorId) {
  213. $visitorId = (int)$visitorId;
  214. $where[] = $this->_getWriteAdapter()->quoteInto('visitor_id = ?', $visitorId);
  215. }
  216. if (!$where) {
  217. return $this;
  218. }
  219. $this->_getWriteAdapter()->delete($this->getMainTable(), $where);
  220. return $this;
  221. }
  222. }