Ejemplo n.º 1
0
        public override WidgetBase UnPackWidget(ZipFileInfoCollection pack)
        {
            Models.SectionWidget widget = null;
            foreach (ZipFileInfo item in pack)
            {
                if (item.RelativePath.EndsWith("-widget.json"))
                {
                    widget = JsonConvert.DeserializeObject <Models.SectionWidget>(Encoding.UTF8.GetString(item.FileBytes), new SectionContentJsonConverter());
                    continue;
                }

                using (var fs = File.Create((ApplicationContext as CMSApplicationContext).MapPath("~" + item.RelativePath)))
                {
                    fs.Write(item.FileBytes, 0, item.FileBytes.Length);
                }

                if (item.RelativePath.EndsWith(".json"))
                {
                    var template = JsonConvert.DeserializeObject <SectionTemplate>(Encoding.UTF8.GetString(item.FileBytes));
                    if (_sectionTemplateService.Count(m => m.TemplateName == template.TemplateName) > 0)
                    {
                        _sectionTemplateService.Update(template);
                    }
                    else
                    {
                        _sectionTemplateService.Add(template);
                    }
                }
            }
            return(widget);
        }
Ejemplo n.º 2
0
        private void UpdateSectionTemplates(IEnumerable <SectionTemplate> sectionTemplates, IEnumerable <Type> controllers)
        {
            if (sectionTemplates == null || !sectionTemplates.Any())
            {
                return;
            }

            foreach (var sectionTemplate in sectionTemplates)
            {
                var templateController = controllers.FirstOrDefault(x => x.Name == sectionTemplate.ControllerName);

                if (templateController == null)
                {
                    throw new ArgumentException(string.Format(Messages.ControllerNotFound, sectionTemplate.ControllerName));
                }

                var controllerActions = GetActionsForController(templateController).ToList();

                var controllerActionNames = controllerActions.Select(x => x.Name).ToList();
                var sectionActionNames    = sectionTemplate.PageTemplates.Select(x => x.ActionName).ToList();

                var sectionTemplateAttribute = (CmsSectionTemplateAttribute)templateController.GetCustomAttributes(typeof(CmsSectionTemplateAttribute), false).FirstOrDefault();

                if (sectionTemplateAttribute == null)
                {
                    throw new ArgumentException(string.Format(Messages.ControllerDoesNotHaveCmsSectionTempalteAttribute, templateController.Name));
                }

                //update properties on the section template
                _sectionTemplateService.Update(sectionTemplate, sectionTemplateAttribute.Name, sectionTemplateAttribute.IconImageName, false);

                var pageTemplatesToRemove = sectionTemplate.PageTemplates.Where(x => !controllerActionNames.Contains(x.ActionName)).ToList();
                var newActionsToAdd       = controllerActions.Where(x => !sectionActionNames.Contains(x.Name)).ToList();
                var pageTemplatesToUpdate = sectionTemplate.PageTemplates.Where(x => controllerActionNames.Contains(x.ActionName)).ToList();

                //remove any page templates that don't have an action anymore
                RemovePageTemplatesWithNoExistingAction(pageTemplatesToRemove);

                //create page templates for any actions that don't have a template with that action name
                CreatePageTemplatesForActions(sectionTemplate, newActionsToAdd);

                //update any page templates that still have an action
                UpdatePageTemplates(pageTemplatesToUpdate, controllerActions);
            }
        }