Beispiel #1
0
        public ActionResult AddWidget(int hostId, string widgetType, string zone, string returnUrl)
        {
            if (!IsAuthorizedToManageWidgets())
            {
                return(new HttpUnauthorizedResult());
            }

            var widgetPart = _services.ContentManager.New <WidgetPart>(widgetType);

            if (widgetPart == null)
            {
                return(HttpNotFound());
            }

            try {
                var widgetPosition = _widgetManager.GetWidgets(hostId).Count(widget => widget.Zone == widgetPart.Zone) + 1;
                widgetPart.Position        = widgetPosition.ToString(CultureInfo.InvariantCulture);
                widgetPart.Zone            = zone;
                widgetPart.AvailableLayers = _widgetsService.GetLayers().ToList();
                widgetPart.LayerPart       = _widgetManager.GetContentLayer();

                var model = _services.ContentManager.BuildEditor(widgetPart).HostId(hostId);
                return(View(model));
            }
            catch (Exception exception) {
                Logger.Error(T("Creating widget failed: {0}", exception.Message).Text);
                _services.Notifier.Error(T("Creating widget failed: {0}", exception.Message));
                return(this.RedirectLocal(returnUrl, () => RedirectToAction("Edit", "Admin", new { area = "Contents" })));
            }
        }
Beispiel #2
0
        protected override DriverResult Editor(LayerPart layerPart, IUpdateModel updater, dynamic shapeHelper)
        {
            if (updater.TryUpdateModel(layerPart, Prefix, null, null))
            {
                if (String.IsNullOrWhiteSpace(layerPart.LayerRule))
                {
                    layerPart.LayerRule = "true";
                }

                if (_widgetsService.GetLayers()
                    .Any(l =>
                         l.Id != layerPart.Id &&
                         String.Equals(l.Name, layerPart.Name, StringComparison.InvariantCultureIgnoreCase)))
                {
                    updater.AddModelError("Name", T("A Layer with the same name already exists"));
                }

                try {
                    _ruleManager.Matches(layerPart.LayerRule);
                }
                catch (Exception e) {
                    updater.AddModelError("Description", T("The rule is not valid: {0}", e.Message));
                }
            }

            return(Editor(layerPart, shapeHelper));
        }
        public LayerPart GetContentLayer()
        {
            var contentLayer = _widgetsService.GetLayers().FirstOrDefault(x => x.Name == "ContentWidgets")
                               ?? _widgetsService.CreateLayer("ContentWidgets", "This layer never activates, but is needed for the widgets hosted by content items for now.", "false");

            return(contentLayer);
        }
Beispiel #4
0
        public ActionResult Index(int?layerId)
        {
            ExtensionDescriptor currentTheme = _siteThemeService.GetSiteTheme();

            if (currentTheme == null)
            {
                Services.Notifier.Error(T("To manage widgets you must have a theme enabled."));
                return(RedirectToAction("Index", "Admin", new { area = "Dashboard" }));
            }

            IEnumerable <LayerPart> layers = _widgetsService.GetLayers().ToList();

            if (!layers.Any())
            {
                Services.Notifier.Error(T("There are no widget layers defined. A layer will need to be added in order to add widgets to any part of the site."));
                return(RedirectToAction("AddLayer"));
            }

            LayerPart currentLayer = layerId == null
                ? layers.FirstOrDefault()
                : layers.FirstOrDefault(layer => layer.Id == layerId);

            if (currentLayer == null && layerId != null)   // Incorrect layer id passed
            {
                Services.Notifier.Error(T("Layer not found: {0}", layerId));
                return(RedirectToAction("Index"));
            }

            IEnumerable <string> allZones           = _widgetsService.GetZones();
            IEnumerable <string> currentThemesZones = _widgetsService.GetZones(currentTheme);

            string zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id);
            string zonePreviewImage     = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null;

            dynamic viewModel = Shape.ViewModel()
                                .CurrentTheme(currentTheme)
                                .CurrentLayer(currentLayer)
                                .Layers(layers)
                                .Widgets(_widgetsService.GetWidgets())
                                .Zones(currentThemesZones)
                                .OrphanZones(allZones.Except(currentThemesZones))
                                .OrphanWidgets(_widgetsService.GetOrphanedWidgets())
                                .ZonePreviewImage(zonePreviewImage);

            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
            return(View((object)viewModel));
        }
Beispiel #5
0
        protected override DriverResult Editor(WidgetsContainerPart part, dynamic shapeHelper)
        {
            return(ContentShape("Parts_WidgetsContainer", () => {
                var settings = part.Settings.GetModel <WidgetsContainerSettings>();

                var currentTheme = _siteThemeService.GetSiteTheme();
                var currentThemesZones = _widgetsService.GetZones(currentTheme).ToList();
                var widgetTypes = _widgetsService.GetWidgetTypeNames().ToList();
                if (!settings.UseHierarchicalAssociation)
                {
                    if (!string.IsNullOrWhiteSpace(settings.AllowedZones))
                    {
                        currentThemesZones = currentThemesZones.Where(x => settings.AllowedZones.Split(',').Contains(x)).ToList();
                    }
                    if (!string.IsNullOrWhiteSpace(settings.AllowedWidgets))
                    {
                        widgetTypes = widgetTypes.Where(x => settings.AllowedWidgets.Split(',').Contains(x)).ToList();
                    }
                }
                else if (settings.HierarchicalAssociation != null && settings.HierarchicalAssociation.Count() > 0)
                {
                    currentThemesZones = currentThemesZones.Where(ctz => settings.HierarchicalAssociation.Select(x => x.ZoneName).Contains(ctz)).ToList();
                    widgetTypes = widgetTypes.Where(w => settings.HierarchicalAssociation.Any(x => x.Widgets.Any(a => a.WidgetType == w || a.WidgetType == "All"))).ToList();
                }

                var widgets = _widgetManager.GetWidgets(part.Id, false);

                var zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id);
                var zonePreviewImage = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null;

                var layer = _widgetsService.GetLayers().First();

                // recupero i contenuti localizzati una try è necessaria in quanto non è detto che un contenuto sia localizzato
                dynamic contentLocalizations;
                try {
                    contentLocalizations = _localizationService
                                           .GetLocalizations(part.ContentItem, VersionOptions.Latest) //the other cultures
                                           .Where(lp =>                                               //as long as a culture has been assigned
                                                  lp.Culture != null && !string.IsNullOrWhiteSpace(lp.Culture.Culture))
                                           .OrderBy(o => o.Culture.Culture)
                                           .ToList();
                } catch {
                    contentLocalizations = null;
                }

                var viewModel = New.ViewModel()
                                .CurrentTheme(currentTheme)
                                .Zones(currentThemesZones)
                                .ContentLocalizations(contentLocalizations)
                                .ZonePreviewImage(zonePreviewImage)
                                .WidgetTypes(widgetTypes)
                                .Widgets(widgets)
                                .ContentItem(part.ContentItem)
                                .LayerId(layer.Id)
                                .CloneFrom(0);

                return shapeHelper.EditorTemplate(TemplateName: "Parts.WidgetsContainer", Model: viewModel, Prefix: Prefix);
            }));
        }
Beispiel #6
0
        public ActionResult IndexPOST()
        {
            if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate the navigation.")))
            {
                return(new HttpUnauthorizedResult());
            }

            var menus = _menuService.GetMenus();

            if (menus.Any())
            {
                _orchardServices.Notifier.Error(T("This step is unnecessary as some menus already exist."));
                return(View());
            }

            // create a Main Menu
            var mainMenu = _menuService.Create("Main Menu");

            _orchardServices.Notifier.Success(T("Main menu created"));

            // assign the Main Menu to all current menu items
            foreach (var menuItem in _menuService.Get())
            {
                // if they don't have a position or a text, then they are not displayed
                if (string.IsNullOrWhiteSpace(menuItem.MenuPosition) || string.IsNullOrEmpty(menuItem.MenuText))
                {
                    continue;
                }
                menuItem.Menu = mainMenu.ContentItem;
            }
            _orchardServices.Notifier.Success(T("Menu items moved to Main menu"));

            // a widget should is created to display the navigation
            var layer = _widgetsService.GetLayers().FirstOrDefault(x => x.Name == "Default");

            if (layer == null)
            {
                _orchardServices.Notifier.Warning(T("Widget could not be created. Please create it manually."));
            }

            var widget = _widgetsService.CreateWidget(layer.Id, "MenuWidget", "Main Menu", "1.0", "Navigation");

            widget.RenderTitle = false;

            var menuWidget = widget.As <MenuWidgetPart>();

            menuWidget.MenuContentItemId = mainMenu.ContentItem.Id;

            menuWidget.StartLevel     = 1;
            menuWidget.Levels         = 1;
            menuWidget.Breadcrumb     = false;
            menuWidget.AddHomePage    = false;
            menuWidget.AddCurrentPage = false;

            _orchardServices.ContentManager.Publish(menuWidget.ContentItem);

            return(View("Index"));
        }
        protected override DriverResult Editor(WidgetPart widgetPart, dynamic shapeHelper)
        {
            widgetPart.AvailableZones  = _widgetsService.GetZones();
            widgetPart.AvailableLayers = _widgetsService.GetLayers();

            var results = new List <DriverResult> {
                ContentShape("Parts_Widgets_WidgetPart",
                             () => shapeHelper.EditorTemplate(TemplateName: "Parts.Widgets.WidgetPart", Model: widgetPart, Prefix: Prefix))
            };

            if (widgetPart.Id > 0)
            {
                results.Add(ContentShape("Widget_DeleteButton",
                                         deleteButton => deleteButton));
            }

            return(Combined(results.ToArray()));
        }
Beispiel #8
0
        protected override DriverResult Editor(WidgetsContainerPart part, dynamic shapeHelper)
        {
            return(ContentShape("Parts_WidgetsContainer", () => {
                var currentTheme = _siteThemeService.GetSiteTheme();
                var currentThemesZones = _widgetsService.GetZones(currentTheme).ToList();
                var widgetTypes = _widgetsService.GetWidgetTypeNames().ToList();
                var widgets = _widgetManager.GetWidgets(part.Id, VersionOptions.Latest);
                var zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id);
                var zonePreviewImage = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null;
                var layer = _widgetsService.GetLayers().First();

                var viewModel = New.ViewModel()
                                .CurrentTheme(currentTheme)
                                .Zones(currentThemesZones)
                                .ZonePreviewImage(zonePreviewImage)
                                .WidgetTypes(widgetTypes)
                                .Widgets(widgets)
                                .ContentItem(part.ContentItem)
                                .LayerId(layer.Id);

                return shapeHelper.EditorTemplate(TemplateName: "Parts.WidgetsContainer", Model: viewModel, Prefix: Prefix);
            }));
        }
        private LayerPart GetLayer(string layer)
        {
            var layers = _widgetsService.GetLayers();

            return(layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, layer, StringComparison.OrdinalIgnoreCase)));
        }
        public ActionResult Index(int?layerId, string culture)
        {
            ExtensionDescriptor currentTheme = _siteThemeService.GetSiteTheme();

            if (currentTheme == null)
            {
                Services.Notifier.Error(T("To manage widgets you must have a theme enabled."));
                return(RedirectToAction("Index", "Admin", new { area = "Dashboard" }));
            }

            IEnumerable <LayerPart> layers = _widgetsService.GetLayers().OrderBy(x => x.Name).ToList();

            if (!layers.Any())
            {
                Services.Notifier.Error(T("There are no widget layers defined. A layer will need to be added in order to add widgets to any part of the site."));
                return(RedirectToAction("AddLayer"));
            }

            LayerPart currentLayer = layerId == null
                                     // look for the "Default" layer, or take the first if it doesn't exist
                ? layers.FirstOrDefault(x => x.Name == "Default") ?? layers.FirstOrDefault()
                : layers.FirstOrDefault(layer => layer.Id == layerId);

            if (currentLayer == null && layerId != null)   // Incorrect layer id passed
            {
                Services.Notifier.Error(T("Layer not found: {0}", layerId));
                return(RedirectToAction("Index"));
            }

            IEnumerable <string> allZones           = _widgetsService.GetZones();
            IEnumerable <string> currentThemesZones = _widgetsService.GetZones(currentTheme);

            string zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id);
            string zonePreviewImage     = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null;

            var widgets = _widgetsService.GetWidgets();

            if (!String.IsNullOrWhiteSpace(culture))
            {
                widgets = widgets.Where(x => {
                    if (x.Has <ILocalizableAspect>())
                    {
                        return(String.Equals(x.As <ILocalizableAspect>().Culture, culture, StringComparison.InvariantCultureIgnoreCase));
                    }

                    return(false);
                }).ToList();
            }

            var viewModel = Shape.ViewModel()
                            .CurrentTheme(currentTheme)
                            .CurrentLayer(currentLayer)
                            .CurrentCulture(culture)
                            .Layers(layers)
                            .Widgets(widgets)
                            .Zones(currentThemesZones)
                            .Cultures(_cultureManager.ListCultures())
                            .OrphanZones(allZones.Except(currentThemesZones))
                            .OrphanWidgets(_widgetsService.GetOrphanedWidgets())
                            .ZonePreviewImage(zonePreviewImage);

            return(View(viewModel));
        }