mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-21 03:14:10 +08:00
Refactored Tags module
--HG-- branch : dev
This commit is contained in:
@@ -55,10 +55,6 @@ namespace Orchard.Packaging.Controllers {
|
|||||||
return View(new PackagingAddSourceViewModel());
|
return View(new PackagingAddSourceViewModel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult AddSourceViewResult(PackagingAddSourceViewModel model) {
|
|
||||||
return View("AddSource", model);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult AddSource(string url) {
|
public ActionResult AddSource(string url) {
|
||||||
try {
|
try {
|
||||||
@@ -90,7 +86,7 @@ namespace Orchard.Packaging.Controllers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( !ModelState.IsValid )
|
if ( !ModelState.IsValid )
|
||||||
return AddSourceViewResult(new PackagingAddSourceViewModel(){ Url = url });
|
return View(new PackagingAddSourceViewModel { Url = url });
|
||||||
|
|
||||||
_packagingSourceManager.AddSource(new PackagingSource { Id = Guid.NewGuid(), FeedUrl = url, FeedTitle = title });
|
_packagingSourceManager.AddSource(new PackagingSource { Id = Guid.NewGuid(), FeedUrl = url, FeedTitle = title });
|
||||||
_notifier.Information(T("The feed has been added successfully."));
|
_notifier.Information(T("The feed has been added successfully."));
|
||||||
@@ -99,7 +95,7 @@ namespace Orchard.Packaging.Controllers {
|
|||||||
}
|
}
|
||||||
catch ( Exception exception ) {
|
catch ( Exception exception ) {
|
||||||
_notifier.Error(T("Adding feed failed: {0}", exception.Message));
|
_notifier.Error(T("Adding feed failed: {0}", exception.Message));
|
||||||
return AddSourceViewResult(new PackagingAddSourceViewModel() { Url = url });
|
return View(new PackagingAddSourceViewModel { Url = url });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,7 +9,6 @@ using Orchard.Settings;
|
|||||||
using Orchard.Tags.Models;
|
using Orchard.Tags.Models;
|
||||||
using Orchard.Tags.ViewModels;
|
using Orchard.Tags.ViewModels;
|
||||||
using Orchard.Tags.Services;
|
using Orchard.Tags.Services;
|
||||||
using Orchard.UI.Notify;
|
|
||||||
|
|
||||||
namespace Orchard.Tags.Controllers {
|
namespace Orchard.Tags.Controllers {
|
||||||
[ValidateInput(false)]
|
[ValidateInput(false)]
|
||||||
@@ -22,52 +21,41 @@ namespace Orchard.Tags.Controllers {
|
|||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IOrchardServices Services { get; set; }
|
public IOrchardServices Services { get; set; }
|
||||||
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
||||||
|
|
||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public ActionResult Index() {
|
public ActionResult Index() {
|
||||||
try {
|
IEnumerable<Tag> tags = _tagService.GetTags();
|
||||||
IEnumerable<Tag> tags = _tagService.GetTags();
|
var entries = tags.Select(CreateTagEntry).ToList();
|
||||||
var entries = tags.Select(tag => CreateTagEntry(tag)).ToList();
|
var model = new TagsAdminIndexViewModel { Tags = entries };
|
||||||
var model = new TagsAdminIndexViewModel { Tags = entries };
|
return View(model);
|
||||||
return View(model);
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
Services.Notifier.Error(T("Listing tags failed: " + exception.Message));
|
|
||||||
return Index();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Index(FormCollection input) {
|
public ActionResult Index(FormCollection input) {
|
||||||
var viewModel = new TagsAdminIndexViewModel { Tags = new List<TagEntry>(), BulkAction = new TagAdminIndexBulkAction() };
|
var viewModel = new TagsAdminIndexViewModel {Tags = new List<TagEntry>(), BulkAction = new TagAdminIndexBulkAction()};
|
||||||
UpdateModel(viewModel);
|
|
||||||
|
|
||||||
try {
|
if ( !TryUpdateModel(viewModel) ) {
|
||||||
IEnumerable<TagEntry> checkedEntries = viewModel.Tags.Where(t => t.IsChecked);
|
return View(viewModel);
|
||||||
switch (viewModel.BulkAction) {
|
|
||||||
case TagAdminIndexBulkAction.None:
|
|
||||||
break;
|
|
||||||
case TagAdminIndexBulkAction.Delete:
|
|
||||||
if (!Services.Authorizer.Authorize(Permissions.ManageTags, T("Couldn't delete tag")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
foreach (TagEntry entry in checkedEntries) {
|
|
||||||
_tagService.DeleteTag(entry.Tag.Id);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception exception) {
|
|
||||||
Services.Notifier.Error(T("Editing tags failed: " + exception.Message));
|
IEnumerable<TagEntry> checkedEntries = viewModel.Tags.Where(t => t.IsChecked);
|
||||||
return Index();
|
switch (viewModel.BulkAction) {
|
||||||
|
case TagAdminIndexBulkAction.None:
|
||||||
|
break;
|
||||||
|
case TagAdminIndexBulkAction.Delete:
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.ManageTags, T("Couldn't delete tag")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
|
foreach (TagEntry entry in checkedEntries) {
|
||||||
|
_tagService.DeleteTag(entry.Tag.Id);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
@@ -80,67 +68,62 @@ namespace Orchard.Tags.Controllers {
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Create(FormCollection input) {
|
public ActionResult Create(FormCollection input) {
|
||||||
var viewModel = new TagsAdminCreateViewModel();
|
var viewModel = new TagsAdminCreateViewModel();
|
||||||
try {
|
|
||||||
UpdateModel(viewModel);
|
if (!TryUpdateModel(viewModel)) {
|
||||||
if (!Services.Authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
_tagService.CreateTag(viewModel.TagName);
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
Services.Notifier.Error(T("Creating tag failed: " + exception.Message));
|
|
||||||
return View(viewModel);
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
|
_tagService.CreateTag(viewModel.TagName);
|
||||||
|
|
||||||
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult Edit(int id) {
|
public ActionResult Edit(int id) {
|
||||||
try {
|
Tag tag = _tagService.GetTag(id);
|
||||||
Tag tag = _tagService.GetTag(id);
|
|
||||||
var viewModel = new TagsAdminEditViewModel {
|
|
||||||
Id = tag.Id,
|
|
||||||
TagName = tag.TagName,
|
|
||||||
};
|
|
||||||
return View(viewModel);
|
|
||||||
|
|
||||||
|
if(tag == null) {
|
||||||
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
catch (Exception exception) {
|
|
||||||
Services.Notifier.Error(T("Retrieving tag information failed: " + exception.Message));
|
var viewModel = new TagsAdminEditViewModel {
|
||||||
return Index();
|
Id = tag.Id,
|
||||||
}
|
TagName = tag.TagName,
|
||||||
|
};
|
||||||
|
|
||||||
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Edit(FormCollection input) {
|
public ActionResult Edit(FormCollection input) {
|
||||||
var viewModel = new TagsAdminEditViewModel();
|
var viewModel = new TagsAdminEditViewModel();
|
||||||
try {
|
|
||||||
UpdateModel(viewModel);
|
|
||||||
if (!Services.Authorizer.Authorize(Permissions.ManageTags, T("Couldn't edit tag")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
_tagService.UpdateTag(viewModel.Id, viewModel.TagName);
|
if ( !TryUpdateModel(viewModel) ) {
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
Services.Notifier.Error(T("Editing tag failed: " + exception.Message));
|
|
||||||
return View(viewModel);
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.ManageTags, T("Couldn't edit tag")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
|
_tagService.UpdateTag(viewModel.Id, viewModel.TagName);
|
||||||
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult Search(int id) {
|
public ActionResult Search(int id) {
|
||||||
try {
|
Tag tag = _tagService.GetTag(id);
|
||||||
Tag tag = _tagService.GetTag(id);
|
|
||||||
IEnumerable<IContent> contents = _tagService.GetTaggedContentItems(id).ToList();
|
|
||||||
var viewModel = new TagsAdminSearchViewModel {
|
|
||||||
TagName = tag.TagName,
|
|
||||||
Contents = contents,
|
|
||||||
};
|
|
||||||
return View(viewModel);
|
|
||||||
|
|
||||||
|
if (tag == null) {
|
||||||
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
catch (Exception exception) {
|
|
||||||
Services.Notifier.Error(T("Retrieving tagged items failed: " + exception.Message));
|
IEnumerable<IContent> contents = _tagService.GetTaggedContentItems(id).ToList();
|
||||||
return Index();
|
var viewModel = new TagsAdminSearchViewModel {
|
||||||
}
|
TagName = tag.TagName,
|
||||||
|
Contents = contents,
|
||||||
|
};
|
||||||
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TagEntry CreateTagEntry(Tag tag) {
|
private static TagEntry CreateTagEntry(Tag tag) {
|
||||||
|
@@ -1,5 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@@ -8,23 +6,17 @@ using Orchard.Localization;
|
|||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
using Orchard.Settings;
|
using Orchard.Settings;
|
||||||
using Orchard.Tags.Helpers;
|
|
||||||
using Orchard.Tags.Services;
|
using Orchard.Tags.Services;
|
||||||
using Orchard.Tags.ViewModels;
|
using Orchard.Tags.ViewModels;
|
||||||
using Orchard.UI.Notify;
|
|
||||||
|
|
||||||
namespace Orchard.Tags.Controllers {
|
namespace Orchard.Tags.Controllers {
|
||||||
[ValidateInput(false)]
|
[ValidateInput(false)]
|
||||||
public class HomeController : Controller {
|
public class HomeController : Controller {
|
||||||
private readonly ITagService _tagService;
|
private readonly ITagService _tagService;
|
||||||
private readonly IContentManager _contentManager;
|
private readonly IContentManager _contentManager;
|
||||||
private readonly INotifier _notifier;
|
|
||||||
private readonly IAuthorizer _authorizer;
|
|
||||||
|
|
||||||
public HomeController(ITagService tagService, IAuthorizer authorizer, INotifier notifier, IContentManager contentManager) {
|
public HomeController(ITagService tagService, IContentManager contentManager) {
|
||||||
_tagService = tagService;
|
_tagService = tagService;
|
||||||
_authorizer = authorizer;
|
|
||||||
_notifier = notifier;
|
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
}
|
}
|
||||||
@@ -35,82 +27,28 @@ namespace Orchard.Tags.Controllers {
|
|||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public ActionResult Index() {
|
public ActionResult Index() {
|
||||||
try {
|
var tags = _tagService.GetTags();
|
||||||
var tags = _tagService.GetTags();
|
var model = new TagsIndexViewModel { Tags = tags.ToList() };
|
||||||
var model = new TagsIndexViewModel { Tags = tags.ToList() };
|
return View(model);
|
||||||
return View(model);
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
_notifier.Error(T("Listing tags failed: " + exception.Message));
|
|
||||||
return Index();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
public ActionResult Edit(FormCollection input, int taggedContentId, string returnUrl, string newTagName) {
|
|
||||||
try {
|
|
||||||
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
if (!String.IsNullOrEmpty(newTagName)) {
|
|
||||||
foreach (var tagName in TagHelpers.ParseCommaSeparatedTagNames(newTagName)) {
|
|
||||||
if (_tagService.GetTagByName(tagName) == null) {
|
|
||||||
_tagService.CreateTag(tagName);
|
|
||||||
}
|
|
||||||
_tagService.TagContentItem(taggedContentId, tagName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
_notifier.Error(T("Editing tags failed: " + exception.Message));
|
|
||||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
public ActionResult Update(string tags, int taggedContentId, string returnUrl) {
|
|
||||||
try {
|
|
||||||
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
List<string> tagNames = TagHelpers.ParseCommaSeparatedTagNames(tags);
|
|
||||||
_tagService.UpdateTagsForContentItem(taggedContentId, tagNames);
|
|
||||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
_notifier.Error(T("Updating tags failed: " + exception.Message));
|
|
||||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult Search(string tagName) {
|
public ActionResult Search(string tagName) {
|
||||||
try {
|
var tag = _tagService.GetTagByName(tagName);
|
||||||
var tag = _tagService.GetTagByName(tagName);
|
|
||||||
var items =
|
|
||||||
_tagService.GetTaggedContentItems(tag.Id).Select(
|
|
||||||
ic => _contentManager.BuildDisplayModel(ic, "SummaryForSearch"));
|
|
||||||
|
|
||||||
var viewModel = new TagsSearchViewModel {
|
if (tag == null) {
|
||||||
TagName = tag.TagName,
|
|
||||||
Items = items.ToList()
|
|
||||||
};
|
|
||||||
return View(viewModel);
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var items =
|
||||||
|
_tagService.GetTaggedContentItems(tag.Id).Select(
|
||||||
|
ic => _contentManager.BuildDisplayModel(ic, "SummaryForSearch"));
|
||||||
|
|
||||||
|
var viewModel = new TagsSearchViewModel {
|
||||||
|
TagName = tag.TagName,
|
||||||
|
Items = items.ToList()
|
||||||
|
};
|
||||||
|
|
||||||
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -90,7 +90,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Module.txt" />
|
<Content Include="Module.txt" />
|
||||||
<Content Include="Views\Admin\Create.aspx" />
|
<Content Include="Views\Admin\Create.ascx" />
|
||||||
<Content Include="Views\Admin\Edit.aspx" />
|
<Content Include="Views\Admin\Edit.aspx" />
|
||||||
<Content Include="Views\Admin\Index.aspx" />
|
<Content Include="Views\Admin\Index.aspx" />
|
||||||
<Content Include="Views\Admin\Search.aspx" />
|
<Content Include="Views\Admin\Search.aspx" />
|
||||||
|
@@ -40,7 +40,7 @@ namespace Orchard.Tags.Services {
|
|||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public IEnumerable<Tag> GetTags() {
|
public IEnumerable<Tag> GetTags() {
|
||||||
return from tags in _tagRepository.Table.ToList() select tags;
|
return _tagRepository.Table.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tag GetTag(int id) {
|
public Tag GetTag(int id) {
|
||||||
@@ -59,7 +59,7 @@ namespace Orchard.Tags.Services {
|
|||||||
_tagRepository.Create(tag);
|
_tagRepository.Create(tag);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_notifier.Warning(T("Couldn't create tag: " + tagName + "it already exixts"));
|
_notifier.Warning(T("The tag {0} already exists", tagName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ using Orchard.Mvc.ViewModels;
|
|||||||
|
|
||||||
namespace Orchard.Tags.ViewModels {
|
namespace Orchard.Tags.ViewModels {
|
||||||
public class TagsAdminCreateViewModel : BaseViewModel {
|
public class TagsAdminCreateViewModel : BaseViewModel {
|
||||||
[Required, DisplayName("Name:")]
|
[Required, DisplayName("Name")]
|
||||||
public string TagName { get; set; }
|
public string TagName { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ using Orchard.Mvc.ViewModels;
|
|||||||
namespace Orchard.Tags.ViewModels {
|
namespace Orchard.Tags.ViewModels {
|
||||||
public class TagsAdminEditViewModel : BaseViewModel {
|
public class TagsAdminEditViewModel : BaseViewModel {
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
[Required, DisplayName("Name:")]
|
[Required, DisplayName("Name")]
|
||||||
public string TagName { get; set; }
|
public string TagName { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,8 +4,10 @@
|
|||||||
<% using(Html.BeginFormAntiForgeryPost()) { %>
|
<% using(Html.BeginFormAntiForgeryPost()) { %>
|
||||||
<%: Html.ValidationSummary() %>
|
<%: Html.ValidationSummary() %>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<label for="TagName"><%: T("Name:")%></label>
|
<%: Html.LabelFor(m => m.TagName) %>
|
||||||
<input id="TagName" class="text" name="TagName" type="text" value="<%: Model.TagName %>" />
|
<%: Html.TextBoxFor(m => m.TagName, new { @class = "text" })%>
|
||||||
<input type="submit" class="button" value="<%: T("Save") %>" />
|
</fieldset>
|
||||||
|
<fieldset>
|
||||||
|
<input class="button primaryAction" type="submit" value="<%:T("Save") %>" />
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<% } %>
|
<% } %>
|
@@ -4,9 +4,11 @@
|
|||||||
<% using(Html.BeginFormAntiForgeryPost()) { %>
|
<% using(Html.BeginFormAntiForgeryPost()) { %>
|
||||||
<%: Html.ValidationSummary() %>
|
<%: Html.ValidationSummary() %>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<label for="Name"><%: T("Name:") %></label>
|
<%: Html.HiddenFor(m => m.Id) %>
|
||||||
<input id="Id" name="Id" type="hidden" value="<%=Model.Id %>" />
|
<%: Html.LabelFor(m => m.TagName) %>
|
||||||
<input id="TagName" class="text" name="TagName" type="text" value="<%: Model.TagName %>" />
|
<%: Html.TextBoxFor(m => m.TagName, new { @class = "text" })%>
|
||||||
<input type="submit" class="button" value="<%: T("Save") %>" />
|
</fieldset>
|
||||||
</fieldset>
|
<fieldset>
|
||||||
|
<input class="button primaryAction" type="submit" value="<%:T("Save") %>" />
|
||||||
|
</fieldset>
|
||||||
<% } %>
|
<% } %>
|
Reference in New Issue
Block a user