1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Core Website Resource Model
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_Resource_Website extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Define main table
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('core/website', 'website_id');
44: }
45:
46: /**
47: * Initialize unique fields
48: *
49: * @return Mage_Core_Model_Resource_Website
50: */
51: protected function _initUniqueFields()
52: {
53: $this->_uniqueFields = array(array(
54: 'field' => 'code',
55: 'title' => Mage::helper('core')->__('Website with the same code')
56: ));
57: return $this;
58: }
59:
60: /**
61: * Validate website code before object save
62: *
63: * @param Mage_Core_Model_Abstract $object
64: * @return Mage_Core_Model_Resource_Website
65: */
66: protected function _beforeSave(Mage_Core_Model_Abstract $object)
67: {
68: if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $object->getCode())) {
69: Mage::throwException(Mage::helper('core')->__('Website code may only contain letters (a-z), numbers (0-9) or underscore(_), the first character must be a letter'));
70: }
71:
72: return parent::_beforeSave($object);
73: }
74:
75: /**
76: * Perform actions after object save
77: *
78: * @param Mage_Core_Model_Abstract $object
79: * @return Mage_Core_Model_Resource_Website
80: */
81: protected function _afterSave(Mage_Core_Model_Abstract $object)
82: {
83: if ($object->getIsDefault()) {
84: $this->_getWriteAdapter()->update($this->getMainTable(), array('is_default' => 0));
85: $where = array('website_id = ?' => $object->getId());
86: $this->_getWriteAdapter()->update($this->getMainTable(), array('is_default' => 1), $where);
87: }
88: return parent::_afterSave($object);
89: }
90:
91: /**
92: * Remove core configuration data after delete website
93: *
94: * @param Mage_Core_Model_Abstract $model
95: * @return Mage_Core_Model_Resource_Website
96: */
97: protected function _afterDelete(Mage_Core_Model_Abstract $model)
98: {
99: $where = array(
100: 'scope = ?' => 'websites',
101: 'scope_id = ?' => $model->getWebsiteId()
102: );
103:
104: $this->_getWriteAdapter()->delete($this->getTable('core/config_data'), $where);
105:
106: return $this;
107:
108: }
109:
110: /**
111: * Retrieve default stores select object
112: * Select fields website_id, store_id
113: *
114: * @param boolean $withDefault include/exclude default admin website
115: * @return Varien_Db_Select
116: */
117: public function getDefaultStoresSelect($withDefault = false)
118: {
119: $ifNull = $this->_getReadAdapter()
120: ->getCheckSql('store_group_table.default_store_id IS NULL', '0', 'store_group_table.default_store_id');
121: $select = $this->_getReadAdapter()->select()
122: ->from(
123: array('website_table' => $this->getTable('core/website')),
124: array('website_id'))
125: ->joinLeft(
126: array('store_group_table' => $this->getTable('core/store_group')),
127: 'website_table.website_id=store_group_table.website_id'
128: . ' AND website_table.default_group_id = store_group_table.group_id',
129: array('store_id' => $ifNull)
130: );
131: if (!$withDefault) {
132: $select->where('website_table.website_id <> ?', 0);
133: }
134: return $select;
135: }
136: }
137: