PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Mysql4/Design.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 165 lines | 120 code | 20 blank | 25 comment | 25 complexity | 9971dc22152f19b125b6330eb64dd5b0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Core
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Core_Model_Mysql4_Design extends Mage_Core_Model_Mysql4_Abstract
  27. {
  28. protected function _construct()
  29. {
  30. $this->_init('core/design_change', 'design_change_id');
  31. }
  32. public function _beforeSave(Mage_Core_Model_Abstract $object)
  33. {
  34. $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  35. if ($date = $object->getDateFrom()) {
  36. $date = Mage::app()->getLocale()->date($date, $format, null, false);
  37. $object->setDateFrom($date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
  38. } else {
  39. $object->setDateFrom(null);
  40. }
  41. if ($date = $object->getDateTo()) {
  42. $date = Mage::app()->getLocale()->date($date, $format, null, false);
  43. $object->setDateTo($date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
  44. } else {
  45. $object->setDateTo(null);
  46. }
  47. if (!is_null($object->getDateFrom()) && !is_null($object->getDateTo()) && strtotime($object->getDateFrom()) > strtotime($object->getDateTo())){
  48. Mage::throwException(Mage::helper('core')->__('Start date cannot be greater than end date.'));
  49. }
  50. $check = $this->_checkIntersection(
  51. $object->getStoreId(),
  52. $object->getDateFrom(),
  53. $object->getDateTo(),
  54. $object->getId()
  55. );
  56. if ($check){
  57. Mage::throwException(Mage::helper('core')->__('Your design change for the specified store intersects with another one, please specify another date range.'));
  58. }
  59. if (is_null($object->getDateFrom()))
  60. $object->setDateFrom(new Zend_Db_Expr('null'));
  61. if (is_null($object->getDateTo()))
  62. $object->setDateTo(new Zend_Db_Expr('null'));
  63. parent::_beforeSave($object);
  64. }
  65. private function _checkIntersection($storeId, $dateFrom, $dateTo, $currentId)
  66. {
  67. $condition = '(date_to is null AND date_from is null)';
  68. if (!is_null($dateFrom)) {
  69. $condition .= '
  70. OR
  71. (? between date_from and date_to)
  72. OR
  73. (? >= date_from and date_to is null)
  74. OR
  75. (? <= date_to and date_from is null)
  76. ';
  77. } else {
  78. $condition .= '
  79. OR
  80. (date_from is null)
  81. ';
  82. }
  83. if (!is_null($dateTo)) {
  84. $condition .= '
  85. OR
  86. (# between date_from and date_to)
  87. OR
  88. (# >= date_from and date_to is null)
  89. OR
  90. (# <= date_to and date_from is null)
  91. ';
  92. } else {
  93. $condition .= '
  94. OR
  95. (date_to is null)
  96. ';
  97. }
  98. if (is_null($dateFrom) && !is_null($dateTo)) {
  99. $condition .= '
  100. OR
  101. (date_to <= # or date_from <= #)
  102. ';
  103. }
  104. if (!is_null($dateFrom) && is_null($dateTo)) {
  105. $condition .= '
  106. OR
  107. (date_to >= ? or date_from >= ?)
  108. ';
  109. }
  110. if (!is_null($dateFrom) && !is_null($dateTo)) {
  111. $condition .= '
  112. OR
  113. (date_from between ? and #)
  114. OR
  115. (date_to between ? and #)
  116. ';
  117. } else if (is_null($dateFrom) && is_null($dateTo)) {
  118. $condition = false;
  119. }
  120. $select = $this->_getReadAdapter()->select()
  121. ->from(array('main_table'=>$this->getTable('design_change')))
  122. ->where('main_table.store_id = ?', $storeId)
  123. ->where('main_table.design_change_id <> ?', $currentId);
  124. if ($condition) {
  125. $condition = $this->_getReadAdapter()->quoteInto($condition, $dateFrom);
  126. $condition = str_replace('#', '?', $condition);
  127. $condition = $this->_getReadAdapter()->quoteInto($condition, $dateTo);
  128. $select->where($condition);
  129. }
  130. return $this->_getReadAdapter()->fetchOne($select);
  131. }
  132. public function loadChange($storeId, $date = null)
  133. {
  134. if (is_null($date)) {
  135. //$date = new Zend_Db_Expr('NOW()');
  136. $date = now();
  137. }
  138. $select = $this->_getReadAdapter()->select()
  139. ->from(array('main_table'=>$this->getTable('design_change')))
  140. ->where('store_id = ?', $storeId)
  141. ->where('(date_from <= ? or date_from is null)', $date)
  142. ->where('(date_to >= ? or date_to is null)', $date);
  143. return $this->_getReadAdapter()->fetchRow($select);
  144. }
  145. }