mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00
Cleanup tags modules
Move code to remove content item in ITagService Admin page for a tag shows all content items (even the ones that are not published) --HG-- branch : dev
This commit is contained in:
@@ -96,6 +96,27 @@ namespace Orchard.Tests.Modules.Tags.Services {
|
|||||||
Assert.That(thing2.As<TagsPart>().CurrentTags.Any(tagRecord => tagRecord.TagName == "tag3"), Is.True);
|
Assert.That(thing2.As<TagsPart>().CurrentTags.Any(tagRecord => tagRecord.TagName == "tag3"), Is.True);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TagsShouldDeletedAferRemovingContentItem() {
|
||||||
|
var thing = _contentManager.New("thing");
|
||||||
|
_contentManager.Create(thing, VersionOptions.Published);
|
||||||
|
_tagService.UpdateTagsForContentItem(thing, new string[] { "tag1", "tag2", "tag3" });
|
||||||
|
|
||||||
|
ClearSession();
|
||||||
|
|
||||||
|
Assert.That(_tagService.GetTagByName("tag1"), Is.Not.Null);
|
||||||
|
Assert.That(_tagService.GetTagByName("tag2"), Is.Not.Null);
|
||||||
|
Assert.That(_tagService.GetTagByName("tag3"), Is.Not.Null);
|
||||||
|
|
||||||
|
_contentManager.Remove(_contentManager.Get(thing.Id));
|
||||||
|
|
||||||
|
ClearSession();
|
||||||
|
|
||||||
|
Assert.That(_tagService.GetTagByName("tag1"), Is.Null);
|
||||||
|
Assert.That(_tagService.GetTagByName("tag2"), Is.Null);
|
||||||
|
Assert.That(_tagService.GetTagByName("tag3"), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ContentItemsShouldBeReturnedFromTagService() {
|
public void ContentItemsShouldBeReturnedFromTagService() {
|
||||||
var thing1 = _contentManager.New("thing");
|
var thing1 = _contentManager.New("thing");
|
||||||
|
@@ -91,7 +91,7 @@ namespace Orchard.Tags.Controllers {
|
|||||||
TagName = tagRecord.TagName,
|
TagName = tagRecord.TagName,
|
||||||
};
|
};
|
||||||
|
|
||||||
ViewData["ContentItems"] = _tagService.GetTaggedContentItems(id).ToList();
|
ViewData["ContentItems"] = _tagService.GetTaggedContentItems(id, VersionOptions.Latest).ToList();
|
||||||
|
|
||||||
return View(viewModel);
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
@@ -1,38 +1,20 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using JetBrains.Annotations;
|
|
||||||
using Orchard.ContentManagement;
|
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.ContentManagement.Handlers;
|
using Orchard.ContentManagement.Handlers;
|
||||||
using Orchard.Tags.Models;
|
using Orchard.Tags.Models;
|
||||||
|
using Orchard.Tags.Services;
|
||||||
|
|
||||||
namespace Orchard.Tags.Handlers {
|
namespace Orchard.Tags.Handlers {
|
||||||
[UsedImplicitly]
|
|
||||||
public class TagsPartHandler : ContentHandler {
|
public class TagsPartHandler : ContentHandler {
|
||||||
public TagsPartHandler(IRepository<TagsPartRecord> repository, IRepository<TagRecord> tagsRepository, IRepository<ContentTagRecord> tagsContentItemsRepository) {
|
public TagsPartHandler(IRepository<TagsPartRecord> repository, ITagService tagService) {
|
||||||
Filters.Add(StorageFilter.For(repository));
|
Filters.Add(StorageFilter.For(repository));
|
||||||
|
|
||||||
OnRemoved<TagsPart>((context, tags) => {
|
OnRemoved<TagsPart>((context, tags) =>
|
||||||
tagsContentItemsRepository.Flush();
|
tagService.RemoveTagsForContentItem(context.ContentItem));
|
||||||
|
|
||||||
var tagsPart = context.ContentItem.As<TagsPart>();
|
OnIndexing<TagsPart>((context, tagsPart) =>
|
||||||
|
context.DocumentIndex.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze());
|
||||||
// delete orphan tags (for each tag, if there is no other contentItem than the one being deleted, it's an orphan)
|
|
||||||
foreach ( var tag in tagsPart.CurrentTags ) {
|
|
||||||
if ( tagsContentItemsRepository.Fetch(x => x.TagsPartRecord.Id != context.ContentItem.Id).Count() == 0 ) {
|
|
||||||
tagsRepository.Delete(tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete tag links with this contentItem (tagsContentItems)
|
|
||||||
foreach ( var tagsContentItem in tagsContentItemsRepository.Fetch(x => x.TagsPartRecord.Id == context.ContentItem.Id) ) {
|
|
||||||
tagsContentItemsRepository.Delete(tagsContentItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
OnIndexing<TagsPart>((context, tagsPart) => context.DocumentIndex
|
|
||||||
.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -8,6 +8,7 @@ namespace Orchard.Tags.Services {
|
|||||||
TagRecord GetTag(int tagId);
|
TagRecord GetTag(int tagId);
|
||||||
TagRecord GetTagByName(string tagName);
|
TagRecord GetTagByName(string tagName);
|
||||||
IEnumerable<IContent> GetTaggedContentItems(int tagId);
|
IEnumerable<IContent> GetTaggedContentItems(int tagId);
|
||||||
|
IEnumerable<IContent> GetTaggedContentItems(int tagId, VersionOptions options);
|
||||||
|
|
||||||
TagRecord CreateTag(string tagName);
|
TagRecord CreateTag(string tagName);
|
||||||
|
|
||||||
@@ -16,5 +17,6 @@ namespace Orchard.Tags.Services {
|
|||||||
void UpdateTag(int tagId, string tagName);
|
void UpdateTag(int tagId, string tagName);
|
||||||
|
|
||||||
void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem);
|
void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem);
|
||||||
|
void RemoveTagsForContentItem(ContentItem contentItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -109,9 +109,13 @@ namespace Orchard.Tags.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IContent> GetTaggedContentItems(int tagId) {
|
public IEnumerable<IContent> GetTaggedContentItems(int tagId) {
|
||||||
|
return GetTaggedContentItems(tagId, VersionOptions.Published);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IContent> GetTaggedContentItems(int tagId, VersionOptions options) {
|
||||||
return _contentTagRepository
|
return _contentTagRepository
|
||||||
.Fetch(x => x.TagRecord.Id == tagId)
|
.Fetch(x => x.TagRecord.Id == tagId)
|
||||||
.Select(t => _orchardServices.ContentManager.Get(t.TagsPartRecord.Id))
|
.Select(t => _orchardServices.ContentManager.Get(t.TagsPartRecord.Id, options))
|
||||||
.Where(c => c != null);
|
.Where(c => c != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +125,27 @@ namespace Orchard.Tags.Services {
|
|||||||
_contentTagRepository.Create(tagsContentItems);
|
_contentTagRepository.Create(tagsContentItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemoveTagsForContentItem(ContentItem contentItem) {
|
||||||
|
if (contentItem.Id == 0)
|
||||||
|
throw new OrchardException(T("Error removing tag to content item: the content item has not been created yet."));
|
||||||
|
|
||||||
|
_contentTagRepository.Flush();
|
||||||
|
|
||||||
|
var tagsPart = contentItem.As<TagsPart>();
|
||||||
|
|
||||||
|
// delete orphan tags (for each tag, if there is no other contentItem than the one being deleted, it's an orphan)
|
||||||
|
foreach (var tag in tagsPart.CurrentTags) {
|
||||||
|
if (_contentTagRepository.Count(x => x.TagsPartRecord.Id != contentItem.Id) == 0) {
|
||||||
|
_tagRepository.Delete(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete tag links with this contentItem (ContentTagRecords)
|
||||||
|
foreach (var record in _contentTagRepository.Fetch(x => x.TagsPartRecord.Id == contentItem.Id)) {
|
||||||
|
_contentTagRepository.Delete(record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem) {
|
public void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem) {
|
||||||
if (contentItem.Id == 0)
|
if (contentItem.Id == 0)
|
||||||
throw new OrchardException(T("Error adding tag to content item: the content item has not been created yet."));
|
throw new OrchardException(T("Error adding tag to content item: the content item has not been created yet."));
|
||||||
|
Reference in New Issue
Block a user