1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Sales_Model_Resource_Order_Status extends Mage_Core_Model_Resource_Db_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_labelsTable;
42:
43: 44: 45: 46: 47:
48: protected $_stateTable;
49:
50: 51: 52:
53: protected function _construct()
54: {
55: $this->_init('sales/order_status', 'status');
56: $this->_isPkAutoIncrement = false;
57: $this->_labelsTable = $this->getTable('sales/order_status_label');
58: $this->_stateTable = $this->getTable('sales/order_status_state');
59: }
60:
61: 62: 63: 64: 65: 66: 67:
68: protected function _getLoadSelect($field, $value, $object)
69: {
70: if ($field == 'default_state') {
71: $select = $this->_getReadAdapter()->select()
72: ->from($this->getMainTable(), array('label'))
73: ->join(
74: array('state_table' => $this->_stateTable),
75: $this->getMainTable() . '.status = state_table.status',
76: 'status'
77: )
78: ->where('state_table.state = ?', $value)
79: ->order('state_table.is_default DESC')
80: ->limit(1);
81: } else {
82: $select = parent::_getLoadSelect($field, $value, $object);
83: }
84: return $select;
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function getStoreLabels(Mage_Core_Model_Abstract $status)
94: {
95: $select = $this->_getWriteAdapter()->select()
96: ->from($this->_labelsTable, array('store_id', 'label'))
97: ->where('status = ?', $status->getStatus());
98: return $this->_getReadAdapter()->fetchPairs($select);
99: }
100:
101: 102: 103: 104: 105: 106:
107: protected function _afterSave(Mage_Core_Model_Abstract $object)
108: {
109: if ($object->hasStoreLabels()) {
110: $labels = $object->getStoreLabels();
111: $this->_getWriteAdapter()->delete(
112: $this->_labelsTable,
113: array('status = ?' => $object->getStatus())
114: );
115: $data = array();
116: foreach ($labels as $storeId => $label) {
117: if (empty($label)) {
118: continue;
119: }
120: $data[] = array(
121: 'status' => $object->getStatus(),
122: 'store_id' => $storeId,
123: 'label' => $label
124: );
125: }
126: if (!empty($data)) {
127: $this->_getWriteAdapter()->insertMultiple($this->_labelsTable, $data);
128: }
129: }
130: return parent::_afterSave($object);
131: }
132:
133: 134: 135: 136: 137: 138: 139: 140:
141: public function assignState($status, $state, $isDefault)
142: {
143: if ($isDefault) {
144: $this->_getWriteAdapter()->update(
145: $this->_stateTable,
146: array('is_default' => 0),
147: array('state = ?' => $state)
148: );
149: }
150: $this->_getWriteAdapter()->insertOnDuplicate(
151: $this->_stateTable,
152: array(
153: 'status' => $status,
154: 'state' => $state,
155: 'is_default' => (int) $isDefault
156: )
157: );
158: return $this;
159: }
160:
161: 162: 163: 164: 165: 166: 167:
168: public function unassignState($status, $state)
169: {
170: $select = $this->_getWriteAdapter()->select()
171: ->from($this->_stateTable, array('qty' => new Zend_Db_Expr('COUNT(*)')))
172: ->where('state = ?', $state);
173:
174: if ($this->_getWriteAdapter()->fetchOne($select) == 1) {
175: throw new Mage_Core_Exception(
176: Mage::helper('sales')->__('Last status can\'t be unassigned from state.')
177: );
178: }
179: $select = $this->_getWriteAdapter()->select()
180: ->from($this->_stateTable, 'is_default')
181: ->where('state = ?', $state)
182: ->where('status = ?', $status)
183: ->limit(1);
184: $isDefault = $this->_getWriteAdapter()->fetchOne($select);
185: $this->_getWriteAdapter()->delete(
186: $this->_stateTable,
187: array(
188: 'state = ?' => $state,
189: 'status = ?' => $status
190: )
191: );
192:
193: if ($isDefault) {
194: $select = $this->_getWriteAdapter()->select()
195: ->from($this->_stateTable, 'status')
196: ->where('state = ?', $state)
197: ->limit(1);
198: $defaultStatus = $this->_getWriteAdapter()->fetchOne($select);
199: if ($defaultStatus) {
200: $this->_getWriteAdapter()->update(
201: $this->_stateTable,
202: array('is_default' => 1),
203: array(
204: 'state = ?' => $state,
205: 'status = ?' => $defaultStatus
206: )
207: );
208: }
209: }
210: return $this;
211: }
212: }
213: