Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Design.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  protected $dateTime;
20 
26  public function __construct(
27  \Magento\Framework\Model\ResourceModel\Db\Context $context,
29  $connectionName = null
30  ) {
31  $this->dateTime = $dateTime;
32  parent::__construct($context, $connectionName);
33  }
34 
40  protected function _construct()
41  {
42  $this->_init('design_change', 'design_change_id');
43  }
44 
52  public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
53  {
54  if ($date = $object->getDateFrom()) {
55  $object->setDateFrom($this->dateTime->formatDate($date));
56  } else {
57  $object->setDateFrom(null);
58  }
59 
60  if ($date = $object->getDateTo()) {
61  $object->setDateTo($this->dateTime->formatDate($date));
62  } else {
63  $object->setDateTo(null);
64  }
65 
66  if ($object->getDateFrom() !== null
67  && $object->getDateTo() !== null
68  && (new \DateTime($object->getDateFrom()))->getTimestamp()
69  > (new \DateTime($object->getDateTo()))->getTimestamp()
70  ) {
71  throw new \Magento\Framework\Exception\LocalizedException(
72  __('The start date can\'t follow the end date.')
73  );
74  }
75 
76  $check = $this->_checkIntersection(
77  $object->getStoreId(),
78  $object->getDateFrom(),
79  $object->getDateTo(),
80  $object->getId()
81  );
82 
83  if ($check) {
84  throw new \Magento\Framework\Exception\LocalizedException(
85  __('The date range for this design change overlaps another design change for the specified store.')
86  );
87  }
88 
89  if ($object->getDateFrom() === null) {
90  $object->setDateFrom(new \Zend_Db_Expr('null'));
91  }
92  if ($object->getDateTo() === null) {
93  $object->setDateTo(new \Zend_Db_Expr('null'));
94  }
95 
96  parent::_beforeSave($object);
97  }
98 
111  protected function _checkIntersection($storeId, $dateFrom, $dateTo, $currentId)
112  {
113  $connection = $this->getConnection();
114  $select = $connection->select()->from(
115  ['main_table' => $this->getTable('design_change')]
116  )->where(
117  'main_table.store_id = :store_id'
118  )->where(
119  'main_table.design_change_id <> :current_id'
120  );
121 
122  $dateConditions = ['date_to IS NULL AND date_from IS NULL'];
123 
124  if ($dateFrom !== null) {
125  $dateConditions[] = ':date_from BETWEEN date_from AND date_to';
126  $dateConditions[] = ':date_from >= date_from and date_to IS NULL';
127  $dateConditions[] = ':date_from <= date_to and date_from IS NULL';
128  } else {
129  $dateConditions[] = 'date_from IS NULL';
130  }
131 
132  if ($dateTo !== null) {
133  $dateConditions[] = ':date_to BETWEEN date_from AND date_to';
134  $dateConditions[] = ':date_to >= date_from AND date_to IS NULL';
135  $dateConditions[] = ':date_to <= date_to AND date_from IS NULL';
136  } else {
137  $dateConditions[] = 'date_to IS NULL';
138  }
139 
140  if ($dateFrom === null && $dateTo !== null) {
141  $dateConditions[] = 'date_to <= :date_to OR date_from <= :date_to';
142  }
143 
144  if ($dateFrom !== null && $dateTo === null) {
145  $dateConditions[] = 'date_to >= :date_from OR date_from >= :date_from';
146  }
147 
148  if ($dateFrom !== null && $dateTo !== null) {
149  $dateConditions[] = 'date_from BETWEEN :date_from AND :date_to';
150  $dateConditions[] = 'date_to BETWEEN :date_from AND :date_to';
151  } elseif ($dateFrom === null && $dateTo === null) {
152  $dateConditions = [];
153  }
154 
155  $condition = '';
156  if (!empty($dateConditions)) {
157  $condition = '(' . implode(') OR (', $dateConditions) . ')';
158  $select->where($condition);
159  }
160 
161  $bind = ['store_id' => (int)$storeId, 'current_id' => (int)$currentId];
162 
163  if ($dateTo !== null) {
164  $bind['date_to'] = $dateTo;
165  }
166  if ($dateFrom !== null) {
167  $bind['date_from'] = $dateFrom;
168  }
169 
170  $result = $connection->fetchOne($select, $bind);
171  return $result;
172  }
173 
181  public function loadChange($storeId, $date)
182  {
183  $select = $this->getConnection()->select()->from(
184  ['main_table' => $this->getTable('design_change')]
185  )->where(
186  'store_id = :store_id'
187  )->where(
188  'date_from <= :required_date or date_from IS NULL'
189  )->where(
190  'date_to >= :required_date or date_to IS NULL'
191  );
192 
193  $bind = ['store_id' => (int)$storeId, 'required_date' => $date];
194 
195  return $this->getConnection()->fetchRow($select, $bind);
196  }
197 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Design.php:52
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, DateTime $dateTime, $connectionName=null)
Definition: Design.php:26
$connection
Definition: bulk.php:13
_checkIntersection($storeId, $dateFrom, $dateTo, $currentId)
Definition: Design.php:111