using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web.Mvc; using JetBrains.Annotations; using Portoa.Validation.DataAnnotations; using VideoGameQuotes.Api; namespace VideoGameQuotes.Web.Models { public class SubmitQuoteModel : EditQuoteModelBase { public string CaptchaAnswer { get; set; } public string HashedCaptchaAnswer { get; set; } public string UnhashedCaptchaAnswer { get; set; } } public class EditQuoteModel : EditQuoteModelBase { public EditQuoteModel(Quote quote) { QuoteId = quote.Id; Flags = quote.Flags.ToList(); QuoteText = quote.Text; CategoryIds = quote.Categories.Select(category => category.Id).ToList(); GameId = quote.Game.Id; ActionName = "Edit"; } public int QuoteId { get; set; } public List Flags { get; set; } } public abstract class EditQuoteModelBase { protected EditQuoteModelBase() { ControllerName = "quote"; ActionName = "submit"; } public User CurrentUser { get; set; } public string ActionName { get; set; } public string ControllerName { get; set; } [Required(ErrorMessage = "Quote text must be non-empty, saddlebags")] [StringLength(1024, ErrorMessage = "Quote can't be longer than 1024 characters, slut")] [DisplayName("Quote")] public string QuoteText { get; set; } public IList CategoryIds { get; set; } [Required(ErrorMessage = "Quit screwing with the form, faggle"), DisplayName("Game")] public int GameId { get; set; } public IEnumerable AllGames { get; set; } public IEnumerable AllSystems { get; set; } public IEnumerable AllPublishers { get; set; } public IEnumerable AllCategories { get; set; } public IEnumerable GetGameList() { return AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name }); } private static string MakeTable(string tableId, int cellsPerRow, IEnumerable items, Func cellDataCallback) { var table = new TagBuilder("table"); table.MergeAttribute("id", tableId); table.MergeAttribute("class", "checkbox-table"); 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 html, int cellsPerRow = 4) { return MakeTable( "publisher-checkbox-table", cellsPerRow, AllPublishers, publisher => CreateCheckbox("PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name, "publisher") ); } public string MakeSystemTable(HtmlHelper html, int cellsPerRow = 6) { return MakeTable( "system-checkbox-table", cellsPerRow, AllSystems, system => CreateCheckbox("SystemIds", system.Id, "system_" + system.Id, system.Abbreviation, "system") ); } public string MakeCategoryTable(HtmlHelper html, int cellsPerRow = 5) { var categoryIds = CategoryIds ?? new List(); return MakeTable( "category-checkbox-table", cellsPerRow, AllCategories, category => CreateCheckbox("CategoryIds", category.Id, "category_" + category.Id, category.Name, "category", categoryIds.Contains(category.Id)) ); } public string MakeRegionTable(int cellsPerRow = 8) { return MakeTable( "region-checkbox-table", cellsPerRow, Enum.GetValues(typeof(Region)).Cast().Where(region => region != Region.Unknown), region => CreateCheckbox("GameRegions", region, "region_" + (int)region, region.ToString()) ); } private string CreateCheckbox(string name, object value, string id, string labelText, string editType = null, bool isChecked = false) { return string.Format( "{5}", name, value, id, labelText, isChecked ? " checked=\"checked\"" : "", GetEditAndDeleteLinks(editType) ); } private string GetEditAndDeleteLinks([CanBeNull]string editType) { if (editType == null || CurrentUser.Group < UserGroup.Admin) { return string.Empty; } return string.Format("", editType); } } }