PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/vincent.perdereau/picandparts
PHP | 411 lines | 189 code | 44 blank | 178 comment | 8 complexity | 5010a57b9a4e8bf17f7b5923e0a9093a 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Tag
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  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('middlename')
  225. ->addAttributeToSelect('lastname')
  226. ->addAttributeToSelect('email');
  227. $this->getSelect()
  228. ->join(
  229. array('tr' => $tagRelationTable),
  230. 'tr.customer_id = e.entity_id',
  231. array('tag_relation_id', 'product_id', 'active', 'added_in' => 'store_id')
  232. )
  233. ->join(array('t' => $tagTable), 't.tag_id = tr.tag_id', array('*'));
  234. }
  235. /**
  236. * Gets number of rows
  237. *
  238. * @return Varien_Db_Select
  239. */
  240. public function getSelectCountSql()
  241. {
  242. $countSelect = parent::getSelectCountSql();
  243. if ($this->_allowDisableGrouping) {
  244. $countSelect->reset(Zend_Db_Select::COLUMNS);
  245. $countSelect->reset(Zend_Db_Select::GROUP);
  246. $countSelect->columns('COUNT(DISTINCT ' . $this->getCountAttribute() . ')');
  247. }
  248. return $countSelect;
  249. }
  250. /**
  251. * Adds Product names to item
  252. *
  253. * @return Mage_Tag_Model_Resource_Customer_Collection
  254. */
  255. public function addProductName()
  256. {
  257. $productsId = array();
  258. $productsData = array();
  259. foreach ($this->getItems() as $item) {
  260. $productsId[] = $item->getProductId();
  261. }
  262. $productsId = array_unique($productsId);
  263. /* small fix */
  264. if ( sizeof($productsId) == 0 ) {
  265. return;
  266. }
  267. $collection = Mage::getModel('catalog/product')->getCollection()
  268. ->addAttributeToSelect('name')
  269. ->addAttributeToSelect('sku')
  270. ->addIdFilter($productsId);
  271. $collection->load();
  272. foreach ($collection->getItems() as $item) {
  273. $productsData[$item->getId()] = $item->getName();
  274. $productsSku[$item->getId()] = $item->getSku();
  275. }
  276. foreach ($this->getItems() as $item) {
  277. $item->setProduct($productsData[$item->getProductId()]);
  278. $item->setProductSku($productsSku[$item->getProductId()]);
  279. }
  280. return $this;
  281. }
  282. /**
  283. * Adds Product names to select
  284. *
  285. * @return Mage_Tag_Model_Resource_Customer_Collection
  286. */
  287. public function addProductToSelect()
  288. {
  289. $resource = Mage::getModel('catalog/product')->getResource();
  290. // add product attributes to select
  291. foreach (array('name' => 'value') as $field => $fieldName) {
  292. $attr = $resource->getAttribute($field);
  293. $this->_select->joinLeft(
  294. array($field => $attr->getBackend()->getTable()),
  295. 'tr.product_id = ' . $field . '.entity_id AND ' . $field . '.attribute_id = ' . $attr->getId(),
  296. array('product_' . $field => $fieldName)
  297. );
  298. }
  299. // add product fields
  300. $this->_select->joinLeft(
  301. array('p' => $this->getTable('catalog/product')),
  302. 'tr.product_id = p.entity_id',
  303. array('product_sku' => 'sku')
  304. );
  305. return $this;
  306. }
  307. /**
  308. * Sets attribute for count
  309. *
  310. * @param string $value
  311. * @return Mage_Tag_Model_Resource_Customer_Collection
  312. */
  313. public function setCountAttribute($value)
  314. {
  315. $this->_countAttribute = $value;
  316. return $this;
  317. }
  318. /**
  319. * Gets attribure for count
  320. *
  321. * @return string
  322. */
  323. public function getCountAttribute()
  324. {
  325. return $this->_countAttribute;
  326. }
  327. /**
  328. * Adds field to filter
  329. *
  330. * @param string $attribute
  331. * @param array $condition
  332. * @return Mage_Tag_Model_Resource_Customer_Collection
  333. */
  334. public function addFieldToFilter($attribute, $condition = null)
  335. {
  336. if ($attribute == 'name') {
  337. $where = $this->_getConditionSql('t.name', $condition);
  338. $this->getSelect()->where($where, null, Varien_Db_Select::TYPE_CONDITION);
  339. return $this;
  340. } else {
  341. return parent::addFieldToFilter($attribute, $condition);
  342. }
  343. }
  344. /**
  345. * Treat "order by" items as attributes to sort
  346. *
  347. * @return Mage_Tag_Model_Resource_Customer_Collection
  348. */
  349. protected function _renderOrders()
  350. {
  351. if (!$this->_isOrdersRendered) {
  352. parent::_renderOrders();
  353. $orders = $this->getSelect()
  354. ->getPart(Zend_Db_Select::ORDER);
  355. $appliedOrders = array();
  356. foreach ($orders as $order) {
  357. $appliedOrders[$order[0]] = true;
  358. }
  359. foreach ($this->_orders as $field => $direction) {
  360. if (empty($appliedOrders[$field])) {
  361. $this->_select->order(new Zend_Db_Expr($field . ' ' . $direction));
  362. }
  363. }
  364. }
  365. return $this;
  366. }
  367. }