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_Group extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/store_group', 'group_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _afterSave(Mage_Core_Model_Abstract $model)
53: {
54: $this->_updateStoreWebsite($model->getId(), $model->getWebsiteId());
55: $this->_updateWebsiteDefaultGroup($model->getWebsiteId(), $model->getId());
56: $this->_changeWebsite($model);
57:
58: return $this;
59: }
60:
61: 62: 63: 64: 65: 66: 67:
68: protected function _updateWebsiteDefaultGroup($websiteId, $groupId)
69: {
70: $select = $this->_getWriteAdapter()->select()
71: ->from($this->getMainTable(), 'COUNT(*)')
72: ->where('website_id = :website');
73: $count = $this->_getWriteAdapter()->fetchOne($select, array('website' => $websiteId));
74:
75: if ($count == 1) {
76: $bind = array('default_group_id' => $groupId);
77: $where = array('website_id = ?' => $websiteId);
78: $this->_getWriteAdapter()->update($this->getTable('core/website'), $bind, $where);
79: }
80: return $this;
81: }
82:
83: 84: 85: 86: 87: 88:
89: protected function _changeWebsite(Mage_Core_Model_Abstract $model)
90: {
91: if ($model->getOriginalWebsiteId() && $model->getWebsiteId() != $model->getOriginalWebsiteId()) {
92: $select = $this->_getWriteAdapter()->select()
93: ->from($this->getTable('core/website'), 'default_group_id')
94: ->where('website_id = :website_id');
95: $groupId = $this->_getWriteAdapter()->fetchOne($select, array('website_id' => $model->getOriginalWebsiteId()));
96:
97: if ($groupId == $model->getId()) {
98: $bind = array('default_group_id' => 0);
99: $where = array('website_id = ?' => $model->getOriginalWebsiteId());
100: $this->_getWriteAdapter()->update($this->getTable('core/website'), $bind, $where);
101: }
102: }
103: return $this;
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: protected function _updateStoreWebsite($groupId, $websiteId)
114: {
115: $bind = array('website_id' => $websiteId);
116: $where = array('group_id = ?' => $groupId);
117: $this->_getWriteAdapter()->update($this->getTable('core/store'), $bind, $where);
118: return $this;
119: }
120:
121: 122: 123: 124: 125: 126: 127:
128: protected function _saveDefaultStore($groupId, $storeId)
129: {
130: $bind = array('default_store_id' => $storeId);
131: $where = array('group_id = ?' => $groupId);
132: $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $where);
133:
134: return $this;
135: }
136: }
137: