forked from corestoreio/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.go
302 lines (263 loc) · 8.9 KB
/
website.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2015, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"errors"
"fmt"
"sort"
"github.com/corestoreio/csfw/config"
"github.com/corestoreio/csfw/directory"
"github.com/corestoreio/csfw/storage/csdb"
"github.com/corestoreio/csfw/storage/dbr"
"github.com/corestoreio/csfw/utils"
"golang.org/x/text/language"
)
const (
// DefaultWebsiteID is always 0
DefaultWebsiteID int64 = 0
)
type (
// Website represents the overall parent structure of its children Group and Store.
// A website defines the default group ID. A website can contain custom configuration
// settings which overrides the default scope but get itself overridden by the Store scope.
Website struct {
cr config.Reader
w *TableWebsite
// groups contains a slice to all groups associated to one website. This slice can be nil.
groups GroupSlice
// stores contains a slice to all stores associated to one website. This slice can be nil.
stores StoreSlice
}
// WebsiteSlice contains pointer to Website struct and some nifty method receivers.
WebsiteSlice []*Website
// WebsiteOption option func for NewWebsite()
WebsiteOption func(*Website)
)
var (
// ErrWebsiteNotFound when the website has not been found within a slice
ErrWebsiteNotFound = errors.New("Website not found")
// ErrWebsiteDefaultGroupNotFound the default group cannot be found
ErrWebsiteDefaultGroupNotFound = errors.New("Website Default Group not found")
// ErrWebsiteGroupsNotAvailable Groups are in the current context not available and nil
ErrWebsiteGroupsNotAvailable = errors.New("Website Groups not available")
// ErrWebsiteStoresNotAvailable Stores are in the current context not available and nil
ErrWebsiteStoresNotAvailable = errors.New("Website Stores not available")
)
var _ config.ScopeIDer = (*Website)(nil)
var _ config.ScopeCoder = (*Website)(nil)
// SetWebsiteConfig sets the config.Reader to the Website.
// Default reader is config.DefaultManager
func SetWebsiteConfig(cr config.Reader) WebsiteOption {
return func(w *Website) { w.cr = cr }
}
// NewWebsite returns a new pointer to a Website.
func NewWebsite(tw *TableWebsite, opts ...WebsiteOption) *Website {
if tw == nil {
panic(ErrStoreNewArgNil)
}
w := &Website{
cr: config.DefaultManager,
w: tw,
}
w.ApplyOptions(opts...)
return w
}
// ApplyOptions sets the options on a Website
func (w *Website) ApplyOptions(opts ...WebsiteOption) {
for _, opt := range opts {
if opt != nil {
opt(w)
}
}
}
// ScopeID satisfies the interface ScopeIDer and mainly used in the StoreManager for selecting Website,Group ...
func (w *Website) ScopeID() int64 { return w.w.WebsiteID }
// ScopeCode satisfies the interface ScopeCoder
func (w *Website) ScopeCode() string { return w.w.Code.String }
// Data returns the data from the database
func (w *Website) Data() *TableWebsite { return w.w }
// DefaultGroup returns the default Group or an error if not found
func (w *Website) DefaultGroup() (*Group, error) {
for _, g := range w.groups {
if w.w.DefaultGroupID == g.Data().GroupID {
return g, nil
}
}
return nil, ErrWebsiteDefaultGroupNotFound
}
// DefaultStore returns the default store which via the default group.
func (w *Website) DefaultStore() (*Store, error) {
g, err := w.DefaultGroup()
if err != nil {
return nil, err
}
return g.DefaultStore()
}
// Stores returns all stores associated to this website or an error when the stores
// are not available aka not needed.
func (w *Website) Stores() (StoreSlice, error) {
if len(w.stores) > 0 {
return w.stores, nil
}
return nil, ErrWebsiteStoresNotAvailable
}
// Groups returns all groups associated to this website or an error when the groups
// are not available aka not needed.
func (w *Website) Groups() (GroupSlice, error) {
if len(w.groups) > 0 {
return w.groups, nil
}
return nil, ErrWebsiteGroupsNotAvailable
}
// SetGroupsStores uses a group slice and a table slice to set the groups associated to this website
// and the stores associated to this website. It panics if the integrity is incorrect.
func (w *Website) SetGroupsStores(tgs TableGroupSlice, tss TableStoreSlice) *Website {
groups := tgs.FilterByWebsiteID(w.w.WebsiteID)
w.groups = make(GroupSlice, groups.Len(), groups.Len())
for i, g := range groups {
w.groups[i] = NewGroup(g, SetGroupWebsite(w.w), SetGroupConfig(w.cr)).SetStores(tss, nil)
}
stores := tss.FilterByWebsiteID(w.w.WebsiteID)
w.stores = make(StoreSlice, stores.Len(), stores.Len())
for i, s := range stores {
group, err := tgs.FindByID(s.GroupID)
if err != nil {
panic(fmt.Sprintf("Integrity error. A store %#v must be assigned to a group.\nGroupSlice: %#v\n\n", s, tgs))
}
w.stores[i] = NewStore(s, w.w, group, SetStoreConfig(w.cr))
}
return w
}
// ConfigString tries to get a value from the scopeStore if empty
// falls back to default global scope.
// If using etcd or consul maybe this can lead to round trip times because of network access.
func (w *Website) ConfigString(path ...string) string {
val := w.cr.GetString(config.ScopeWebsite(w), config.Path(path...))
if val == "" {
val = w.cr.GetString(config.Path(path...))
}
return val
}
// @todo
func (w *Website) BaseCurrencyCode() (language.Currency, error) {
var c string
if w.ConfigString(PathPriceScope) == PriceScopeGlobal {
c = w.cr.GetString(config.Path(directory.PathCurrencyBase))
} else {
c = w.ConfigString(directory.PathCurrencyBase)
}
return language.ParseCurrency(c)
}
// @todo
func (w *Website) BaseCurrency() directory.Currency {
return directory.Currency{}
}
/*
WebsiteSlice method receivers
*/
// Sort convenience helper
func (ws *WebsiteSlice) Sort() *WebsiteSlice {
sort.Sort(ws)
return ws
}
func (ws WebsiteSlice) Len() int { return len(ws) }
func (ws *WebsiteSlice) Swap(i, j int) { (*ws)[i], (*ws)[j] = (*ws)[j], (*ws)[i] }
func (ws *WebsiteSlice) Less(i, j int) bool {
return (*ws)[i].Data().SortOrder < (*ws)[j].Data().SortOrder
}
// Filter returns a new slice filtered by predicate f
func (ws WebsiteSlice) Filter(f func(*Website) bool) WebsiteSlice {
var nws WebsiteSlice
for _, v := range ws {
if v != nil && f(v) {
nws = append(nws, v)
}
}
return nws
}
// Codes returns a StringSlice with all website codes
func (ws WebsiteSlice) Codes() utils.StringSlice {
if len(ws) == 0 {
return nil
}
var c utils.StringSlice
for _, w := range ws {
if w != nil {
c.Append(w.Data().Code.String)
}
}
return c
}
// IDs returns an Int64Slice with all website ids
func (ws WebsiteSlice) IDs() utils.Int64Slice {
if len(ws) == 0 {
return nil
}
var ids utils.Int64Slice
for _, w := range ws {
if w != nil {
ids.Append(w.Data().WebsiteID)
}
}
return ids
}
/*
TableWebsite and TableWebsiteSlice method receivers
*/
// Load uses a dbr session to load all data from the core_website table into the current slice.
// The variadic 2nd argument can be a call back function to manipulate the select.
// Additional columns or joins cannot be added. This method receiver should only be used in development.
// @see app/code/Magento/Store/Model/Resource/Website/Collection.php::Load()
func (s *TableWebsiteSlice) Load(dbrSess dbr.SessionRunner, cbs ...csdb.DbrSelectCb) (int, error) {
return csdb.LoadSlice(dbrSess, TableCollection, TableIndexWebsite, &(*s), append(cbs, func(sb *dbr.SelectBuilder) *dbr.SelectBuilder {
return sb.OrderBy("main_table.sort_order ASC").OrderBy("main_table.name ASC")
})...)
}
// Len returns the length
func (s TableWebsiteSlice) Len() int { return len(s) }
// FindByID returns a TableWebsite if found by id or an error
func (s TableWebsiteSlice) FindByID(id int64) (*TableWebsite, error) {
for _, w := range s {
if w != nil && w.WebsiteID == id {
return w, nil
}
}
return nil, ErrWebsiteNotFound
}
// FindByCode returns a TableWebsite if found by code or an error
func (s TableWebsiteSlice) FindByCode(code string) (*TableWebsite, error) {
for _, w := range s {
if w != nil && w.Code.Valid && w.Code.String == code {
return w, nil
}
}
return nil, ErrWebsiteNotFound
}
// Filter returns a new slice filtered by predicate f
func (s TableWebsiteSlice) Filter(f func(*TableWebsite) bool) TableWebsiteSlice {
var tws TableWebsiteSlice
for _, w := range s {
if w != nil && f(w) {
tws = append(tws, w)
}
}
return tws
}
// @todo review Magento code because of column is_default
//func (s TableWebsite) IsDefault() bool {
// return s.WebsiteID == DefaultWebsiteId
//}
/*
@todo implement Magento\Store\Model\Website
*/