示例#1
0
        private async Task <RepositoryResponse <bool> > CreateDefaultThemeTemplatesAsync(SioCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            var result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };
            string defaultFolder = $"{SioService.GetConfig<string>(SioConstants.ConfigurationKeyword.DefaultBlankTemplateFolder) }";

            //CommonHelper.GetFullPath(new string[] {
            //    SioConstants.Folder.TemplatesFolder,
            //    SioService.GetConfig<string>(SioConstants.ConfigurationKeyword.DefaultTemplateFolder) });
            bool copyResult = FileRepository.Instance.CopyDirectory(defaultFolder, TemplateFolder);

            var files = FileRepository.Instance.GetFilesWithContent(TemplateFolder);
            var id    = _context.SioTemplate.Count() + 1;

            //TODO: Create default asset
            foreach (var file in files)
            {
                SioTemplates.InitViewModel template = new SioTemplates.InitViewModel(
                    new SioTemplate()
                {
                    Id              = id,
                    FileFolder      = file.FileFolder,
                    FileName        = file.Filename,
                    Content         = file.Content,
                    Extension       = file.Extension,
                    CreatedDateTime = DateTime.UtcNow,
                    LastModified    = DateTime.UtcNow,
                    ThemeId         = Model.Id,
                    ThemeName       = Model.Name,
                    FolderType      = file.FolderName,
                    ModifiedBy      = CreatedBy
                }, _context, _transaction);
                var saveResult = await template.SaveModelAsync(true, _context, _transaction);

                ViewModelHelper.HandleResult(saveResult, ref result);
                if (!result.IsSucceed)
                {
                    break;
                }
                else
                {
                    id += 1;
                }
            }
            return(result);
        }
示例#2
0
        public override async Task <RepositoryResponse <bool> > SaveSubModelsAsync(SioTheme parent, SioCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            RepositoryResponse <bool> result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            try
            {
                // Clone Default templates
                Name = SeoHelper.GetSEOString(Title);
                string defaultTemplateFolder = $"{SioService.GetConfig<string>(SioConstants.ConfigurationKeyword.DefaultTemplateFolder) }";
                bool   copyResult            = FileRepository.Instance.CopyDirectory(defaultTemplateFolder, TemplateFolder);
                string defaultAssetsFolder   = CommonHelper.GetFullPath(new string[] {
                    SioConstants.Folder.WebRootPath,
                    "assets",
                    SioConstants.Folder.TemplatesAssetFolder,
                    "default"
                });
                copyResult = FileRepository.Instance.CopyDirectory(defaultAssetsFolder, AssetFolder);

                var files = FileRepository.Instance.GetFilesWithContent(TemplateFolder);
                //TODO: Create default asset
                int id = 0;
                foreach (var file in files)
                {
                    id++;
                    string content = file.Content.Replace("/assets/templates/default", $"/{ SioConstants.Folder.FileFolder}/{SioConstants.Folder.TemplatesAssetFolder}/{Name}");
                    SioTemplates.InitViewModel template = new SioTemplates.InitViewModel(
                        new SioTemplate()
                    {
                        Id              = id,
                        FileFolder      = file.FileFolder,
                        FileName        = file.Filename,
                        Content         = content,
                        Extension       = file.Extension,
                        CreatedDateTime = DateTime.UtcNow,
                        LastModified    = DateTime.UtcNow,
                        ThemeId         = Model.Id,
                        ThemeName       = Model.Name,
                        FolderType      = file.FolderName,
                        ModifiedBy      = CreatedBy
                    }, _context, _transaction);
                    var saveResult = await template.SaveModelAsync(true, _context, _transaction);

                    result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
                    if (!saveResult.IsSucceed)
                    {
                        result.Exception = saveResult.Exception;
                        result.Errors.AddRange(saveResult.Errors);
                        break;
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Errors.Add(ex.Message);
                result.Exception = ex;
                return(result);
            }
        }
        public override async Task <RepositoryResponse <bool> > SaveSubModelsAsync(SioTheme parent, SioCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            RepositoryResponse <bool> result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            if (TemplateAsset.Content != null || TemplateAsset.FileStream != null)
            {
                ImportTheme(_context, _transaction);
            }
            if (Asset.Content != null || Asset.FileStream != null)
            {
                Asset.FileFolder = AssetFolder;
                Asset.Filename   = "assets";
                string fullPath = CommonHelper.GetFullPath(new string[] {
                    SioConstants.Folder.WebRootPath,
                    Asset.FileFolder
                });
                FileRepository.Instance.EmptyFolder(fullPath);
                var isSaved = FileRepository.Instance.SaveWebFile(Asset);
                result.IsSucceed = isSaved;
                if (isSaved)
                {
                    result.IsSucceed = FileRepository.Instance.UnZipFile(Asset);
                    if (!result.IsSucceed)
                    {
                        result.Errors.Add("Cannot unzip file");
                    }
                }
                else
                {
                    result.Errors.Add("Cannot saved asset file");
                }
            }
            if (Id == 0)
            {
                string defaultFolder = CommonHelper.GetFullPath(new string[] { SioConstants.Folder.TemplatesFolder, Name == "Default" ? "Default"
                    : SioService.GetConfig <string>(SioConstants.ConfigurationKeyword.DefaultTemplateFolder) });
                bool copyResult = FileRepository.Instance.CopyDirectory(defaultFolder, TemplateFolder);
                var  files      = copyResult ? FileRepository.Instance.GetFilesWithContent(TemplateFolder) : new List <FileViewModel>();
                //TODO: Create default asset
                foreach (var file in files)
                {
                    SioTemplates.InitViewModel template = new SioTemplates.InitViewModel(
                        new SioTemplate()
                    {
                        FileFolder      = file.FileFolder,
                        FileName        = file.Filename,
                        Content         = file.Content,
                        Extension       = file.Extension,
                        CreatedDateTime = DateTime.UtcNow,
                        LastModified    = DateTime.UtcNow,
                        ThemeId         = Model.Id,
                        ThemeName       = Model.Name,
                        FolderType      = file.FolderName,
                        ModifiedBy      = CreatedBy
                    });
                    var saveResult = await template.SaveModelAsync(true, _context, _transaction);

                    result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
                    if (!saveResult.IsSucceed)
                    {
                        result.Exception = saveResult.Exception;
                        result.Errors.AddRange(saveResult.Errors);
                        break;
                    }
                }
            }

            // Actived Theme
            if (IsActived)
            {
                SystemConfigurationViewModel config = (await SystemConfigurationViewModel.Repository.GetSingleModelAsync(
                                                           c => c.Keyword == SioConstants.ConfigurationKeyword.ThemeName && c.Specificulture == Specificulture
                                                           , _context, _transaction)).Data;
                if (config == null)
                {
                    config = new SystemConfigurationViewModel()
                    {
                        Keyword        = SioConstants.ConfigurationKeyword.ThemeName,
                        Specificulture = Specificulture,
                        Category       = "Site",
                        DataType       = SioDataType.Text,
                        Description    = "Cms Theme",
                        Value          = Name
                    };
                }
                else
                {
                    config.Value = Name;
                }

                var saveConfigResult = await config.SaveModelAsync(false, _context, _transaction);

                if (!saveConfigResult.IsSucceed)
                {
                    Errors.AddRange(saveConfigResult.Errors);
                }
                else
                {
                    //SioCmsService.Instance.RefreshConfigurations(_context, _transaction);
                }
                result.IsSucceed = result.IsSucceed && saveConfigResult.IsSucceed;

                SystemConfigurationViewModel configId = (await SystemConfigurationViewModel.Repository.GetSingleModelAsync(
                                                             c => c.Keyword == SioConstants.ConfigurationKeyword.ThemeId && c.Specificulture == Specificulture, _context, _transaction)).Data;
                if (configId == null)
                {
                    configId = new SystemConfigurationViewModel()
                    {
                        Keyword        = SioConstants.ConfigurationKeyword.ThemeId,
                        Specificulture = Specificulture,
                        Category       = "Site",
                        DataType       = SioDataType.Text,
                        Description    = "Cms Theme Id",
                        Value          = Model.Id.ToString()
                    };
                }
                else
                {
                    configId.Value = Model.Id.ToString();
                }
                var saveResult = await configId.SaveModelAsync(false, _context, _transaction);

                if (!saveResult.IsSucceed)
                {
                    Errors.AddRange(saveResult.Errors);
                }
                result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
            }

            if (Asset.Content != null || Asset.FileStream != null)
            {
                var           files     = FileRepository.Instance.GetWebFiles(AssetFolder);
                StringBuilder strStyles = new StringBuilder();

                foreach (var css in files.Where(f => f.Extension == ".css"))
                {
                    strStyles.Append($"   <link href='{css.FileFolder}/{css.Filename}{css.Extension}' rel='stylesheet'/>");
                }
                StringBuilder strScripts = new StringBuilder();
                foreach (var js in files.Where(f => f.Extension == ".js"))
                {
                    strScripts.Append($"  <script src='{js.FileFolder}/{js.Filename}{js.Extension}'></script>");
                }
                var layout = SioTemplates.InitViewModel.Repository.GetSingleModel(
                    t => t.FileName == "_Layout" && t.ThemeId == Model.Id
                    , _context, _transaction);
                layout.Data.Content = layout.Data.Content.Replace("<!--[STYLES]-->"
                                                                  , string.Format(@"{0}"
                                                                                  , strStyles));
                layout.Data.Content = layout.Data.Content.Replace("<!--[SCRIPTS]-->"
                                                                  , string.Format(@"{0}"
                                                                                  , strScripts));

                await layout.Data.SaveModelAsync(true, _context, _transaction);
            }

            return(result);
        }