1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_SalesRule_Model_Resource_Rule extends Mage_Rule_Model_Resource_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_associatedEntitiesMap = array(
43: 'website' => array(
44: 'associations_table' => 'salesrule/website',
45: 'rule_id_field' => 'rule_id',
46: 'entity_id_field' => 'website_id'
47: ),
48: 'customer_group' => array(
49: 'associations_table' => 'salesrule/customer_group',
50: 'rule_id_field' => 'rule_id',
51: 'entity_id_field' => 'customer_group_id'
52: )
53: );
54:
55: 56: 57:
58: protected function _construct()
59: {
60: $this->_init('salesrule/rule', 'rule_id');
61: }
62:
63: 64: 65: 66: 67: 68: 69:
70: protected function _afterLoad(Mage_Core_Model_Abstract $object)
71: {
72: $object->setData('customer_group_ids', (array)$this->getCustomerGroupIds($object->getId()));
73: $object->setData('website_ids', (array)$this->getWebsiteIds($object->getId()));
74:
75: parent::_afterLoad($object);
76: return $this;
77: }
78:
79: 80: 81: 82: 83: 84: 85:
86: public function _beforeSave(Mage_Core_Model_Abstract $object)
87: {
88: if (!$object->getDiscountQty()) {
89: $object->setDiscountQty(new Zend_Db_Expr('NULL'));
90: }
91:
92: parent::_beforeSave($object);
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104:
105: protected function _afterSave(Mage_Core_Model_Abstract $object)
106: {
107: if ($object->hasStoreLabels()) {
108: $this->saveStoreLabels($object->getId(), $object->getStoreLabels());
109: }
110:
111: if ($object->hasWebsiteIds()) {
112: $websiteIds = $object->getWebsiteIds();
113: if (!is_array($websiteIds)) {
114: $websiteIds = explode(',', (string)$websiteIds);
115: }
116: $this->bindRuleToEntity($object->getId(), $websiteIds, 'website');
117: }
118:
119: if ($object->hasCustomerGroupIds()) {
120: $customerGroupIds = $object->getCustomerGroupIds();
121: if (!is_array($customerGroupIds)) {
122: $customerGroupIds = explode(',', (string)$customerGroupIds);
123: }
124: $this->bindRuleToEntity($object->getId(), $customerGroupIds, 'customer_group');
125: }
126:
127:
128: $ruleProductAttributes = array_merge(
129: $this->getProductAttributes(serialize($object->getConditions()->asArray())),
130: $this->getProductAttributes(serialize($object->getActions()->asArray()))
131: );
132: if (count($ruleProductAttributes)) {
133: $this->setActualProductAttributes($object, $ruleProductAttributes);
134: }
135:
136:
137: if ($object->getUseAutoGeneration() && $object->hasDataChanges()) {
138: Mage::getResourceModel('salesrule/coupon')->updateSpecificCoupons($object);
139: }
140: return parent::_afterSave($object);
141: }
142:
143: 144: 145: 146: 147: 148: 149: 150:
151: public function getCustomerUses($rule, $customerId)
152: {
153: $read = $this->_getReadAdapter();
154: $select = $read->select()->from($this->getTable('rule_customer'), array('cnt'=>'count(*)'))
155: ->where('rule_id = :rule_id')
156: ->where('customer_id = :customer_id');
157: return $read->fetchOne($select, array(':rule_id' => $rule->getRuleId(), ':customer_id' => $customerId));
158: }
159:
160: 161: 162: 163: 164: 165: 166: 167:
168: public function saveStoreLabels($ruleId, $labels)
169: {
170: $deleteByStoreIds = array();
171: $table = $this->getTable('salesrule/label');
172: $adapter = $this->_getWriteAdapter();
173:
174: $data = array();
175: foreach ($labels as $storeId => $label) {
176: if (Mage::helper('core/string')->strlen($label)) {
177: $data[] = array('rule_id' => $ruleId, 'store_id' => $storeId, 'label' => $label);
178: } else {
179: $deleteByStoreIds[] = $storeId;
180: }
181: }
182:
183: $adapter->beginTransaction();
184: try {
185: if (!empty($data)) {
186: $adapter->insertOnDuplicate(
187: $table,
188: $data,
189: array('label')
190: );
191: }
192:
193: if (!empty($deleteByStoreIds)) {
194: $adapter->delete($table, array(
195: 'rule_id=?' => $ruleId,
196: 'store_id IN (?)' => $deleteByStoreIds
197: ));
198: }
199: } catch (Exception $e) {
200: $adapter->rollback();
201: throw $e;
202:
203: }
204: $adapter->commit();
205:
206: return $this;
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function getStoreLabels($ruleId)
216: {
217: $select = $this->_getReadAdapter()->select()
218: ->from($this->getTable('salesrule/label'), array('store_id', 'label'))
219: ->where('rule_id = :rule_id');
220: return $this->_getReadAdapter()->fetchPairs($select, array(':rule_id' => $ruleId));
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: public function getStoreLabel($ruleId, $storeId)
231: {
232: $select = $this->_getReadAdapter()->select()
233: ->from($this->getTable('salesrule/label'), 'label')
234: ->where('rule_id = :rule_id')
235: ->where('store_id IN(0, :store_id)')
236: ->order('store_id DESC');
237: return $this->_getReadAdapter()->fetchOne($select, array(':rule_id' => $ruleId, ':store_id' => $storeId));
238: }
239:
240: 241: 242: 243: 244: 245: 246:
247: public function getActiveAttributes($websiteId, $customerGroupId)
248: {
249: $read = $this->_getReadAdapter();
250: $select = $read->select()
251: ->from(array('a' => $this->getTable('salesrule/product_attribute')),
252: new Zend_Db_Expr('DISTINCT ea.attribute_code'))
253: ->joinInner(array('ea' => $this->getTable('eav/attribute')), 'ea.attribute_id = a.attribute_id', array());
254: return $read->fetchAll($select);
255: }
256:
257: 258: 259: 260: 261: 262: 263:
264: public function setActualProductAttributes($rule, $attributes)
265: {
266: $write = $this->_getWriteAdapter();
267: $write->delete($this->getTable('salesrule/product_attribute'), array('rule_id=?' => $rule->getId()));
268:
269:
270: $attributeIds = array();
271: $select = $this->_getReadAdapter()->select()
272: ->from(array('a' => $this->getTable('eav/attribute')), array('a.attribute_id'))
273: ->where('a.attribute_code IN (?)', array($attributes));
274: $attributesFound = $this->_getReadAdapter()->fetchAll($select);
275: if ($attributesFound) {
276: foreach ($attributesFound as $attribute) {
277: $attributeIds[] = $attribute['attribute_id'];
278: }
279:
280: $data = array();
281: foreach ($rule->getCustomerGroupIds() as $customerGroupId) {
282: foreach ($rule->getWebsiteIds() as $websiteId) {
283: foreach ($attributeIds as $attribute) {
284: $data[] = array (
285: 'rule_id' => $rule->getId(),
286: 'website_id' => $websiteId,
287: 'customer_group_id' => $customerGroupId,
288: 'attribute_id' => $attribute
289: );
290: }
291: }
292: }
293: $write->insertMultiple($this->getTable('salesrule/product_attribute'), $data);
294: }
295:
296: return $this;
297: }
298:
299: 300: 301: 302: 303: 304: 305:
306: public function getProductAttributes($serializedString)
307: {
308: $result = array();
309: if (preg_match_all('~s:32:"salesrule/rule_condition_product";s:9:"attribute";s:\d+:"(.*?)"~s',
310: $serializedString, $matches)){
311: foreach ($matches[1] as $offset => $attributeCode) {
312: $result[] = $attributeCode;
313: }
314: }
315:
316: return $result;
317: }
318: }
319: