-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagsModule.cs
63 lines (54 loc) · 1.93 KB
/
TagsModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using Geta.Tags.Helpers;
using Geta.Tags.Implementations;
using Geta.Tags.Interfaces;
namespace Geta.Tags
{
[ModuleDependency(typeof (ServiceContainerInitialization))]
public class TagsModule : IInitializableModule
{
private ITagService _tagService;
private PageTypeRepository _pageTypeRepository;
private void OnPublishedPage(object sender, PageEventArgs e)
{
var page = e.Page;
var tags = GetPageTags(page);
_tagService.Save(page.PageGuid, tags);
}
private IEnumerable<string> GetPageTags(PageData page)
{
var pageType = _pageTypeRepository.Load(page.PageTypeID);
return pageType.PropertyDefinitions
.Where(TagsHelper.IsTagProperty)
.SelectMany(x => GetPropertyTags(page, x));
}
private static IEnumerable<string> GetPropertyTags(PageData page, PropertyDefinition propertyDefinition)
{
var tagNames = page[propertyDefinition.Name] as string;
return tagNames == null
? Enumerable.Empty<string>()
: tagNames.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
}
public void Initialize(InitializationEngine context)
{
_tagService = new TagService();
_pageTypeRepository = ServiceLocator.Current.GetInstance<PageTypeRepository>();
DataFactory.Instance.PublishedPage += OnPublishedPage;
}
public void Uninitialize(InitializationEngine context)
{
DataFactory.Instance.PublishedPage -= OnPublishedPage;
}
public void Preload(string[] parameters)
{
}
}
}