Inheritance: System.Entity
        /// <summary>
        /// Gets the widget view model.
        /// </summary>
        /// <param name="pageWidget">The page widget.</param>
        /// <returns></returns>
        public static WidgetHolderViewModel GetWidgetViewModel(PageWidget pageWidget)
        {
            if (pageWidget == null || pageWidget.Widget == null)
                return null;

            ICoreWidget coreWidget =
                MvcApplication.Widgets.FirstOrDefault(wd => wd.Identifier == pageWidget.Widget.Identifier);

            var permissionService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();
            ICorePrincipal currentPrincipal = HttpContext.Current.CorePrincipal();

            var widgetModel = new WidgetHolderViewModel
            {
                Widget = pageWidget,
                WidgetInstance = new CoreWidgetInstance
                {
                    InstanceId = pageWidget.InstanceId,
                    WidgetIdentifier = pageWidget.Widget.Identifier,
                    PageSettings = new CorePageSettings { PageId = pageWidget.Page.Id },
                    PageWidgetId = pageWidget.Id
                },

                SystemWidget = coreWidget,

                Access = coreWidget is BaseWidget ?
                                        permissionService.GetAccess(((BaseWidget)coreWidget).Operations,
                                        HttpContext.Current.CorePrincipal(), coreWidget.GetType(), pageWidget.EntityId,
                                        currentPrincipal != null && pageWidget.User != null && pageWidget.User.PrincipalId == currentPrincipal.PrincipalId) : null,
                PageAccess = permissionService.GetAccess(pageWidget.Page.Operations, currentPrincipal, typeof(Page),
                                        pageWidget.Page.Id, currentPrincipal != null && pageWidget.Page.User != null
                                        && pageWidget.Page.User.PrincipalId == currentPrincipal.PrincipalId),



            };

            if (pageWidget.Page.IsTemplate && pageWidget.Widget.IsPlaceHolder)
            {
                widgetModel.Access[((BaseWidget)widgetModel.SystemWidget).PermissionOperationCode] = false;
            }

            return widgetModel;

        }
Example #2
0
 public virtual void AddWidget(PageWidget widget)
 {
     widgets.Add(widget);
 }
Example #3
0
 /// <summary>
 /// Removes the widget.
 /// </summary>
 /// <param name="widget">The widget.</param>
 public virtual void RemoveWidget(PageWidget widget)
 {
     widgets.Remove(widget);
 }
        /// <summary>
        /// Determines whether [is manage widget allowed] [the specified widget identifier].
        /// </summary>
        /// <param name="pageWidget">The page widget.</param>
        /// <param name="user">The user.</param>
        /// <param name="entityId">The entity id.</param>
        /// <returns>
        /// 	<c>true</c> if [is manage widget allowed] [the specified widget identifier]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsManageWidgetAllowed(PageWidget pageWidget, ICorePrincipal user, long entityId)
        {
            bool isAllowed = true;

            if (pageWidget.Widget!=null)
            {
                ICoreWidget widget =
                    MvcApplication.Widgets.FirstOrDefault(item => item.Identifier == pageWidget.Widget.Identifier);
                if (widget != null && widget is BaseWidget)
                {
                    bool isOwner = pageWidget.User != null && user != null &&
                                   pageWidget.User.PrincipalId == user.PrincipalId;
                    var permissionService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();
                    isAllowed = permissionService.IsAllowed((widget as BaseWidget).ManageOperationCode, user,
                                                            widget.GetType(), entityId, isOwner,
                                                            PermissionOperationLevel.Object);
                }
            }

            return isAllowed;
        }
 /// <summary>
 /// Clones the widget instance.
 /// </summary>
 /// <param name="widget">The widget.</param>
 /// <returns></returns>
 private static long? CloneWidgetInstance(PageWidget widget)
 {
     var systemWidget = MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == widget.Widget.Identifier);
     if (widget.InstanceId != null)
     {
         return systemWidget.Clone(new CoreWidgetInstance { InstanceId = widget.InstanceId, WidgetIdentifier = systemWidget.Identifier });
     }
     return null;
 }
        /// <summary>
        /// Adds the widget to page.
        /// </summary>
        /// <param name="pageId">The page id.</param>
        /// <param name="widgetId">The widget id.</param>
        /// <param name="user">The user.</param>
        /// <param name="templateWidgetId">The template widget id.</param>
        /// <returns></returns>
        public static PageWidget AddWidgetToPage(long pageId, long? widgetId, ICorePrincipal user, long? templateWidgetId)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var widgetService = ServiceLocator.Current.GetInstance<IWidgetService>();
            var pageWidgetService = ServiceLocator.Current.GetInstance<IPageWidgetService>();

            var page = pageService.Find(pageId);

            if (page != null)
            {
                page.Widgets.Update(
                        wd =>
                        {
                            wd.OrderNumber =
                                wd.ColumnNumber == 1
                                     ? wd.OrderNumber + 1
                                     : wd.OrderNumber;
                        }
                        );

                pageService.Save(page);
                Widget widget = widgetService.Find(widgetId ?? 0);
                if (widget == null || widgetService.IsWidgetEnable(widget))
                {
                    var newPageWidget = new PageWidget
                                            {
                                                Page = page,
                                                ColumnNumber = 1,
                                                OrderNumber = 1,
                                                User =
                                                    user != null ? new User { Id = user.PrincipalId } : null,
                                                Widget = widget,
                                            };
                    if (templateWidgetId.HasValue)
                    {
                        newPageWidget.TemplateWidgetId = templateWidgetId;
                    }

                    if (pageWidgetService.Save(newPageWidget))
                        return newPageWidget;
                }
            }

            return null;
        }
        public static bool ClonePageSettingsFromTemplate(Page template, Page targetPage, long? widgetId)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var permissionCommonService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();

            template.Widgets.Where(widget => widget.Widget.IsPlaceHolder).AsParallel().ForAll(widget =>
            {
                var pageWidget = new PageWidget();
                pageWidget.InjectFrom<CloneEntityInjection>(widget);
                pageWidget.Page = targetPage;
                pageWidget.ParentWidgetId = widget.Id;

                //copy widget settings
                if (widget.Settings != null)
                {
                    pageWidget.Settings = (PageWidgetSettings)new PageWidgetSettings().InjectFrom<CloneEntityInjection>(widget.Settings);
                    pageWidget.Settings.LookAndFeelSettings = new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(widget.Settings.LookAndFeelSettings) as LookAndFeelSettings;
                    pageWidget.Settings.Widget = pageWidget;
                }
                if (widgetId.HasValue)
                {
                    var widgetService = ServiceLocator.Current.GetInstance<IWidgetService>();
                    pageWidget.Widget = widgetService.Find(widgetId.Value);
                }
                pageWidget.TemplateWidgetId = widget.Id;
                targetPage.AddWidget(pageWidget);
            });
            targetPage.Template = template;

            if (pageService.Save(targetPage))
            {
                //copy permissions and update page styles
                foreach (var item in targetPage.Widgets)
                {
                    if (item.ParentWidgetId == null) continue;
                    var systemWidget = MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == item.Widget.Identifier);
                    var sourceWidget = template.Widgets.FirstOrDefault(w => w.Id == item.ParentWidgetId);
                    if (sourceWidget != null)
                    {
                        if (targetPage.Settings != null && !String.IsNullOrEmpty(targetPage.Settings.CustomCSS))
                        {
                            targetPage.Settings.CustomCSS = targetPage.Settings.CustomCSS.Replace(String.Format(PageWidgetTemplate, sourceWidget.Id), String.Format(PageWidgetTemplate, item.Id));
                        }
                        permissionCommonService.CloneObjectPermisions(systemWidget.GetType(), sourceWidget.Id, item.Id);
                    }
                }
                return pageService.Save(targetPage);
            }
            return false;
        }
        /// <summary>
        /// Clones the target page settings to source page.
        /// </summary>
        /// <param name="sourcePage">The source page.</param>
        /// <param name="targetPage">The target page.</param>
        /// <returns></returns>
        public static bool ClonePageSettings(Page sourcePage, Page targetPage)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var permissionCommonService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();

            sourcePage.Widgets.AsParallel().ForAll(widget =>
            {
                var pageWidget = new PageWidget();
                pageWidget.InjectFrom<CloneEntityInjection>(widget);
                pageWidget.Page = targetPage;
                pageWidget.ParentWidgetId = widget.Id;

                //copy widget settings
                if (widget.Settings != null)
                {
                    pageWidget.Settings = (PageWidgetSettings)new PageWidgetSettings().InjectFrom<CloneEntityInjection>(widget.Settings);
                    pageWidget.Settings.LookAndFeelSettings = new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(widget.Settings.LookAndFeelSettings) as LookAndFeelSettings;
                    pageWidget.Settings.Widget = pageWidget;
                }

                //clone page widget instance
                if (widget.InstanceId != null)
                {
                    pageWidget.InstanceId = CloneWidgetInstance(widget);
                }
                targetPage.AddWidget(pageWidget);
            });

            //copy page layout
            targetPage.PageLayout = (PageLayout)new PageLayout { Id = targetPage.PageLayout.Id }.InjectFrom<CloneEntityInjection>(sourcePage.PageLayout);
            targetPage.PageLayout.Page = targetPage;
            sourcePage.PageLayout.ColumnWidths.AsParallel().ForAll(column =>
                                                                       {
                                                                           var columnWidth = (PageLayoutColumnWidthValue)new PageLayoutColumnWidthValue().InjectFrom<CloneEntityInjection>(column);
                                                                           columnWidth.PageLayout = targetPage.PageLayout;
                                                                           targetPage.PageLayout.AddColumnWidth(columnWidth);
                                                                       });

            //copy page settings)
            if (sourcePage.Settings != null)
            {
                targetPage.Settings = (PageSettings)new PageSettings().InjectFrom<CloneEntityInjection>(sourcePage.Settings);
                targetPage.Settings.LookAndFeelSettings = (LookAndFeelSettings)new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(sourcePage.Settings.LookAndFeelSettings);
                targetPage.Settings.Page = targetPage;
            }

            if (pageService.Save(targetPage))
            {
                //copy permissions and update page styles
                foreach (var item in targetPage.Widgets)
                {
                    if (item.ParentWidgetId == null) continue;
                    var systemWidget = MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == item.Widget.Identifier);
                    var sourceWidget = sourcePage.Widgets.FirstOrDefault(w => w.Id == item.ParentWidgetId);
                    if (sourceWidget != null)
                    {
                        if (targetPage.Settings != null && !String.IsNullOrEmpty(targetPage.Settings.CustomCSS))
                        {
                            targetPage.Settings.CustomCSS = targetPage.Settings.CustomCSS.Replace(String.Format(PageWidgetTemplate, sourceWidget.Id), String.Format(PageWidgetTemplate, item.Id));
                        }
                        permissionCommonService.CloneObjectPermisions(systemWidget.GetType(), sourceWidget.Id, item.Id);
                    }
                }
                return pageService.Save(targetPage);
            }
            return false;
        }
        /// <summary>
        /// Unlinks the page.
        /// </summary>
        /// <param name="page">The page.</param>
        public static void UnlinkPage(Page page)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var permissionCommonService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();
            var pageWidgetService = ServiceLocator.Current.GetInstance<IPageWidgetService>();
            if (page.Template != null)
            {
                page.Template.Widgets.AsEnumerable().Each(widget =>
                                                              {
                                                                  PageWidget pageWidget = null;
                                                                  if(widget.Widget.IsPlaceHolder)
                                                                  {
                                                                      pageWidget =
                                                                          page.Widgets.FirstOrDefault(
                                                                              currentPageWidget =>
                                                                              currentPageWidget.TemplateWidgetId ==
                                                                              widget.Id);
                                                                      pageWidget.ColumnNumber = widget.ColumnNumber;
                                                                      pageWidget.OrderNumber = widget.OrderNumber;
                                                                      pageWidget.TemplateWidgetId = null;
                                                                  }
                                                                  if (pageWidget == null)
                                                                  {
                                                                      pageWidget = new PageWidget();
                                                                      pageWidget.InjectFrom<CloneEntityInjection>(widget);
                                                                      pageWidget.Page = page;
                                                                      pageWidget.ParentWidgetId = widget.Id;
                                                                      //copy widget settings
                                                                      if (widget.Settings != null)
                                                                      {
                                                                          pageWidget.Settings =
                                                                              (PageWidgetSettings)
                                                                              new PageWidgetSettings().InjectFrom
                                                                                  <CloneEntityInjection>(widget.Settings);
                                                                          pageWidget.Settings.LookAndFeelSettings =
                                                                              new LookAndFeelSettings().InjectFrom
                                                                                  <CloneEntityInjection>(
                                                                                      widget.Settings.
                                                                                          LookAndFeelSettings) as
                                                                              LookAndFeelSettings;
                                                                          pageWidget.Settings.Widget = pageWidget;
                                                                      }

                                                                      //clone page widget instance
                                                                      if (widget.InstanceId != null)
                                                                      {
                                                                          pageWidget.InstanceId =
                                                                              CloneWidgetInstance(widget);
                                                                      }
                                                                      //TODO: save collection
                                                                      pageWidgetService.Save(pageWidget);
                                                                      page.AddWidget(pageWidget);
                                                                  }
                                                              });

                //copy page layout
                page.PageLayout =
                    (PageLayout)
                    new PageLayout().InjectFrom<CloneEntityInjection>(
                        page.Template.PageLayout);
                page.PageLayout.Page = page;
                page.Template.PageLayout.ColumnWidths.AsParallel().ForAll(column =>
                                                                           {
                                                                               var columnWidth =
                                                                                   (PageLayoutColumnWidthValue)
                                                                                   new PageLayoutColumnWidthValue().
                                                                                       InjectFrom<CloneEntityInjection>(
                                                                                           column);
                                                                               columnWidth.PageLayout =
                                                                                   page.PageLayout;
                                                                               page.PageLayout.AddColumnWidth(
                                                                                   columnWidth);
                                                                           });

                //copy page settings)
                if (page.Template.Settings != null)
                {
                    page.Settings =
                        (PageSettings)new PageSettings().InjectFrom<CloneEntityInjection>(page.Template.Settings);
                    page.Settings.LookAndFeelSettings =
                        (LookAndFeelSettings)
                        new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(
                            page.Template.Settings.LookAndFeelSettings);
                    page.Settings.Page = page;
                }

                if (pageService.Save(page))
                {
                    //copy permissions and update page styles
                    foreach (var item in page.Widgets)
                    {
                        if (item.ParentWidgetId == null) continue;
                        var systemWidget =
                            MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == item.Widget.Identifier);
                        var sourceWidget = page.Template.Widgets.FirstOrDefault(w => w.Id == item.ParentWidgetId);
                        if (sourceWidget != null)
                        {
                            if (page.Settings != null && !String.IsNullOrEmpty(page.Settings.CustomCSS))
                            {
                                page.Settings.CustomCSS =
                                    page.Settings.CustomCSS.Replace(
                                        String.Format(PageWidgetTemplate, sourceWidget.Id),
                                        String.Format(PageWidgetTemplate, item.Id));
                            }
                            permissionCommonService.CloneObjectPermisions(systemWidget.GetType(), sourceWidget.Id,
                                                                          item.Id);
                        }
                    }
                    page.Template = null;
                    pageService.Save(page);
                }
            }
        }