mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-18 17:47:54 +08:00
- Tags: Tagging/Creating Tags for content items
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041999
This commit is contained in:
@@ -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<int> tagsForContentItem = new List<int>();
|
||||
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) {
|
||||
|
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
@@ -10,16 +10,20 @@ namespace Orchard.Tags.Models {
|
||||
public class HasTags : ContentPart {
|
||||
public HasTags() {
|
||||
AllTags = new List<Tag>();
|
||||
CurrentTags = new List<Tag>();
|
||||
}
|
||||
|
||||
public IEnumerable<Tag> AllTags { get; set; }
|
||||
public IList<Tag> AllTags { get; set; }
|
||||
public IList<Tag> CurrentTags { get; set; }
|
||||
}
|
||||
|
||||
public class HasTagsProvider : ContentProvider {
|
||||
private readonly IRepository<Tag> _tagsRepository;
|
||||
private readonly IRepository<TagsContentItems> _tagsContentItemsRepository;
|
||||
|
||||
public HasTagsProvider(IRepository<Tag> tagsRepository) {
|
||||
public HasTagsProvider(IRepository<Tag> tagsRepository, IRepository<TagsContentItems> tagsContentItemsRepository) {
|
||||
_tagsRepository = tagsRepository;
|
||||
_tagsContentItemsRepository = tagsContentItemsRepository;
|
||||
Filters.Add(new ActivatingFilter<HasTags>("sandboxpage"));
|
||||
}
|
||||
|
||||
@@ -37,6 +41,11 @@ namespace Orchard.Tags.Models {
|
||||
|
||||
HasTags tags = context.ContentItem.Get<HasTags>();
|
||||
tags.AllTags = _tagsRepository.Table.ToList();
|
||||
IEnumerable<TagsContentItems> tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == context.ContentItem.Id);
|
||||
foreach (var tagContentItem in tagsContentItems) {
|
||||
Tag tag = _tagsRepository.Get(tagContentItem.TagId);
|
||||
tags.CurrentTags.Add(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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<Permission> GetPermissions() {
|
||||
return new List<Permission> {
|
||||
CreateTag,
|
||||
ApplyTag,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -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<Tag> GetTags();
|
||||
void CreateTag(Tag tag);
|
||||
Tag GetTagByName(string tagName);
|
||||
void CreateTag(string tagName);
|
||||
void TagContentItem(int contentItemId, string tagName);
|
||||
void UpdateTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem);
|
||||
}
|
||||
|
||||
public class TagService : ITagService {
|
||||
private readonly IRepository<Tag> _tagRepository;
|
||||
private readonly IRepository<TagsContentItems> _tagsContentItemsRepository;
|
||||
private readonly IAuthorizer _authorizer;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public TagService(IRepository<Tag> tagRepository, IAuthorizer authorizer, INotifier notifier) {
|
||||
public TagService(IRepository<Tag> tagRepository,
|
||||
IRepository<TagsContentItems> 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<Tag> 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<int> tagsForContentItem) {
|
||||
List<int> newTagsForContentItem = new List<int>(tagsForContentItem);
|
||||
IEnumerable<TagsContentItems> 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
|
||||
|
@@ -6,16 +6,29 @@
|
||||
<%= Html.ValidationSummary() %>
|
||||
<div class="yui-g">
|
||||
<h2 class="separator">Edit Tags</h2>
|
||||
<%= Html.Hidden("TaggedContentId", Model.ContentItem.Id) %>
|
||||
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
|
||||
<h3>Choose from existing tags</h3>
|
||||
<ol>
|
||||
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
|
||||
<% foreach (var tag in Model.AllTags) { %>
|
||||
<li>
|
||||
<label for"<%= tag.TagName %>"><%= tag.TagName %>:</label>
|
||||
<input type="checkbox" value="true" name="<%= tag.TagName %>"/>
|
||||
<% if (Model.CurrentTags.Contains(tag)) {%>
|
||||
<input type="checkbox" value="true" checked="checked" name="Checkbox.<%=tag.Id%>"/>
|
||||
<% } else {%>
|
||||
<input type="checkbox" value="true" name="Checkbox.<%=tag.Id%>"/>
|
||||
<% } %>
|
||||
</li>
|
||||
<% } %>
|
||||
<li>
|
||||
<input type="submit" class="button" value="Save" />
|
||||
<input type="submit" class="button" name="submit.Save" value="Save" />
|
||||
</li>
|
||||
</ol>
|
||||
<h3>Or add a new tag</h3>
|
||||
<ol>
|
||||
<li>
|
||||
<input id="NewTagName" class="inputText inputTextLarge" name="NewTagName" type="text" value="" />
|
||||
<input type="submit" class="button" name="submit.Add" value="Add" />
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
@@ -10,5 +10,4 @@
|
||||
|
||||
<% } %>
|
||||
</div>
|
||||
<% Html.EndForm(); %>
|
||||
<% Html.Include("Footer"); %>
|
Reference in New Issue
Block a user