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 class Mage_Core_Model_Mysql4_Website extends Mage_Core_Model_Mysql4_Abstract
00029 {
00030 protected function _construct()
00031 {
00032 $this->_init('core/website', 'website_id');
00033 }
00034
00035
00036
00037
00038
00039
00040 protected function _initUniqueFields()
00041 {
00042 $this->_uniqueFields = array(array(
00043 'field' => 'code',
00044 'title' => Mage::helper('core')->__('Website with the same code')
00045 ));
00046 return $this;
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00056 {
00057 if(!preg_match('/^[a-z]+[a-z0-9_]*$/', $object->getCode())) {
00058 Mage::throwException(Mage::helper('core')->__('Website code should contain only letters (a-z), numbers (0-9) or underscore(_), first character should be a letter'));
00059 }
00060
00061 return parent::_beforeSave($object);
00062 }
00063
00064
00065
00066
00067
00068
00069
00070 protected function _afterSave(Mage_Core_Model_Abstract $object)
00071 {
00072 if ($object->getIsDefault()) {
00073 $this->_getWriteAdapter()->update(
00074 $this->getMainTable(),
00075 array('is_default' => 0),
00076 1
00077 );
00078 $this->_getWriteAdapter()->update(
00079 $this->getMainTable(),
00080 array('is_default' => 1),
00081 $this->_getWriteAdapter()->quoteInto('website_id=?', $object->getId())
00082 );
00083 }
00084 return parent::_afterSave($object);
00085 }
00086
00087 protected function _afterDelete(Mage_Core_Model_Abstract $model)
00088 {
00089 $this->_getWriteAdapter()->delete(
00090 $this->getTable('core/config_data'),
00091 $this->_getWriteAdapter()->quoteInto("scope = 'websites' AND scope_id = ?", $model->getWebsiteId())
00092 );
00093 return $this;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 public function getDefaultStoresSelect($withDefault = false) {
00104 $select = $this->_getReadAdapter()->select()
00105 ->from(array('website_table' => $this->getTable('core/website')), array('website_id'))
00106 ->joinLeft(
00107 array('store_group_table' => $this->getTable('core/store_group')),
00108 '`website_table`.`website_id`=`store_group_table`.`website_id`'
00109 . ' AND `website_table`.`default_group_id`=`store_group_table`.`group_id`',
00110 array('store_id' => 'IFNULL(`store_group_table`.`default_store_id`, 0)')
00111 );
00112 if (!$withDefault) {
00113 $select->where('`website_table`.`website_id` <> ?', 0);
00114 }
00115 return $select;
00116 }
00117
00118 }