PageRenderTime 695ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Orchard.Azure/Orchard.Azure.Web/Modules/Orchard.Projections/Controllers/PropertyController.cs

http://associativy.codeplex.com
C# | 231 lines | 185 code | 40 blank | 6 comment | 28 complexity | d1d2e5a39897e2e97c9cfbfc035033b7 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause, Apache-2.0, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Web.Mvc;
  6. using Orchard.Data;
  7. using Orchard.DisplayManagement;
  8. using Orchard.Forms.Services;
  9. using Orchard.Localization;
  10. using Orchard.Projections.Models;
  11. using Orchard.Projections.Services;
  12. using Orchard.Projections.ViewModels;
  13. using Orchard.Security;
  14. using Orchard.UI.Admin;
  15. using Orchard.UI.Notify;
  16. namespace Orchard.Projections.Controllers {
  17. [ValidateInput(false), Admin]
  18. public class PropertyController : Controller {
  19. public IOrchardServices Services { get; set; }
  20. private readonly IFormManager _formManager;
  21. private readonly IProjectionManager _projectionManager;
  22. private readonly IRepository<PropertyRecord> _repository;
  23. private readonly IRepository<LayoutRecord> _layoutRepository;
  24. private readonly IPropertyService _propertyService;
  25. public PropertyController(
  26. IOrchardServices services,
  27. IFormManager formManager,
  28. IShapeFactory shapeFactory,
  29. IProjectionManager projectionManager,
  30. IRepository<PropertyRecord> repository,
  31. IRepository<LayoutRecord> layoutRepository,
  32. IPropertyService propertyService,
  33. IQueryService queryService) {
  34. Services = services;
  35. _formManager = formManager;
  36. _projectionManager = projectionManager;
  37. _repository = repository;
  38. _layoutRepository = layoutRepository;
  39. _propertyService = propertyService;
  40. Shape = shapeFactory;
  41. }
  42. public Localizer T { get; set; }
  43. public dynamic Shape { get; set; }
  44. public ActionResult Add(int id) {
  45. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage queries")))
  46. return new HttpUnauthorizedResult();
  47. var viewModel = new PropertyAddViewModel { Id = id, Properties = _projectionManager.DescribeProperties() };
  48. return View(viewModel);
  49. }
  50. [HttpPost]
  51. public ActionResult Delete(int id, int propertyId) {
  52. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage queries")))
  53. return new HttpUnauthorizedResult();
  54. var property = _repository.Get(propertyId);
  55. if(property == null) {
  56. return HttpNotFound();
  57. }
  58. property.LayoutRecord.Properties.Remove(property);
  59. _repository.Delete(property);
  60. Services.Notifier.Information(T("Property deleted"));
  61. return RedirectToAction("Edit", "Layout", new { id });
  62. }
  63. public ActionResult Edit(int id, string category, string type, int propertyId = -1) {
  64. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage queries")))
  65. return new HttpUnauthorizedResult();
  66. var property = _projectionManager.DescribeProperties().SelectMany(x => x.Descriptors).Where(x => x.Category == category && x.Type == type).FirstOrDefault();
  67. if (property == null) {
  68. return HttpNotFound();
  69. }
  70. var viewModel = new PropertyEditViewModel {
  71. Id = id,
  72. Description = String.Empty,
  73. Property = property
  74. };
  75. dynamic form = null;
  76. // build the form, and let external components alter it
  77. if (property.Form != null) {
  78. form = _formManager.Build(property.Form);
  79. viewModel.Form = form;
  80. }
  81. // bind form with existing values.
  82. if (propertyId != -1) {
  83. var propertyRecord = _repository.Get(propertyId);
  84. if (propertyRecord != null) {
  85. viewModel.Description = propertyRecord.Description;
  86. if (property.Form != null) {
  87. var parameters = FormParametersHelper.FromString(propertyRecord.State);
  88. _formManager.Bind(form, new DictionaryValueProvider<string>(parameters, CultureInfo.InvariantCulture));
  89. }
  90. viewModel.CreateLabel = propertyRecord.CreateLabel;
  91. viewModel.ExcludeFromDisplay = propertyRecord.ExcludeFromDisplay;
  92. viewModel.Label = propertyRecord.Label;
  93. viewModel.LinkToContent = propertyRecord.LinkToContent;
  94. viewModel.CustomizeLabelHtml = propertyRecord.CustomizeLabelHtml;
  95. viewModel.CustomizePropertyHtml = propertyRecord.CustomizePropertyHtml;
  96. viewModel.CustomizeWrapperHtml = propertyRecord.CustomizeWrapperHtml;
  97. viewModel.CustomLabelCss = propertyRecord.CustomLabelCss;
  98. viewModel.CustomLabelTag = propertyRecord.CustomLabelTag;
  99. viewModel.CustomPropertyCss = propertyRecord.CustomPropertyCss;
  100. viewModel.CustomPropertyTag = propertyRecord.CustomPropertyTag;
  101. viewModel.CustomWrapperCss = propertyRecord.CustomWrapperCss;
  102. viewModel.CustomWrapperTag = propertyRecord.CustomWrapperTag;
  103. viewModel.NoResultText = propertyRecord.NoResultText;
  104. viewModel.ZeroIsEmpty = propertyRecord.ZeroIsEmpty;
  105. viewModel.HideEmpty = propertyRecord.HideEmpty;
  106. viewModel.RewriteOutput = propertyRecord.RewriteOutput;
  107. viewModel.RewriteText = propertyRecord.RewriteText;
  108. viewModel.StripHtmlTags = propertyRecord.StripHtmlTags;
  109. viewModel.TrimLength = propertyRecord.TrimLength;
  110. viewModel.AddEllipsis = propertyRecord.AddEllipsis;
  111. viewModel.MaxLength = propertyRecord.MaxLength;
  112. viewModel.TrimOnWordBoundary = propertyRecord.TrimOnWordBoundary;
  113. viewModel.PreserveLines = propertyRecord.PreserveLines;
  114. viewModel.TrimWhiteSpace = propertyRecord.TrimWhiteSpace;
  115. }
  116. }
  117. return View(viewModel);
  118. }
  119. [HttpPost, ActionName("Edit")]
  120. public ActionResult EditPost(int id, string category, string type, [DefaultValue(-1)]int propertyId, FormCollection formCollection) {
  121. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage queries")))
  122. return new HttpUnauthorizedResult();
  123. var layout = _layoutRepository.Get(id);
  124. var property = _projectionManager.DescribeProperties().SelectMany(x => x.Descriptors).Where(x => x.Category == category && x.Type == type).FirstOrDefault();
  125. var model = new PropertyEditViewModel();
  126. TryUpdateModel(model);
  127. // validating form values
  128. _formManager.Validate(new ValidatingContext { FormName = property.Form, ModelState = ModelState, ValueProvider = ValueProvider });
  129. if (ModelState.IsValid) {
  130. var propertyRecord = layout.Properties.Where(f => f.Id == propertyId).FirstOrDefault();
  131. // add new property record if it's a newly created property
  132. if (propertyRecord == null) {
  133. propertyRecord = new PropertyRecord {
  134. Category = category,
  135. Type = type,
  136. Position = layout.Properties.Count
  137. };
  138. layout.Properties.Add(propertyRecord);
  139. }
  140. var dictionary = formCollection.AllKeys.ToDictionary(key => key, formCollection.Get);
  141. // save form parameters
  142. propertyRecord.State = FormParametersHelper.ToString(dictionary);
  143. propertyRecord.Description = model.Description;
  144. propertyRecord.CreateLabel = model.CreateLabel;
  145. propertyRecord.ExcludeFromDisplay = model.ExcludeFromDisplay;
  146. propertyRecord.Label = model.Label;
  147. propertyRecord.LinkToContent = model.LinkToContent;
  148. propertyRecord.CustomizeLabelHtml = model.CustomizeLabelHtml;
  149. propertyRecord.CustomizePropertyHtml = model.CustomizePropertyHtml;
  150. propertyRecord.CustomizeWrapperHtml = model.CustomizeWrapperHtml;
  151. propertyRecord.CustomLabelCss = model.CustomLabelCss;
  152. propertyRecord.CustomLabelTag = model.CustomLabelTag;
  153. propertyRecord.CustomPropertyCss = model.CustomPropertyCss;
  154. propertyRecord.CustomPropertyTag = model.CustomPropertyTag;
  155. propertyRecord.CustomWrapperCss = model.CustomWrapperCss;
  156. propertyRecord.CustomWrapperTag = model.CustomWrapperTag;
  157. propertyRecord.NoResultText = model.NoResultText;
  158. propertyRecord.ZeroIsEmpty = model.ZeroIsEmpty;
  159. propertyRecord.HideEmpty = model.HideEmpty;
  160. propertyRecord.RewriteOutput = model.RewriteOutput;
  161. propertyRecord.RewriteText = model.RewriteText;
  162. propertyRecord.StripHtmlTags = model.StripHtmlTags;
  163. propertyRecord.TrimLength = model.TrimLength;
  164. propertyRecord.AddEllipsis = model.AddEllipsis;
  165. propertyRecord.MaxLength = model.MaxLength;
  166. propertyRecord.TrimOnWordBoundary = model.TrimOnWordBoundary;
  167. propertyRecord.PreserveLines = model.PreserveLines;
  168. propertyRecord.TrimWhiteSpace = model.TrimWhiteSpace;
  169. return RedirectToAction("Edit", "Layout", new { id });
  170. }
  171. // model is invalid, display it again
  172. var form = _formManager.Build(property.Form);
  173. _formManager.Bind(form, formCollection);
  174. var viewModel = new PropertyEditViewModel { Id = id, Description = model.Description, Property = property, Form = form };
  175. return View(viewModel);
  176. }
  177. public ActionResult Move(string direction, int id, int layoutId) {
  178. if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage queries")))
  179. return new HttpUnauthorizedResult();
  180. switch (direction) {
  181. case "up": _propertyService.MoveUp(id);
  182. break;
  183. case "down": _propertyService.MoveDown(id);
  184. break;
  185. default:
  186. throw new ArgumentException("direction");
  187. }
  188. return RedirectToAction("Edit", "Layout", new { id = layoutId });
  189. }
  190. }
  191. }