|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text.RegularExpressions; |
|
using Telerik.Microsoft.Practices.Unity; |
|
using Telerik.Sitefinity.Abstractions; |
|
using Telerik.Sitefinity.Abstractions.VirtualPath.Configuration; |
|
using Telerik.Sitefinity.Configuration; |
|
using Telerik.Sitefinity.Data; |
|
using Telerik.Sitefinity.Localization; |
|
using Telerik.Sitefinity.Modules.GenericContent.Web.UI; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
using Telerik.Sitefinity.Personalization; |
|
using Telerik.Sitefinity.Personalization.Impl; |
|
using Telerik.Sitefinity.Personalization.Impl.Configuration; |
|
using Telerik.Sitefinity.Personalization.Impl.Model; |
|
using Telerik.Sitefinity.Services; |
|
using Telerik.Sitefinity.Workflow; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class Installer |
|
{ |
|
public static void PreApplicationStart() |
|
{ |
|
Bootstrapper.Initialized += (new EventHandler<ExecutedEventArgs>(Installer.Bootstrapper_Initialized)); |
|
} |
|
|
|
private static void Bootstrapper_Initialized(object sender, ExecutedEventArgs e) |
|
{ |
|
if (e.CommandName == "Bootstrapped") |
|
{ |
|
SystemManager.RunWithElevatedPrivilegeDelegate worker = new SystemManager.RunWithElevatedPrivilegeDelegate(CreateSampleWorker); |
|
SystemManager.RunWithElevatedPrivilege(worker); |
|
} |
|
} |
|
|
|
private static void CreateSampleWorker(object[] parameters) |
|
{ |
|
if (!IsPersonalizationModuleInstalled()) |
|
{ |
|
return; |
|
} |
|
|
|
AddVirtualPathToEmbeddedRes(); |
|
CreateCriteria(); |
|
RegisterCriteria(); |
|
CreateSegment(); |
|
CreateAndPersonalizePage(); |
|
} |
|
|
|
private static bool IsPersonalizationModuleInstalled() |
|
{ |
|
return SystemManager.ApplicationModules != null && |
|
|
|
SystemManager.ApplicationModules.ContainsKey(PersonalizationModule.ModuleName) && |
|
!(SystemManager.ApplicationModules[PersonalizationModule.ModuleName] is InactiveModule); |
|
} |
|
|
|
private static void AddVirtualPathToEmbeddedRes() |
|
{ |
|
//Register the resource file |
|
|
|
Res.RegisterResource<CustomPersonalizationResources>(); |
|
|
|
var virtualPathConfig = Config.Get<VirtualPathSettingsConfig>(); |
|
|
|
if (!virtualPathConfig.VirtualPaths.Contains(virtualPath + "*")) |
|
{ |
|
var pathConfig = new VirtualPathElement(virtualPathConfig.VirtualPaths) |
|
{ |
|
VirtualPath = Installer.virtualPath + "*", |
|
ResolverName = "EmbeddedResourceResolver", |
|
ResourceLocation = "DayOfWeekPersonalization" |
|
}; |
|
|
|
virtualPathConfig.VirtualPaths.Add(pathConfig); |
|
|
|
ConfigManager.GetManager().SaveSection(virtualPathConfig); |
|
} |
|
} |
|
|
|
private static void CreateCriteria() |
|
{ |
|
var personalizationConfig = Config.Get<PersonalizationConfig>(); |
|
|
|
if (!personalizationConfig.Criteria.Contains(Res.Get<CustomPersonalizationResources>().Day)) |
|
{ |
|
CriterionElement ageCriterion = new CriterionElement(personalizationConfig.Criteria) |
|
{ |
|
Name = Res.Get<CustomPersonalizationResources>().Day, |
|
Title = Res.Get<CustomPersonalizationResources>().Day, |
|
ResourceClassId = typeof(CustomPersonalizationResources).Name, |
|
CriterionEditorUrl = "DayOfWeekPersonalization.DayOfWeekEditor.ascx", |
|
ConsoleCriterionEditorUrl = "DayOfWeekPersonalization.DayOfWeekEditor.ascx", |
|
CriterionVirtualPathPrefix = Installer.virtualPath, |
|
GroupName = "DayOfWeekPersonalization", |
|
}; |
|
|
|
personalizationConfig.Criteria.Add(ageCriterion); |
|
} |
|
} |
|
|
|
private static void RegisterCriteria() |
|
{ |
|
ObjectFactory.Container.RegisterType( |
|
|
|
typeof(ICriterionEvaluator), |
|
typeof(DayOfWeekEvaluator), |
|
|
|
Res.Get<CustomPersonalizationResources>().Day, |
|
|
|
new ContainerControlledLifetimeManager(), |
|
new InjectionConstructor()); |
|
} |
|
|
|
public static void CreateSegment() |
|
{ |
|
var personalizationManager = PersonalizationManager.GetManager(); |
|
|
|
using (new ElevatedModeRegion(personalizationManager)) |
|
{ |
|
if (!personalizationManager.GetSegments().Any(s => s.Id == Installer.segmentId)) |
|
{ |
|
var segment = personalizationManager.CreateSegment(Installer.segmentId); |
|
|
|
segment.Name = "Weekend segment"; |
|
segment.Description = "Pages will be personalized according to the day of the week"; |
|
segment.IsActive = true; |
|
|
|
CriteriaGroup group = new CriteriaGroup(); |
|
Criterion saturdayCriterion = new Criterion(); |
|
|
|
saturdayCriterion.CriterionValue = ((int)DayOfWeek.Saturday).ToString(); |
|
saturdayCriterion.CriterionDisplayValue = Res.Get<CustomPersonalizationResources>().Saturday; |
|
saturdayCriterion.CriterionName = Res.Get<CustomPersonalizationResources>().Day; |
|
saturdayCriterion.CriterionTitle = Res.Get<CustomPersonalizationResources>().Day; |
|
|
|
Criterion sundayCriterion = new Criterion(); |
|
|
|
sundayCriterion.CriterionValue = ((int)DayOfWeek.Sunday).ToString(); |
|
sundayCriterion.CriterionDisplayValue = Res.Get<CustomPersonalizationResources>().Sunday; |
|
sundayCriterion.CriterionName = Res.Get<CustomPersonalizationResources>().Day; |
|
sundayCriterion.CriterionTitle = Res.Get<CustomPersonalizationResources>().Day; |
|
|
|
group.Criteria.Add(saturdayCriterion); |
|
group.Criteria.Add(sundayCriterion); |
|
segment.CriteriaGroups.Add(group); |
|
personalizationManager.SaveChanges(); |
|
} |
|
} |
|
} |
|
|
|
private static void CreateAndPersonalizePage() |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
|
|
if (!pageManager.GetPageNodes().Any(a => a.Name == Installer.pageName)) |
|
{ |
|
CreatePage(Installer.pageName, false); |
|
AddControlToPage(); |
|
PersonalizePage(); |
|
} |
|
} |
|
|
|
public static void CreatePage(string pageName, bool isHomePage) |
|
{ |
|
PageManager manager = PageManager.GetManager(); |
|
|
|
PageData pageData = null; |
|
PageNode pageNode = null; |
|
|
|
// Get the parent node Id |
|
|
|
var parentPageNodeId = SiteInitializer.CurrentFrontendRootNodeId; |
|
|
|
PageNode parent = manager.GetPageNode(parentPageNodeId); |
|
|
|
// Check whether exists |
|
|
|
var initialPageNode = manager.GetPageNodes().Where(n => n.Id == Installer.pageId).SingleOrDefault(); |
|
|
|
if (initialPageNode != null) |
|
{ |
|
return; |
|
} |
|
|
|
// Create the page |
|
|
|
pageNode = manager.CreatePage(parent, Installer.pageId, NodeType.Standard); |
|
|
|
//pageData.NavigationNode = pageNode; |
|
|
|
pageData = pageNode.GetPageData(); |
|
pageData.HtmlTitle = pageName; |
|
pageNode.Title = pageName; |
|
pageNode.Description = pageName; |
|
pageNode.Name = pageName; |
|
pageNode.ShowInNavigation = true; |
|
|
|
// Check whether home page |
|
|
|
if (isHomePage) |
|
{ |
|
SystemManager.CurrentContext.CurrentSite.SetHomePage(Installer.pageId); |
|
} |
|
|
|
manager.SaveChanges(); |
|
|
|
// Publish |
|
|
|
var bag = new Dictionary<string, string>(); |
|
bag.Add("ContentType", typeof(PageNode).FullName); |
|
WorkflowManager.MessageWorkflow(Installer.pageId, typeof(PageNode), null, "Publish", false, bag); |
|
} |
|
|
|
public static void AddControlToPage() |
|
{ |
|
//get a PageManager object |
|
|
|
PageManager pageManager = PageManager.GetManager(); |
|
|
|
var pageData = pageManager.GetPageNode(Installer.pageId).GetPageData(); |
|
var draftPage = pageManager.EditPage(pageData.Id); |
|
var contentBlock = new ContentBlock(); |
|
|
|
contentBlock.Html = Installer.weekdayContent + Installer.instructionContent; |
|
|
|
var draftControl = pageManager.CreateControl<PageDraftControl>(contentBlock, "Body"); |
|
|
|
draftControl.Caption = "Content block"; |
|
draftPage.Controls.Add(draftControl); |
|
|
|
//Save the changes |
|
|
|
pageManager.PublishPageDraft(draftPage, SystemManager.CurrentContext.Culture); |
|
pageManager.SaveChanges(); |
|
} |
|
|
|
public static void PersonalizePage() |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
|
|
var pageNode = pageManager.GetPageNode(Installer.pageId); |
|
|
|
if (pageNode != null) |
|
{ |
|
var personalizationManager = PersonalizationManager.GetManager(); |
|
|
|
personalizationManager.CreatePersonalizedPage(pageNode.GetPageData().Id, Installer.segmentId); |
|
personalizationManager.SaveChanges(); |
|
|
|
var personalizedPageData = pageManager.GetPageDataList().FirstOrDefault(p => p.PersonalizationSegmentId == Installer.segmentId); |
|
|
|
if (personalizedPageData != null) |
|
{ |
|
var draftPage = pageManager.EditPage(personalizedPageData.Id); |
|
var contentItem = draftPage.Controls.FirstOrDefault(); |
|
var currentControl = pageManager.LoadControl(contentItem) as ContentBlock; |
|
|
|
currentControl.Html = Installer.weekendContent + Installer.instructionContent; |
|
|
|
// Copy the properties of the current Content Block item to the newly created |
|
|
|
pageManager.ReadProperties(currentControl, contentItem); |
|
|
|
//Save the changes |
|
|
|
pageManager.PublishPageDraft(draftPage); |
|
pageManager.SaveChanges(); |
|
} |
|
} |
|
} |
|
|
|
private static readonly string pageName = "DayOfWeekPage"; |
|
private static readonly string virtualPath = "~/SFCustomPersonalization/"; |
|
private static readonly string weekdayContent = |
|
@"<h1>Weekday page.</h1> |
|
<p>This page is visible during weekdays.</p>"; |
|
private static readonly string weekendContent = |
|
@"<h1>Weekend page.</h1> |
|
<p>This page is visible during weekends.</p>"; |
|
private static readonly string instructionContent = |
|
@"<p>To change when this page is visible</p> |
|
<ol> |
|
<li>Go to Sitefinity's Backend</li> |
|
<li>Select <i>Marketing > Personalization</i></li> |
|
<li>Select the <i>Weekend segment</i></li> |
|
<li>Modify the existing <i>characteristics</i></li> |
|
<li>Click <i>Save changes</i></li> |
|
<li>Refresh this page</li> |
|
</ol>"; |
|
private static readonly Guid segmentId = new Guid("2E68BB39-9D4E-4D8E-B53A-551B81D2FBA4"); |
|
private static readonly Guid pageId = new Guid("5B9B0941-3B78-4F23-842D-95820FDC5D0C"); |
|
public const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+"; |
|
public const string UrlNameReplaceString = "-"; |
|
} |
|
} |