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_Core_Model_Resource_Design extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/design_change', 'design_change_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function _beforeSave(Mage_Core_Model_Abstract $object)
54: {
55: if ($date = $object->getDateFrom()) {
56: $object->setDateFrom($this->formatDate($date));
57: } else {
58: $object->setDateFrom(null);
59: }
60:
61: if ($date = $object->getDateTo()) {
62: $object->setDateTo($this->formatDate($date));
63: } else {
64: $object->setDateTo(null);
65: }
66:
67: if (!is_null($object->getDateFrom()) && !is_null($object->getDateTo())
68: && Varien_Date::toTimestamp($object->getDateFrom()) > Varien_Date::toTimestamp($object->getDateTo())) {
69: Mage::throwException(Mage::helper('core')->__('Start date cannot be greater than end date.'));
70: }
71:
72: $check = $this->_checkIntersection(
73: $object->getStoreId(),
74: $object->getDateFrom(),
75: $object->getDateTo(),
76: $object->getId()
77: );
78:
79: if ($check) {
80: Mage::throwException(
81: Mage::helper('core')->__('Your design change for the specified store intersects with another one, please specify another date range.'));
82: }
83:
84: if ($object->getDateFrom() === null)
85: $object->setDateFrom(new Zend_Db_Expr('null'));
86: if ($object->getDateTo() === null)
87: $object->setDateTo(new Zend_Db_Expr('null'));
88:
89: parent::_beforeSave($object);
90: }
91:
92:
93: 94: 95: 96: 97: 98: 99: 100: 101:
102: protected function _checkIntersection($storeId, $dateFrom, $dateTo, $currentId)
103: {
104: $adapter = $this->_getReadAdapter();
105: $select = $adapter->select()
106: ->from(array('main_table'=>$this->getTable('design_change')))
107: ->where('main_table.store_id = :store_id')
108: ->where('main_table.design_change_id <> :current_id');
109:
110: $dateConditions = array('date_to IS NULL AND date_from IS NULL');
111:
112: if (!is_null($dateFrom)) {
113: $dateConditions[] = ':date_from BETWEEN date_from AND date_to';
114: $dateConditions[] = ':date_from >= date_from and date_to IS NULL';
115: $dateConditions[] = ':date_from <= date_to and date_from IS NULL';
116: } else {
117: $dateConditions[] = 'date_from IS NULL';
118: }
119:
120: if (!is_null($dateTo)) {
121: $dateConditions[] = ':date_to BETWEEN date_from AND date_to';
122: $dateConditions[] = ':date_to >= date_from AND date_to IS NULL';
123: $dateConditions[] = ':date_to <= date_to AND date_from IS NULL';
124: } else {
125: $dateConditions[] = 'date_to IS NULL';
126: }
127:
128: if (is_null($dateFrom) && !is_null($dateTo)) {
129: $dateConditions[] = 'date_to <= :date_to OR date_from <= :date_to';
130: }
131:
132: if (!is_null($dateFrom) && is_null($dateTo)) {
133: $dateConditions[] = 'date_to >= :date_from OR date_from >= :date_from';
134: }
135:
136: if (!is_null($dateFrom) && !is_null($dateTo)) {
137: $dateConditions[] = 'date_from BETWEEN :date_from AND :date_to';
138: $dateConditions[] = 'date_to BETWEEN :date_from AND :date_to';
139: } elseif (is_null($dateFrom) && is_null($dateTo)) {
140: $dateConditions = array();
141: }
142:
143: $condition = '';
144: if (!empty($dateConditions)) {
145: $condition = '(' . implode(') OR (', $dateConditions) . ')';
146: $select->where($condition);
147: }
148:
149: $bind = array(
150: 'store_id' => (int)$storeId,
151: 'current_id' => (int)$currentId,
152: );
153:
154: if (!is_null($dateTo)) {
155: $bind['date_to'] = $dateTo;
156: }
157: if (!is_null($dateFrom)) {
158: $bind['date_from'] = $dateFrom;
159: }
160:
161: $result = $adapter->fetchOne($select, $bind);
162: return $result;
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function loadChange($storeId, $date = null)
173: {
174: if (is_null($date)) {
175: $date = Varien_Date::now();
176: }
177:
178: $select = $this->_getReadAdapter()->select()
179: ->from(array('main_table' => $this->getTable('design_change')))
180: ->where('store_id = :store_id')
181: ->where('date_from <= :required_date or date_from IS NULL')
182: ->where('date_to >= :required_date or date_to IS NULL');
183:
184: $bind = array(
185: 'store_id' => (int)$storeId,
186: 'required_date' => $date
187: );
188:
189: return $this->_getReadAdapter()->fetchRow($select, $bind);
190: }
191: }
192: