PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Tag/Model/Resource/Tag/Collection.php

https://bitbucket.org/acidel/buykoala
PHP | 423 lines | 209 code | 43 blank | 171 comment | 22 complexity | 4be46bb4324787f7737ddf42e9b63b0b MD5 | raw file
  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_Tag
  23. * @copyright Copyright (c) 2011 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. * Tag collection model
  28. *
  29. * @category Mage
  30. * @package Mage_Tag
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Tag_Model_Resource_Tag_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * Use getFlag('store_filter') & setFlag('store_filter', true) instead.
  37. *
  38. * @var bool
  39. */
  40. protected $_isStoreFilter = false;
  41. /**
  42. * Joined tables
  43. *
  44. * @var array
  45. */
  46. protected $_joinFlags = array();
  47. /**
  48. * Mapping for fields
  49. *
  50. * @var array
  51. */
  52. public $_map = array(
  53. 'fields' => array(
  54. 'tag_id' => 'main_table.tag_id'
  55. ),
  56. );
  57. /**
  58. * Define resource model and model
  59. *
  60. */
  61. protected function _construct()
  62. {
  63. $this->_init('tag/tag');
  64. }
  65. /**
  66. * Loads collection
  67. *
  68. * @param bool $printQuery
  69. * @param bool $logQuery
  70. * @return Mage_Tag_Model_Resource_Tag_Collection
  71. */
  72. public function load($printQuery = false, $logQuery = false)
  73. {
  74. if ($this->isLoaded()) {
  75. return $this;
  76. }
  77. parent::load($printQuery, $logQuery);
  78. if ($this->getFlag('add_stores_after')) {
  79. $this->_addStoresVisibility();
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Set flag about joined table.
  85. * setFlag method must be used in future.
  86. *
  87. * @deprecated after 1.3.2.3
  88. *
  89. * @param string $table
  90. * @return Mage_Tag_Model_Resource_Tag_Collection
  91. */
  92. public function setJoinFlag($table)
  93. {
  94. $this->setFlag($table, true);
  95. return $this;
  96. }
  97. /**
  98. * Get flag's status about joined table.
  99. * getFlag method must be used in future.
  100. *
  101. * @deprecated after 1.3.2.3
  102. *
  103. * @param string $table
  104. * @return bool
  105. */
  106. public function getJoinFlag($table)
  107. {
  108. return $this->getFlag($table);
  109. }
  110. /**
  111. * Unset value of join flag.
  112. * Set false (bool) value to flag instead in future.
  113. *
  114. * @deprecated after 1.3.2.3
  115. *
  116. * @param string $table
  117. * @return Mage_Tag_Model_Resource_Tag_Collection
  118. */
  119. public function unsetJoinFlag($table = null)
  120. {
  121. $this->setFlag($table, false);
  122. return $this;
  123. }
  124. /**
  125. * Sett
  126. *
  127. * @param int $limit
  128. * @return Mage_Tag_Model_Resource_Tag_Collection
  129. */
  130. public function limit($limit)
  131. {
  132. $this->getSelect()->limit($limit);
  133. return $this;
  134. }
  135. /**
  136. * Replacing popularity by sum of popularity and base_popularity
  137. *
  138. * @param int $limit
  139. * @return Mage_Tag_Model_Resource_Tag_Collection
  140. */
  141. public function addPopularity($limit = null)
  142. {
  143. if (!$this->getFlag('popularity')) {
  144. $this->getSelect()
  145. ->joinLeft(
  146. array('relation' => $this->getTable('tag/relation')),
  147. 'main_table.tag_id = relation.tag_id',
  148. array()
  149. )
  150. ->joinLeft(
  151. array('summary' => $this->getTable('tag/summary')),
  152. 'relation.tag_id = summary.tag_id AND relation.store_id = summary.store_id',
  153. array('popularity')
  154. )
  155. ->group('main_table.tag_id');
  156. /*
  157. * Allow analytic function usage
  158. */
  159. $this->_useAnalyticFunction = true;
  160. if (!is_null($limit)) {
  161. $this->getSelect()->limit($limit);
  162. }
  163. $this->setFlag('popularity');
  164. }
  165. return $this;
  166. }
  167. /**
  168. * Adds summary
  169. *
  170. * @param int $storeId
  171. * @return Mage_Tag_Model_Resource_Tag_Collection
  172. */
  173. public function addSummary($storeId)
  174. {
  175. if (!$this->getFlag('summary')) {
  176. $tableAlias = 'summary';
  177. $joinCondition = $this->getConnection()
  178. ->quoteInto(' AND ' . $tableAlias . '.store_id IN(?)', $storeId);
  179. $this->getSelect()
  180. ->joinLeft(
  181. array($tableAlias => $this->getTable('tag/summary')),
  182. 'main_table.tag_id = ' . $tableAlias . '.tag_id' . $joinCondition,
  183. array('store_id','popularity', 'customers', 'products'
  184. ));
  185. $this->addFilterToMap('store_id', $tableAlias . '.store_id');
  186. $this->addFilterToMap('popularity', $tableAlias . '.popularity');
  187. $this->addFilterToMap('customers', $tableAlias . '.customers');
  188. $this->addFilterToMap('products', $tableAlias . '.products');
  189. $this->setFlag('summary', true);
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Adds store visibility
  195. *
  196. * @return Mage_Tag_Model_Resource_Tag_Collection
  197. */
  198. public function addStoresVisibility()
  199. {
  200. $this->setFlag('add_stores_after', true);
  201. return $this;
  202. }
  203. /**
  204. * Adds store visibility
  205. *
  206. * @return Mage_Tag_Model_Resource_Tag_Collection
  207. */
  208. protected function _addStoresVisibility()
  209. {
  210. $tagIds = $this->getColumnValues('tag_id');
  211. $tagsStores = array();
  212. if (sizeof($tagIds) > 0) {
  213. $select = $this->getConnection()->select()
  214. ->from($this->getTable('tag/summary'), array('store_id', 'tag_id'))
  215. ->where('tag_id IN(?)', $tagIds);
  216. $tagsRaw = $this->getConnection()->fetchAll($select);
  217. foreach ($tagsRaw as $tag) {
  218. if (!isset($tagsStores[$tag['tag_id']])) {
  219. $tagsStores[$tag['tag_id']] = array();
  220. }
  221. $tagsStores[$tag['tag_id']][] = $tag['store_id'];
  222. }
  223. }
  224. foreach ($this as $item) {
  225. if (isset($tagsStores[$item->getId()])) {
  226. $item->setStores($tagsStores[$item->getId()]);
  227. } else {
  228. $item->setStores(array());
  229. }
  230. }
  231. return $this;
  232. }
  233. /**
  234. * Adds field to filter
  235. *
  236. * @param string $field
  237. * @param array $condition
  238. * @return Mage_Tag_Model_Resource_Tag_Collection
  239. */
  240. public function addFieldToFilter($field, $condition = null)
  241. {
  242. if ($this->getFlag('relation') && 'popularity' == $field) {
  243. // TOFIX
  244. $this->getSelect()->having(
  245. $this->_getConditionSql('COUNT(relation.tag_relation_id)', $condition)
  246. );
  247. } elseif ($this->getFlag('summary') && in_array(
  248. $field, array('customers', 'products', 'uses', 'historical_uses', 'popularity')
  249. )) {
  250. $this->getSelect()->where($this->_getConditionSql('summary.' . $field, $condition));
  251. } else {
  252. parent::addFieldToFilter($field, $condition);
  253. }
  254. return $this;
  255. }
  256. /**
  257. * Get sql for get record count
  258. *
  259. * @return string
  260. */
  261. public function getSelectCountSql()
  262. {
  263. $select = parent::getSelectCountSql();
  264. $select->reset(Zend_Db_Select::COLUMNS);
  265. $select->reset(Zend_Db_Select::GROUP);
  266. $select->reset(Zend_Db_Select::HAVING);
  267. $select->columns('COUNT(DISTINCT main_table.tag_id)');
  268. return $select;
  269. }
  270. /**
  271. * Add filter by store
  272. *
  273. * @param array | int $storeId
  274. * @param bool $allFilter
  275. * @return Mage_Tag_Model_Resource_Tag_Collection
  276. */
  277. public function addStoreFilter($storeId, $allFilter = true)
  278. {
  279. if (!$this->getFlag('store_filter')) {
  280. $this->getSelect()->joinLeft(
  281. array('summary_store' => $this->getTable('tag/summary')),
  282. 'main_table.tag_id = summary_store.tag_id'
  283. );
  284. $this->getSelect()->where('summary_store.store_id IN (?)', $storeId);
  285. $this->getSelect()->group('main_table.tag_id');
  286. if ($this->getFlag('relation') && $allFilter) {
  287. $this->getSelect()->where('relation.store_id IN (?)', $storeId);
  288. }
  289. if ($this->getFlag('prelation') && $allFilter) {
  290. $this->getSelect()->where('prelation.store_id IN (?)', $storeId);
  291. }
  292. /*
  293. * Allow Analytic functions usage
  294. */
  295. $this->_useAnalyticFunction = true;
  296. $this->setFlag('store_filter', true);
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Adds filtering by active
  302. *
  303. * @return Mage_Tag_Model_Resource_Tag_Collection
  304. */
  305. public function setActiveFilter()
  306. {
  307. $statusActive = Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE;
  308. $this->getSelect()->where('relation.active = ?', $statusActive);
  309. if ($this->getFlag('prelation')) {
  310. $this->getSelect()->where('prelation.active = ?', $statusActive);
  311. }
  312. return $this;
  313. }
  314. /**
  315. * Adds filter by status
  316. *
  317. * @param int $status
  318. * @return Mage_Tag_Model_Resource_Tag_Collection
  319. */
  320. public function addStatusFilter($status)
  321. {
  322. $this->getSelect()->where('main_table.status = ?', $status);
  323. return $this;
  324. }
  325. /**
  326. * Adds filter by product id
  327. *
  328. * @param int $productId
  329. * @return Mage_Tag_Model_Resource_Tag_Collection
  330. */
  331. public function addProductFilter($productId)
  332. {
  333. $this->addFieldToFilter('relation.product_id', $productId);
  334. if ($this->getFlag('prelation')) {
  335. $this->addFieldToFilter('prelation.product_id', $productId);
  336. }
  337. return $this;
  338. }
  339. /**
  340. * Adds filter by customer id
  341. *
  342. * @param int $customerId
  343. * @return Mage_Tag_Model_Resource_Tag_Collection
  344. */
  345. public function addCustomerFilter($customerId)
  346. {
  347. $this->getSelect()
  348. ->where('relation.customer_id = ?', $customerId);
  349. if ($this->getFlag('prelation')) {
  350. $this->getSelect()
  351. ->where('prelation.customer_id = ?', $customerId);
  352. }
  353. return $this;
  354. }
  355. /**
  356. * Adds grouping by tag id
  357. *
  358. * @return Mage_Tag_Model_Resource_Tag_Collection
  359. */
  360. public function addTagGroup()
  361. {
  362. $this->getSelect()->group('main_table.tag_id');
  363. $this->_useAnalyticFunction = true;
  364. return $this;
  365. }
  366. /**
  367. * Joins tag/relation table
  368. *
  369. * @return Mage_Tag_Model_Resource_Tag_Collection
  370. */
  371. public function joinRel()
  372. {
  373. $this->setFlag('relation', true);
  374. $this->getSelect()->joinLeft(
  375. array('relation' => $this->getTable('tag/relation')),
  376. 'main_table.tag_id=relation.tag_id'
  377. );
  378. return $this;
  379. }
  380. }