Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private readonly IStoreContext _storeContext;
- private readonly ISettingService _settingService;
- private readonly ILogger _logger;
- private readonly ILocalizationService _localizationService;
- private readonly IWorkContext _workContext;
- private readonly ITopicService _topicService;
- private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
- #endregion
- #region const
- public PopupEngageFilterAttribute()
- {
- this._storeContext = EngineContext.Current.Resolve<IStoreContext>();
- this._settingService = EngineContext.Current.Resolve<ISettingService>();
- this._logger = EngineContext.Current.Resolve<ILogger>();
- this._localizationService = EngineContext.Current.Resolve<ILocalizationService>();
- this._workContext = EngineContext.Current.Resolve<IWorkContext>();
- this._topicService = EngineContext.Current.Resolve<ITopicService>();
- this._newsLetterSubscriptionService = EngineContext.Current.Resolve<INewsLetterSubscriptionService>();
- }
- #endregion
- #region methods
- public void PopupEngageOnResultExecuted(ActionExecutedContext filterContext)
- {
- var storeId = _storeContext.CurrentStore.Id;
- LicenseImplementer licenseImplementer = new LicenseImplementer();
- // load plugin settings
- var _setting = _settingService.LoadSetting<PopupEngageSetting>(storeId);
- var allStoreSettings = _settingService.LoadSetting<PopupEngageSetting>(0);
- //check plugin is enabled or not
- if (_setting.PopupEngageEnabled)
- {
- // check license
- //if (!licenseImplementer.IsLicenseActive(allStoreSettings.LicenseKey, allStoreSettings.OtherLicenseSettings))
- // return;
- StringBuilder sb = new StringBuilder();
- string bioepEngageScript = string.Empty;
- string popupEngageView = string.Empty;
- string popupEngageScript = string.Empty;
- string newsLetterScript = string.Empty;
- // get current customer
- var customer = _workContext.CurrentCustomer;
- // check customer cart
- string customerCart = Convert.ToString(customer.HasShoppingCartItems);
- // set cookie for customer cart
- filterContext.HttpContext.Response.Cookies.Append("CustomerCart", customerCart, new CookieOptions() { Path = "/", HttpOnly = false, Secure = false });
- if(customerCart == "True")
- {
- // get bioep script file
- Stream bioepScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.bioep.min.js");
- if (bioepScriptFile != null)
- using (StreamReader reader = new StreamReader(bioepScriptFile))
- {
- bioepEngageScript = reader.ReadToEnd();
- }
- // get PopupEngage script
- string path = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Script"), "PopupEngage.js");
- if (File.Exists(path))
- {
- popupEngageScript = File.ReadAllText(path);
- }
- // check current customers role
- var customerRole = customer.CustomerRoles.Where(x => x.Name == "Guests").FirstOrDefault();
- if (customerRole != null)
- {
- // get Popup View file
- string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngageNewsLetter.html");
- if (File.Exists(popupEngageViewFile))
- {
- popupEngageView = File.ReadAllText(popupEngageViewFile);
- }
- // get NewsLetter Script file
- Stream newsLetterScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.NewsLetter.js");
- if (newsLetterScriptFile != null)
- using (StreamReader reader = new StreamReader(newsLetterScriptFile))
- {
- newsLetterScript = reader.ReadToEnd();
- }
- }
- else
- {
- // get Popup View file
- string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngage.html");
- if (File.Exists(popupEngageViewFile))
- {
- popupEngageView = File.ReadAllText(popupEngageViewFile);
- }
- }
- var topicBody=string.Empty;
- // get topic from settings
- var topic = _setting.TopicName;
- if (!string.IsNullOrEmpty(topic))
- {
- // get topic by system name
- var topicRecord = _topicService.GetTopicBySystemName(topic);
- if(topicRecord != null)
- {
- topicBody = topicRecord.Body;
- }
- }
- // replace new line with slash and double coute with single coute
- popupEngageView = popupEngageView.Replace(Environment.NewLine, String.Empty).Replace(""", "'");
- topicBody = topicBody.Replace(Environment.NewLine, String.Empty).Replace(""", "'");
- // append script
- sb.Append("<script type="text/javascript" src="/wwwroot/lib/jquery-1.10.2.min.js">nt");
- sb.Append(bioepEngageScript);
- sb.Append(popupEngageScript);
- sb.Append("$("" + popupEngageView + "").insertAfter(".newsletter");");
- sb.Append("$('.popupengage_popupmsg').html("" + topicBody + "");");
- sb.Append(newsLetterScript);
- sb.Append("</script>n");
- var bytes = Encoding.ASCII.GetBytes(sb.ToString());
- filterContext.HttpContext.Response.Body.WriteAsync(bytes,0, bytes.Length);
- }
- }
- }
- #endregion
Add Comment
Please, Sign In to add comment