Skip to content

Commit 370bb03

Browse files
committed
smartstore#164 Add multistore support for polls
1 parent 99719ca commit 370bb03

17 files changed

Lines changed: 424 additions & 45 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* #90 Directly set order status to completed
2020
* #413 Orders: Add a PDF export\download of selected orders
2121
* #69 Award reward points for product reviews
22+
* #164 Add multistore support for polls
2223

2324
###Improvements###
2425
* Task Scheduler:

src/Libraries/SmartStore.Core/Domain/Polls/Poll.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using SmartStore.Core.Domain.Localization;
4+
using SmartStore.Core.Domain.Stores;
45

56
namespace SmartStore.Core.Domain.Polls
67
{
78
/// <summary>
89
/// Represents a poll
910
/// </summary>
10-
public partial class Poll : BaseEntity
11+
public partial class Poll : BaseEntity, IStoreMappingSupported
1112
{
1213
private ICollection<PollAnswer> _pollAnswers;
1314

@@ -55,6 +56,11 @@ public partial class Poll : BaseEntity
5556
/// Gets or sets the poll end date and time
5657
/// </summary>
5758
public DateTime? EndDateUtc { get; set; }
59+
60+
/// <summary>
61+
/// Gets or sets a value indicating whether the entity is limited/restricted to certain stores
62+
/// </summary>
63+
public bool LimitedToStores { get; set; }
5864

5965
/// <summary>
6066
/// Gets or sets the news comments

src/Libraries/SmartStore.Data/Migrations/201407191010096_BetterReturnRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public override void Up()
2424
public override void Down()
2525
{
2626
DropColumn("dbo.Order", "UpdatedOnUtc");
27+
DropColumn("dbo.Order", "RewardPointsRemaining");
2728
}
2829

2930
public bool RollbackOnFailure

src/Libraries/SmartStore.Data/Migrations/201408071158147_MultistorePoll.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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
using SmartStore.Data.Setup;
6+
7+
public partial class MultistorePoll : DbMigration, ILocaleResourcesProvider, IDataSeeder<SmartObjectContext>
8+
{
9+
public override void Up()
10+
{
11+
AddColumn("dbo.Poll", "LimitedToStores", c => c.Boolean(nullable: false));
12+
}
13+
14+
public override void Down()
15+
{
16+
DropColumn("dbo.Poll", "LimitedToStores");
17+
}
18+
19+
public bool RollbackOnFailure
20+
{
21+
get { return false; }
22+
}
23+
24+
public void Seed(SmartObjectContext context)
25+
{
26+
context.MigrateLocaleResources(MigrateLocaleResources);
27+
}
28+
29+
public void MigrateLocaleResources(LocaleResourcesBuilder builder)
30+
{
31+
}
32+
}
33+
}

src/Libraries/SmartStore.Data/Migrations/201408071158147_MultistorePoll.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
@@ -175,6 +175,10 @@
175175
<Compile Include="Migrations\201407191010096_BetterReturnRequest.Designer.cs">
176176
<DependentUpon>201407191010096_BetterReturnRequest.cs</DependentUpon>
177177
</Compile>
178+
<Compile Include="Migrations\201408071158147_MultistorePoll.cs" />
179+
<Compile Include="Migrations\201408071158147_MultistorePoll.Designer.cs">
180+
<DependentUpon>201408071158147_MultistorePoll.cs</DependentUpon>
181+
</Compile>
178182
<Compile Include="Setup\Builder\SettingsBuilder.cs" />
179183
<Compile Include="Setup\Builder\SettingsMigrator.cs" />
180184
<Compile Include="Setup\DbMigrationContext.cs" />
@@ -361,6 +365,9 @@
361365
<EmbeddedResource Include="Migrations\201407191010096_BetterReturnRequest.resx">
362366
<DependentUpon>201407191010096_BetterReturnRequest.cs</DependentUpon>
363367
</EmbeddedResource>
368+
<EmbeddedResource Include="Migrations\201408071158147_MultistorePoll.resx">
369+
<DependentUpon>201408071158147_MultistorePoll.cs</DependentUpon>
370+
</EmbeddedResource>
364371
<EmbeddedResource Include="Sql\Indexes.sql" />
365372
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
366373
</ItemGroup>

src/Libraries/SmartStore.Services/Polls/IPollService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public partial interface IPollService
1919
/// Gets a poll
2020
/// </summary>
2121
/// <param name="systemKeyword">The poll system keyword</param>
22+
/// <param name="showHidden">A value indicating whether to show hidden records</param>
2223
/// <returns>Poll</returns>
23-
Poll GetPollBySystemKeyword(string systemKeyword, int languageId);
24+
Poll GetPollBySystemKeyword(string systemKeyword, int languageId, bool showHidden = false);
2425

2526
/// <summary>
2627
/// Gets poll collection
@@ -31,8 +32,7 @@ public partial interface IPollService
3132
/// <param name="pageSize">Page size</param>
3233
/// <param name="showHidden">A value indicating whether to show hidden records</param>
3334
/// <returns>Poll collection</returns>
34-
IPagedList<Poll> GetPolls(int languageId, bool loadShownOnHomePageOnly,
35-
int pageIndex, int pageSize, bool showHidden = false);
35+
IPagedList<Poll> GetPolls(int languageId, bool loadShownOnHomePageOnly, int pageIndex, int pageSize, bool showHidden = false);
3636

3737
/// <summary>
3838
/// Deletes a poll

src/Libraries/SmartStore.Services/Polls/PollService.cs

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SmartStore.Core.Caching;
55
using SmartStore.Core.Data;
66
using SmartStore.Core.Domain.Polls;
7+
using SmartStore.Core.Domain.Stores;
78
using SmartStore.Core.Events;
89

910
namespace SmartStore.Services.Polls
@@ -19,8 +20,10 @@ public partial class PollService : IPollService
1920
private readonly IRepository<Poll> _pollRepository;
2021
private readonly IRepository<PollAnswer> _pollAnswerRepository;
2122
private readonly IRepository<PollVotingRecord> _pollVotingRecords;
23+
private readonly IRepository<StoreMapping> _storeMappingRepository;
2224
private readonly ICacheManager _cacheManager;
2325
private readonly IEventPublisher _eventPublisher;
26+
private readonly IStoreContext _storeContext;
2427

2528
#endregion
2629

@@ -29,20 +32,58 @@ public partial class PollService : IPollService
2932
public PollService(IRepository<Poll> pollRepository,
3033
IRepository<PollAnswer> pollAnswerRepository,
3134
IRepository<PollVotingRecord> pollVotingRecords,
32-
ICacheManager cacheManager, IEventPublisher eventPublisher)
35+
IRepository<StoreMapping> storeMappingRepository,
36+
ICacheManager cacheManager, IEventPublisher eventPublisher,
37+
IStoreContext storeContext)
3338
{
3439
this._pollRepository = pollRepository;
3540
this._pollAnswerRepository = pollAnswerRepository;
3641
this._pollVotingRecords = pollVotingRecords;
42+
this._storeMappingRepository = storeMappingRepository;
3743
this._cacheManager = cacheManager;
3844
this._eventPublisher = eventPublisher;
45+
this._storeContext = storeContext;
46+
47+
this.QuerySettings = DbQuerySettings.Default;
3948
}
4049

50+
public DbQuerySettings QuerySettings { get; set; }
51+
4152
#endregion
4253

43-
#region Methods
54+
#region Utilities
4455

45-
/// <summary>
56+
private IQueryable<Poll> Filter(IQueryable<Poll> query, bool showHidden)
57+
{
58+
if (!showHidden)
59+
{
60+
var utcNow = DateTime.UtcNow;
61+
query = query.Where(p => p.Published);
62+
query = query.Where(p => !p.StartDateUtc.HasValue || p.StartDateUtc <= utcNow);
63+
query = query.Where(p => !p.EndDateUtc.HasValue || p.EndDateUtc >= utcNow);
64+
65+
if (!QuerySettings.IgnoreMultiStore)
66+
{
67+
var currentStoreId = _storeContext.CurrentStore.Id;
68+
69+
query =
70+
from p in query
71+
join sm in _storeMappingRepository.Table
72+
on new { c1 = p.Id, c2 = "Poll" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into p_sm
73+
from sm in p_sm.DefaultIfEmpty()
74+
where !p.LimitedToStores || currentStoreId == sm.StoreId
75+
select p;
76+
}
77+
}
78+
79+
return query;
80+
}
81+
82+
#endregion Utilities
83+
84+
#region Methods
85+
86+
/// <summary>
4687
/// Gets a poll
4788
/// </summary>
4889
/// <param name="pollId">The poll identifier</param>
@@ -60,15 +101,20 @@ public virtual Poll GetPollById(int pollId)
60101
/// </summary>
61102
/// <param name="systemKeyword">The poll system keyword</param>
62103
/// <param name="languageId">Language identifier. 0 if you want to get all polls</param>
104+
/// <param name="showHidden">A value indicating whether to show hidden records</param>
63105
/// <returns>Poll</returns>
64-
public virtual Poll GetPollBySystemKeyword(string systemKeyword, int languageId)
106+
public virtual Poll GetPollBySystemKeyword(string systemKeyword, int languageId, bool showHidden = false)
65107
{
66108
if (String.IsNullOrWhiteSpace(systemKeyword))
67109
return null;
68110

69-
var query = from p in _pollRepository.Table
70-
where p.SystemKeyword == systemKeyword && p.LanguageId == languageId
71-
select p;
111+
var query =
112+
from p in _pollRepository.Table
113+
where p.SystemKeyword == systemKeyword && p.LanguageId == languageId
114+
select p;
115+
116+
query = Filter(query, showHidden);
117+
72118
var poll = query.FirstOrDefault();
73119
return poll;
74120
}
@@ -82,26 +128,22 @@ public virtual Poll GetPollBySystemKeyword(string systemKeyword, int languageId)
82128
/// <param name="pageSize">Page size</param>
83129
/// <param name="showHidden">A value indicating whether to show hidden records</param>
84130
/// <returns>Poll collection</returns>
85-
public virtual IPagedList<Poll> GetPolls(int languageId, bool loadShownOnHomePageOnly,
86-
int pageIndex, int pageSize, bool showHidden = false)
131+
public virtual IPagedList<Poll> GetPolls(int languageId, bool loadShownOnHomePageOnly, int pageIndex, int pageSize, bool showHidden = false)
87132
{
88133
var query = _pollRepository.Table;
89-
if (!showHidden)
90-
{
91-
var utcNow = DateTime.UtcNow;
92-
query = query.Where(p => p.Published);
93-
query = query.Where(p => !p.StartDateUtc.HasValue || p.StartDateUtc <= utcNow);
94-
query = query.Where(p => !p.EndDateUtc.HasValue || p.EndDateUtc >= utcNow);
95-
}
96-
if (loadShownOnHomePageOnly)
134+
query = Filter(query, showHidden);
135+
136+
if (loadShownOnHomePageOnly)
97137
{
98138
query = query.Where(p => p.ShowOnHomePage);
99139
}
100-
if (languageId > 0)
140+
141+
if (languageId > 0)
101142
{
102143
query = query.Where(p => p.LanguageId == languageId);
103144
}
104-
query = query.OrderBy(p => p.DisplayOrder);
145+
146+
query = query.OrderBy(p => p.DisplayOrder);
105147

106148
var polls = new PagedList<Poll>(query, pageIndex, pageSize);
107149
return polls;

src/Libraries/SmartStore.Services/Stores/IStoreMappingService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public partial interface IStoreMappingService
3737
/// <returns>Store mapping records</returns>
3838
IList<StoreMapping> GetStoreMappings<T>(T entity) where T : BaseEntity, IStoreMappingSupported;
3939

40+
/// <summary>
41+
/// Save the store napping for an entity
42+
/// </summary>
43+
/// <typeparam name="T">Entity type</typeparam>
44+
/// <param name="entity">The entity</param>
45+
/// <param name="selectedStoreIds">Array of selected store ids</param>
46+
void SaveStoreMappings<T>(T entity, int[] selectedStoreIds) where T : BaseEntity, IStoreMappingSupported;
47+
4048
/// <summary>
4149
/// Inserts a store mapping record
4250
/// </summary>

0 commit comments

Comments
 (0)