/app/code/core/Enterprise/GiftWrapping/Model/Resource/Wrapping/Collection.php

https://bitbucket.org/kdms/sh-magento · PHP · 187 lines · 97 code · 14 blank · 76 comment · 9 complexity · 2f122c479a75ee129d895b02fab32802 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 Enterprise
  22. * @package Enterprise_GiftWrapping
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Gift Wrapping Collection
  28. *
  29. * @category Enterprise
  30. * @package Enterprise_GiftWrapping
  31. */
  32. class Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  33. extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * Intialize collection
  37. *
  38. * @return void
  39. */
  40. protected function _construct()
  41. {
  42. $this->_init('enterprise_giftwrapping/wrapping');
  43. $this->_map['fields']['wrapping_id'] = 'main_table.wrapping_id';
  44. }
  45. /**
  46. * Redeclare after load method to add website IDs to items
  47. *
  48. * @return Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  49. */
  50. protected function _afterLoad()
  51. {
  52. parent::_afterLoad();
  53. if ($this->getFlag('add_websites_to_result') && $this->_items) {
  54. $select = $this->getConnection()->select()
  55. ->from($this->getTable('enterprise_giftwrapping/website'), array(
  56. 'wrapping_id',
  57. 'website_id'
  58. ))
  59. ->where('wrapping_id IN (?)', array_keys($this->_items));
  60. $websites = $this->getConnection()->fetchAll($select);
  61. foreach ($this->_items as $item) {
  62. $websiteIds = array();
  63. foreach ($websites as $website) {
  64. if ($item->getId() == $website['wrapping_id']) {
  65. $websiteIds[] = $website['website_id'];
  66. }
  67. }
  68. if (count($websiteIds)) {
  69. $item->setWebsiteIds($websiteIds);
  70. }
  71. }
  72. }
  73. return $this;
  74. }
  75. /**
  76. * Init flag for adding wrapping website ids to collection result
  77. *
  78. * @param bool|null $flag
  79. * @return Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  80. */
  81. public function addWebsitesToResult($flag = null)
  82. {
  83. $flag = ($flag === null) ? true : $flag;
  84. $this->setFlag('add_websites_to_result', $flag);
  85. return $this;
  86. }
  87. /**
  88. * Limit gift wrapping collection by specific website
  89. *
  90. * @param int|array|Mage_Core_Model_Website $websiteId
  91. * @return Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  92. */
  93. public function applyWebsiteFilter($websiteId)
  94. {
  95. if (!$this->getFlag('is_website_table_joined')) {
  96. $this->setFlag('is_website_table_joined', true);
  97. $this->getSelect()->joinInner(
  98. array('website' => $this->getTable('enterprise_giftwrapping/website')),
  99. 'main_table.wrapping_id = website.wrapping_id',
  100. array()
  101. );
  102. }
  103. if ($websiteId instanceof Mage_Core_Model_Website) {
  104. $websiteId = $websiteId->getId();
  105. }
  106. $this->getSelect()->where('website.website_id IN (?)', $websiteId);
  107. return $this;
  108. }
  109. /**
  110. * Limit gift wrapping collection by status
  111. *
  112. * @return Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  113. */
  114. public function applyStatusFilter()
  115. {
  116. $this->getSelect()->where('main_table.status = 1');
  117. return $this;
  118. }
  119. /**
  120. * Add specified field to collection filter
  121. * Redeclared in order to be able to limit collection by specific website
  122. * @see self::applyWebsiteFilter()
  123. *
  124. * @param string $field
  125. * @param mixed $condition
  126. * @return Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  127. */
  128. public function addFieldToFilter($field, $condition = null)
  129. {
  130. if ($field == 'website_ids') {
  131. return $this->applyWebsiteFilter($condition);
  132. }
  133. return parent::addFieldToFilter($field, $condition);
  134. }
  135. /**
  136. * Convert collection to array for select options
  137. *
  138. * @return array
  139. */
  140. public function toOptionArray()
  141. {
  142. return array_merge(array(array(
  143. 'value' => '',
  144. 'label' => Mage::helper('enterprise_giftwrapping')->__('Please select')
  145. )), $this->_toOptionArray('wrapping_id', 'design'));
  146. }
  147. /* Add store attributes to collection
  148. *
  149. * @param int $storeId
  150. * @return Enterprise_GiftWrapping_Model_Resource_Wrapping_Collection
  151. */
  152. public function addStoreAttributesToResult($storeId = 0)
  153. {
  154. $adapter = $this->getConnection();
  155. $select = $adapter->select();
  156. $select->from(array('m' => $this->getMainTable()), array('*'));
  157. $select->joinLeft(
  158. array('d' => $this->getTable('enterprise_giftwrapping/attribute')),
  159. 'd.wrapping_id = m.wrapping_id AND d.store_id = 0',
  160. array('')
  161. );
  162. $select->joinLeft(
  163. array('s' => $this->getTable('enterprise_giftwrapping/attribute')),
  164. 's.wrapping_id = m.wrapping_id AND s.store_id = ' . $storeId,
  165. array('design' => $adapter->getIfNullSql('s.design', 'd.design'))
  166. );
  167. $this->getSelect()->reset()->from(
  168. array('main_table' => $select),
  169. array('*')
  170. );
  171. return $this;
  172. }
  173. }