PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kdms/sh-magento
PHP | 376 lines | 164 code | 39 blank | 173 comment | 6 complexity | 11e49d98df0a4f3abcee1e12e1b7af2b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Tags customer collection
  28. *
  29. * @category Mage
  30. * @package Mage_Tag
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Tag_Model_Resource_Customer_Collection extends Mage_Customer_Model_Resource_Customer_Collection
  34. {
  35. /**
  36. * Allows disabling grouping
  37. *
  38. * @var bool
  39. */
  40. protected $_allowDisableGrouping = true;
  41. /**
  42. * Count attribute for count sql
  43. *
  44. * @var string
  45. */
  46. protected $_countAttribute = 'tr.tag_id';
  47. /**
  48. * Array with joined tables
  49. *
  50. * @var array
  51. */
  52. protected $_joinFlags = array();
  53. /**
  54. * Prepare select
  55. *
  56. * @return Mage_Tag_Model_Resource_Customer_Collection
  57. */
  58. public function _initSelect()
  59. {
  60. parent::_initSelect();
  61. $this->_joinFields();
  62. $this->_setIdFieldName('tag_relation_id');
  63. return $this;
  64. }
  65. /**
  66. * Set flag about joined table.
  67. * setFlag method must be used in future.
  68. *
  69. * @deprecated after 1.3.2.3
  70. *
  71. * @param string $table
  72. * @return Mage_Tag_Model_Resource_Customer_Collection
  73. */
  74. public function setJoinFlag($table)
  75. {
  76. $this->setFlag($table, true);
  77. return $this;
  78. }
  79. /**
  80. * Get flag's status about joined table.
  81. * getFlag method must be used in future.
  82. *
  83. * @deprecated after 1.3.2.3
  84. *
  85. * @param string $table
  86. * @return bool
  87. */
  88. public function getJoinFlag($table)
  89. {
  90. return $this->getFlag($table);
  91. }
  92. /**
  93. * Unset value of join flag.
  94. * Set false (bool) value to flag instead in future.
  95. *
  96. * @deprecated after 1.3.2.3
  97. *
  98. * @param string $table
  99. * @return Mage_Tag_Model_Resource_Customer_Collection
  100. */
  101. public function unsetJoinFlag($table = null)
  102. {
  103. $this->setFlag($table, false);
  104. return $this;
  105. }
  106. /**
  107. * Adds filter by tag is
  108. *
  109. * @param int $tagId
  110. * @return Mage_Tag_Model_Resource_Customer_Collection
  111. */
  112. public function addTagFilter($tagId)
  113. {
  114. $this->getSelect()
  115. ->where('tr.tag_id = ?', $tagId);
  116. return $this;
  117. }
  118. /**
  119. * adds filter by product id
  120. *
  121. * @param int $productId
  122. * @return Mage_Tag_Model_Resource_Customer_Collection
  123. */
  124. public function addProductFilter($productId)
  125. {
  126. $this->getSelect()
  127. ->where('tr.product_id = ?', $productId);
  128. return $this;
  129. }
  130. /**
  131. * Apply filter by store id(s).
  132. *
  133. * @param int|array $storeId
  134. * @return Mage_Tag_Model_Resource_Customer_Collection
  135. */
  136. public function addStoreFilter($storeId)
  137. {
  138. $this->getSelect()->where('tr.store_id IN (?)', $storeId);
  139. return $this;
  140. }
  141. /**
  142. * Adds filter by status
  143. *
  144. * @param int $status
  145. * @return Mage_Tag_Model_Resource_Customer_Collection
  146. */
  147. public function addStatusFilter($status)
  148. {
  149. $this->getSelect()
  150. ->where('t.status = ?', $status);
  151. return $this;
  152. }
  153. /**
  154. * Adds desc order by tag relation id
  155. *
  156. * @return Mage_Tag_Model_Resource_Customer_Collection
  157. */
  158. public function addDescOrder()
  159. {
  160. $this->getSelect()
  161. ->order('tr.tag_relation_id desc');
  162. return $this;
  163. }
  164. /**
  165. * Adds grouping by tag id
  166. *
  167. * @return Mage_Tag_Model_Resource_Customer_Collection
  168. */
  169. public function addGroupByTag()
  170. {
  171. $this->getSelect()
  172. ->group('tr.tag_id');
  173. /*
  174. * Allow analytic functions usage
  175. */
  176. $this->_useAnalyticFunction = true;
  177. $this->_allowDisableGrouping = true;
  178. return $this;
  179. }
  180. /**
  181. * Adds grouping by customer id
  182. *
  183. * @return Mage_Tag_Model_Resource_Customer_Collection
  184. */
  185. public function addGroupByCustomer()
  186. {
  187. $this->getSelect()
  188. ->group('tr.customer_id');
  189. $this->_allowDisableGrouping = false;
  190. return $this;
  191. }
  192. /**
  193. * Disables grouping
  194. *
  195. * @return Mage_Tag_Model_Resource_Customer_Collection
  196. */
  197. public function addGroupByCustomerProduct()
  198. {
  199. // Nothing need to group
  200. $this->_allowDisableGrouping = false;
  201. return $this;
  202. }
  203. /**
  204. * Adds filter by customer id
  205. *
  206. * @param int $customerId
  207. * @return Mage_Tag_Model_Resource_Customer_Collection
  208. */
  209. public function addCustomerFilter($customerId)
  210. {
  211. $this->getSelect()->where('tr.customer_id = ?', $customerId);
  212. return $this;
  213. }
  214. /**
  215. * Joins tables to select
  216. *
  217. */
  218. protected function _joinFields()
  219. {
  220. $tagRelationTable = $this->getTable('tag/relation');
  221. $tagTable = $this->getTable('tag/tag');
  222. //TODO: add full name logic
  223. $this->addAttributeToSelect('firstname')
  224. ->addAttributeToSelect('lastname')
  225. ->addAttributeToSelect('email');
  226. $this->getSelect()
  227. ->join(
  228. array('tr' => $tagRelationTable),
  229. 'tr.customer_id = e.entity_id',
  230. array('tag_relation_id', 'product_id', 'active')
  231. )
  232. ->join(array('t' => $tagTable), 't.tag_id = tr.tag_id', array('*'));
  233. }
  234. /**
  235. * Gets number of rows
  236. *
  237. * @return Varien_Db_Select
  238. */
  239. public function getSelectCountSql()
  240. {
  241. $countSelect = parent::getSelectCountSql();
  242. if ($this->_allowDisableGrouping) {
  243. $countSelect->reset(Zend_Db_Select::COLUMNS);
  244. $countSelect->reset(Zend_Db_Select::GROUP);
  245. $countSelect->columns('COUNT(DISTINCT ' . $this->getCountAttribute() . ')');
  246. }
  247. return $countSelect;
  248. }
  249. /**
  250. * Adds Product names to select
  251. *
  252. * @return Mage_Tag_Model_Resource_Customer_Collection
  253. */
  254. public function addProductName()
  255. {
  256. $productsId = array();
  257. $productsData = array();
  258. foreach ($this->getItems() as $item) {
  259. $productsId[] = $item->getProductId();
  260. }
  261. $productsId = array_unique($productsId);
  262. /* small fix */
  263. if ( sizeof($productsId) == 0 ) {
  264. return;
  265. }
  266. $collection = Mage::getModel('catalog/product')->getCollection()
  267. ->addAttributeToSelect('name')
  268. ->addAttributeToSelect('sku')
  269. ->addIdFilter($productsId);
  270. $collection->load();
  271. foreach ($collection->getItems() as $item) {
  272. $productsData[$item->getId()] = $item->getName();
  273. $productsSku[$item->getId()] = $item->getSku();
  274. }
  275. foreach ($this->getItems() as $item) {
  276. $item->setProduct($productsData[$item->getProductId()]);
  277. $item->setProductSku($productsSku[$item->getProductId()]);
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Sets order by attribute
  283. *
  284. * @param string $attribute
  285. * @param string $dir
  286. * @return Mage_Tag_Model_Resource_Customer_Collection
  287. */
  288. public function setOrder($attribute, $dir = 'desc')
  289. {
  290. switch( $attribute ) {
  291. case 'name':
  292. case 'status':
  293. $this->getSelect()->order($attribute . ' ' . $dir);
  294. break;
  295. default:
  296. parent::setOrder($attribute, $dir);
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Sets attribute for count
  302. *
  303. * @param string $value
  304. * @return Mage_Tag_Model_Resource_Customer_Collection
  305. */
  306. public function setCountAttribute($value)
  307. {
  308. $this->_countAttribute = $value;
  309. return $this;
  310. }
  311. /**
  312. * Gets attribure for count
  313. *
  314. * @return string
  315. */
  316. public function getCountAttribute()
  317. {
  318. return $this->_countAttribute;
  319. }
  320. /**
  321. * Adds field to filter
  322. *
  323. * @param string $attribute
  324. * @param array $condition
  325. * @return Mage_Tag_Model_Resource_Customer_Collection
  326. */
  327. public function addFieldToFilter($attribute, $condition = null)
  328. {
  329. if ($attribute == 'name') {
  330. $where = $this->_getConditionSql('t.name', $condition);
  331. $this->getSelect()->where($where, null, Varien_Db_Select::TYPE_CONDITION);
  332. return $this;
  333. } else {
  334. return parent::addFieldToFilter($attribute, $condition);
  335. }
  336. }
  337. }