using System.Globalization; using System.Linq.Expressions; using System.Text; using Nop.Core; using Nop.Core.Domain.Common; using Nop.Core.Domain.Seo; using Nop.Core.Infrastructure; using Nop.Data; using Nop.Services.Configuration; using Nop.Services.Seo; namespace Nop.Services.Installation; /// /// Installation service /// public partial class InstallationService : IInstallationService { #region Fields protected readonly INopDataProvider _dataProvider; protected readonly INopFileProvider _fileProvider; protected readonly IWebHelper _webHelper; protected string _defaultCustomerEmail; #endregion #region Ctor public InstallationService(INopDataProvider dataProvider, INopFileProvider fileProvider, IWebHelper webHelper) { _dataProvider = dataProvider; _fileProvider = fileProvider; _webHelper = webHelper; } #endregion #region Utilities /// /// Returns queryable source for specified mapping class for current connection, /// mapped to database table or view. /// /// Entity type /// Queryable source protected virtual IQueryable Table() where TEntity : BaseEntity { return _dataProvider.GetTable(); } /// /// Gets the first entity identifier /// /// Entity type /// A function to test each element for a condition /// A task that represents the asynchronous operation /// The task result contains the entity identifier or null, if entity is not exists protected virtual async Task GetFirstEntityIdAsync(Expression> predicate = null) where TEntity : BaseEntity { var entity = await Table().FirstOrDefaultAsync(predicate ?? (_ => true)); return entity?.Id; } /// /// Get the entity entry by identifier /// /// Entity entry identifier /// /// A task that represents the asynchronous operation /// The task result contains the entity entry /// protected virtual async Task GetByIdAsync(int? id) where TEntity : BaseEntity { if (!id.HasValue || id == 0) return null; return await Table().FirstOrDefaultAsync(entity => entity.Id == id.Value); } /// /// Validate search engine name /// /// Entity /// Search engine name to validate /// /// A task that represents the asynchronous operation /// The task result contains the valid seName /// protected virtual async Task ValidateSeNameAsync(T entity, string seName) where T : BaseEntity { //duplicate of ValidateSeName method of \Nop.Services\Seo\UrlRecordService.cs (we cannot inject it here) ArgumentNullException.ThrowIfNull(entity); //validation var okChars = "abcdefghijklmnopqrstuvwxyz1234567890 _-"; seName = seName.Trim().ToLowerInvariant(); var sb = new StringBuilder(); foreach (var c in seName.ToCharArray()) { var c2 = c.ToString(); if (okChars.Contains(c2)) sb.Append(c2); } seName = sb.ToString(); seName = seName.Replace(" ", "-"); while (seName.Contains("--")) seName = seName.Replace("--", "-"); while (seName.Contains("__")) seName = seName.Replace("__", "_"); //max length seName = CommonHelper.EnsureMaximumLength(seName, NopSeoDefaults.SearchEngineNameLength); //ensure this seName is not reserved yet var i = 2; var tempSeName = seName; while (true) { //check whether such slug already exists (and that is not the current entity) var currentSeName = tempSeName; var query = Table().Where(ur => currentSeName != null && ur.Slug == currentSeName); var urlRecord = await query.FirstOrDefaultAsync(); var entityName = entity.GetType().Name; var reserved = urlRecord != null && !(urlRecord.EntityId == entity.Id && urlRecord.EntityName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase)); if (!reserved) break; tempSeName = $"{seName}-{i}"; i++; } seName = tempSeName; return seName; } #endregion #region Methods /// /// Install required data /// /// Default user email /// Default user password /// Language pack info /// RegionInfo /// CultureInfo /// A task that represents the asynchronous operation public virtual async Task InstallRequiredDataAsync(string defaultUserEmail, string defaultUserPassword, (string languagePackDownloadLink, int languagePackProgress) languagePackInfo, RegionInfo regionInfo, CultureInfo cultureInfo) { if (string.IsNullOrEmpty(_defaultCustomerEmail) || !_defaultCustomerEmail.Equals(defaultUserEmail)) { _defaultCustomerEmail = defaultUserEmail; _defaultCustomerId = null; } await InstallStoresAsync(); await InstallMeasuresAsync(regionInfo); await InstallTaxCategoriesAsync(); await InstallLanguagesAsync(languagePackInfo, cultureInfo, regionInfo); await InstallCurrenciesAsync(cultureInfo, regionInfo); await InstallCountriesAndStatesAsync(); await InstallShippingMethodsAsync(); await InstallDeliveryDatesAsync(); await InstallProductAvailabilityRangesAsync(); await InstallEmailAccountsAsync(); await InstallMessageTemplatesAsync(); await InstallTopicTemplatesAsync(); await InstallSettingsAsync(regionInfo); await InstallCustomersAndUsersAsync(defaultUserPassword); await InstallTopicsAsync(); await InstallActivityLogTypesAsync(); await InstallProductTemplatesAsync(); await InstallCategoryTemplatesAsync(); await InstallManufacturerTemplatesAsync(); await InstallScheduleTasksAsync(); await InstallReturnRequestReasonsAsync(); await InstallReturnRequestActionsAsync(); } /// /// Install sample data /// /// Default user email /// A task that represents the asynchronous operation public virtual async Task InstallSampleDataAsync(string defaultUserEmail) { if (string.IsNullOrEmpty(_defaultCustomerEmail) || !_defaultCustomerEmail.Equals(defaultUserEmail)) { _defaultCustomerEmail = defaultUserEmail; _defaultCustomerId = null; } await InstallSampleCustomersAsync(); await InstallCheckoutAttributesAsync(); await InstallSpecificationAttributesAsync(); await InstallProductAttributesAsync(); await InstallCategoriesAsync(); await InstallManufacturersAsync(); await InstallProductsAsync(); await InstallForumsAsync(); await InstallDiscountsAsync(); await InstallBlogPostsAsync(); await InstallNewsAsync(); await InstallPollsAsync(); await InstallWarehousesAsync(); await InstallVendorsAsync(); await InstallAffiliatesAsync(); await InstallOrdersAsync(); await InstallActivityLogsAsync(); await InstallSearchTermsAsync(); var settingService = EngineContext.Current.Resolve(); await settingService.SaveSettingAsync(new DisplayDefaultMenuItemSettings { DisplayHomepageMenuItem = false, DisplayNewProductsMenuItem = false, DisplayProductSearchMenuItem = false, DisplayCustomerInfoMenuItem = false, DisplayBlogMenuItem = false, DisplayForumsMenuItem = false, DisplayContactUsMenuItem = false }); } #endregion }