Design.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\ResourceModel;
  7. use Magento\Framework\Stdlib\DateTime;
  8. /**
  9. * Design Change Resource Model
  10. */
  11. class Design extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  12. {
  13. /**
  14. * @var DateTime
  15. */
  16. protected $dateTime;
  17. /**
  18. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  19. * @param DateTime $dateTime
  20. * @param string $connectionName
  21. */
  22. public function __construct(
  23. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  24. DateTime $dateTime,
  25. $connectionName = null
  26. ) {
  27. $this->dateTime = $dateTime;
  28. parent::__construct($context, $connectionName);
  29. }
  30. /**
  31. * Define main table and primary key
  32. *
  33. * @return void
  34. */
  35. protected function _construct()
  36. {
  37. $this->_init('design_change', 'design_change_id');
  38. }
  39. /**
  40. * Perform actions before object save
  41. *
  42. * @param \Magento\Framework\Model\AbstractModel $object
  43. * @return $this
  44. * @throws \Magento\Framework\Exception\LocalizedException
  45. */
  46. public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  47. {
  48. if ($date = $object->getDateFrom()) {
  49. $object->setDateFrom($this->dateTime->formatDate($date));
  50. } else {
  51. $object->setDateFrom(null);
  52. }
  53. if ($date = $object->getDateTo()) {
  54. $object->setDateTo($this->dateTime->formatDate($date));
  55. } else {
  56. $object->setDateTo(null);
  57. }
  58. if ($object->getDateFrom() !== null
  59. && $object->getDateTo() !== null
  60. && (new \DateTime($object->getDateFrom()))->getTimestamp()
  61. > (new \DateTime($object->getDateTo()))->getTimestamp()
  62. ) {
  63. throw new \Magento\Framework\Exception\LocalizedException(
  64. __('The start date can\'t follow the end date.')
  65. );
  66. }
  67. $check = $this->_checkIntersection(
  68. $object->getStoreId(),
  69. $object->getDateFrom(),
  70. $object->getDateTo(),
  71. $object->getId()
  72. );
  73. if ($check) {
  74. throw new \Magento\Framework\Exception\LocalizedException(
  75. __('The date range for this design change overlaps another design change for the specified store.')
  76. );
  77. }
  78. if ($object->getDateFrom() === null) {
  79. $object->setDateFrom(new \Zend_Db_Expr('null'));
  80. }
  81. if ($object->getDateTo() === null) {
  82. $object->setDateTo(new \Zend_Db_Expr('null'));
  83. }
  84. parent::_beforeSave($object);
  85. }
  86. /**
  87. * Check intersections
  88. *
  89. * @param int $storeId
  90. * @param string $dateFrom
  91. * @param string $dateTo
  92. * @param int $currentId
  93. * @return string
  94. *
  95. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  96. * @SuppressWarnings(PHPMD.NPathComplexity)
  97. */
  98. protected function _checkIntersection($storeId, $dateFrom, $dateTo, $currentId)
  99. {
  100. $connection = $this->getConnection();
  101. $select = $connection->select()->from(
  102. ['main_table' => $this->getTable('design_change')]
  103. )->where(
  104. 'main_table.store_id = :store_id'
  105. )->where(
  106. 'main_table.design_change_id <> :current_id'
  107. );
  108. $dateConditions = ['date_to IS NULL AND date_from IS NULL'];
  109. if ($dateFrom !== null) {
  110. $dateConditions[] = ':date_from BETWEEN date_from AND date_to';
  111. $dateConditions[] = ':date_from >= date_from and date_to IS NULL';
  112. $dateConditions[] = ':date_from <= date_to and date_from IS NULL';
  113. } else {
  114. $dateConditions[] = 'date_from IS NULL';
  115. }
  116. if ($dateTo !== null) {
  117. $dateConditions[] = ':date_to BETWEEN date_from AND date_to';
  118. $dateConditions[] = ':date_to >= date_from AND date_to IS NULL';
  119. $dateConditions[] = ':date_to <= date_to AND date_from IS NULL';
  120. } else {
  121. $dateConditions[] = 'date_to IS NULL';
  122. }
  123. if ($dateFrom === null && $dateTo !== null) {
  124. $dateConditions[] = 'date_to <= :date_to OR date_from <= :date_to';
  125. }
  126. if ($dateFrom !== null && $dateTo === null) {
  127. $dateConditions[] = 'date_to >= :date_from OR date_from >= :date_from';
  128. }
  129. if ($dateFrom !== null && $dateTo !== null) {
  130. $dateConditions[] = 'date_from BETWEEN :date_from AND :date_to';
  131. $dateConditions[] = 'date_to BETWEEN :date_from AND :date_to';
  132. } elseif ($dateFrom === null && $dateTo === null) {
  133. $dateConditions = [];
  134. }
  135. $condition = '';
  136. if (!empty($dateConditions)) {
  137. $condition = '(' . implode(') OR (', $dateConditions) . ')';
  138. $select->where($condition);
  139. }
  140. $bind = ['store_id' => (int)$storeId, 'current_id' => (int)$currentId];
  141. if ($dateTo !== null) {
  142. $bind['date_to'] = $dateTo;
  143. }
  144. if ($dateFrom !== null) {
  145. $bind['date_from'] = $dateFrom;
  146. }
  147. $result = $connection->fetchOne($select, $bind);
  148. return $result;
  149. }
  150. /**
  151. * Load changes for specific store and date
  152. *
  153. * @param int $storeId
  154. * @param string $date
  155. * @return array
  156. */
  157. public function loadChange($storeId, $date)
  158. {
  159. $select = $this->getConnection()->select()->from(
  160. ['main_table' => $this->getTable('design_change')]
  161. )->where(
  162. 'store_id = :store_id'
  163. )->where(
  164. 'date_from <= :required_date or date_from IS NULL'
  165. )->where(
  166. 'date_to >= :required_date or date_to IS NULL'
  167. );
  168. $bind = ['store_id' => (int)$storeId, 'required_date' => $date];
  169. return $this->getConnection()->fetchRow($select, $bind);
  170. }
  171. }