00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Core_Model_Mysql4_Store_Group extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 protected function _construct()
00038 {
00039 $this->_init('core/store_group', 'group_id');
00040 }
00041
00042 protected function _afterSave(Mage_Core_Model_Abstract $model)
00043 {
00044 $this->_updateStoreWebsite($model->getId(), $model->getWebsiteId());
00045 $this->_updateWebsiteDefaultGroup($model->getWebsiteId(), $model->getId());
00046 $this->_changeWebsite($model);
00047
00048 return $this;
00049 }
00050
00051 protected function _updateWebsiteDefaultGroup($websiteId, $groupId)
00052 {
00053 $write = $this->_getWriteAdapter();
00054 $cnt = $write->fetchOne($write->select()
00055 ->from($this->getTable('core/store_group'), array('count'=>'COUNT(*)'))
00056 ->where($write->quoteInto('website_id=?', $websiteId)),
00057 'count');
00058 if ($cnt == 1) {
00059 $write->update($this->getTable('core/website'),
00060 array('default_group_id' => $groupId),
00061 $write->quoteInto('website_id=?', $websiteId)
00062 );
00063 }
00064 return $this;
00065 }
00066
00067 protected function _changeWebsite(Mage_Core_Model_Abstract $model) {
00068 if ($model->getOriginalWebsiteId() && $model->getWebsiteId() != $model->getOriginalWebsiteId()) {
00069 $write = $this->_getWriteAdapter();
00070 $groupId = $write->fetchOne($write->select()
00071 ->from($this->getTable('core/website'), 'default_group_id')
00072 ->where($write->quoteInto('website_id=?', $model->getOriginalWebsiteId())),
00073 'default_group_id'
00074 );
00075 if ($groupId == $model->getId()) {
00076 $write->update($this->getTable('core/website'),
00077 array('default_group_id'=>0),
00078 $write->quoteInto('website_id=?', $model->getOriginalWebsiteId()));
00079 }
00080 }
00081 return $this;
00082 }
00083
00084 protected function _updateStoreWebsite($groupId, $websiteId)
00085 {
00086 $write = $this->_getWriteAdapter();
00087 $bind = array('website_id'=>$websiteId);
00088 $condition = $write->quoteInto('group_id=?', $groupId);
00089 $this->_getWriteAdapter()->update($this->getTable('core/store'), $bind, $condition);
00090 return $this;
00091 }
00092
00093 protected function _saveDefaultStore($groupId, $storeId)
00094 {
00095 $write = $this->_getWriteAdapter();
00096 $bind = array('default_store_id'=>$storeId);
00097 $condition = $write->quoteInto('group_id=?', $groupId);
00098 $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $condition);
00099 return $this;
00100 }
00101 }