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: abstract class Mage_Rule_Model_Resource_Abstract extends Mage_Core_Model_Resource_Db_Abstract
35: {
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: protected $_associatedEntitiesMap = array();
57:
58: 59: 60: 61: 62: 63: 64:
65: public function _beforeSave(Mage_Core_Model_Abstract $object)
66: {
67: $fromDate = $object->getFromDate();
68: if ($fromDate instanceof Zend_Date) {
69: $object->setFromDate($fromDate->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
70: } elseif (!is_string($fromDate) || empty($fromDate)) {
71: $object->setFromDate(null);
72: }
73:
74: $toDate = $object->getToDate();
75: if ($toDate instanceof Zend_Date) {
76: $object->setToDate($toDate->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
77: } elseif (!is_string($toDate) || empty($toDate)) {
78: $object->setToDate(null);
79: }
80:
81: parent::_beforeSave($object);
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92: 93:
94: public function bindRuleToEntity($ruleIds, $entityIds, $entityType)
95: {
96: if (empty($ruleIds) || empty($entityIds)) {
97: return $this;
98: }
99: $adapter = $this->_getWriteAdapter();
100: $entityInfo = $this->_getAssociatedEntityInfo($entityType);
101:
102: if (!is_array($ruleIds)) {
103: $ruleIds = array((int) $ruleIds);
104: }
105: if (!is_array($entityIds)) {
106: $entityIds = array((int) $entityIds);
107: }
108:
109: $data = array();
110: $count = 0;
111:
112: $adapter->beginTransaction();
113:
114: try {
115: foreach ($ruleIds as $ruleId) {
116: foreach ($entityIds as $entityId) {
117: $data[] = array(
118: $entityInfo['entity_id_field'] => $entityId,
119: $entityInfo['rule_id_field'] => $ruleId
120: );
121: $count++;
122: if (($count % 1000) == 0) {
123: $adapter->insertOnDuplicate(
124: $this->getTable($entityInfo['associations_table']),
125: $data,
126: array($entityInfo['rule_id_field'])
127: );
128: $data = array();
129: }
130: }
131: }
132: if (!empty($data)) {
133: $adapter->insertOnDuplicate(
134: $this->getTable($entityInfo['associations_table']),
135: $data,
136: array($entityInfo['rule_id_field'])
137: );
138: }
139:
140: $adapter->delete($this->getTable($entityInfo['associations_table']),
141: $adapter->quoteInto($entityInfo['rule_id_field'] . ' IN (?) AND ', $ruleIds) .
142: $adapter->quoteInto($entityInfo['entity_id_field'] . ' NOT IN (?)', $entityIds)
143: );
144: } catch (Exception $e) {
145: $adapter->rollback();
146: throw $e;
147:
148: }
149:
150: $adapter->commit();
151:
152: return $this;
153: }
154:
155: 156: 157: 158: 159: 160: 161: 162: 163:
164: public function unbindRuleFromEntity($ruleIds = array(), $entityIds = array(), $entityType)
165: {
166: $writeAdapter = $this->_getWriteAdapter();
167: $entityInfo = $this->_getAssociatedEntityInfo($entityType);
168:
169: if (!is_array($entityIds)) {
170: $entityIds = array((int) $entityIds);
171: }
172: if (!is_array($ruleIds)) {
173: $ruleIds = array((int) $ruleIds);
174: }
175:
176: $where = array();
177: if (!empty($ruleIds)) {
178: $where[] = $writeAdapter->quoteInto($entityInfo['rule_id_field'] . ' IN (?)', $ruleIds);
179: }
180: if (!empty($entityIds)) {
181: $where[] = $writeAdapter->quoteInto($entityInfo['entity_id_field'] . ' IN (?)', $entityIds);
182: }
183:
184: $writeAdapter->delete($this->getTable($entityInfo['associations_table']), implode(' AND ', $where));
185:
186: return $this;
187: }
188:
189: 190: 191: 192: 193: 194: 195: 196:
197: public function getAssociatedEntityIds($ruleId, $entityType)
198: {
199: $entityInfo = $this->_getAssociatedEntityInfo($entityType);
200:
201: $select = $this->_getReadAdapter()->select()
202: ->from($this->getTable($entityInfo['associations_table']), array($entityInfo['entity_id_field']))
203: ->where($entityInfo['rule_id_field'] . ' = ?', $ruleId);
204:
205: return $this->_getReadAdapter()->fetchCol($select);
206: }
207:
208: 209: 210: 211: 212: 213:
214: public function getWebsiteIds($ruleId)
215: {
216: return $this->getAssociatedEntityIds($ruleId, 'website');
217: }
218:
219: 220: 221: 222: 223: 224:
225: public function getCustomerGroupIds($ruleId)
226: {
227: return $this->getAssociatedEntityIds($ruleId, 'customer_group');
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237:
238: protected function _getAssociatedEntityInfo($entityType)
239: {
240: if (isset($this->_associatedEntitiesMap[$entityType])) {
241: return $this->_associatedEntitiesMap[$entityType];
242: }
243:
244: $e = Mage::exception(
245: 'Mage_Core',
246: Mage::helper('rule')->__(
247: 'There is no information about associated entity type "%s".', $entityType
248: )
249: );
250: throw $e;
251: }
252: }
253: