|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Nop.Plugin.Shipping.USPS.Domain; |
| 5 | +using Nop.Plugin.Shipping.USPS.Models; |
| 6 | +using Nop.Services.Configuration; |
| 7 | +using Nop.Services.Localization; |
| 8 | +using Nop.Services.Security; |
| 9 | +using Nop.Web.Framework; |
| 10 | +using Nop.Web.Framework.Controllers; |
| 11 | +using Nop.Web.Framework.Mvc.Filters; |
| 12 | + |
| 13 | +namespace Nop.Plugin.Shipping.USPS.Controllers |
| 14 | +{ |
| 15 | + [AuthorizeAdmin] |
| 16 | + [Area(AreaNames.Admin)] |
| 17 | + public class ShippingUSPSController : BasePluginController |
| 18 | + { |
| 19 | + #region Fields |
| 20 | + |
| 21 | + private readonly USPSSettings _uspsSettings; |
| 22 | + private readonly ISettingService _settingService; |
| 23 | + private readonly ILocalizationService _localizationService; |
| 24 | + private readonly IPermissionService _permissionService; |
| 25 | + |
| 26 | + #endregion |
| 27 | + |
| 28 | + #region Ctor |
| 29 | + |
| 30 | + public ShippingUSPSController(USPSSettings uspsSettings, |
| 31 | + ISettingService settingService, |
| 32 | + ILocalizationService localizationService, |
| 33 | + IPermissionService permissionService) |
| 34 | + { |
| 35 | + this._uspsSettings = uspsSettings; |
| 36 | + this._settingService = settingService; |
| 37 | + this._localizationService = localizationService; |
| 38 | + this._permissionService = permissionService; |
| 39 | + } |
| 40 | + |
| 41 | + #endregion |
| 42 | + |
| 43 | + #region Methods |
| 44 | + |
| 45 | + public IActionResult Configure() |
| 46 | + { |
| 47 | + if (!_permissionService.Authorize(StandardPermissionProvider.ManageShippingSettings)) |
| 48 | + return AccessDeniedView(); |
| 49 | + |
| 50 | + var model = new USPSShippingModel(); |
| 51 | + model.Url = _uspsSettings.Url; |
| 52 | + model.Username = _uspsSettings.Username; |
| 53 | + model.Password = _uspsSettings.Password; |
| 54 | + model.AdditionalHandlingCharge = _uspsSettings.AdditionalHandlingCharge; |
| 55 | + |
| 56 | + // Load Domestic service names |
| 57 | + string carrierServicesOfferedDomestic = _uspsSettings.CarrierServicesOfferedDomestic; |
| 58 | + foreach (string service in USPSServices.DomesticServices) |
| 59 | + model.AvailableCarrierServicesDomestic.Add(service); |
| 60 | + |
| 61 | + if (!String.IsNullOrEmpty(carrierServicesOfferedDomestic)) |
| 62 | + foreach (string service in USPSServices.DomesticServices) |
| 63 | + { |
| 64 | + string serviceId = USPSServices.GetServiceIdDomestic(service); |
| 65 | + if (!String.IsNullOrEmpty(serviceId)) |
| 66 | + { |
| 67 | + // Add delimiters [] so that single digit IDs aren't found in multi-digit IDs |
| 68 | + if (carrierServicesOfferedDomestic.Contains($"[{serviceId}]")) |
| 69 | + model.CarrierServicesOfferedDomestic.Add(service); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Load Internation service names |
| 74 | + string carrierServicesOfferedInternational = _uspsSettings.CarrierServicesOfferedInternational; |
| 75 | + foreach (string service in USPSServices.InternationalServices) |
| 76 | + model.AvailableCarrierServicesInternational.Add(service); |
| 77 | + |
| 78 | + if (!String.IsNullOrEmpty(carrierServicesOfferedInternational)) |
| 79 | + foreach (string service in USPSServices.InternationalServices) |
| 80 | + { |
| 81 | + string serviceId = USPSServices.GetServiceIdInternational(service); |
| 82 | + if (!String.IsNullOrEmpty(serviceId)) |
| 83 | + { |
| 84 | + // Add delimiters [] so that single digit IDs aren't found in multi-digit IDs |
| 85 | + if (carrierServicesOfferedInternational.Contains($"[{serviceId}]")) |
| 86 | + model.CarrierServicesOfferedInternational.Add(service); |
| 87 | + } |
| 88 | + } |
| 89 | + return View("~/Plugins/Shipping.USPS/Views/Configure.cshtml", model); |
| 90 | + } |
| 91 | + |
| 92 | + [HttpPost] |
| 93 | + [AdminAntiForgery] |
| 94 | + public IActionResult Configure(USPSShippingModel model) |
| 95 | + { |
| 96 | + if (!_permissionService.Authorize(StandardPermissionProvider.ManageShippingSettings)) |
| 97 | + return AccessDeniedView(); |
| 98 | + |
| 99 | + if (!ModelState.IsValid) |
| 100 | + return Configure(); |
| 101 | + |
| 102 | + //save settings |
| 103 | + _uspsSettings.Url = model.Url; |
| 104 | + _uspsSettings.Username = model.Username; |
| 105 | + _uspsSettings.Password = model.Password; |
| 106 | + _uspsSettings.AdditionalHandlingCharge = model.AdditionalHandlingCharge; |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + // Save selected Domestic services |
| 111 | + var carrierServicesOfferedDomestic = new StringBuilder(); |
| 112 | + int carrierServicesDomesticSelectedCount = 0; |
| 113 | + if (model.CheckedCarrierServicesDomestic != null) |
| 114 | + { |
| 115 | + foreach (var cs in model.CheckedCarrierServicesDomestic) |
| 116 | + { |
| 117 | + carrierServicesDomesticSelectedCount++; |
| 118 | + |
| 119 | + string serviceId = USPSServices.GetServiceIdDomestic(cs); |
| 120 | + //unselect any other services if NONE is selected |
| 121 | + if (!String.IsNullOrEmpty(serviceId) && serviceId.Equals("NONE")) |
| 122 | + { |
| 123 | + carrierServicesOfferedDomestic.Clear(); |
| 124 | + carrierServicesOfferedDomestic.AppendFormat("[{0}]:", serviceId); |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + if (!String.IsNullOrEmpty(serviceId)) |
| 129 | + { |
| 130 | + // Add delimiters [] so that single digit IDs aren't found in multi-digit IDs |
| 131 | + carrierServicesOfferedDomestic.AppendFormat("[{0}]:", serviceId); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + // Add default options if no services were selected |
| 136 | + if (carrierServicesDomesticSelectedCount == 0) |
| 137 | + _uspsSettings.CarrierServicesOfferedDomestic = "[1]:[3]:[4]:"; |
| 138 | + else |
| 139 | + _uspsSettings.CarrierServicesOfferedDomestic = carrierServicesOfferedDomestic.ToString(); |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + // Save selected International services |
| 144 | + var carrierServicesOfferedInternational = new StringBuilder(); |
| 145 | + int carrierServicesInternationalSelectedCount = 0; |
| 146 | + if (model.CheckedCarrierServicesInternational != null) |
| 147 | + { |
| 148 | + foreach (var cs in model.CheckedCarrierServicesInternational) |
| 149 | + { |
| 150 | + carrierServicesInternationalSelectedCount++; |
| 151 | + string serviceId = USPSServices.GetServiceIdInternational(cs); |
| 152 | + // unselect other services if NONE is selected |
| 153 | + if (!String.IsNullOrEmpty(serviceId) && serviceId.Equals("NONE")) |
| 154 | + { |
| 155 | + carrierServicesOfferedInternational.Clear(); |
| 156 | + carrierServicesOfferedInternational.AppendFormat("[{0}]:", serviceId); |
| 157 | + break; |
| 158 | + } |
| 159 | + if (!String.IsNullOrEmpty(serviceId)) |
| 160 | + { |
| 161 | + // Add delimiters [] so that single digit IDs aren't found in multi-digit IDs |
| 162 | + carrierServicesOfferedInternational.AppendFormat("[{0}]:", serviceId); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + // Add default options if no services were selected |
| 167 | + if (carrierServicesInternationalSelectedCount == 0) |
| 168 | + _uspsSettings.CarrierServicesOfferedInternational = "[2]:[15]:[1]:"; |
| 169 | + else |
| 170 | + _uspsSettings.CarrierServicesOfferedInternational = carrierServicesOfferedInternational.ToString(); |
| 171 | + |
| 172 | + |
| 173 | + _settingService.SaveSetting(_uspsSettings); |
| 174 | + |
| 175 | + SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved")); |
| 176 | + |
| 177 | + return Configure(); |
| 178 | + } |
| 179 | + |
| 180 | + #endregion |
| 181 | + } |
| 182 | +} |
0 commit comments