PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Trunk/OrchardCms/Orchard/Orchard.Web/Modules/Orchard.Rules/Controllers/AdminController.cs

https://bitbucket.org/Maslow/breusable-codeplex
C# | 319 lines | 257 code | 60 blank | 2 comment | 37 complexity | 4689eec5a4cddabdef359f6b1b6224eb MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4. using System.Web.Routing;
  5. using Orchard.Rules.Models;
  6. using Orchard.Rules.Services;
  7. using Orchard.Rules.ViewModels;
  8. using Orchard.ContentManagement;
  9. using Orchard.Core.Contents.Controllers;
  10. using Orchard.Data;
  11. using Orchard.DisplayManagement;
  12. using Orchard.Localization;
  13. using Orchard.Security;
  14. using Orchard.UI.Notify;
  15. using System;
  16. using Orchard.Settings;
  17. using Orchard.UI.Navigation;
  18. namespace Orchard.Rules.Controllers {
  19. [ValidateInput(false)]
  20. public class AdminController : Controller, IUpdateModel {
  21. private readonly ISiteService _siteService;
  22. private readonly IRulesManager _rulesManager;
  23. private readonly IRulesServices _rulesServices;
  24. public AdminController(
  25. IOrchardServices services,
  26. IShapeFactory shapeFactory,
  27. ISiteService siteService,
  28. IRulesManager rulesManager,
  29. IRulesServices rulesServices,
  30. IRepository<RuleRecord> repository) {
  31. _siteService = siteService;
  32. _rulesManager = rulesManager;
  33. _rulesServices = rulesServices;
  34. Services = services;
  35. T = NullLocalizer.Instance;
  36. Shape = shapeFactory;
  37. }
  38. dynamic Shape { get; set; }
  39. public IOrchardServices Services { get; set; }
  40. public Localizer T { get; set; }
  41. public ActionResult Index(RulesIndexOptions options, PagerParameters pagerParameters) {
  42. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to list rules")))
  43. return new HttpUnauthorizedResult();
  44. var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
  45. // default options
  46. if (options == null)
  47. options = new RulesIndexOptions();
  48. var rules = _rulesServices.GetRules();
  49. switch (options.Filter) {
  50. case RulesFilter.Disabled:
  51. rules = rules.Where(r => r.Enabled == false);
  52. break;
  53. case RulesFilter.Enabled:
  54. rules = rules.Where(u => u.Enabled);
  55. break;
  56. }
  57. if (!String.IsNullOrWhiteSpace(options.Search)) {
  58. rules = rules.Where(r => r.Name.Contains(options.Search));
  59. }
  60. var pagerShape = Shape.Pager(pager).TotalItemCount(rules.Count());
  61. switch (options.Order) {
  62. case RulesOrder.Name:
  63. rules = rules.OrderBy(u => u.Name);
  64. break;
  65. }
  66. var results = rules
  67. .Skip(pager.GetStartIndex())
  68. .Take(pager.PageSize)
  69. .ToList();
  70. var model = new RulesIndexViewModel {
  71. Rules = results.Select(x => new RulesEntry {
  72. Rule = x,
  73. IsChecked = false,
  74. RuleId = x.Id
  75. }).ToList(),
  76. Options = options,
  77. Pager = pagerShape
  78. };
  79. // maintain previous route data when generating page links
  80. var routeData = new RouteData();
  81. routeData.Values.Add("Options.Filter", options.Filter);
  82. routeData.Values.Add("Options.Search", options.Search);
  83. routeData.Values.Add("Options.Order", options.Order);
  84. pagerShape.RouteData(routeData);
  85. return View(model);
  86. }
  87. [HttpPost]
  88. [FormValueRequired("submit.BulkEdit")]
  89. public ActionResult Index(FormCollection input) {
  90. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  91. return new HttpUnauthorizedResult();
  92. var viewModel = new RulesIndexViewModel { Rules = new List<RulesEntry>(), Options = new RulesIndexOptions() };
  93. UpdateModel(viewModel);
  94. var checkedEntries = viewModel.Rules.Where(c => c.IsChecked);
  95. switch (viewModel.Options.BulkAction) {
  96. case RulesBulkAction.None:
  97. break;
  98. case RulesBulkAction.Enable:
  99. foreach (var entry in checkedEntries) {
  100. _rulesServices.GetRule(entry.RuleId).Enabled = true;
  101. }
  102. break;
  103. case RulesBulkAction.Disable:
  104. foreach (var entry in checkedEntries) {
  105. _rulesServices.GetRule(entry.RuleId).Enabled = false;
  106. }
  107. break;
  108. case RulesBulkAction.Delete:
  109. foreach (var entry in checkedEntries) {
  110. _rulesServices.DeleteRule(entry.RuleId);
  111. }
  112. break;
  113. }
  114. return RedirectToAction("Index");
  115. }
  116. public ActionResult Move(string direction, int id, int actionId) {
  117. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  118. return new HttpUnauthorizedResult();
  119. switch(direction) {
  120. case "up" : _rulesServices.MoveUp(actionId);
  121. break;
  122. case "down": _rulesServices.MoveDown(actionId);
  123. break;
  124. default:
  125. throw new ArgumentException("direction");
  126. }
  127. return RedirectToAction("Edit", new { id });
  128. }
  129. public ActionResult Create() {
  130. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  131. return new HttpUnauthorizedResult();
  132. return View(new CreateRuleViewModel());
  133. }
  134. [HttpPost, ActionName("Create")]
  135. public ActionResult CreatePost(CreateRuleViewModel viewModel) {
  136. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  137. return new HttpUnauthorizedResult();
  138. if (!ModelState.IsValid) {
  139. Services.TransactionManager.Cancel();
  140. }
  141. else {
  142. var rule = _rulesServices.CreateRule(viewModel.Name);
  143. return RedirectToAction("Edit", new { id = rule.Id });
  144. }
  145. return View(viewModel);
  146. }
  147. public ActionResult Edit(int id) {
  148. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to edit rules")))
  149. return new HttpUnauthorizedResult();
  150. var rule = _rulesServices.GetRule(id);
  151. var viewModel = new EditRuleViewModel {
  152. Id = rule.Id,
  153. Enabled = rule.Enabled,
  154. Name = rule.Name
  155. };
  156. #region Load events
  157. var eventEntries = new List<EventEntry>();
  158. var allEvents = _rulesManager.DescribeEvents().SelectMany(x => x.Descriptors);
  159. foreach (var eventRecord in rule.Events) {
  160. var category = eventRecord.Category;
  161. var type = eventRecord.Type;
  162. var ev = allEvents.Where(x => category == x.Category && type == x.Type).FirstOrDefault();
  163. if (ev != null) {
  164. var eventParameters = FormParametersHelper.FromString(eventRecord.Parameters);
  165. eventEntries.Add(
  166. new EventEntry {
  167. Category = ev.Category,
  168. Type = ev.Type,
  169. EventRecordId = eventRecord.Id,
  170. DisplayText = ev.Display(new EventContext { Properties = eventParameters })
  171. });
  172. }
  173. }
  174. viewModel.Events = eventEntries;
  175. #endregion
  176. #region Load actions
  177. var actionEntries = new List<ActionEntry>();
  178. var allActions = _rulesManager.DescribeActions().SelectMany(x => x.Descriptors);
  179. foreach (var actionRecord in rule.Actions.OrderBy(x => x.Position)) {
  180. var category = actionRecord.Category;
  181. var type = actionRecord.Type;
  182. var action = allActions.Where(x => category == x.Category && type == x.Type).FirstOrDefault();
  183. if (action != null) {
  184. var actionParameters = FormParametersHelper.FromString(actionRecord.Parameters);
  185. actionEntries.Add(
  186. new ActionEntry {
  187. Category = action.Category,
  188. Type = action.Type,
  189. ActionRecordId = actionRecord.Id,
  190. DisplayText = action.Display(new ActionContext { Properties = actionParameters })
  191. });
  192. }
  193. }
  194. viewModel.Actions = actionEntries;
  195. #endregion
  196. return View(viewModel);
  197. }
  198. [HttpPost, ActionName("Edit")]
  199. [FormValueRequired("submit.Save")]
  200. public ActionResult EditPost(EditRuleViewModel viewModel) {
  201. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  202. return new HttpUnauthorizedResult();
  203. if (!ModelState.IsValid) {
  204. Services.TransactionManager.Cancel();
  205. }
  206. else {
  207. var rule = _rulesServices.GetRule(viewModel.Id);
  208. rule.Name = viewModel.Name;
  209. rule.Enabled = viewModel.Enabled;
  210. Services.Notifier.Information(T("Rule Saved"));
  211. return RedirectToAction("Edit", new { id = rule.Id });
  212. }
  213. return View(viewModel);
  214. }
  215. [HttpPost, ActionName("Edit")]
  216. [FormValueRequired("submit.SaveAndEnable")]
  217. public ActionResult EditAndEnablePost(EditRuleViewModel viewModel) {
  218. viewModel.Enabled = true;
  219. return EditPost(viewModel);
  220. }
  221. [HttpPost]
  222. public ActionResult Delete(int id) {
  223. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  224. return new HttpUnauthorizedResult();
  225. var rule = _rulesServices.GetRule(id);
  226. if (rule != null) {
  227. _rulesServices.DeleteRule(id);
  228. Services.Notifier.Information(T("Rule {0} deleted", rule.Name));
  229. }
  230. return RedirectToAction("Index");
  231. }
  232. public ActionResult Enable(int id) {
  233. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  234. return new HttpUnauthorizedResult();
  235. var rule = _rulesServices.GetRule(id);
  236. if (rule != null) {
  237. rule.Enabled = true;
  238. Services.Notifier.Information(T("Rule enabled"));
  239. }
  240. return RedirectToAction("Index");
  241. }
  242. public ActionResult Disable(int id) {
  243. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
  244. return new HttpUnauthorizedResult();
  245. var rule = _rulesServices.GetRule(id);
  246. if (rule != null) {
  247. rule.Enabled = false;
  248. Services.Notifier.Information(T("Rule disabled"));
  249. }
  250. return RedirectToAction("Index");
  251. }
  252. bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
  253. return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
  254. }
  255. public void AddModelError(string key, LocalizedString errorMessage) {
  256. ModelState.AddModelError(key, errorMessage.ToString());
  257. }
  258. }
  259. }