Skip to content

Commit 048a49e

Browse files
committed
Resolves smartstore#508 Limit country settings to stores
1 parent a93bb0e commit 048a49e

14 files changed

Lines changed: 377 additions & 65 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* #554 Flag to indicate that a new payment notification (IPN) arrived for an order since last opening\editing of the order
1212
* Setting whether and how to display sub-categories on a category page (hide, above products, bottom of page)
1313
* Debitoor: Extend option "Book invoice if paid" to also (optionally) mail the invoice as a PDF
14+
* #508 Limit country settings to stores
1415

1516
### Improvements
1617
* PDF converter: first init is much faster now

src/Libraries/SmartStore.Core/Domain/Directory/Country.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System.Collections.Generic;
2+
using System.Runtime.Serialization;
23
using SmartStore.Core.Domain.Localization;
34
using SmartStore.Core.Domain.Shipping;
4-
using System.Runtime.Serialization;
5+
using SmartStore.Core.Domain.Stores;
56

67
namespace SmartStore.Core.Domain.Directory
78
{
89
/// <summary>
910
/// Represents a country
1011
/// </summary>
1112
[DataContract]
12-
public partial class Country : BaseEntity, ILocalizedEntity
13+
public partial class Country : BaseEntity, ILocalizedEntity, IStoreMappingSupported
1314
{
1415
private ICollection<StateProvince> _stateProvinces;
1516
private ICollection<ShippingMethod> _restrictedShippingMethods;
@@ -68,6 +69,12 @@ public partial class Country : BaseEntity, ILocalizedEntity
6869
/// </summary>
6970
[DataMember]
7071
public int DisplayOrder { get; set; }
72+
73+
/// <summary>
74+
/// Gets or sets a value indicating whether the entity is limited/restricted to certain stores
75+
/// </summary>
76+
[DataMember]
77+
public bool LimitedToStores { get; set; }
7178

7279
/// <summary>
7380
/// Gets or sets the state/provinces

src/Libraries/SmartStore.Data/Migrations/201505061135313_CountryMultistore.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class CountryMultistore : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
AddColumn("dbo.Country", "LimitedToStores", c => c.Boolean(nullable: false));
11+
}
12+
13+
public override void Down()
14+
{
15+
DropColumn("dbo.Country", "LimitedToStores");
16+
}
17+
}
18+
}

src/Libraries/SmartStore.Data/Migrations/201505061135313_CountryMultistore.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/Libraries/SmartStore.Data/SmartStore.Data.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@
264264
<Compile Include="Migrations\201505052021483_PdfConverterRoleFix.Designer.cs">
265265
<DependentUpon>201505052021483_PdfConverterRoleFix.cs</DependentUpon>
266266
</Compile>
267+
<Compile Include="Migrations\201505061135313_CountryMultistore.cs" />
268+
<Compile Include="Migrations\201505061135313_CountryMultistore.Designer.cs">
269+
<DependentUpon>201505061135313_CountryMultistore.cs</DependentUpon>
270+
</Compile>
267271
<Compile Include="Setup\Builder\SettingsBuilder.cs" />
268272
<Compile Include="Setup\Builder\SettingsMigrator.cs" />
269273
<Compile Include="Setup\DbMigrationContext.cs" />
@@ -515,6 +519,9 @@
515519
<EmbeddedResource Include="Migrations\201505052021483_PdfConverterRoleFix.resx">
516520
<DependentUpon>201505052021483_PdfConverterRoleFix.cs</DependentUpon>
517521
</EmbeddedResource>
522+
<EmbeddedResource Include="Migrations\201505061135313_CountryMultistore.resx">
523+
<DependentUpon>201505061135313_CountryMultistore.cs</DependentUpon>
524+
</EmbeddedResource>
518525
<EmbeddedResource Include="Sql\Indexes.sql" />
519526
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
520527
</ItemGroup>

src/Libraries/SmartStore.Services/Directory/CountryService.cs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using SmartStore.Core;
45
using SmartStore.Core.Caching;
56
using SmartStore.Core.Data;
67
using SmartStore.Core.Domain.Directory;
8+
using SmartStore.Core.Domain.Stores;
79
using SmartStore.Core.Events;
810

911
namespace SmartStore.Services.Directory
@@ -25,26 +27,28 @@ public partial class CountryService : ICountryService
2527
private readonly IRepository<Country> _countryRepository;
2628
private readonly IEventPublisher _eventPublisher;
2729
private readonly ICacheManager _cacheManager;
30+
private readonly IStoreContext _storeContext;
31+
private readonly IRepository<StoreMapping> _storeMappingRepository;
2832

2933
#endregion
3034

3135
#region Ctor
3236

33-
/// <summary>
34-
/// Ctor
35-
/// </summary>
36-
/// <param name="cacheManager">Cache manager</param>
37-
/// <param name="countryRepository">Country repository</param>
38-
/// <param name="eventPublisher">Event published</param>
3937
public CountryService(ICacheManager cacheManager,
4038
IRepository<Country> countryRepository,
41-
IEventPublisher eventPublisher)
39+
IEventPublisher eventPublisher,
40+
IStoreContext storeContext,
41+
IRepository<StoreMapping> storeMappingRepository)
4242
{
4343
_cacheManager = cacheManager;
4444
_countryRepository = countryRepository;
4545
_eventPublisher = eventPublisher;
46+
_storeContext = storeContext;
47+
_storeMappingRepository = storeMappingRepository;
4648
}
4749

50+
public DbQuerySettings QuerySettings { get; set; }
51+
4852
#endregion
4953

5054
#region Methods
@@ -76,10 +80,31 @@ public virtual IList<Country> GetAllCountries(bool showHidden = false)
7680
string key = string.Format(COUNTRIES_ALL_KEY, showHidden);
7781
return _cacheManager.Get(key, () =>
7882
{
79-
var query = from c in _countryRepository.Table
80-
orderby c.DisplayOrder, c.Name
81-
where showHidden || c.Published
82-
select c;
83+
var query = _countryRepository.Table;
84+
85+
if (!showHidden)
86+
query = query.Where(c => c.Published);
87+
88+
query = query.OrderBy(c => c.DisplayOrder).ThenBy(c => c.Name);
89+
90+
if (!showHidden && !QuerySettings.IgnoreMultiStore)
91+
{
92+
var currentStoreId = _storeContext.CurrentStore.Id;
93+
query = from c in query
94+
join sc in _storeMappingRepository.Table
95+
on new { c1 = c.Id, c2 = "Country" } equals new { c1 = sc.EntityId, c2 = sc.EntityName } into c_sm
96+
from sc in c_sm.DefaultIfEmpty()
97+
where !c.LimitedToStores || currentStoreId == sc.StoreId
98+
select c;
99+
100+
query = from c in query
101+
group c by c.Id into cGroup
102+
orderby cGroup.Key
103+
select cGroup.FirstOrDefault();
104+
105+
query = query.OrderBy(c => c.DisplayOrder).ThenBy(c => c.Name);
106+
}
107+
83108
var countries = query.ToList();
84109
return countries;
85110
});
@@ -95,11 +120,9 @@ public virtual IList<Country> GetAllCountriesForBilling(bool showHidden = false)
95120
string key = string.Format(COUNTRIES_BILLING_KEY, showHidden);
96121
return _cacheManager.Get(key, () =>
97122
{
98-
var query = from c in _countryRepository.Table
99-
orderby c.DisplayOrder, c.Name
100-
where (showHidden || c.Published) && c.AllowsBilling
101-
select c;
102-
var countries = query.ToList();
123+
var allCountries = GetAllCountries(showHidden);
124+
125+
var countries = allCountries.Where(x => x.AllowsBilling).ToList();
103126
return countries;
104127
});
105128
}
@@ -113,12 +136,10 @@ public virtual IList<Country> GetAllCountriesForShipping(bool showHidden = false
113136
{
114137
string key = string.Format(COUNTRIES_SHIPPING_KEY, showHidden);
115138
return _cacheManager.Get(key, () =>
116-
{
117-
var query = from c in _countryRepository.Table
118-
orderby c.DisplayOrder, c.Name
119-
where (showHidden || c.Published) && c.AllowsShipping
120-
select c;
121-
var countries = query.ToList();
139+
{
140+
var allCountries = GetAllCountries(showHidden);
141+
142+
var countries = allCountries.Where(x => x.AllowsShipping).ToList();
122143
return countries;
123144
});
124145
}
@@ -160,11 +181,14 @@ public virtual Country GetCountryByTwoOrThreeLetterIsoCode(string letterIsoCode)
160181
/// <returns>Country</returns>
161182
public virtual Country GetCountryByTwoLetterIsoCode(string twoLetterIsoCode)
162183
{
184+
if (twoLetterIsoCode.IsEmpty())
185+
return null;
186+
163187
var query = from c in _countryRepository.Table
164188
where c.TwoLetterIsoCode == twoLetterIsoCode
165189
select c;
166-
var country = query.FirstOrDefault();
167190

191+
var country = query.FirstOrDefault();
168192
return country;
169193
}
170194

@@ -175,9 +199,13 @@ public virtual Country GetCountryByTwoLetterIsoCode(string twoLetterIsoCode)
175199
/// <returns>Country</returns>
176200
public virtual Country GetCountryByThreeLetterIsoCode(string threeLetterIsoCode)
177201
{
202+
if (threeLetterIsoCode.IsEmpty())
203+
return null;
204+
178205
var query = from c in _countryRepository.Table
179206
where c.ThreeLetterIsoCode == threeLetterIsoCode
180207
select c;
208+
181209
var country = query.FirstOrDefault();
182210
return country;
183211
}

src/Libraries/SmartStore.Services/ExportImport/ExportManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,7 @@ public virtual string ExportCustomersToXml(IList<Customer> customers)
20292029
xmlWriter.WriteElementString("SubjectToVat", null, address.Country.SubjectToVat.ToString());
20302030
xmlWriter.WriteElementString("Published", null, address.Country.Published.ToString());
20312031
xmlWriter.WriteElementString("DisplayOrder", null, address.Country.DisplayOrder.ToString());
2032+
xmlWriter.WriteElementString("LimitedToStores", null, address.Country.LimitedToStores.ToString());
20322033
xmlWriter.WriteEndElement(); // Country
20332034
}
20342035

0 commit comments

Comments
 (0)