PageController.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Controllers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » CmsServices » Controllers » PageController.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data.Objects;
using System.Web.Mvc;
using System.Web.Security;
using Everest.Library.Data;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Data.Rule;
using Everest.Library.Data.Entity;
using Everest.Library.Json;
using Everest.Library.Extjs.Tree;
using Everest.Library.ExtensionMethod;
using Everest.Library.Versioning;

using Everest.CmsServices.Services;
using Everest.CmsServices.Models;
using Everest.Library;
using Everest.CmsServices.Extension.Module;
namespace Everest.CmsServices.Controllers{
    internal class QueriedComponentInPage
    {
        public int ContentTemplateInPageHolderId { get; set; }
        public string Position { get; set; }
        private string component;
        public string Component
        {
            get
            {
                if ((ComponentType)ComponentType == Everest.CmsServices.Models.ComponentType.WebForm)
                {
                    return CachedData.GetWebForm(new Guid(component)).FormName;
                }
                else
                {
                    return component;
                }
            }
            set
            {
                component = value;
            }
        }
        public int ComponentType { get; set; }
        public int? Order { get; set; }
    }

    internal class ComponentsInPageObjectClass
    {
        public int ContentTemplateInPageHolderId
        {
            get;
            set;
        }

        public string Position
        {
            get;
            set;
        }

        public int? Order
        {
            get;
            set;
        }
        public string ControlId { get; set; }

        public string ComponentValue
        {
            get;
            set;
        }

        public List<object> ParameterValues
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class PageController : CmsExtController
    {
        IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();

        #region ContentTemplateInPage
        /// <summary>
        /// Gets the content templates in page list.
        /// </summary>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult GetContentTemplatesInPageList(Guid pageUUID)
        {
            IEverestCmsDataContext context = EverestCmsEntities.GetDataContext();
            var query = context.Cms_ContentTemplateInPageHolder.Where(c => c.Cms_Page.UUID == pageUUID)
                        .Select(c => new QueriedComponentInPage()
                        {
                            ContentTemplateInPageHolderId = c.ContentTemplateInHolderId,
                            Position = c.Position,
                            Component = String.IsNullOrEmpty(c.ComponentValue) ? c.Cms_ContentTemplate.Name : c.ComponentValue,
                            ComponentType = c.ComponentType,
                            Order = c.Order
                        });
            return Json(new ExtJsonReaderObject(query.ToArray(), query.Count()));
        }
        /// <summary>
        /// Gets the content page to page details.
        /// </summary>
        /// <param name="contentTemplateInPageHolderId">The content page in page holder id.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult GetContentTemplateToPageDetails(int contentTemplateInPageHolderId)
        {
            object jsonData = QueryContentTemplateToPageDetails(contentTemplateInPageHolderId);
            return Json(new JsonResultData() { success = true, data = jsonData });
        }

        /// <summary>
        /// Queries the content page to page details.
        /// </summary>
        /// <param name="contentTemplateInPageHolderId">The content page in page holder id.</param>
        /// <returns></returns>
        private static ComponentsInPageObjectClass QueryContentTemplateToPageDetails(int contentTemplateInPageHolderId)
        {
            IEverestCmsDataContext context = EverestCmsEntities.GetDataContext();

            //query the component value
            var componentInHolder = context.Cms_ContentTemplateInPageHolder.Where(p => p.ContentTemplateInHolderId == contentTemplateInPageHolderId)
                        .Select(p => new
                        {
                            ContentTemplateInPageHolderId = p.ContentTemplateInHolderId,
                            p.Position,
                            Order = p.Order,
                            p.ControlId,
                            //split the ComponentTemplateId and ComponentValue because there are use different data type(string and int)
                            ContentTemplateId = p.Cms_ContentTemplate == null ? 0 : p.Cms_ContentTemplate.ContentTemplateId,
                            ComponentValue = p.ComponentValue,
                            ComponentType = p.ComponentType
                        }).First();

            List<object> paramsValues = new List<object>();

            var oldParamValues = context.Cms_ParameterValues.Where(p => p.Cms_ContentTemplateInPageHolder.ContentTemplateInHolderId == contentTemplateInPageHolderId)
                                  .Select(p => new
                                  {
                                      ParameterName = p.Name,
                                      DataType = p.DataType,
                                      Value = p.Value
                                  }).ToList();
            if ((ComponentType)componentInHolder.ComponentType == ComponentType.ContentTemplate)
            {
                //union the contentTemplate Parameters.
                var contentParameters = context.Cms_ContentTemplateInPageHolder.Where(p => p.ContentTemplateInHolderId == contentTemplateInPageHolderId)
                    .Select(p => p.Cms_ContentTemplate.Cms_ContentTemplateParameters).First();
                if (contentParameters != null)
                {
                    foreach (var parameter in contentParameters)
                    {
                        var existsValue = oldParamValues.Where(p => p.ParameterName == parameter.Name).Select(p => p.Value).FirstOrDefault();
                        paramsValues.Add(new
                        {
                            ParameterName = parameter.Name,
                            DataType = parameter.DataType,
                            Value = existsValue
                        });

                    }
                }
            }
            if ((ComponentType)componentInHolder.ComponentType == ComponentType.Module)
            {
                var moduleInfo = ModuleManager.GetModule(componentInHolder.ComponentValue, CmsGlobal.RootApplicationName);
                foreach (var parameter in moduleInfo.Configuration.Settings)
                {
                    var existsValue = oldParamValues.Where(p => p.ParameterName == parameter.Key).Select(p => p.Value).FirstOrDefault();
                    if (string.IsNullOrEmpty(existsValue))
                    {
                        existsValue = parameter.Value;
                    }
                    paramsValues.Add(new
                    {
                        ParameterName = parameter.Key,
                        DataType = "String",
                        Value = existsValue,
                        //Editor = parameter.Editor.ToString(),
                        Items = parameter.Items
                    });
                }
            }

            var jsonData = new ComponentsInPageObjectClass()
            {
                ContentTemplateInPageHolderId = componentInHolder.ContentTemplateInPageHolderId,
                Position = componentInHolder.Position,
                Order = componentInHolder.Order,
                ControlId = componentInHolder.ControlId,
                ComponentValue = Component.ToValue(componentInHolder.ContentTemplateId == 0 ? componentInHolder.ComponentValue : componentInHolder.ContentTemplateId.ToString(), (ComponentType)componentInHolder.ComponentType),
                ParameterValues = paramsValues
            };

            return jsonData;
        }
        /// <summary>
        /// Submits the content tempate to page.
        /// </summary>
        /// <param name="add">if set to <c>true</c> [add].</param>
        /// <param name="closeForm">if set to <c>true</c> [close form].</param>
        /// <returns></returns>
        [ValidateInput(false)]
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult SubmitContentTempateToPage(bool add, bool closeForm)
        {
            JsonResultData resultData = new JsonResultData();
            Cms_ContentTemplateInPageHolder contentTemplateInHolder = null;

            if (add)
            {
                contentTemplateInHolder = new Cms_ContentTemplateInPageHolder();
                Guid pageUUID = new Guid(Request.Form["pageUUID"]);
                contentTemplateInHolder.Cms_Page = dataContext.QueryPage(pageUUID).First();

                dataContext.AddToCms_ContentTemplateInPageHolder(contentTemplateInHolder);
            }
            else
            {
                int contentTemplateInPageHolderId = int.Parse(Request.Form["oldData.ContentTemplateInPageHolderId"]);
                contentTemplateInHolder = (from c in dataContext.Cms_ContentTemplateInPageHolder
                                           where c.ContentTemplateInHolderId == contentTemplateInPageHolderId
                                           select c).First();
                //delete the old parameter values
                contentTemplateInHolder.Cms_ParameterValues.Load();
                while (contentTemplateInHolder.Cms_ParameterValues.Count > 0)
                {
                    dataContext.DeleteObject(contentTemplateInHolder.Cms_ParameterValues.First());
                }
                contentTemplateInHolder.Cms_ParameterValues.Clear();

            }
            //set new holder id
            string position = Request.Form["Position"];
            contentTemplateInHolder.Position = position;

            string componentValue;
            ComponentType componentType;
            string requestComponentValue = Request.Form["ComponentValue"];
            Component.ParseValue(requestComponentValue, out componentValue, out componentType);
            contentTemplateInHolder.ComponentType = (int)componentType;
            switch (componentType)
            {
                case ComponentType.ContentTemplate:
                    contentTemplateInHolder.Cms_ContentTemplate = dataContext.QueryContentTemplate(int.Parse(componentValue)).First();
                    break;
                case ComponentType.Module:
                case ComponentType.WebForm:
                    contentTemplateInHolder.ComponentValue = componentValue;
                    break;
                default:
                    break;
            }

            contentTemplateInHolder.Order = int.Parse(Request.Form["Order"]);
            contentTemplateInHolder.ControlId = Request.Form["ControlId"];
            Dictionary<string, string>[] parameterValues = Request.Form["ParameterValues"].DeserializeJSON<Dictionary<string, string>[]>();
            if (parameterValues.Length > 0)
            {
                foreach (var item in parameterValues)
                {
                    Cms_ParameterValues paramterValue = Cms_ParameterValues.CreateCms_ParameterValues(item);
                    contentTemplateInHolder.Cms_ParameterValues.Add(paramterValue);
                }
            }
            dataContext.SaveChanges();
            if (closeForm == false && resultData.success)
            {
                resultData.closeForm = false;
                resultData.data = QueryContentTemplateToPageDetails(contentTemplateInHolder.ContentTemplateInHolderId);

            }
            return Json(resultData);
        }

        /// <summary>
        /// Deletes the content page in page holder.
        /// </summary>
        /// <param name="ContentTemplateInPageHolderId">The content page in page holder id.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult DeleteContentTemplateInPageHolder(int ContentTemplateInPageHolderId)
        {
            IEverestCmsDataContext context = EverestCmsEntities.GetDataContext();
            var data = (from c in context.Cms_ContentTemplateInPageHolder
                        where c.ContentTemplateInHolderId == ContentTemplateInPageHolderId
                        select c).First();
            context.DeleteObject(data);
            context.SaveChanges();
            return Json(new JsonResultData() { success = true });
        }
        #endregion

        #region AddContentTemplateToPages
        /// <summary>
        /// Adds the content template to pages.
        /// </summary>
        /// <param name="uuid">The UUID.The content template uuid</param>
        /// <param name="positions">The positions.</param>
        /// <param name="pages">The pages.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult AddContentTemplateToPages(Guid uuid, string position, string pages, int order)
        {
            JsonResultData resultData = new JsonResultData();

            var oldPages = dataContext.QueryContentTemplateInPages(uuid, position).Select(ctp => ctp.Cms_Page).ToArray();
            if (!StringExtensions.IsNullOrEmptyTrim(position))
            {
                string[] pageArr = pages.Split(',');
                var contentTemplate = dataContext.QueryContentTemplate(uuid).First();

                Dictionary<string, string>[] parameterValues = Request.Form["ParameterValues"].DeserializeJSON<Dictionary<string, string>[]>();

                foreach (var strPageUUID in pageArr.Where(p => !oldPages.Any(ep => ep.PageName == p)))
                {
                    if (!StringExtensions.IsNullOrEmptyTrim(strPageUUID))
                    {
                        var pageUUID = new Guid(strPageUUID);

                        Cms_ContentTemplateInPageHolder contentTemplateInHolder = new Cms_ContentTemplateInPageHolder();
                        contentTemplateInHolder.Cms_ContentTemplate = contentTemplate;
                        contentTemplateInHolder.Position = position;
                        contentTemplateInHolder.Order = order;
                        contentTemplateInHolder.Cms_Page = dataContext.QueryPage(pageUUID).First();
                        if (parameterValues.Length > 0)
                        {
                            foreach (var item in parameterValues)
                            {
                                Cms_ParameterValues paramterValue = Cms_ParameterValues.CreateCms_ParameterValues(item);
                                contentTemplateInHolder.Cms_ParameterValues.Add(paramterValue);
                            }
                        }

                        dataContext.AddToCms_ContentTemplateInPageHolder(contentTemplateInHolder);
                    }
                }
                foreach (var deletePage in oldPages.Where(p => !pageArr.Contains(p.PageName)))
                {
                    var contentTemplateInPositions = dataContext.QueryContentTemplateInPageHolders(deletePage.PageId, position, uuid);
                    foreach (var contentTemplateInPosition in contentTemplateInPositions)
                    {
                        dataContext.DeleteObject(contentTemplateInPosition);
                    }
                }
                dataContext.SaveChanges();
            }
            return Json(resultData);
        }
        #endregion

        #region BatchSitting
        /// <summary>
        /// SubmitBatchSitting
        /// </summary>
        /// <param name="json"></param>
        /// <param name="application"></param>
        /// <param name="pageId"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult SubmitBatchSitting(string json, string application, Guid pageUUID)
        {
            try
            {
                var trans = dataContext.BeginTransaction();
                {
                    //Do Clear Start
                    var contentTemplateInPageHolder = (from d in dataContext.Cms_ContentTemplateInPageHolder
                                                       where d.Cms_Page.UUID == pageUUID
                                                       select d).ToList();
                    foreach (var templateItem in contentTemplateInPageHolder)
                    {
                        templateItem.Cms_ParameterValues.Load();
                        while (templateItem.Cms_ParameterValues.Count > 0)
                        {
                            Cms_ParameterValues paraItem = templateItem.Cms_ParameterValues.First();
                            templateItem.Cms_ParameterValues.Remove(paraItem);
                            dataContext.DeleteObject(paraItem);
                        }
                        dataContext.DeleteObject(templateItem);
                    }
                    dataContext.SaveChanges();
                    //Do Clear End

                    //Do Save Start
                    var allSitting = json.DeserializeJSON<Dictionary<string, List<BatchSittingData>>>();
                    foreach (var sitting in allSitting)//Positions
                    {
                        string positionName = sitting.Key;
                        for (int i = 0; i < sitting.Value.Count; i++)//Templates
                        {
                            BatchSittingData data = sitting.Value[i];

                            int order = i;
                            string componentName = data.Component.Name;
                            string componentValue = data.Component.Value;
                            ComponentType componentType;
                            Component.ParseValue(componentValue, out componentValue, out componentType);

                            //Add ContentTemplateInPageHolder Start
                            Cms_ContentTemplateInPageHolder templateInHolder = new Cms_ContentTemplateInPageHolder();
                            templateInHolder.Cms_Page = dataContext.QueryPage(pageUUID).First();
                            templateInHolder.Position = positionName;
                            templateInHolder.Order = order;
                            templateInHolder.ComponentType = (int)componentType;

                            switch (componentType)
                            {
                                case ComponentType.ContentTemplate:
                                    templateInHolder.Cms_ContentTemplate = dataContext.QueryContentTemplate(int.Parse(componentValue)).First();
                                    break;

                                case ComponentType.Module:
                                case ComponentType.WebForm:
                                    templateInHolder.ComponentValue = componentValue;
                                    break;

                                default:
                                    break;
                            }
                            dataContext.AddToCms_ContentTemplateInPageHolder(templateInHolder);
                            //Add ContentTemplateInPageHolder End

                            //Add ParameterValues Start
                            foreach (Cms_ParameterValues paras in data.Parameters)//Parameters
                            {
                                templateInHolder.Cms_ParameterValues.Add(paras);
                            }
                            //Add ParameterValues End
                        }
                    }
                    dataContext.SaveChanges();
                    //Do Save End

                    dataContext.Commit();
                }
            }
            catch
            {
                dataContext.Rollback();
                throw;
            }

            return Json(new
            {
                success = true
            });
        }

        /// <summary>
        /// QueryBatchSitting
        /// </summary>
        /// <param name="application"></param>
        /// <param name="pageTemplateUUID"></param>
        /// <param name="pageId"></param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult QueryBatchSitting(string application, Guid pageUUID)
        {
            //componentsInPage  PageController.GetContentTemplatesInPageList
            var componentsInPageQuery = dataContext.Cms_ContentTemplateInPageHolder.Where(c => c.Cms_Page.UUID == pageUUID)
                        .Select(c => new
                        {
                            ContentTemplateInPageHolderId = c.ContentTemplateInHolderId,
                            c.Position,
                            Component = string.IsNullOrEmpty(c.ComponentValue) ? c.Cms_ContentTemplate.Name : c.ComponentValue,
                            ComponentType = c.ComponentType,
                            Order = c.Order,
                            ComponentValue = c.ComponentValue,
                            ContentTemplateId = c.Cms_ContentTemplate == null ? 0 : c.Cms_ContentTemplate.ContentTemplateId,
                            Parameters = c.Cms_ParameterValues
                        }).ToArray();

            var componentsInPageList = new List<ComponentsInPageObjectClass>();
            foreach (var com in componentsInPageQuery)
            {
                var detail = QueryContentTemplateToPageDetails(com.ContentTemplateInPageHolderId);
                componentsInPageList.Add(detail);
            }
            var componentsInPageListWrap = componentsInPageList.Select(c => new
            {
                c.ContentTemplateInPageHolderId,
                c.Position,
                c.Order,
                c.ComponentValue,
                Parameters = new ExtJsonReaderObject()
                {
                    Rows = c.ParameterValues,
                    TotalCount = c.ParameterValues.Count
                }
            }).OrderBy(c => c.Order).ToArray();

            //pageHolders   PageTemplateController.GetPageHoldersForCombobox
            var pageTemplate = dataContext.Cms_Page.Where(p => p.UUID == pageUUID).Select(p => new { UUID = p.Cms_PageTemplate.UUID }).First();
            var pageHoldersQuery = from h in dataContext.Cms_PageTemplateHolders
                                   where h.Cms_PageTemplate.UUID == pageTemplate.UUID
                                   select h;
            var pageHolders = pageHoldersQuery.ToArray().ToComboboxItems(h => h.Name, h => h.Name.ToString());

            //conponents    ContentTemplateController.GetContentTemplateForCombobox
            ContentTemplateService contentTemplateService = UnityManager.Resolve<ContentTemplateService>();
            var webComponents = contentTemplateService.GetComponentsForPage(application);
            var conponents = webComponents.ToComboboxItems(wc => string.Format(wc.ComponentName),
                wc => wc.ToValue(), wc => Url.Content(wc.Image), wc => wc.ComponentType.ToString(), false);

            //result
            return Json(new
            {
                PageHolders = new ExtJsonReaderObject(pageHolders, pageHolders.Count),
                Components = new ExtJsonReaderObject(conponents, conponents.Count()),
                ComponentsInPageList = new ExtJsonReaderObject(componentsInPageListWrap, componentsInPageListWrap.Length),
            });
        }

        class BatchSittingData
        {
            public ComboboxItem Component
            {
                get;
                set;
            }

            public List<Cms_ParameterValues> Parameters
            {
                get;
                set;
            }
        }
        #endregion

        #region Localize
        /// <summary>
        /// Localizes the content template.
        /// </summary>
        /// <param name="pageId">The page id.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult Localize(int pageId, string application)
        {
            JsonResultData resultData = new JsonResultData();

            var aspnet_Application = dataContext.QueryApplication(application).First();

            var originalPage = dataContext.QueryPage(pageId).FirstOrDefault();

            if (originalPage != null)
            {
                var localizedPage = originalPage.CopyNew();
                localizedPage.aspnet_Applications = aspnet_Application;
                localizedPage.ApplicationLevel = localizedPage.aspnet_Applications.ApplicationLevel;
                localizedPage.UserName = User.Identity.Name;
                localizedPage.ModifiedDate = DateTime.Now;
                localizedPage.Base = originalPage;
                localizedPage.OriginalUUID = originalPage.OriginalUUID;

                dataContext.AddToCms_Page(localizedPage);

                dataContext.SaveChanges();

                localizedPage.Checkin(User.Identity.Name, string.Format(Resources.CheckinByLocalization, originalPage.PageId));

                //
                resultData.data = localizedPage;
            }
            return Json(resultData);
        }

        #endregion

        #region Combobox

        /// <summary>
        /// Gets the type of the data rule value.
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDataRuleValueType()
        {
            string[] names = Enum.GetNames(typeof(DataRuleValueType));

            var item = names.ToComboboxItems(s => s, s => s, false);
            return Json(new ExtJsonReaderObject(item, item.Count));
        }

        /// <summary>
        /// Gets the pages.
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPages(string application, Guid? pageUUID)
        {
            var pages = dataContext.QueryPages(application);
            if (pageUUID.HasValue)
            {
                pages = pages.Where(p => p.UUID != pageUUID.Value);
            }
            return Json(new ExtJsonReaderObject(pages.ToComboboxItems<Cms_Page>(p => p.VirtualPath, p => p.VirtualPath.ToString(), true), pages.Count()));
        }
        /// <summary>
        /// Gets the pages by layout template.
        /// </summary>
        /// <param name="pageTemplateUUID">The page template UUID.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public ActionResult GetPagesByLayoutTemplate(Guid contentTemplateUUID, string pageTemplateUUID, string application, string position)
        {
            if (!StringExtensions.IsNullOrEmptyTrim(pageTemplateUUID) && !StringExtensions.IsNullOrEmptyTrim(position))
            {
                Guid uuid = new Guid(pageTemplateUUID);
                List<ComboboxItem> comboboxItems = new List<ComboboxItem>();
                //Application
                var pageQuery = dataContext.QueryPagesByPageTemplate(uuid, application);
                foreach (var page in pageQuery)
                {
                    var contentTemplateInPositionCount = dataContext.QueryContentTemplateInPageHolders(page.PageId, position, contentTemplateUUID).Count();
                    comboboxItems.Add(new ComboboxItem(page.PageName + string.Format(" [{0}]", contentTemplateInPositionCount), page.UUID.ToString())
                    {
                        Checked = contentTemplateInPositionCount != 0
                    });
                }
                return Json(new ExtJsonReaderObject(comboboxItems, comboboxItems.Count));
            }
            return null;
        }
        #endregion

        #region IStandardActions Members

        /// <summary>
        /// Gets the page nodes.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="id">The id.</param>
        /// <param name="parentPageUUID">The parent page UUID.</param>
        /// <param name="dataUrl">The data URL.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult GetPageNodes(string application, string id, Guid? parentPageUUID, string dataUrl)
        {
            List<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
            var pages = dataContext.QueryPagesByParent(application, parentPageUUID).OrderBy(p => p.NavigationOrder)
                .Select(p => new
                {
                    p.PageName,
                    p.UUID,
                    p.ShowInNavigation,
                    p.NavigationText,
                    p.aspnet_Applications.ApplicationName
                });
            foreach (var page in pages)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Text = page.PageName;
                treeNode.ParentNode = id;
                treeNode.AddAttribute("pageName", page.PageName);
                treeNode.AddAttribute("virtualPath", CachedData.GetPageVirtualPath(page.UUID));
                treeNode.AddAttribute("application", application);
                treeNode.AddAttribute("pageApplication", page.ApplicationName);
                treeNode.AddAttribute("parentPageUUID", page.UUID.ToString());
                treeNode.AddAttribute("formType", "form");
                treeNode.AddAttribute("dataUrl", dataUrl);
                treeNode.AddAttribute("UUID", page.UUID);
                //for relation
                treeNode.AddAttribute("folderUUID", page.UUID);
                treeNode.AddAttribute("schema", "Page");
                treeNode.AddAttribute("folderType", FolderType.PageNode.ToString());
                treeNode.IconCls = "PageNode";
                if (page.ShowInNavigation)
                {
                    treeNode.Cls = "NavigationPage";
                    treeNode.Qtip = Resources.ShowInNavigation;
                }
                treeNode.AddAttribute("usedBy", "Kooboo_Page/usedby");
                treeNodes.Add(treeNode.Attributes);
            }
            return Json(treeNodes);
        }
        #region ListPageObject
        private class ListPageObject
        {
            public Guid UUID { get; set; }
            public int PageId { get; set; }
            public string PageName { get; set; }
            public string PageTemplate { get; set; }
            public string Url { get; set; }
            public string DefaultParams { get; set; }
            public string Application { get; set; }
            public bool IsLocalized { get; set; }
            public Guid? BaseUUID { get; set; }

            public string VirtualPath
            {
                get
                {
                    return CachedData.GetPageVirtualPath(this.UUID);
                }
            }
        }
        #endregion
        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult GetList()
        {
            int start, limit;
            EnsurePaging(out start, out limit);

            string application = Request.Form["application"];
            IQueryable<Cms_Page> queryable = dataContext.QueryPages(application).OrderByDescending(p => p.PageId);
            string pageName = Request.Form["PageName"];
            if (!StringExtensions.IsNullOrEmptyTrim(pageName))
            {
                queryable = queryable.Where(p => p.PageName.Contains(pageName));
            }
            queryable = queryable.OrderByDescending(p => p.PageId);
            queryable = OrderByRequest(queryable);
            var queryResult = queryable.Select<Cms_Page, object>(p => new ListPageObject()
            {
                UUID = p.UUID,
                PageId = p.PageId,
                PageName = p.PageName,
                PageTemplate = p.Cms_PageTemplate.Name,
                Url = p.Url,
                DefaultParams = p.DefaultParams,
                Application = p.aspnet_Applications.ApplicationName,
                IsLocalized = p.aspnet_Applications.ApplicationName == application,
                BaseUUID = p.Base == null ? null : (Guid?)p.Base.UUID
            });
            return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
        }

        /// <summary>
        /// Gets the detail.
        /// </summary>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult GetDetail()
        {
            JsonResultData resultData = new JsonResultData();
            Guid uuid = new Guid(Request.Form["UUID"]);
            object data = GetPageDetail(uuid);

            resultData.data = data;
            return Json(resultData);
        }

        /// <summary>
        /// Gets the page details.
        /// </summary>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        private object GetPageDetail(Guid uuid)
        {
            var data = dataContext.QueryPage(uuid).Select(
                        p => new
                        {
                            p.UUID,
                            Application = p.aspnet_Applications.ApplicationName,
                            PageId = p.PageId,
                            PageName = p.PageName,
                            p.ShowInNavigation,
                            p.NavigationText,
                            p.NavigationOrder,
                            p.Denies,
                            p.Allows,
                            Parent = p.Cms_ParentPage == null ? new Nullable<Guid>() : p.Cms_ParentPage.UUID,
                            PageTemplate = p.Cms_PageTemplate.Name,
                            PageTemplateUUID = p.Cms_PageTemplate.UUID,
                            Url = p.Url,
                            Cms_DataRule = p.Cms_DataRule,
                            DefaultParams = p.DefaultParams,
                            p.Cms_Plugin
                        }).First();
            return new
            {
                data.UUID,
                data.PageId,
                data.PageName,
                FormTitle = data.PageName,
                data.PageTemplate,
                data.PageTemplateUUID,
                data.Application,
                data.Url,
                data.ShowInNavigation,
                Parent = data.Parent.HasValue ? CachedData.GetPageVirtualPath(data.Parent.Value) : "",
                data.NavigationText,
                data.NavigationOrder,
                data.Denies,
                data.Allows,
                DataRules = data.Cms_DataRule.ToDataRuleQuery(),
                Plugins = data.Cms_Plugin.Select(p => new
                {
                    p.PluginId,
                    p.PluginName,
                    p.PluginType
                }).ToArray(),
                data.DefaultParams,
                VirtualPath = CachedData.GetPageVirtualPath(data.UUID)
            };
        }

        /// <summary>
        /// Submits the specified add.
        /// </summary>
        /// <param name="add">if set to <c>true</c> [add].</param>
        /// <param name="closeForm">The close form.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult Submit(bool add, bool? closeForm)
        {
            JsonResultData resultData = new JsonResultData();
            Cms_Page page = null;
            IEverestCmsDataContext context = EverestCmsEntities.GetDataContext();
            try
            {
                string application = Request.Form["application"];
                if (add)
                {
                    page = new Cms_Page();
                    page.UUID = Guid.NewGuid();
                    page.OriginalUUID = page.UUID;

                    page.PageName = Request.Form["PageName"];
                    var pageTemplateName = Request.Form["PageTemplate"];
                    page.Cms_PageTemplate = dataContext.QueryPageTemplate(application, pageTemplateName).First();
                    page.aspnet_Applications = dataContext.QueryApplication(application).First();
                    page.ApplicationLevel = page.aspnet_Applications.ApplicationLevel;

                    context.AddToCms_Page(page);

                }
                else
                {
                    var pageTemplateName = Request.Form["PageTemplate"];
                    int pageId = int.Parse(Request.Form["oldData.PageId"]);
                    page = dataContext.QueryPage(pageId).First();
                    page.PageName = Request.Form["PageName"];
                    //page.Cms_PageTemplateReference.Load();
                    //if (page.Cms_PageTemplate.Name != pageTemplateName)
                    //{

                    //}
                    //
                    page.Cms_PageTemplate = dataContext.QueryPageTemplate(application, pageTemplateName).First();
                    page.ClearDataRule(dataContext);
                    page.ClearPlugins(dataContext);

                }
                page.Url = Request.Form["Url"];
                page.ModifiedDate = DateTime.Now;
                page.UserName = User.Identity.Name;

                if (StringExtensions.IsTrue(Request.Form["ShowInNavigation"]))
                {
                    page.ShowInNavigation = true;
                }
                else
                    page.ShowInNavigation = false;

                page.NavigationText = Request.Form["NavigationText"];

                if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["NavigationOrder"]))
                {
                    page.NavigationOrder = int.Parse(Request.Form["NavigationOrder"]);
                }
                if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["Parent"]) && Request.Form["Parent"] != "null")
                {
                    var parentPage = CachedData.GetPageByVirtualPath(application, Request.Form["Parent"]);
                    if (parentPage != null)
                    {
                        var parentUUID = parentPage.UUID;
                        page.Cms_ParentPage = dataContext.QueryPage(parentUUID).FirstOrDefault();
                    }

                }
                else
                {
                    page.Cms_ParentPage = null;
                }
                page.Denies = Request.Form["Denies"];
                page.Allows = Request.Form["Allows"];

                //page.DataRule = Request.Form["DataRule"];
                page.DefaultParams = Request.Form["DefaultParams"];

                #region DataRule
                page.ClearDataRule(dataContext);
                IDictionary<string, string>[] newDataRules = Request.Form["DataRules"].DeserializeJSON<Dictionary<string, string>[]>();
                int itemCount = 0;
                for (int i = 0; i < newDataRules.Length; i++)
                {
                    var dataRule = Cms_DataRule.CreateCms_DataRule(dataContext, newDataRules[i]);
                    if (dataRule != null)
                    {
                        itemCount++;
                        dataRule.Order = itemCount;
                        page.Cms_DataRule.Add(dataRule);
                    }
                }
                #endregion

                #region Plugin
                Cms_Plugin[] plugins = Request.Form["Plugins"].DeserializeJSON<Cms_Plugin[]>();
                foreach (var plugin in plugins)
                {
                    page.Cms_Plugin.Add(plugin);
                }
                #endregion

                context.SaveChanges();
                page.Checkin(User.Identity.Name, "");

                if (resultData.success && closeForm == false)
                {
                    resultData.closeForm = false;
                    resultData.data = GetPageDetail(page.UUID);
                }
            }
            catch (RuleViolationException ruleException)
            {
                ruleException.Issues.UpdateResultDataWithViolations(resultData);
                Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
            }

            return Json(resultData);
        }

        /// <summary>
        /// Deletes the specified UUID.
        /// </summary>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        [PermissionFilter(Permission = FolderType.Page)]
        public ActionResult Delete(Guid uuid)
        {
            var resultData = new JsonResultData();
            var page = dataContext.QueryPage(uuid).First();
            if (BeReferenced(uuid))
            {
                resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, page.PageName);
            }
            else
            {
                page.Delete(dataContext);
                page.ClearVersions();
                dataContext.SaveChanges();
            }
            return Json(resultData);
        }

        private bool BeReferenced(Guid uuid)
        {
            if (dataContext.QueryPagesByParent(uuid).Count() > 0)
            {
                return true;
            }
            if (dataContext.QueryPageLocalizations(uuid).Exists())
            {
                return true;
            }
            return false;
        }

        #endregion

        #region Relations
        public ActionResult UsedBy(Guid uuid, string dataUrl)
        {
            IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();

            #region Children
            var childrenNodes = new TreeNode();
            childrenNodes.Text = Resources.Children;
            childrenNodes.IconCls = FolderType.Page.ToString();
            childrenNodes.Expanded = true;
            treeNodes.Add(childrenNodes.Attributes);

            var children = dataContext.QueryPagesByParent(uuid)
                .Select(p => new
                {
                    Page = p,
                    p.aspnet_Applications.ApplicationName
                });
            childrenNodes.children = new List<IDictionary<string, object>>();
            foreach (var content in children)
            {
                var pageNode = new TreeNode();
                childrenNodes.AddChild(pageNode);
                pageNode.Text = new UniqueName(content.ApplicationName, content.Page.VirtualPath).ToString();
                pageNode.IconCls = FolderType.PageNode.ToString();
                pageNode.AddAttribute("uuid", content.Page.UUID);
                pageNode.AddAttribute("dataUrl", dataUrl);
            }
            #endregion

            #region Localized By
            var localizedByNodes = new TreeNode();
            treeNodes.Add(localizedByNodes.Attributes);
            localizedByNodes.Text = Resources.LocalizedBy;
            localizedByNodes.IconCls = FolderType.Page.ToString();
            localizedByNodes.Expanded = true;

            var localizations = dataContext.QueryPageLocalizations(uuid)
                .Select(p => new
                {
                    Page = p,
                    p.aspnet_Applications.ApplicationName
                });
            localizedByNodes.children = new List<IDictionary<string, object>>();
            foreach (var page in localizations)
            {
                var pageNode = new TreeNode();
                localizedByNodes.AddChild(pageNode);
                pageNode.Text = new UniqueName(page.ApplicationName, page.Page.VirtualPath).ToString();
                pageNode.AddAttribute("uuid", page.Page.UUID);
                pageNode.IconCls = FolderType.PageNode.ToString();
                pageNode.AddAttribute("dataUrl", dataUrl);
            }

            #endregion

            #region Localize From
            var localizeFromNodes = new TreeNode();
            treeNodes.Add(localizeFromNodes.Attributes);
            localizeFromNodes.Text = Resources.LocalizeFrom;
            localizeFromNodes.IconCls = FolderType.Page.ToString();
            localizeFromNodes.Expanded = true;

            var localizeFrom = dataContext.QueryPage(uuid).Where(c => c.Base != null)
                .Select(p => new
                {
                    Page = p,
                    p.Base.aspnet_Applications.ApplicationName
                });
            localizeFromNodes.children = new List<IDictionary<string, object>>();
            foreach (var page in localizeFrom)
            {
                var pageNode = new TreeNode();
                localizeFromNodes.AddChild(pageNode);
                pageNode.Text = new UniqueName(page.ApplicationName, page.Page.VirtualPath).ToString();
                pageNode.IconCls = FolderType.PageNode.ToString();
                pageNode.AddAttribute("uuid", page.Page.UUID);
                pageNode.AddAttribute("dataUrl", dataUrl);
            }

            #endregion

            return Json(treeNodes);
        }
        #endregion

    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.