Skip to content

Commit d37dc7e

Browse files
committed
Theme selection from settigs/themes
1 parent 17446f4 commit d37dc7e

File tree

9 files changed

+64
-73
lines changed

9 files changed

+64
-73
lines changed

src/App/Pages/Admin/Settings/Index.cshtml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<label class="form-group-label">Title</label>
1616
<input type="text" asp-for="BlogItem.Title" class="form-control" />
1717
<span asp-validation-for="BlogItem.Title" class="text-danger"></span>
18+
<input type="hidden" asp-for="BlogItem.Theme" />
1819
</div>
1920
<div class="form-group">
2021
<label class="form-group-label">Description</label>
@@ -26,10 +27,6 @@
2627
<input type="text" asp-for="BlogItem.ItemsPerPage" class="form-control" />
2728
<span asp-validation-for="BlogItem.ItemsPerPage" class="text-danger"></span>
2829
</div>
29-
<div class="form-group">
30-
<label class="form-group-label">Theme</label>
31-
<select asp-for="BlogItem.Theme" asp-items="Model.BlogItem.BlogThemes" class="form-control custom-select"></select>
32-
</div>
3330
<div class="form-group">
3431
<label class="form-group-label">Logo image</label>
3532
<div class="input-group">
@@ -60,6 +57,7 @@
6057
@if (Model.ShowMessage)
6158
{
6259
<script>toastr.success('@Model.Message')</script>
60+
Model.Message = "";
6361
}
6462
<script src="~/admin/js/app/fileManagerController.js"></script>
65-
}
63+
}

src/App/Pages/Admin/Settings/Index.cshtml.cs

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using Core.Data;
33
using Core.Services;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.AspNetCore.Mvc.Rendering;
6-
using System.Collections.Generic;
7-
using System.Linq;
85
using System.Threading.Tasks;
96

107
namespace App.Pages.Admin.Settings
@@ -15,72 +12,45 @@ public class IndexModel : AdminPageModel
1512
public BlogItem BlogItem { get; set; }
1613

1714
IDataService _db;
18-
IStorageService _storage;
1915
INotificationService _ns;
2016

21-
public IndexModel(IDataService db, IStorageService storage, INotificationService ns)
17+
public IndexModel(IDataService db, INotificationService ns)
2218
{
2319
_db = db;
24-
_storage = storage;
2520
_ns = ns;
2621
BlogItem = new BlogItem();
2722
}
2823

2924
public async Task<IActionResult> OnGetAsync()
3025
{
31-
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
32-
IsAdmin = author.IsAdmin;
33-
34-
Notifications = await _ns.GetNotifications(author.Id);
35-
36-
if (!author.IsAdmin)
26+
await SetModel();
27+
if (!IsAdmin)
3728
return RedirectToPage("../Shared/_Error", new { code = 403 });
3829

39-
BlogItem = await _db.CustomFields.GetBlogSettings();
40-
BlogItem.BlogThemes = GetThemes();
41-
4230
return Page();
4331
}
4432

4533
public async Task<IActionResult> OnPost()
4634
{
4735
if (!ModelState.IsValid)
48-
{
4936
return Page();
50-
}
5137

5238
await _db.CustomFields.SaveBlogSettings(BlogItem);
53-
5439
Message = Resources.Updated;
5540

56-
return RedirectToPage("Index");
41+
await SetModel();
42+
if (!IsAdmin)
43+
return RedirectToPage("../Shared/_Error", new { code = 403 });
44+
45+
return Page();
5746
}
5847

59-
List<SelectListItem> GetThemes()
48+
async Task SetModel()
6049
{
61-
var themes = new List<SelectListItem>();
62-
var combined = new List<string>();
63-
64-
var storageThemes = _storage.GetThemes();
65-
66-
if(storageThemes != null)
67-
combined.AddRange(storageThemes);
68-
69-
if(AppConfig.EmbeddedThemes != null)
70-
combined.AddRange(AppConfig.EmbeddedThemes);
71-
72-
combined = combined.Distinct().ToList();
73-
combined.Sort();
74-
75-
if (combined != null && combined.Count > 0)
76-
{
77-
foreach (var theme in combined)
78-
{
79-
themes.Add(new SelectListItem { Text = theme, Value = theme });
80-
}
81-
}
82-
83-
return themes;
50+
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
51+
IsAdmin = author.IsAdmin;
52+
Notifications = await _ns.GetNotifications(author.Id);
53+
BlogItem = await _db.CustomFields.GetBlogSettings();
8454
}
8555
}
8656
}

src/App/Pages/Admin/Settings/Themes.cshtml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@
1010
<div class="bf-posts-grid d-flex" aria-label="posts">
1111
@foreach (var theme in Model.Themes)
1212
{
13+
var img = $"{Url.Content("~/")}{theme.Cover}";
14+
var lnkSet = $"{Url.Content("~/admin/settings/themes")}?id={theme.Title}&act=set";
15+
var lnkDel = $"{Url.Content("~/admin/settings/themes")}?id={theme.Title}&act=del";
1316
<div class="post-grid-col">
1417
<div class="post-grid-item">
15-
<a class="item-link" style="background-image:url(/lib/img/img-placeholder.png);"><div class="item-title mt-auto">@theme.Value</div></a>
18+
<a class="item-link" style="background-image:url(@img);"><div class="item-title mt-auto">&nbsp;</div></a>
1619
<div class="item-info d-flex align-items-center">
17-
<span class="item-date mr-auto">@theme.Value</span>
18-
@if (theme.Value == Model.BlogItem.Theme)
20+
<span class="item-date mr-auto">@theme.Title</span>
21+
@if (theme.IsCurrent)
1922
{
20-
<button class="btn-unstyled item-favorite ml-3" data-tooltip="" title="" data-original-title="default">
23+
<a class="btn-unstyled item-favorite ml-3" data-tooltip="" title="" data-original-title="default">
2124
<i class="fas fa-star"></i>
22-
</button>
25+
</a>
2326
<a class="item-show ml-3" href="#" target="_blank" data-tooltip="" title="" data-original-title="settings">
2427
<i class="fas fa-sliders-h"></i>
2528
</a>
2629
}
2730
else
2831
{
29-
<button class="btn-unstyled item-favorite ml-3" data-tooltip="" title="" data-original-title="set as default">
32+
<a class="btn-unstyled item-favorite ml-3" href="@lnkSet" data-tooltip="" title="" data-original-title="set as default">
3033
<i class="far fa-star"></i>
31-
</button>
32-
<a class="item-show ml-4" href="#" data-tooltip="" title="" data-original-title="Delete">
34+
</a>
35+
<a class="item-show ml-4" href="@lnkDel" data-tooltip="" title="" data-original-title="Delete">
3336
<i class="fas fa-trash" style="color: #ff6666"></i>
3437
</a>
3538
}
@@ -45,5 +48,6 @@
4548
@if (Model.ShowMessage)
4649
{
4750
<script>toastr.success('@Model.Message')</script>
51+
Model.Message = "";
4852
}
4953
}

src/App/Pages/Admin/Settings/Themes.cshtml.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using Core.Data;
33
using Core.Services;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.AspNetCore.Mvc.Rendering;
65
using System.Collections.Generic;
6+
using System.IO;
77
using System.Linq;
88
using System.Threading.Tasks;
99

@@ -15,8 +15,7 @@ public class ThemesModel : AdminPageModel
1515
IStorageService _storage;
1616
INotificationService _ns;
1717

18-
[BindProperty]
19-
public IEnumerable<SelectListItem> Themes { get; set; }
18+
public IEnumerable<ThemeItem> Themes { get; set; }
2019
public BlogItem BlogItem { get; set; }
2120

2221
public ThemesModel(IDataService db, IStorageService storage, INotificationService ns)
@@ -26,7 +25,7 @@ public ThemesModel(IDataService db, IStorageService storage, INotificationServic
2625
_ns = ns;
2726
}
2827

29-
public async Task<IActionResult> OnGetAsync()
28+
public async Task<IActionResult> OnGetAsync(string id, string act)
3029
{
3130
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
3231
IsAdmin = author.IsAdmin;
@@ -36,15 +35,28 @@ public async Task<IActionResult> OnGetAsync()
3635
if (!IsAdmin)
3736
return RedirectToPage("../Shared/_Error", new { code = 403 });
3837

38+
if(act == "set" && !string.IsNullOrEmpty(id))
39+
{
40+
var theme = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogTheme);
41+
theme.Content = id;
42+
_db.Complete();
43+
Message = Resources.Updated;
44+
}
45+
46+
if(act == "del" && !string.IsNullOrEmpty(id))
47+
{
48+
Message = Resources.Removed;
49+
}
50+
3951
BlogItem = await _db.CustomFields.GetBlogSettings();
4052
Themes = GetThemes();
4153

4254
return Page();
4355
}
4456

45-
List<SelectListItem> GetThemes()
57+
List<ThemeItem> GetThemes()
4658
{
47-
var themes = new List<SelectListItem>();
59+
var themes = new List<ThemeItem>();
4860
var combined = new List<string>();
4961

5062
var storageThemes = _storage.GetThemes();
@@ -60,10 +72,17 @@ List<SelectListItem> GetThemes()
6072

6173
if (combined != null && combined.Count > 0)
6274
{
63-
var current = new SelectListItem();
75+
var current = new ThemeItem();
6476
foreach (var theme in combined)
6577
{
66-
var item = new SelectListItem { Text = theme, Value = theme };
78+
var slash = Path.DirectorySeparatorChar.ToString();
79+
var file = $"{AppSettings.WebRootPath}{slash}themes{slash}{theme}{slash}{theme}.png";
80+
var item = new ThemeItem {
81+
Title = theme,
82+
Cover = System.IO.File.Exists(file) ? $"themes/{theme}/{theme}.png" : "lib/img/img-placeholder.png",
83+
IsCurrent = theme == BlogItem.Theme
84+
};
85+
6786
if (theme == BlogItem.Theme)
6887
current = item;
6988
else
@@ -75,4 +94,11 @@ List<SelectListItem> GetThemes()
7594
return themes;
7695
}
7796
}
97+
98+
public class ThemeItem
99+
{
100+
public string Title { get; set; }
101+
public string Cover { get; set; }
102+
public bool IsCurrent { get; set; }
103+
}
78104
}
441 KB
Loading
258 KB
Loading

src/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>2.1.0.2</Version>
5+
<Version>2.1.0.3</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

src/Core/Data/Models/AppModel.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Microsoft.AspNetCore.Mvc.Rendering;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
1+
using System.ComponentModel.DataAnnotations;
42

53
namespace Core.Data
64
{
@@ -33,6 +31,5 @@ public class BlogItem
3331
[Required]
3432
[StringLength(120)]
3533
public string Theme { get; set; }
36-
public IList<SelectListItem> BlogThemes { get; set; }
3734
}
3835
}

src/Core/Data/Repositories/CustomFieldRepository.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public async Task SaveBlogSettings(BlogItem blog)
4747
var items = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogItemsPerPage).FirstOrDefault();
4848
var cover = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogCover).FirstOrDefault();
4949
var logo = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogLogo).FirstOrDefault();
50-
var theme = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogTheme).FirstOrDefault();
5150

5251
if (title == null) _db.CustomFields.Add(new CustomField { AuthorId = 0, Name = Constants.BlogTitle, Content = blog.Title });
5352
else title.Content = blog.Title;
@@ -64,9 +63,6 @@ public async Task SaveBlogSettings(BlogItem blog)
6463
if (logo == null) _db.CustomFields.Add(new CustomField { AuthorId = 0, Name = Constants.BlogLogo, Content = blog.Logo });
6564
else logo.Content = blog.Logo;
6665

67-
if (theme == null) _db.CustomFields.Add(new CustomField { AuthorId = 0, Name = Constants.BlogTheme, Content = blog.Theme });
68-
else theme.Content = blog.Theme;
69-
7066
await _db.SaveChangesAsync();
7167
}
7268
}

0 commit comments

Comments
 (0)