2011-02-12 23:53:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
2011-02-23 01:30:53 +00:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
2011-02-23 21:53:51 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2011-02-23 01:30:53 +00:00
|
|
|
|
using Portoa.Validation.DataAnnotations;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Models {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
ActionName = "Edit";
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 09:19:32 +00:00
|
|
|
|
public User CurrentUser { get; set; }
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public string ActionName { get; set; }
|
|
|
|
|
public string ControllerName { get; set; }
|
|
|
|
|
|
|
|
|
|
public int QuoteId { get; set; }
|
|
|
|
|
public List<QuoteFlag> Flags { get; set; }
|
|
|
|
|
|
2011-02-23 01:30:53 +00:00
|
|
|
|
[Required(ErrorMessage = "Quote text must be non-empty, saddlebags")]
|
|
|
|
|
[StringLength(1024, ErrorMessage = "Quote can't be longer than 1024 characters, slut")]
|
|
|
|
|
[DisplayName("Quote")]
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public string QuoteText { get; set; }
|
2011-02-13 09:55:01 +00:00
|
|
|
|
[DisplayName("Categories")]
|
|
|
|
|
public IList<int> CategoryIds { get; set; }
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
2011-02-23 01:30:53 +00:00
|
|
|
|
[GreaterThanZero(ErrorMessage = "Quit screwing with the form, faggle"), DisplayName("Game")]
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public int GameId { get; set; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Game> AllGames { get; set; }
|
|
|
|
|
public IEnumerable<GamingSystem> AllSystems { get; set; }
|
|
|
|
|
public IEnumerable<Publisher> AllPublishers { get; set; }
|
2011-02-13 09:55:01 +00:00
|
|
|
|
public IEnumerable<Category> AllCategories { get; set; }
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
public IEnumerable<SelectListItem> GetGameList() {
|
2011-02-22 09:39:59 +00:00
|
|
|
|
return AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name });
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-14 02:50:33 +00:00
|
|
|
|
private static string MakeTable<T>(string tableId, int cellsPerRow, IEnumerable<T> items, Func<T, string> cellDataCallback) {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
var table = new TagBuilder("table");
|
2011-02-14 02:50:33 +00:00
|
|
|
|
table.MergeAttribute("id", tableId);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 01:46:54 +00:00
|
|
|
|
public string MakePublisherTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 4) {
|
2011-02-13 09:55:01 +00:00
|
|
|
|
return MakeTable(
|
2011-02-14 02:50:33 +00:00
|
|
|
|
"publisher-checkbox-table",
|
2011-02-19 09:41:09 +00:00
|
|
|
|
cellsPerRow,
|
|
|
|
|
AllPublishers,
|
2011-02-23 21:53:51 +00:00
|
|
|
|
publisher => CreateCheckbox("PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name, "publisher")
|
2011-02-13 09:55:01 +00:00
|
|
|
|
);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 21:53:51 +00:00
|
|
|
|
public string MakeSystemTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 6) {
|
2011-02-13 09:55:01 +00:00
|
|
|
|
return MakeTable(
|
2011-02-14 02:50:33 +00:00
|
|
|
|
"system-checkbox-table",
|
2011-02-19 09:41:09 +00:00
|
|
|
|
cellsPerRow,
|
|
|
|
|
AllSystems,
|
2011-02-23 21:53:51 +00:00
|
|
|
|
system => CreateCheckbox("SystemIds", system.Id, "system_" + system.Id, system.Abbreviation, "system")
|
2011-02-13 09:55:01 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public string MakeCategoryTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 5) {
|
|
|
|
|
var categoryIds = CategoryIds ?? new List<int>();
|
2011-02-13 09:55:01 +00:00
|
|
|
|
return MakeTable(
|
2011-02-14 02:50:33 +00:00
|
|
|
|
"category-checkbox-table",
|
2011-02-19 09:41:09 +00:00
|
|
|
|
cellsPerRow,
|
|
|
|
|
AllCategories,
|
2011-02-23 21:53:51 +00:00
|
|
|
|
category => CreateCheckbox("CategoryIds", category.Id, "category_" + category.Id, category.Name, "category", categoryIds.Contains(category.Id))
|
2011-02-13 09:55:01 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public string MakeRegionTable(int cellsPerRow = 8) {
|
2011-02-13 09:55:01 +00:00
|
|
|
|
return MakeTable(
|
2011-02-14 02:50:33 +00:00
|
|
|
|
"region-checkbox-table",
|
2011-02-19 09:41:09 +00:00
|
|
|
|
cellsPerRow,
|
2011-02-23 01:30:53 +00:00
|
|
|
|
Enum.GetValues(typeof(Region)).Cast<Region>().Where(region => region != Region.Unknown),
|
2011-02-19 09:41:09 +00:00
|
|
|
|
region => CreateCheckbox("GameRegions", region, "region_" + (int)region, region.ToString())
|
2011-02-13 09:55:01 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 21:53:51 +00:00
|
|
|
|
private string CreateCheckbox(string name, object value, string id, string labelText, string editType = null, bool isChecked = false) {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
return string.Format(
|
2011-02-23 21:53:51 +00:00
|
|
|
|
"<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" id=\"{2}\"{4}/><label for=\"{2}\">{3}</label>{5}",
|
2011-02-19 09:41:09 +00:00
|
|
|
|
name,
|
|
|
|
|
value,
|
|
|
|
|
id,
|
|
|
|
|
labelText,
|
2011-02-23 21:53:51 +00:00
|
|
|
|
isChecked ? " checked=\"checked\"" : "",
|
|
|
|
|
GetEditAndDeleteLinks(editType)
|
2011-02-19 09:41:09 +00:00
|
|
|
|
);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
2011-02-23 21:53:51 +00:00
|
|
|
|
|
|
|
|
|
private string GetEditAndDeleteLinks([CanBeNull]string editType) {
|
|
|
|
|
if (editType == null || CurrentUser.Group < UserGroup.Admin) {
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Format("<a href=\"#\" class=\"edit-link edit-{0}-link\" title=\"edit\"></a><a href=\"#\" class=\"delete-link delete-{0}-link\" title=\"delete\"></a>", editType);
|
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|