public override RepositoryResponse <bool> SaveSubModels(SiocTheme parent, SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            RepositoryResponse <bool> result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            if (Asset != null && Asset.Length > 0)
            {
                string filename = FileRepository.Instance.SaveWebFile(Asset, AssetFolder);
                if (!string.IsNullOrEmpty(filename))
                {
                    FileRepository.Instance.UnZipFile(filename, AssetFolder);
                }
            }
            if (Id == 0)
            {
                string defaultFolder = CommonHelper.GetFullPath(new string[] { SWCmsConstants.Parameters.TemplatesFolder, SWCmsConstants.Default.DefaultTemplateFolder });
                bool   copyResult    = FileRepository.Instance.CopyDirectory(defaultFolder, TemplateFolder);
                var    files         = copyResult ? FileRepository.Instance.GetFilesWithContent(TemplateFolder) : new System.Collections.Generic.List <FileViewModel>();
                foreach (var file in files)
                {
                    BETemplateViewModel template = new BETemplateViewModel(
                        new SiocTemplate()
                    {
                        FileFolder      = file.FileFolder,
                        FileName        = file.Filename,
                        Content         = file.Content,
                        Extension       = file.Extension,
                        CreatedDateTime = DateTime.UtcNow,
                        LastModified    = DateTime.UtcNow,
                        TemplateId      = Model.Id,
                        TemplateName    = Model.Name,
                        FolderType      = file.FolderName,
                        ModifiedBy      = CreatedBy
                    });
                    var saveResult = template.SaveModel(true, _context, _transaction);
                    result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
                    if (!saveResult.IsSucceed)
                    {
                        result.Exception = saveResult.Exception;
                        result.Errors.AddRange(saveResult.Errors);
                        break;
                    }
                }
            }
            return(result);
        }
예제 #2
0
        public override RepositoryResponse <bool> SaveSubModels(SiocTheme parent, SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            RepositoryResponse <bool> result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            if (Asset != null && Asset.Length > 0)
            {
                string filename = FileRepository.Instance.SaveWebFile(Asset, AssetFolder);
                if (!string.IsNullOrEmpty(filename))
                {
                    FileRepository.Instance.UnZipFile(filename, AssetFolder);
                }
            }
            if (Id == 0)
            {
                string defaultFolder = CommonHelper.GetFullPath(new string[] { SWCmsConstants.Parameters.TemplatesFolder, Name == "Default" ? "Default" : SWCmsConstants.Default.DefaultTemplateFolder });
                bool   copyResult    = FileRepository.Instance.CopyDirectory(defaultFolder, TemplateFolder);
                var    files         = copyResult ? FileRepository.Instance.GetFilesWithContent(TemplateFolder) : new System.Collections.Generic.List <FileViewModel>();
                //TODO: Create default asset
                foreach (var file in files)
                {
                    BETemplateViewModel template = new BETemplateViewModel(
                        new SiocTemplate()
                    {
                        FileFolder      = file.FileFolder,
                        FileName        = file.Filename,
                        Content         = file.Content,
                        Extension       = file.Extension,
                        CreatedDateTime = DateTime.UtcNow,
                        LastModified    = DateTime.UtcNow,
                        TemplateId      = Model.Id,
                        TemplateName    = Model.Name,
                        FolderType      = file.FolderName,
                        ModifiedBy      = CreatedBy
                    }, _context, _transaction);
                    var saveResult = template.SaveModel(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)
            {
                InfoConfigurationViewModel config = (InfoConfigurationViewModel.Repository.GetSingleModel(
                                                         c => c.Keyword == SWCmsConstants.ConfigurationKeyword.Theme && c.Specificulture == Specificulture
                                                         , _context, _transaction)).Data;

                if (config == null)
                {
                    config = new InfoConfigurationViewModel(new SiocConfiguration()
                    {
                        Keyword        = SWCmsConstants.ConfigurationKeyword.Theme,
                        Specificulture = Specificulture,
                        Category       = SWCmsConstants.ConfigurationType.User,
                        DataType       = (int)SWCmsConstants.DataType.String,
                        Description    = "Cms Theme",
                        Value          = Name
                    }, _context, _transaction)
                    ;
                }
                else
                {
                    config.Value = Name;
                }

                var saveConfigResult = config.SaveModel(false, _context, _transaction);
                if (!saveConfigResult.IsSucceed)
                {
                    Errors.AddRange(saveConfigResult.Errors);
                }
                else
                {
                    GlobalConfigurationService.Instance.RefreshConfigurations(_context, _transaction);
                }
                result.IsSucceed = result.IsSucceed && saveConfigResult.IsSucceed;

                InfoConfigurationViewModel configId = (InfoConfigurationViewModel.Repository.GetSingleModel(
                                                           c => c.Keyword == SWCmsConstants.ConfigurationKeyword.ThemeId && c.Specificulture == Specificulture, _context, _transaction)).Data;
                if (configId == null)
                {
                    configId = new InfoConfigurationViewModel(new SiocConfiguration()
                    {
                        Keyword        = SWCmsConstants.ConfigurationKeyword.ThemeId,
                        Specificulture = Specificulture,
                        Category       = SWCmsConstants.ConfigurationType.User,
                        DataType       = (int)SWCmsConstants.DataType.String,
                        Description    = "Cms Theme Id",
                        Value          = Model.Id.ToString()
                    }, _context, _transaction)
                    ;
                }
                else
                {
                    configId.Value = Model.Id.ToString();
                }
                var saveResult = configId.SaveModel(false, _context, _transaction);
                if (!saveResult.IsSucceed)
                {
                    Errors.AddRange(saveResult.Errors);
                }
                else
                {
                    GlobalConfigurationService.Instance.RefreshConfigurations(_context, _transaction);
                }
                result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
            }

            if (Asset != null && Asset.Length > 0 && Id == 0)
            {
                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 = BETemplateViewModel.Repository.GetSingleModel(
                    t => t.FileName == "_Layout" && t.TemplateId == 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));

                layout.Data.SaveModel(true, _context, _transaction);
            }

            return(result);
        }