Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Status.php
Go to the documentation of this file.
1 <?php
8 
10 use Psr\Log\LoggerInterface as LogWriter;
13 use \Magento\Sales\Model\ResourceModel\EntityAbstract;
14 use \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
15 
22 {
28  protected $labelsTable;
29 
35  protected $stateTable;
36 
42  protected function _construct()
43  {
44  $this->_init('sales_order_status', 'status');
45  $this->_isPkAutoIncrement = false;
46  $this->labelsTable = $this->getTable('sales_order_status_label');
47  $this->stateTable = $this->getTable('sales_order_status_state');
48  }
49 
58  protected function _getLoadSelect($field, $value, $object)
59  {
60  if ($field == 'default_state') {
61  $select = $this->getConnection()->select()->from(
62  $this->getMainTable(),
63  ['label']
64  )->join(
65  ['state_table' => $this->stateTable],
66  $this->getMainTable() . '.status = state_table.status',
67  'status'
68  )->where(
69  'state_table.state = ?',
70  $value
71  )->order(
72  'state_table.is_default DESC'
73  )->limit(
74  1
75  );
76  } else {
77  $select = parent::_getLoadSelect($field, $value, $object);
78  }
79  return $select;
80  }
81 
88  public function getStoreLabels(\Magento\Sales\Model\Order\Status $status)
89  {
90  $select = $this->getConnection()->select()
91  ->from(['ssl' => $this->labelsTable], [])
92  ->where('status = ?', $status->getStatus())
93  ->columns([
94  'store_id',
95  'label',
96  ]);
97  return $this->getConnection()->fetchPairs($select);
98  }
99 
106  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
107  {
108  if ($object->hasStoreLabels()) {
109  $labels = $object->getStoreLabels();
110  $this->getConnection()->delete($this->labelsTable, ['status = ?' => $object->getStatus()]);
111  $data = [];
112  foreach ($labels as $storeId => $label) {
113  if (empty($label)) {
114  continue;
115  }
116  $data[] = ['status' => $object->getStatus(), 'store_id' => $storeId, 'label' => $label];
117  }
118  if (!empty($data)) {
119  $this->getConnection()->insertMultiple($this->labelsTable, $data);
120  }
121  }
122  return parent::_afterSave($object);
123  }
124 
134  public function assignState($status, $state, $isDefault, $visibleOnFront = false)
135  {
136  if ($isDefault) {
137  $this->getConnection()->update(
138  $this->stateTable,
139  ['is_default' => 0],
140  ['state = ?' => $state]
141  );
142  }
143  $this->getConnection()->insertOnDuplicate(
144  $this->stateTable,
145  [
146  'status' => $status,
147  'state' => $state,
148  'is_default' => (int)$isDefault,
149  'visible_on_front' => (int)$visibleOnFront
150  ]
151  );
152  return $this;
153  }
154 
163  public function unassignState($status, $state)
164  {
165  $this->getConnection()->beginTransaction();
166  try {
167  $isStateDefault = $this->checkIsStateDefault($state, $status);
168  $this->getConnection()->delete(
169  $this->stateTable,
170  [
171  'state = ?' => $state,
172  'status = ?' => $status
173  ]
174  );
175  if ($isStateDefault) {
176  $newDefaultStatus = $this->getStatusByState($state);
177  if ($newDefaultStatus) {
178  $this->getConnection()->update(
179  $this->stateTable,
180  ['is_default' => 1],
181  [
182  'state = ?' => $state,
183  'status = ?' => $newDefaultStatus
184  ]
185  );
186  }
187  }
188  $this->getConnection()->commit();
189  } catch (\Exception $e) {
190  $this->getConnection()->rollBack();
191  throw new LocalizedException(__('The status needs to remain assigned to its state.'));
192  }
193 
194  return $this;
195  }
196 
203  public function checkIsStateLast($state)
204  {
205  return (1 == $this->getConnection()->fetchOne(
206  $this->getConnection()->select()
207  ->from(['sss' => $this->stateTable], [])
208  ->where('state = ?', $state)
209  ->columns([new\Zend_Db_Expr('COUNT(1)')])
210  ));
211  }
212 
219  public function checkIsStatusUsed($status)
220  {
221  return (bool)$this->getConnection()->fetchOne(
222  $this->getConnection()->select()
223  ->from(['sfo' => $this->getTable('sales_order')], [])
224  ->where('status = ?', $status)
225  ->limit(1)
226  ->columns([new \Zend_Db_Expr(1)])
227  );
228  }
229 
237  protected function checkIsStateDefault($state, $status)
238  {
239  return (bool)$this->getConnection()->fetchOne(
240  $this->getConnection()->select()
241  ->from(['sss' => $this->stateTable], [])
242  ->where('state = ?', $state)
243  ->where('status = ?', $status)
244  ->limit(1)
245  ->columns(['is_default'])
246  );
247  }
248 
256  protected function getStatusByState($state)
257  {
258  return (string)$this->getConnection()->fetchOne(
259  $select = $this->getConnection()->select()
260  ->from(['sss' => $this->stateTable, []])
261  ->where('state = ?', $state)
262  ->limit(1)
263  ->columns(['status'])
264  );
265  }
266 }
getStoreLabels(\Magento\Sales\Model\Order\Status $status)
Definition: Status.php:88
_getLoadSelect($field, $value, $object)
Definition: Status.php:58
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$status
Definition: order_status.php:8
assignState($status, $state, $isDefault, $visibleOnFront=false)
Definition: Status.php:134
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Status.php:106