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:
35: class Mage_Core_Model_Resource_Store extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/store', 'store_id');
44: }
45:
46: 47: 48: 49: 50:
51: protected function _initUniqueFields()
52: {
53: $this->_uniqueFields = array(array(
54: 'field' => 'code',
55: 'title' => Mage::helper('core')->__('Store with the same code')
56: ));
57: return $this;
58: }
59:
60: 61: 62: 63: 64: 65:
66: protected function _beforeSave(Mage_Core_Model_Abstract $model)
67: {
68: if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $model->getCode())) {
69: Mage::throwException(
70: Mage::helper('core')->__('The store code may contain only letters (a-z), numbers (0-9) or underscore(_), the first character must be a letter'));
71: }
72:
73: return $this;
74: }
75:
76: 77: 78: 79: 80: 81:
82: protected function _afterSave(Mage_Core_Model_Abstract $object)
83: {
84: parent::_afterSave($object);
85: $this->_updateGroupDefaultStore($object->getGroupId(), $object->getId());
86: $this->_changeGroup($object);
87:
88: return $this;
89: }
90:
91: 92: 93: 94: 95: 96:
97: protected function _afterDelete(Mage_Core_Model_Abstract $model)
98: {
99: $where = array(
100: 'scope = ?' => 'stores',
101: 'scope_id = ?' => $model->getStoreId()
102: );
103:
104: $this->_getWriteAdapter()->delete(
105: $this->getTable('core/config_data'),
106: $where
107: );
108: return $this;
109: }
110:
111: 112: 113: 114: 115: 116: 117:
118: protected function _updateGroupDefaultStore($groupId, $storeId)
119: {
120: $adapter = $this->_getWriteAdapter();
121:
122: $bindValues = array('group_id' => (int)$groupId);
123: $select = $adapter->select()
124: ->from($this->getMainTable(), array('count' => 'COUNT(*)'))
125: ->where('group_id = :group_id');
126: $count = $adapter->fetchOne($select, $bindValues);
127:
128: if ($count == 1) {
129: $bind = array('default_store_id' => (int)$storeId);
130: $where = array('group_id = ?' => (int)$groupId);
131: $adapter->update($this->getTable('core/store_group'), $bind, $where);
132: }
133:
134: return $this;
135: }
136:
137: 138: 139: 140: 141: 142:
143: protected function _changeGroup(Mage_Core_Model_Abstract $model)
144: {
145: if ($model->getOriginalGroupId() && $model->getGroupId() != $model->getOriginalGroupId()) {
146: $adapter = $this->_getReadAdapter();
147: $select = $adapter->select()
148: ->from($this->getTable('core/store_group'), 'default_store_id')
149: ->where($adapter->quoteInto('group_id=?', $model->getOriginalGroupId()));
150: $storeId = $adapter->fetchOne($select, 'default_store_id');
151:
152: if ($storeId == $model->getId()) {
153: $bind = array('default_store_id' => Mage_Core_Model_App::ADMIN_STORE_ID);
154: $where = array('group_id = ?' => $model->getOriginalGroupId());
155: $this->_getWriteAdapter()->update($this->getTable('core/store_group'), $bind, $where);
156: }
157: }
158: return $this;
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168:
169: protected function _getLoadSelect($field, $value, $object)
170: {
171: $select = parent::_getLoadSelect($field, $value, $object);
172: $select->order('sort_order');
173: return $select;
174: }
175: }
176: