public HttpResponseMessage Save([FromBody] List <DashboardViewModel> model)
        {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

            // dashboard data is stored using the user id so, we need to get the user
            var currentUser = PortalsHelpers.GetCurrentUser();

            // loop through the dashboard items and get them ready for persisting
            foreach (var dashboardModel in model)
            {
                // this will be our final list of items that we'll store
                var dashboardItems = new List <DashboardItem>();

                foreach (var dashboardItem in dashboardModel.DashboardItems)
                {
                    // this ensures that the user has access to the dashboard item they're trying to save
                    ToolboxItem toolboxItem = null;
                    if (PortalsHelpers.GetToolboxItem(dashboardItem.ControllerType, out toolboxItem))
                    {
                        // get the properties that users are able to change on the front end
                        var configurableProperties = PortalsHelpers.GetConfigurableProperties(toolboxItem.ControllerType);

                        var configuredProperties = dashboardItem.Properties;//.FromJson<List<PortalsItemProperty>>();

                        // just to keep things tidy...
                        configuredProperties.RemoveAll(p => p.Value == string.Empty);

                        // if the user has actually set any configurable property values, make sure they're valid
                        foreach (var property in configuredProperties)
                        {
                            // check to make sure a previously configurable property is still configurable
                            if (!configurableProperties.Any(p => p.Name == property.Name))
                            {
                                configuredProperties.Remove(property);
                            }
                        }

                        // done with sanitization, let's build the item we're going to persist
                        var item = new DashboardItem()
                        {
                            ControllerType = dashboardItem.ControllerType,
                            Properties     = configuredProperties.Select(p => new PortalsItemProperty()
                            {
                                Name = p.Name, Value = p.Value
                            }).ToList()
                        };

                        dashboardItems.Add(item);
                    }
                }

                currentUser.SaveDashboard(dashboardModel.DashboardId, dashboardItems);
            }

            return(response);
        }
        public ActionResult Configure()
        {
            if (SystemManager.IsDesignMode)
            {
                // rendering these widgets in design mode would just get messy, best not to do it
                return(View("PageEditor"));
            }

            var model = new DashboardToolboxViewModel();

            model.DashboardToolboxItems = new List <DashboardToolboxItemViewModel>();

            // get all the toolbox items that the currently logged in user has access to
            var toolboxItems = PortalsHelpers.GetToolboxItems();

            // loop through the toolbox items and create view models for them
            foreach (var toolboxItem in toolboxItems)
            {
                var item = new DashboardToolboxItemViewModel();

                item.ControllerType = toolboxItem.Value.ControllerType;
                item.Title          = toolboxItem.Value.Title;
                item.ThumbnailUrl   = toolboxItem.Value.CssClass;

                // get all the configurable properties for this item
                // (this is necessary for the front end designers to have correct information)
                var configurableProperties = PortalsHelpers.GetConfigurableProperties(toolboxItem.Value.ControllerType);

                // this will be the property list that we pass to the view
                var propertyViewModels = new List <PortalsItemPropertyViewModel>();

                foreach (var property in configurableProperties)
                {
                    // build the view model for the front end
                    var propertyViewModel = new PortalsItemPropertyViewModel();

                    propertyViewModel.Name        = property.Name;
                    propertyViewModel.DisplayName = configurableProperties.Where(p => p.Name == property.Name).Single().DashboardConfigurableAttribute.DisplayName ?? property.Name;
                    propertyViewModel.Value       = string.Empty;

                    propertyViewModels.Add(propertyViewModel);
                }

                item.Properties = propertyViewModels;

                model.DashboardToolboxItems.Add(item);
            }

            return(View("Configure", model));
        }
Пример #3
0
        // *sigh* yes....i'm doing this in the controller. Feel free to move it somewhere else. :)
        private DashboardViewModel GetDashboardModel(bool executeControllers = true)
        {
            var dashboardsManager = UserDashboardsManager.GetManager();

            // dashboard data is stored using the user id so, we need to get the user
            var currentUser = PortalsHelpers.GetCurrentUser();

            // get this dashboard's data for the current user
            var dashboard = currentUser.GetDashboard(DashboardId);

            // instantiate a new DashboardViewModel and set the property values that we know we need
            var model = new DashboardViewModel();

            model.DashboardId    = DashboardId;
            model.DashboardItems = new List <DashboardItemViewModel>();

            // loop through the dashboard items and create view models for them
            foreach (var dashboardItem in dashboard.DashboardItems)
            {
                // get the corresponding Sitefinity ToolboxItem for the given dashboard item type
                ToolboxItem toolboxItem = null;
                if (PortalsHelpers.GetToolboxItem(dashboardItem.ControllerType, out toolboxItem))
                {
                    // MvcControllerProxy is the source of all this sorcery
                    var proxy = new MvcControllerProxy()
                    {
                        ControllerName = toolboxItem.ControllerType
                    };

                    // get the properties that users are able to change on the front end
                    // this will *not* include any user defined values...just properties that *can* be configured
                    var configurableProperties = PortalsHelpers.GetConfigurableProperties(toolboxItem.ControllerType);

                    // this will be the property list (with user values set) that we pass to the view
                    var propertyViewModels = new List <PortalsItemPropertyViewModel>();

                    foreach (var property in configurableProperties)
                    {
                        string value = string.Empty;
                        // if the user has actually set any configurable property value, transfer the value to the controller
                        if (dashboardItem.Properties.Any(p => p.Name == property.Name))
                        {
                            value = dashboardItem.Properties.Single(p => p.Name == property.Name).Value;

                            // type the value correctly
                            object propValue = PortalsHelpers.GetValueFromString(value, property.PropertyType);

                            // fairly confident that this isn't a good way to do this but...it works
                            var backingProperty = configurableProperties.Where(p => p.Name == property.Name).Single().DashboardConfigurableAttribute.BackingProperty ?? property.Name;

                            proxy.Controller.SetProperty(backingProperty, propValue);
                        }

                        // build the view model for the front end
                        var propertyViewModel = new PortalsItemPropertyViewModel();

                        propertyViewModel.Name        = property.Name;
                        propertyViewModel.DisplayName = configurableProperties.Where(p => p.Name == property.Name).Single().DashboardConfigurableAttribute.DisplayName ?? property.Name;
                        propertyViewModel.Value       = value;

                        propertyViewModels.Add(propertyViewModel);
                    }

                    // build a new DashboardItemViewModel with all the properties we need to render, configure, and save the item
                    var dashboardItemModel = new DashboardItemViewModel();

                    dashboardItemModel.Title          = toolboxItem.Title;
                    dashboardItemModel.ControllerType = toolboxItem.ControllerType;
                    dashboardItemModel.Properties     = propertyViewModels;
                    dashboardItemModel.Html           = executeControllers ? ExecuteController(proxy) : string.Empty;

                    model.DashboardItems.Add(dashboardItemModel);
                }
            }

            return(model);
        }