1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Store\Model\ResourceModel;
/**
* Website Resource Model
*
* @api
* @since 100.0.2
*/
class Website extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Define main table
*
* @return void
*/
protected function _construct()
{
$this->_init('store_website', 'website_id');
}
/**
* Initialize unique fields
*
* @return $this
*/
protected function _initUniqueFields()
{
$this->_uniqueFields = [['field' => 'code', 'title' => __('Website with the same code')]];
return $this;
}
/**
* Read all information about websites.
*
* Convert information to next format:
* [website_code => [website_data (website_id, code, name, etc...)]]
*
* @return array
* @since 100.1.3
*/
public function readAllWebsites()
{
$websites = [];
$select = $this->getConnection()
->select()
->from($this->getTable('store_website'));
foreach ($this->getConnection()->fetchAll($select) as $websiteData) {
$websites[$websiteData['code']] = $websiteData;
}
return $websites;
}
/**
* Validate website code before object save
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
if (!preg_match('/^[a-z]+[a-z0-9_]*$/i', $object->getCode())) {
throw new \Magento\Framework\Exception\LocalizedException(
__(
'Website code may only contain letters (a-z), numbers (0-9) or underscore (_),'
. ' and the first character must be a letter.'
)
);
}
return parent::_beforeSave($object);
}
/**
* Perform actions after object save
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
{
if ($object->getIsDefault()) {
$this->getConnection()->update($this->getMainTable(), ['is_default' => 0]);
$where = ['website_id = ?' => $object->getId()];
$this->getConnection()->update($this->getMainTable(), ['is_default' => 1], $where);
}
return parent::_afterSave($object);
}
/**
* Remove configuration data after delete website
*
* @param \Magento\Framework\Model\AbstractModel $model
* @return $this
*/
protected function _afterDelete(\Magento\Framework\Model\AbstractModel $model)
{
$where = [
'scope = ?' => \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES,
'scope_id = ?' => $model->getWebsiteId(),
];
$this->getConnection()->delete($this->getTable('core_config_data'), $where);
return $this;
}
/**
* Retrieve default stores select object
* Select fields website_id, store_id
*
* @param bool $includeDefault include/exclude default admin website
* @return \Magento\Framework\DB\Select
*/
public function getDefaultStoresSelect($includeDefault = false)
{
$ifNull = $this->getConnection()->getCheckSql(
'store_group_table.default_store_id IS NULL',
'0',
'store_group_table.default_store_id'
);
$select = $this->getConnection()->select()->from(
['website_table' => $this->getTable('store_website')],
['website_id']
)->joinLeft(
['store_group_table' => $this->getTable('store_group')],
'website_table.website_id=store_group_table.website_id' .
' AND website_table.default_group_id = store_group_table.group_id',
['store_id' => $ifNull]
);
if (!$includeDefault) {
$select->where('website_table.website_id <> ?', 0);
}
return $select;
}
/**
* Get total number of persistent entities in the system, excluding the admin website by default
*
* @param bool $includeDefault
* @return int
*/
public function countAll($includeDefault = false)
{
$connection = $this->getConnection();
$select = $connection->select()->from($this->getMainTable(), 'COUNT(*)');
if (!$includeDefault) {
$select->where('website_id <> ?', 0);
}
return (int)$connection->fetchOne($select);
}
}