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_Rule
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: /**
28: * Abstract Rule entity resource collection model
29: *
30: * @category Mage
31: * @package Mage_Rule
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Rule_Model_Resource_Rule_Collection_Abstract
35: extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Store associated with rule entities information map
39: *
40: * Example:
41: * array(
42: * 'entity_type1' => array(
43: * 'associations_table' => 'table_name',
44: * 'rule_id_field' => 'rule_id',
45: * 'entity_id_field' => 'entity_id'
46: * ),
47: * 'entity_type2' => array(
48: * 'associations_table' => 'table_name',
49: * 'rule_id_field' => 'rule_id',
50: * 'entity_id_field' => 'entity_id'
51: * )
52: * ....
53: * )
54: *
55: * @var array
56: */
57: protected $_associatedEntitiesMap = array();
58:
59: /**
60: * Quote rule environment
61: *
62: * @deprecated after 1.6.1.0
63: *
64: * @var Mage_Rule_Model_Environment
65: */
66: protected $_env;
67:
68: /**
69: * Add website ids to rules data
70: *
71: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
72: */
73: protected function _afterLoad()
74: {
75: parent::_afterLoad();
76: if ($this->getFlag('add_websites_to_result') && $this->_items) {
77: /** @var Mage_Rule_Model_Abstract $item */
78: foreach ($this->_items as $item) {
79: $item->afterLoad();
80: }
81: }
82:
83: return $this;
84: }
85:
86: /**
87: * Init flag for adding rule website ids to collection result
88: *
89: * @param bool|null $flag
90: *
91: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
92: */
93: public function addWebsitesToResult($flag = null)
94: {
95: $flag = ($flag === null) ? true : $flag;
96: $this->setFlag('add_websites_to_result', $flag);
97: return $this;
98: }
99:
100: /**
101: * Limit rules collection by specific websites
102: *
103: * @param int|array|Mage_Core_Model_Website $websiteId
104: *
105: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
106: */
107: public function addWebsiteFilter($websiteId)
108: {
109: $entityInfo = $this->_getAssociatedEntityInfo('website');
110: if (!$this->getFlag('is_website_table_joined')) {
111: $this->setFlag('is_website_table_joined', true);
112: if ($websiteId instanceof Mage_Core_Model_Website) {
113: $websiteId = $websiteId->getId();
114: }
115:
116: $subSelect = $this->getConnection()->select()
117: ->from(array('website' => $this->getTable($entityInfo['associations_table'])), '')
118: ->where('website.' . $entityInfo['entity_id_field'] . ' IN (?)', $websiteId);
119: $this->getSelect()->exists(
120: $subSelect,
121: 'main_table.' . $entityInfo['rule_id_field'] . ' = website.' . $entityInfo['rule_id_field']
122: );
123: }
124: return $this;
125: }
126:
127: /**
128: * Provide support for website id filter
129: *
130: * @param string $field
131: * @param mixed $condition
132: *
133: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
134: */
135: public function addFieldToFilter($field, $condition = null)
136: {
137: if ($field == 'website_ids') {
138: return $this->addWebsiteFilter($condition);
139: }
140:
141: parent::addFieldToFilter($field, $condition);
142: return $this;
143: }
144:
145: /**
146: * Filter collection to only active or inactive rules
147: *
148: * @param int $isActive
149: *
150: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
151: */
152: public function addIsActiveFilter($isActive = 1)
153: {
154: if (!$this->getFlag('is_active_filter')) {
155: $this->addFieldToFilter('is_active', (int)$isActive ? 1 : 0);
156: $this->setFlag('is_active_filter', true);
157: }
158: return $this;
159: }
160:
161: /**
162: * Retrieve correspondent entity information (associations table name, columns names)
163: * of rule's associated entity by specified entity type
164: *
165: * @param string $entityType
166: *
167: * @return array
168: */
169: protected function _getAssociatedEntityInfo($entityType)
170: {
171: if (isset($this->_associatedEntitiesMap[$entityType])) {
172: return $this->_associatedEntitiesMap[$entityType];
173: }
174:
175: $e = Mage::exception(
176: 'Mage_Core',
177: Mage::helper('rule')->__(
178: 'There is no information about associated entity type "%s".', $entityType
179: )
180: );
181: throw $e;
182: }
183:
184:
185:
186:
187:
188: /**
189: * Set environment for all rules in collection
190: *
191: * @deprecated after 1.6.2.0
192: *
193: * @param Mage_Rule_Model_Environment $env
194: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
195: */
196: public function setEnv(Mage_Rule_Model_Environment $env = null)
197: {
198: $this->_env = $env;
199: return $this;
200: }
201:
202: /**
203: * Retrieve environment for the rules in collection
204: *
205: * @deprecated after 1.6.2.0
206: *
207: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
208: */
209: public function getEnv()
210: {
211: return $this->_env;
212: }
213:
214: /**
215: * Set filter for the collection based on the environment
216: *
217: * @deprecated after 1.6.2.0
218: *
219: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
220: */
221: public function setActiveFilter()
222: {
223: return $this;
224: }
225:
226: /**
227: * Process the quote with all the rules in collection
228: *
229: * @deprecated after 1.6.2.0
230: *
231: * @return Mage_Rule_Model_Resource_Rule_Collection_Abstract
232: */
233: public function process()
234: {
235: return $this;
236: }
237: }
238: