Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation with script inclusion and checkbox for runnin …
…it on the admin
- Loading branch information
Showing
9 changed files
with
166 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<Placement> | ||
<Place Parts_AnalyticsSettings_SiteSettings_Edit="Content:10"/> | ||
</Placement> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
Views/EditorTemplates/Parts.AnalyticsSettings.SiteSettings.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |