Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions website/MyWebApp/Controllers/AdminBlockTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,53 @@ public async Task<IActionResult> Index()
return View(items);
}

public IActionResult Create()
public async Task<IActionResult> Create()
{
await LoadPagesAsync();
return View(new BlockTemplate());
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(BlockTemplate model)
public async Task<IActionResult> Create(BlockTemplate model, List<int>? pageIds, string? zone, string? role)
{
if (!ModelState.IsValid) return View(model);
if (!ModelState.IsValid)
{
await LoadPagesAsync();
return View(model);
}
model.Html = _sanitizer.Sanitize(model.Html);
_db.BlockTemplates.Add(model);
_db.BlockTemplateVersions.Add(new BlockTemplateVersion { Template = model, Html = model.Html });
await _db.SaveChangesAsync();
await AddSectionsAsync(model, pageIds, zone, role);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}

public async Task<IActionResult> Edit(int id)
{
var item = await _db.BlockTemplates.FindAsync(id);
if (item == null) return NotFound();
await LoadPagesAsync();
return View(item);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(BlockTemplate model)
public async Task<IActionResult> Edit(BlockTemplate model, List<int>? pageIds, string? zone, string? role)
{
if (!ModelState.IsValid) return View(model);
if (!ModelState.IsValid)
{
await LoadPagesAsync();
return View(model);
}
model.Html = _sanitizer.Sanitize(model.Html);
_db.Update(model);
_db.BlockTemplateVersions.Add(new BlockTemplateVersion { BlockTemplateId = model.Id, Html = model.Html });
await _db.SaveChangesAsync();
await AddSectionsAsync(model, pageIds, zone, role);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}

Expand Down Expand Up @@ -208,4 +222,34 @@ public async Task<IActionResult> AddToPage(int id, List<int> pageIds, string zon
return RedirectToAction(nameof(Index));
}

private async Task AddSectionsAsync(BlockTemplate template, List<int>? pageIds, string? zone, string? role)
{
if (pageIds == null || pageIds.Count == 0 || string.IsNullOrWhiteSpace(zone))
return;
var roleEntity = await _db.Roles.FirstOrDefaultAsync(r => r.Name == role);
int? roleId = roleEntity?.Id;
zone = zone!.Trim();
if (pageIds.Contains(0))
{
pageIds = await _db.Pages.Select(p => p.Id).ToListAsync();
}
foreach (var pageId in pageIds)
{
var sort = await _db.PageSections
.Where(s => s.PageId == pageId && s.Zone == zone)
.Select(s => s.SortOrder)
.DefaultIfEmpty(-1)
.MaxAsync() + 1;
var section = new PageSection
{
PageId = pageId,
Zone = zone,
SortOrder = sort,
Html = template.Html,
Type = PageSectionType.Html,
RoleId = roleId
};
_db.PageSections.Add(section);
}
}
}
13 changes: 8 additions & 5 deletions website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
</div>
<div>
<label>Role</label>
<select name="role" required>
<option value="">Select role</option>
<option value="Admin">Admin</option>
<option value="Editor">Editor</option>
<option value="Viewer">Viewer</option>

<select name="role">
<option value="">(none)</option>
@foreach (var r in ViewBag.Roles as List<Role>)
{
<option value="@r.Name">@r.Name</option>
}

</select>
</div>
<button type="submit">Add</button>
Expand Down
2 changes: 2 additions & 0 deletions website/MyWebApp/Views/AdminBlockTemplate/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<select id="section-select" style="display:none"></select>
</div>
</div>

@await Html.PartialAsync("_PageAssignment")
<button type="submit">Save</button>
</form>

Expand Down
2 changes: 2 additions & 0 deletions website/MyWebApp/Views/AdminBlockTemplate/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<select id="section-select" style="display:none"></select>
</div>
</div>

@await Html.PartialAsync("_PageAssignment")
<button type="submit">Save</button>
</form>

Expand Down
3 changes: 1 addition & 2 deletions website/MyWebApp/Views/AdminBlockTemplate/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
<button type="submit">Import</button>
</form>
<table>
<thead><tr><th>Name</th><th></th><th></th><th></th></tr></thead>
<thead><tr><th>Name</th><th></th><th></th></tr></thead>
<tbody>
@foreach (var t in Model)
{
<tr>
<td>@t.Name</td>
<td><a asp-action="Edit" asp-route-id="@t.Id">Edit</a></td>
<td><a asp-action="Delete" asp-route-id="@t.Id">Delete</a></td>
<td><a asp-action="AddToPage" asp-route-id="@t.Id">Add to Page</a></td>
</tr>
}
</tbody>
Expand Down
41 changes: 41 additions & 0 deletions website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@using MyWebApp.Models
<div class="block-assignment">
<h3>Page Assignment</h3>
<div>
<label>Pages</label>
<input type="text" id="page-display" readonly />
<select id="page-select" name="pageIds" multiple size="5">
<option value="0">All Pages</option>
@foreach (var p in ViewBag.Pages as List<Page>)

Check warning on line 9 in website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 9 in website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
<option value="@p.Id">@p.Slug</option>
}
</select>
</div>
<div>
<label>Zone</label>
<input type="text" name="zone" />
</div>
<div>
<label>Role</label>
<select name="role">
<option value="">(none)</option>
@foreach (var r in ViewBag.Roles as List<Role>)
{
<option value="@r.Name">@r.Name</option>
}
</select>
</div>
</div>
@section Scripts {
<script>
const select = document.getElementById('page-select');
const display = document.getElementById('page-display');
function updateDisplay() {
const names = Array.from(select.selectedOptions).map(o => o.text);
display.value = names.join(', ');
}
select.addEventListener('change', updateDisplay);
updateDisplay();
</script>
}
Loading