vgquotes/Src/VideoGameQuotes.Web/Models/EditQuoteModel.cs
tmont b45c82e5fa * quote editing, although not quite finished
* flag dismissal
* fixed some mapping issues by adding inverse="true" to some one-to-many relationships
2011-02-19 09:41:09 +00:00

154 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using VideoGameQuotes.Api;
using VideoGameQuotes.Web.Validation;
namespace VideoGameQuotes.Web.Models {
public class EditQuoteModel {
public EditQuoteModel() {
ControllerName = "Quote";
ActionName = "Submit";
}
public EditQuoteModel(Quote quote) : this() {
QuoteId = quote.Id;
Flags = quote.Flags.ToList();
QuoteText = quote.Text;
CategoryIds = quote.Categories.Select(category => category.Id).ToList();
GameId = quote.Game.Id;
PublisherIds = quote.Game.Publishers.Select(publisher => publisher.Id).ToList();
SystemIds = quote.Game.Systems.Select(system => system.Id).ToList();
ActionName = "Edit";
}
public string ActionName { get; set; }
public string ControllerName { get; set; }
public int QuoteId { get; set; }
public List<QuoteFlag> Flags { get; set; }
[NonEmptyText(ErrorMessage = "Quote text must be non-empty"), DisplayName("Quote")]
public string QuoteText { get; set; }
[DisplayName("Categories")]
public IList<int> CategoryIds { get; set; }
public int GameId { get; set; }
//if a new game is created
[DisplayName("Name")]
public string GameName { get; set; }
[DisplayName("Website")]
public string GameWebsite { get; set; }
[DisplayName("Region")]
public Region GameRegions { get; set; }
[DisplayName("Publishers")]
public IList<int> PublisherIds { get; set; }
[DisplayName("Systems")]
public IList<int> SystemIds { get; set; }
//if a new publisher is created
[DisplayName("Name")]
public string PublisherName { get; set; }
[DisplayName("Website")]
public string PublisherWebsite { get; set; }
//if a new system is created
[DisplayName("Name")]
public string SystemName { get; set; }
[DisplayName("Abbreviation")]
public string SystemAbbreviation { get; set; }
[DisplayName("Release Date")]
public DateTime SystemReleaseDate { get; set; }
public IEnumerable<Game> AllGames { get; set; }
public IEnumerable<GamingSystem> AllSystems { get; set; }
public IEnumerable<Publisher> AllPublishers { get; set; }
public IEnumerable<Category> AllCategories { get; set; }
public IEnumerable<SelectListItem> GetGameList() {
return new[] { new SelectListItem { Value = "0", Text = "--none--" } }
.Concat(AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name }));
}
private static string MakeTable<T>(string tableId, int cellsPerRow, IEnumerable<T> items, Func<T, string> cellDataCallback) {
var table = new TagBuilder("table");
table.MergeAttribute("id", tableId);
var count = 0;
var row = new TagBuilder("tr");
var cell = new TagBuilder("td");
foreach (var item in items) {
if (count % cellsPerRow == 0) {
if (count > 0) {
table.InnerHtml += row.ToString(TagRenderMode.Normal);
}
row.InnerHtml = string.Empty;
}
cell.InnerHtml = cellDataCallback(item);
row.InnerHtml += cell.ToString(TagRenderMode.Normal);
count++;
}
if (count > 0) {
//last row
table.InnerHtml += row.ToString(TagRenderMode.Normal);
}
return table.ToString(TagRenderMode.Normal);
}
public string MakePublisherTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 8) {
return MakeTable(
"publisher-checkbox-table",
cellsPerRow,
AllPublishers,
publisher => CreateCheckbox("PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name)
);
}
public string MakeSystemTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 8) {
return MakeTable(
"system-checkbox-table",
cellsPerRow,
AllSystems,
system => CreateCheckbox("SystemIds", system.Id, "system_" + system.Id, system.Abbreviation)
);
}
public string MakeCategoryTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 5) {
var categoryIds = CategoryIds ?? new List<int>();
return MakeTable(
"category-checkbox-table",
cellsPerRow,
AllCategories,
category => CreateCheckbox("CategoryIds", category.Id, "category_" + category.Id, category.Name, categoryIds.Contains(category.Id))
);
}
public string MakeRegionTable(int cellsPerRow = 8) {
return MakeTable(
"region-checkbox-table",
cellsPerRow,
(IEnumerable<Region>)Enum.GetValues(typeof(Region)),
region => CreateCheckbox("GameRegions", region, "region_" + (int)region, region.ToString())
);
}
private static string CreateCheckbox(string name, object value, string id, string labelText, bool isChecked = false) {
return string.Format(
"<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" id=\"{2}\"{4}/><label for=\"{2}\">{3}</label>",
name,
value,
id,
labelText,
isChecked ? " checked=\"checked\"" : ""
);
}
}
}