diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Controllers/TagsController.cs b/src/Orchard.Web/Packages/Orchard.Tags/Controllers/TagsController.cs index b72ae0763..762e09414 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Controllers/TagsController.cs +++ b/src/Orchard.Web/Packages/Orchard.Tags/Controllers/TagsController.cs @@ -1,11 +1,10 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Orchard.Localization; using Orchard.Logging; -using Orchard.Models; using Orchard.Settings; -using Orchard.Tags.Models; using Orchard.Tags.Services; using Orchard.Tags.ViewModels; using Orchard.UI.Notify; @@ -44,9 +43,27 @@ namespace Orchard.Tags.Controllers { } } - [AcceptVerbs(HttpVerbs.Post)] - public ActionResult Edit(FormCollection input, string returnUrl) { + [HttpPost] + public ActionResult Edit(FormCollection input, int taggedContentId, string returnUrl, string newTagName) { try { + if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Save"])) { + if (!_authorizer.Authorize(Permissions.ApplyTag, T("Couldn't apply tag"))) + return new HttpUnauthorizedResult(); + List tagsForContentItem = new List(); + foreach (string key in input.Keys) { + if (key.StartsWith("Checkbox.") && input[key] == "true") { + int tagId = Convert.ToInt32(key.Substring("Checkbox.".Length)); + tagsForContentItem.Add(tagId); + } + } + _tagService.UpdateTagsForContentItem(taggedContentId, tagsForContentItem); + } + else { + if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag"))) + return new HttpUnauthorizedResult(); + _tagService.CreateTag(newTagName); + _tagService.TagContentItem(taggedContentId, newTagName); + } if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } @@ -54,6 +71,9 @@ namespace Orchard.Tags.Controllers { } catch (Exception exception) { _notifier.Error(T("Editing tags failed: " + exception.Message)); + if (!String.IsNullOrEmpty(returnUrl)) { + return Redirect(returnUrl); + } return RedirectToAction("Index"); } } @@ -62,15 +82,14 @@ namespace Orchard.Tags.Controllers { return View(new TagsCreateViewModel()); } - [AcceptVerbs(HttpVerbs.Post)] + [HttpPost] public ActionResult Create(FormCollection input) { var viewModel = new TagsCreateViewModel(); try { UpdateModel(viewModel, input.ToValueProvider()); if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag"))) return new HttpUnauthorizedResult(); - Tag tag = new Tag { TagName = viewModel.TagName }; - _tagService.CreateTag(tag); + _tagService.CreateTag(viewModel.TagName); return RedirectToAction("Index"); } catch (Exception exception) { diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Models/Tag.cs b/src/Orchard.Web/Packages/Orchard.Tags/Models/Tag.cs index d80ab9cb2..a31538501 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Models/Tag.cs +++ b/src/Orchard.Web/Packages/Orchard.Tags/Models/Tag.cs @@ -3,4 +3,10 @@ public virtual int Id { get; set; } public virtual string TagName { get; set; } } + + public class TagsContentItems { + public virtual int Id { get; set; } + public virtual int TagId { get; set; } + public virtual int ContentItemId { get; set; } + } } diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Models/TagsHandler.cs b/src/Orchard.Web/Packages/Orchard.Tags/Models/TagsHandler.cs index 98fa455ba..09f6afff1 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Models/TagsHandler.cs +++ b/src/Orchard.Web/Packages/Orchard.Tags/Models/TagsHandler.cs @@ -10,16 +10,20 @@ namespace Orchard.Tags.Models { public class HasTags : ContentPart { public HasTags() { AllTags = new List(); + CurrentTags = new List(); } - public IEnumerable AllTags { get; set; } + public IList AllTags { get; set; } + public IList CurrentTags { get; set; } } public class HasTagsProvider : ContentProvider { private readonly IRepository _tagsRepository; + private readonly IRepository _tagsContentItemsRepository; - public HasTagsProvider(IRepository tagsRepository) { + public HasTagsProvider(IRepository tagsRepository, IRepository tagsContentItemsRepository) { _tagsRepository = tagsRepository; + _tagsContentItemsRepository = tagsContentItemsRepository; Filters.Add(new ActivatingFilter("sandboxpage")); } @@ -37,6 +41,11 @@ namespace Orchard.Tags.Models { HasTags tags = context.ContentItem.Get(); tags.AllTags = _tagsRepository.Table.ToList(); + IEnumerable tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == context.ContentItem.Id); + foreach (var tagContentItem in tagsContentItems) { + Tag tag = _tagsRepository.Get(tagContentItem.TagId); + tags.CurrentTags.Add(tag); + } } } } diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Permissions.cs b/src/Orchard.Web/Packages/Orchard.Tags/Permissions.cs index 78be5b215..412347327 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Permissions.cs +++ b/src/Orchard.Web/Packages/Orchard.Tags/Permissions.cs @@ -4,6 +4,7 @@ using Orchard.Security.Permissions; namespace Orchard.Tags { public class Permissions : IPermissionProvider { public static readonly Permission CreateTag = new Permission { Description = "Creating a Tag", Name = "CreateTag" }; + public static readonly Permission ApplyTag = new Permission { Description = "Applying a Tag", Name = "ApplyTag" }; public string PackageName { get { @@ -14,6 +15,7 @@ namespace Orchard.Tags { public IEnumerable GetPermissions() { return new List { CreateTag, + ApplyTag, }; } } diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Services/TagService.cs b/src/Orchard.Web/Packages/Orchard.Tags/Services/TagService.cs index 48d5d8ecf..7d05039b1 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Services/TagService.cs +++ b/src/Orchard.Web/Packages/Orchard.Tags/Services/TagService.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using Orchard.Data; +using Orchard.Localization; using Orchard.Logging; using Orchard.Security; using Orchard.Settings; @@ -10,32 +11,73 @@ using Orchard.UI.Notify; namespace Orchard.Tags.Services { public interface ITagService : IDependency { IEnumerable GetTags(); - void CreateTag(Tag tag); + Tag GetTagByName(string tagName); + void CreateTag(string tagName); + void TagContentItem(int contentItemId, string tagName); + void UpdateTagsForContentItem(int contentItemId, IEnumerable tagsForContentItem); } public class TagService : ITagService { private readonly IRepository _tagRepository; + private readonly IRepository _tagsContentItemsRepository; private readonly IAuthorizer _authorizer; private readonly INotifier _notifier; - public TagService(IRepository tagRepository, IAuthorizer authorizer, INotifier notifier) { + public TagService(IRepository tagRepository, + IRepository tagsContentItemsRepository, + IAuthorizer authorizer, INotifier notifier) { _tagRepository = tagRepository; + _tagsContentItemsRepository = tagsContentItemsRepository; _authorizer = authorizer; _notifier = notifier; Logger = NullLogger.Instance; + T = NullLocalizer.Instance; } public ILogger Logger { get; set; } public ISite CurrentSite { get; set; } + public Localizer T { get; set; } #region ITagService Members public IEnumerable GetTags() { - return from comment in _tagRepository.Table.ToList() select comment; + return from tags in _tagRepository.Table.ToList() select tags; } - public void CreateTag(Tag tag) { - _tagRepository.Create(tag); + public Tag GetTagByName(string tagName) { + return _tagRepository.Get(x => x.TagName == tagName); + } + + public void CreateTag(string tagName) { + if (_tagRepository.Get(x => x.TagName == tagName) == null) { + Tag tag = new Tag { TagName = tagName }; + _tagRepository.Create(tag); + } + else { + _notifier.Warning(T("Couldn't create tag: " + tagName + "it already exixts")); + } + } + + public void TagContentItem(int contentItemId, string tagName) { + Tag tag = GetTagByName(tagName); + TagsContentItems tagsContentItems = new TagsContentItems { ContentItemId = contentItemId, TagId = tag.Id }; + _tagsContentItemsRepository.Create(tagsContentItems); + } + + public void UpdateTagsForContentItem(int contentItemId, IEnumerable tagsForContentItem) { + List newTagsForContentItem = new List(tagsForContentItem); + IEnumerable currentTagsForContentItem = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == contentItemId); + foreach (var tagContentItem in currentTagsForContentItem) { + if (!newTagsForContentItem.Contains(tagContentItem.TagId)) { + _tagsContentItemsRepository.Delete(tagContentItem); + } + else { + newTagsForContentItem.Remove(tagContentItem.TagId); + } + } + foreach (var newTagForContentItem in newTagsForContentItem) { + _tagsContentItemsRepository.Create(new TagsContentItems { ContentItemId = contentItemId, TagId = newTagForContentItem }); + } } #endregion diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Views/Models/DisplayTemplates/HasTags.ascx b/src/Orchard.Web/Packages/Orchard.Tags/Views/Models/DisplayTemplates/HasTags.ascx index 16159fe43..ffaaa5d96 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Views/Models/DisplayTemplates/HasTags.ascx +++ b/src/Orchard.Web/Packages/Orchard.Tags/Views/Models/DisplayTemplates/HasTags.ascx @@ -6,16 +6,29 @@ <%= Html.ValidationSummary() %>

Edit Tags

+ <%= Html.Hidden("TaggedContentId", Model.ContentItem.Id) %> + <%= Html.Hidden("ReturnUrl", Context.Request.Url) %> +

Choose from existing tags

    - <%= Html.Hidden("ReturnUrl", Context.Request.Url) %> <% foreach (var tag in Model.AllTags) { %>
  1. - + <% if (Model.CurrentTags.Contains(tag)) {%> + + <% } else {%> + + <% } %>
  2. <% } %>
  3. - + +
  4. +
+

Or add a new tag

+
    +
  1. + +
diff --git a/src/Orchard.Web/Packages/Orchard.Tags/Views/Tags/Index.aspx b/src/Orchard.Web/Packages/Orchard.Tags/Views/Tags/Index.aspx index 786cbc2e8..b9c4b501d 100644 --- a/src/Orchard.Web/Packages/Orchard.Tags/Views/Tags/Index.aspx +++ b/src/Orchard.Web/Packages/Orchard.Tags/Views/Tags/Index.aspx @@ -10,5 +10,4 @@   <% } %> - <% Html.EndForm(); %> <% Html.Include("Footer"); %> \ No newline at end of file