|
| 1 | +using SeoToolkit.Umbraco.ScriptManager.Core.Enums; |
| 2 | +using SeoToolkit.Umbraco.ScriptManager.Core.Helpers; |
| 3 | +using SeoToolkit.Umbraco.ScriptManager.Core.Interfaces; |
| 4 | +using SeoToolkit.Umbraco.ScriptManager.Core.Models.Business; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Umbraco.Cms.Core.PropertyEditors; |
| 11 | + |
| 12 | +namespace SeoToolkit.Umbraco.ScriptManager.Core.ScriptDefinitions |
| 13 | +{ |
| 14 | + public class PiwikProDefinition : IScriptDefinition |
| 15 | + { |
| 16 | + private readonly ViewRenderHelper _viewRenderHelper; |
| 17 | + private const string accountAddressKey = "accountAddress"; |
| 18 | + private const string siteIdKey = "siteId"; |
| 19 | + |
| 20 | + //Name of the definition |
| 21 | + public string Name => "Piwik PRO"; |
| 22 | + //Alias of the definition. Used in the database |
| 23 | + public string Alias => "piwikpro"; |
| 24 | + |
| 25 | + //Umbraco Configuration fields of the definition |
| 26 | + public ConfigurationField[] Fields => new ConfigurationField[] |
| 27 | + { |
| 28 | + new () { |
| 29 | + Key = accountAddressKey, |
| 30 | + Name = "Account Address", |
| 31 | + Description = "Your Piwik PRO account address. Make sure the address contains https:// and a trailing slash.", |
| 32 | + View = "textstring" |
| 33 | + }, |
| 34 | + new () { |
| 35 | + Key = siteIdKey, |
| 36 | + Name = "Site Id", |
| 37 | + Description = "Your Piwik PRO site ID", |
| 38 | + View = "textstring" |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + public PiwikProDefinition(ViewRenderHelper viewRenderHelper) |
| 43 | + { |
| 44 | + _viewRenderHelper = viewRenderHelper; |
| 45 | + } |
| 46 | + |
| 47 | + //Render function where you add your script to the correct location. You can use the ViewRenderHelper to render a cshtml view. |
| 48 | + public void Render(ScriptRenderModel model, Dictionary<string, object> config) |
| 49 | + { |
| 50 | + if (!config.ContainsKey(accountAddressKey) || string.IsNullOrWhiteSpace(config[accountAddressKey]?.ToString())) |
| 51 | + return; |
| 52 | + |
| 53 | + if (!config.ContainsKey(siteIdKey) || string.IsNullOrWhiteSpace(config[siteIdKey]?.ToString())) |
| 54 | + return; |
| 55 | + |
| 56 | + var accountAddress = config[accountAddressKey].ToString(); |
| 57 | + var siteId = config[siteIdKey].ToString(); |
| 58 | + |
| 59 | + string[] viewData = new[] { accountAddress, siteId }; |
| 60 | + |
| 61 | + model.AddScript(ScriptPositionType.BodyTop, _viewRenderHelper.RenderView("~/Views/ScriptManager/PiwikPro/Script.cshtml", viewData)); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments