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

/magento/app/code/core/Mage/Reports/Model/Resource/Product/Index/Collection/Abstract.php

https://bitbucket.org/jit_bec/shopifine
PHP | 165 lines | 74 code | 12 blank | 79 comment | 7 complexity | 425d7ec971adea2641babca4e85aaa15 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_Reports
  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. * Reports Product Index Abstract Product Resource Collection
  28. *
  29. * @category Mage
  30. * @package Mage_Reports
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  34. extends Mage_Catalog_Model_Resource_Product_Collection
  35. {
  36. /**
  37. * Customer id
  38. *
  39. * @var null|int
  40. */
  41. protected $_customerId = null;
  42. /**
  43. * Retrieve Product Index table name
  44. *
  45. */
  46. abstract protected function _getTableName();
  47. /**
  48. * Join index table
  49. *
  50. * @return Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  51. */
  52. protected function _joinIdxTable()
  53. {
  54. if (!$this->getFlag('is_idx_table_joined')) {
  55. $this->joinTable(
  56. array('idx_table' => $this->_getTableName()),
  57. 'product_id=entity_id',
  58. array(
  59. 'product_id' => 'product_id',
  60. 'item_store_id' => 'store_id',
  61. 'added_at' => 'added_at'
  62. ),
  63. $this->_getWhereCondition()
  64. );
  65. $this->setFlag('is_idx_table_joined', true);
  66. }
  67. return $this;
  68. }
  69. /**
  70. * Add Viewed Products Index to Collection
  71. *
  72. * @return Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  73. */
  74. public function addIndexFilter()
  75. {
  76. $this->_joinIdxTable();
  77. $this->_productLimitationFilters['store_table'] = 'idx_table';
  78. $this->setFlag('url_data_object', true);
  79. $this->setFlag('do_not_use_category_id', true);
  80. return $this;
  81. }
  82. /**
  83. * Add filter by product ids
  84. *
  85. * @param array $ids
  86. * @return Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  87. */
  88. public function addFilterByIds($ids)
  89. {
  90. if (empty($ids)) {
  91. $this->getSelect()->where('1=0');
  92. } else {
  93. $this->getSelect()->where('e.entity_id IN(?)', $ids);
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Retrieve Where Condition to Index table
  99. *
  100. * @return array
  101. */
  102. protected function _getWhereCondition()
  103. {
  104. $condition = array();
  105. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  106. $condition['customer_id'] = Mage::getSingleton('customer/session')->getCustomerId();
  107. } elseif ($this->_customerId) {
  108. $condition['customer_id'] = $this->_customerId;
  109. } else {
  110. $condition['visitor_id'] = Mage::getSingleton('log/visitor')->getId();
  111. }
  112. return $condition;
  113. }
  114. /**
  115. * Set customer id, that will be used in 'whereCondition'
  116. *
  117. * @param int $id
  118. * @return Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  119. */
  120. public function setCustomerId($id)
  121. {
  122. $this->_customerId = (int)$id;
  123. return $this;
  124. }
  125. /**
  126. * Add order by "added at"
  127. *
  128. * @param string $dir
  129. * @return Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  130. */
  131. public function setAddedAtOrder($dir = self::SORT_ORDER_DESC)
  132. {
  133. if ($this->getFlag('is_idx_table_joined')) {
  134. $this->getSelect()->order('added_at ' . $dir);
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Add exclude Product Ids
  140. *
  141. * @param int|array $productIds
  142. * @return Mage_Reports_Model_Resource_Product_Index_Collection_Abstract
  143. */
  144. public function excludeProductIds($productIds)
  145. {
  146. if (empty($productIds)) {
  147. return $this;
  148. }
  149. $this->_joinIdxTable();
  150. $this->getSelect()->where('idx_table.product_id NOT IN(?)', $productIds);
  151. return $this;
  152. }
  153. }