Guest User

Untitled

a guest
Nov 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. private readonly IStoreContext _storeContext;
  2. private readonly ISettingService _settingService;
  3. private readonly ILogger _logger;
  4. private readonly ILocalizationService _localizationService;
  5. private readonly IWorkContext _workContext;
  6. private readonly ITopicService _topicService;
  7. private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
  8.  
  9. #endregion
  10.  
  11. #region const
  12.  
  13. public PopupEngageFilterAttribute()
  14. {
  15. this._storeContext = EngineContext.Current.Resolve<IStoreContext>();
  16. this._settingService = EngineContext.Current.Resolve<ISettingService>();
  17. this._logger = EngineContext.Current.Resolve<ILogger>();
  18. this._localizationService = EngineContext.Current.Resolve<ILocalizationService>();
  19. this._workContext = EngineContext.Current.Resolve<IWorkContext>();
  20. this._topicService = EngineContext.Current.Resolve<ITopicService>();
  21. this._newsLetterSubscriptionService = EngineContext.Current.Resolve<INewsLetterSubscriptionService>();
  22. }
  23.  
  24. #endregion
  25.  
  26. #region methods
  27.  
  28. public void PopupEngageOnResultExecuted(ActionExecutedContext filterContext)
  29. {
  30. var storeId = _storeContext.CurrentStore.Id;
  31.  
  32. LicenseImplementer licenseImplementer = new LicenseImplementer();
  33.  
  34. // load plugin settings
  35. var _setting = _settingService.LoadSetting<PopupEngageSetting>(storeId);
  36. var allStoreSettings = _settingService.LoadSetting<PopupEngageSetting>(0);
  37.  
  38. //check plugin is enabled or not
  39. if (_setting.PopupEngageEnabled)
  40. {
  41. // check license
  42. //if (!licenseImplementer.IsLicenseActive(allStoreSettings.LicenseKey, allStoreSettings.OtherLicenseSettings))
  43. // return;
  44.  
  45. StringBuilder sb = new StringBuilder();
  46. string bioepEngageScript = string.Empty;
  47. string popupEngageView = string.Empty;
  48. string popupEngageScript = string.Empty;
  49. string newsLetterScript = string.Empty;
  50.  
  51. // get current customer
  52. var customer = _workContext.CurrentCustomer;
  53.  
  54. // check customer cart
  55. string customerCart = Convert.ToString(customer.HasShoppingCartItems);
  56.  
  57. // set cookie for customer cart
  58. filterContext.HttpContext.Response.Cookies.Append("CustomerCart", customerCart, new CookieOptions() { Path = "/", HttpOnly = false, Secure = false });
  59.  
  60. if(customerCart == "True")
  61. {
  62. // get bioep script file
  63. Stream bioepScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.bioep.min.js");
  64. if (bioepScriptFile != null)
  65. using (StreamReader reader = new StreamReader(bioepScriptFile))
  66. {
  67. bioepEngageScript = reader.ReadToEnd();
  68. }
  69.  
  70. // get PopupEngage script
  71. string path = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Script"), "PopupEngage.js");
  72.  
  73. if (File.Exists(path))
  74. {
  75. popupEngageScript = File.ReadAllText(path);
  76. }
  77.  
  78. // check current customers role
  79. var customerRole = customer.CustomerRoles.Where(x => x.Name == "Guests").FirstOrDefault();
  80. if (customerRole != null)
  81. {
  82. // get Popup View file
  83. string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngageNewsLetter.html");
  84. if (File.Exists(popupEngageViewFile))
  85. {
  86. popupEngageView = File.ReadAllText(popupEngageViewFile);
  87. }
  88.  
  89. // get NewsLetter Script file
  90. Stream newsLetterScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.NewsLetter.js");
  91. if (newsLetterScriptFile != null)
  92. using (StreamReader reader = new StreamReader(newsLetterScriptFile))
  93. {
  94. newsLetterScript = reader.ReadToEnd();
  95. }
  96. }
  97. else
  98. {
  99. // get Popup View file
  100. string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngage.html");
  101. if (File.Exists(popupEngageViewFile))
  102. {
  103. popupEngageView = File.ReadAllText(popupEngageViewFile);
  104. }
  105. }
  106.  
  107. var topicBody=string.Empty;
  108.  
  109. // get topic from settings
  110. var topic = _setting.TopicName;
  111. if (!string.IsNullOrEmpty(topic))
  112. {
  113. // get topic by system name
  114. var topicRecord = _topicService.GetTopicBySystemName(topic);
  115. if(topicRecord != null)
  116. {
  117. topicBody = topicRecord.Body;
  118. }
  119. }
  120.  
  121. // replace new line with slash and double coute with single coute
  122. popupEngageView = popupEngageView.Replace(Environment.NewLine, String.Empty).Replace(""", "'");
  123. topicBody = topicBody.Replace(Environment.NewLine, String.Empty).Replace(""", "'");
  124.  
  125. // append script
  126. sb.Append("<script type="text/javascript" src="/wwwroot/lib/jquery-1.10.2.min.js">nt");
  127. sb.Append(bioepEngageScript);
  128. sb.Append(popupEngageScript);
  129. sb.Append("$("" + popupEngageView + "").insertAfter(".newsletter");");
  130. sb.Append("$('.popupengage_popupmsg').html("" + topicBody + "");");
  131. sb.Append(newsLetterScript);
  132. sb.Append("</script>n");
  133. var bytes = Encoding.ASCII.GetBytes(sb.ToString());
  134. filterContext.HttpContext.Response.Body.WriteAsync(bytes,0, bytes.Length);
  135. }
  136. }
  137. }
  138. #endregion
Add Comment
Please, Sign In to add comment