Navigation Menu

Skip to content

Commit

Permalink
Initial implementation with script inclusion and checkbox for runnin …
Browse files Browse the repository at this point in the history
…it on the admin
  • Loading branch information
Piedone committed Feb 28, 2014
1 parent 2f4368e commit 2ed9d28
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 41 deletions.
36 changes: 36 additions & 0 deletions Drivers/AnalyticsSettingsPartDriver.cs
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lombiq.SimpleAnalytics.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;

namespace Lombiq.SimpleAnalytics.Drivers
{
public class AnalyticsSettingsPartDriver : ContentPartDriver<AnalyticsSettingsPart>
{
protected override DriverResult Editor(AnalyticsSettingsPart part, dynamic shapeHelper)
{
return Editor(part, null, shapeHelper);

}

protected override DriverResult Editor(AnalyticsSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
{
return ContentShape("Parts_AnalyticsSettings_SiteSettings_Edit",
() =>
{
if (updater != null)
{
updater.TryUpdateModel(part, Prefix, null, null);
}
return shapeHelper.EditorTemplate(
TemplateName: "Parts.AnalyticsSettings.SiteSettings",
Model: part,
Prefix: Prefix);
});
}
}
}
49 changes: 49 additions & 0 deletions Filters/AnalyticsScriptInjectingFilter.cs
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orchard.Mvc.Filters;
using Orchard.Settings;
using Orchard.UI.Admin;
using Orchard.UI.Resources;
using Orchard.ContentManagement;
using Lombiq.SimpleAnalytics.Models;

namespace Lombiq.SimpleAnalytics.Filters
{
public class AnalyticsScriptInjectingFilter : FilterProvider, IResultFilter
{
private readonly ISiteService _siteService;
private readonly IResourceManager _resourceManager;


public AnalyticsScriptInjectingFilter(
ISiteService siteService,
IResourceManager resourceManager)
{
_siteService = siteService;
_resourceManager = resourceManager;
}


public void OnResultExecuting(ResultExecutingContext filterContext)
{
// Should only run on a full view rendering result and on the frontend only.
if (!(filterContext.Result is ViewResult)) return;

var settings = _siteService.GetSiteSettings().As<AnalyticsSettingsPart>();

if (!settings.IncludeOnAdmin && AdminFilter.IsApplied(filterContext.RequestContext)) return;

if (string.IsNullOrEmpty(settings.AnalyticsScript)) return;

// In case you haven't seen javascript in an MVC result filter today.
_resourceManager.RegisterHeadScript("<script type=\"text/javascript\">" + settings.AnalyticsScript + "</script>");
}

public void OnResultExecuted(ResultExecutedContext filterContext)
{
}
}
}
17 changes: 17 additions & 0 deletions Handlers/AnalyticsSettingsPartHandler.cs
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lombiq.SimpleAnalytics.Models;
using Orchard.ContentManagement.Handlers;

namespace Lombiq.SimpleAnalytics.Handlers
{
public class AnalyticsSettingsPartHandler : ContentHandler
{
public AnalyticsSettingsPartHandler()
{
Filters.Add(new ActivatingFilter<AnalyticsSettingsPart>("Site"));
}
}
}
28 changes: 19 additions & 9 deletions Lombiq.SimpleAnalytics.csproj
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand All @@ -19,6 +19,11 @@
<OldToolsVersion>4.0</OldToolsVersion>
<UpgradeBackupLocation />
<TargetFrameworkProfile />
<UseIISExpress>false</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -63,16 +68,8 @@
</ItemGroup>
<ItemGroup>
<Content Include="Web.config" />
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />
<Content Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" />
<Folder Include="Properties" />
<Folder Include="Controllers" />
<Folder Include="Views" />
<Folder Include="Models" />
<Folder Include="Scripts" />
<Folder Include="Styles" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Orchard\Orchard.Framework.csproj">
Expand All @@ -84,6 +81,19 @@
<Name>Orchard.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Drivers\AnalyticsSettingsPartDriver.cs" />
<Compile Include="Filters\AnalyticsScriptInjectingFilter.cs" />
<Compile Include="Handlers\AnalyticsSettingsPartHandler.cs" />
<Compile Include="Models\AnalyticsSettingsPart.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include="Placement.info" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\EditorTemplates\Parts.AnalyticsSettings.SiteSettings.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
23 changes: 23 additions & 0 deletions Models/AnalyticsSettingsPart.cs
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;

namespace Lombiq.SimpleAnalytics.Models
{
public class AnalyticsSettingsPart : ContentPart
{
public string AnalyticsScript
{
get { return this.Retrieve(x => x.AnalyticsScript); }
set { this.Store(x => x.AnalyticsScript, value); }
}

public bool IncludeOnAdmin
{
get { return this.Retrieve(x => x.IncludeOnAdmin); }
set { this.Store(x => x.IncludeOnAdmin, value); }
}
}
}
3 changes: 3 additions & 0 deletions Placement.info
@@ -0,0 +1,3 @@
<Placement>
<Place Parts_AnalyticsSettings_SiteSettings_Edit="Content:10"/>
</Placement>
16 changes: 0 additions & 16 deletions Scripts/Web.config

This file was deleted.

16 changes: 0 additions & 16 deletions Styles/Web.config

This file was deleted.

19 changes: 19 additions & 0 deletions Views/EditorTemplates/Parts.AnalyticsSettings.SiteSettings.cshtml
@@ -0,0 +1,19 @@
@model Lombiq.SimpleAnalytics.Models.AnalyticsSettingsPart

<fieldset>
<legend>@T("Analytics Settings")</legend>
<ol>
<li>
@Html.LabelFor(m => m.AnalyticsScript, T("Analytics script"))
@Html.TextAreaFor(m => m.AnalyticsScript, new { @class = "text large" })
@Html.ValidationMessageFor(m => m.AnalyticsScript)
<span class="hint">@T("This script will be injected into the head of the site, wrapped into a script tag (so don't include the script tag). E.g. you can copy your async Google Analytics tracking code here.")</span>
</li>
<li>
@Html.EditorFor(m => m.IncludeOnAdmin)
<label for="@Html.FieldIdFor(m => m.IncludeOnAdmin)" class="forcheckbox">@T("Include script on the admin site")</label>
@Html.ValidationMessageFor(m => m.IncludeOnAdmin)
<span class="hint">@T("If checked, the script will be included not just on the frontend but also on the admin.")</span>
</li>
</ol>
</fieldset>

0 comments on commit 2ed9d28

Please sign in to comment.