00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Core_Model_Mysql4_Design extends Mage_Core_Model_Mysql4_Abstract
00029 {
00030 protected function _construct()
00031 {
00032 $this->_init('core/design_change', 'design_change_id');
00033 }
00034
00035 public function _beforeSave(Mage_Core_Model_Abstract $object)
00036 {
00037 $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00038 if ($date = $object->getDateFrom()) {
00039 $date = Mage::app()->getLocale()->date($date, $format, null, false);
00040 $object->setDateFrom($date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
00041 } else {
00042 $object->setDateFrom(null);
00043 }
00044
00045 if ($date = $object->getDateTo()) {
00046 $date = Mage::app()->getLocale()->date($date, $format, null, false);
00047 $object->setDateTo($date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
00048 } else {
00049 $object->setDateTo(null);
00050 }
00051
00052 if (!is_null($object->getDateFrom()) && !is_null($object->getDateTo()) && strtotime($object->getDateFrom()) > strtotime($object->getDateTo())){
00053 Mage::throwException(Mage::helper('core')->__('Start date can\'t be greater than end date'));
00054 }
00055
00056 $check = $this->_checkIntersection(
00057 $object->getStoreId(),
00058 $object->getDateFrom(),
00059 $object->getDateTo(),
00060 $object->getId()
00061 );
00062
00063 if ($check){
00064 Mage::throwException(Mage::helper('core')->__('Your design change for the specified store intersects with another one, please specify another date range'));
00065 }
00066
00067 if (is_null($object->getDateFrom()))
00068 $object->setDateFrom(new Zend_Db_Expr('null'));
00069 if (is_null($object->getDateTo()))
00070 $object->setDateTo(new Zend_Db_Expr('null'));
00071
00072 parent::_beforeSave($object);
00073 }
00074
00075 private function _checkIntersection($storeId, $dateFrom, $dateTo, $currentId)
00076 {
00077 $condition = '(date_to is null AND date_from is null)';
00078 if (!is_null($dateFrom)) {
00079 $condition .= '
00080 OR
00081 (? between date_from and date_to)
00082 OR
00083 (? >= date_from and date_to is null)
00084 OR
00085 (? <= date_to and date_from is null)
00086 ';
00087 } else {
00088 $condition .= '
00089 OR
00090 (date_from is null)
00091 ';
00092 }
00093
00094 if (!is_null($dateTo)) {
00095 $condition .= '
00096 OR
00097 (# between date_from and date_to)
00098 OR
00099 (# >= date_from and date_to is null)
00100 OR
00101 (# <= date_to and date_from is null)
00102 ';
00103 } else {
00104 $condition .= '
00105 OR
00106 (date_to is null)
00107 ';
00108 }
00109
00110 if (is_null($dateFrom) && !is_null($dateTo)) {
00111 $condition .= '
00112 OR
00113 (date_to <= # or date_from <= #)
00114 ';
00115 }
00116 if (!is_null($dateFrom) && is_null($dateTo)) {
00117 $condition .= '
00118 OR
00119 (date_to >= ? or date_from >= ?)
00120 ';
00121 }
00122
00123 if (!is_null($dateFrom) && !is_null($dateTo)) {
00124 $condition .= '
00125 OR
00126 (date_from between ? and #)
00127 OR
00128 (date_to between ? and #)
00129 ';
00130 } else if (is_null($dateFrom) && is_null($dateTo)) {
00131 $condition = false;
00132 }
00133
00134 $select = $this->_getReadAdapter()->select()
00135 ->from(array('main_table'=>$this->getTable('design_change')))
00136 ->where('main_table.store_id = ?', $storeId)
00137 ->where('main_table.design_change_id <> ?', $currentId);
00138
00139 if ($condition) {
00140 $condition = $this->_getReadAdapter()->quoteInto($condition, $dateFrom);
00141 $condition = str_replace('#', '?', $condition);
00142 $condition = $this->_getReadAdapter()->quoteInto($condition, $dateTo);
00143
00144 $select->where($condition);
00145 }
00146
00147 return $this->_getReadAdapter()->fetchOne($select);
00148 }
00149
00150 public function loadChange($storeId, $date = null)
00151 {
00152 if (is_null($date)) {
00153
00154 $date = now();
00155 }
00156
00157 $select = $this->_getReadAdapter()->select()
00158 ->from(array('main_table'=>$this->getTable('design_change')))
00159 ->where('store_id = ?', $storeId)
00160 ->where('(date_from <= ? or date_from is null)', $date)
00161 ->where('(date_to >= ? or date_to is null)', $date);
00162
00163 return $this->_getReadAdapter()->fetchRow($select);
00164 }
00165 }