UIBuilderController.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 » UIBuilderController.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.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Everest.Library.Mvc;
using System.Collections.Specialized;
using Everest.CmsServices.Models;
using Everest.Library.Extjs;
using System.IO;
using System.Xml;
using System.Text;
using System.Globalization;
using Everest.Library.ExtensionMethod;

namespace Everest.CmsServices.Controllers{
    public class UIBuilderController : EverestControllerBase
    {
        private IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
        private const string DefaultCustomStylesPath = @"Kooboojs/uibuilder/VisualJS/DefaultCustomStyles.css";

        #region Designer

        private Cms_CustomerForm QueryFormByUUID(string formUUID)
        {
            if (StringExtensions.IsNullOrEmptyTrim(formUUID))
                return null;

            Guid uid = new Guid(formUUID);
            Cms_CustomerForm form = (from f in dataContext.Cms_CustomerForm
                                     where f.UUID == uid
                                     select f).First();
            return form;
        }

        private string FormUUID
        {
            get
            {
                return this.Request.Form["__FormUUID"];
            }
            set
            {
                this.ViewData["__FormUUID"] = value;
            }
        }

        private string FormName
        {
            get
            {
                return this.Request.Form["__FormName"];
            }
            set
            {
                this.ViewData["__FormName"] = value;
            }
        }

        private string FormScript
        {
            get
            {
                return this.Request.Form["__FormScript"];
            }
            set
            {
                this.ViewData["__FormScript"] = value;
            }
        }

        private string FeedbackScript
        {
            get
            {
                return this.Request.Form["__FeedbackScript"];
            }
            set
            {
                this.ViewData["__FeedbackScript"] = value;
            }
        }

        private string CustomStyles
        {
            get
            {
                string cssText = this.Request.Form["__CustomStyles"];
                if (StringExtensions.IsNullOrEmptyTrim(cssText))
                {
                    string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DefaultCustomStylesPath);
                    if (System.IO.File.Exists(filePath))
                        cssText = System.IO.File.ReadAllText(filePath);
                    
                }
                return cssText;
            }
            set
            {
                this.ViewData["__CustomStyles"] = value;
            }
        }

        private string Application
        {
            get
            {
                return this.Request.Form["__Application"];
            }
            set
            {
                this.ViewData["__Application"] = value;
            }
        }

        public ActionResult Designer()
        {
            this.ViewData["__CurrentCulture"] = CultureInfo.CurrentUICulture.Name;

            // save data
            if (this.Request.QueryString["DataState"] == "callback")
            {
                Cms_CustomerForm form = null;
                string responseText = string.Empty;
                if (StringExtensions.IsNullOrEmptyTrim(this.FormUUID))
                {
                    form = new Cms_CustomerForm();
                    form.UUID = Guid.NewGuid();
                    form.aspnet_Applications = dataContext.QueryApplication(this.Application).First();

                    dataContext.AddToCms_CustomerForm(form);
                }
                else
                {
                    //Update
                    form = this.QueryFormByUUID(this.FormUUID);
                }

                if (form != null)
                {
                    form.FormName = this.FormName;
                    form.FormScript = this.FormScript;
                    form.CustomStyles = this.CustomStyles;
                    form.FeedbackScript = this.FeedbackScript;
                    form.UserName = this.User.Identity.Name;
                    form.PostDate = DateTime.Now;
                    dataContext.SaveChanges();
                    responseText = form.UUID.ToString();
                }

                this.Response.Clear();
                this.Response.Write(responseText);
                this.Response.End();
            }
            // load data
            else if (this.Request.QueryString["DataState"] == "edit")
            {
                string formUUID = this.Request.QueryString["FormUUID"];
                Cms_CustomerForm form = this.QueryFormByUUID(formUUID);
                if (form != null)
                {
                    this.FormUUID = form.UUID.ToString();
                    this.FormName = HttpUtility.HtmlEncode(form.FormName);
                    this.FormScript = HttpUtility.HtmlEncode(form.FormScript);
                    this.CustomStyles = HttpUtility.HtmlEncode(form.CustomStyles);
                    this.FeedbackScript = HttpUtility.HtmlEncode(form.FeedbackScript);
                }
            }

            this.Application = this.Request.QueryString["application"];
            return View();
        }

        #endregion

        #region Preview

        private string CustomStylesHolder
        {
            set
            {
                string myVal = value;
                
                if (StringExtensions.IsNullOrEmptyTrim(myVal))
                    myVal = this.CustomStyles;

                this.ViewData["CustomStylesHolder"] = myVal;
            }
        }

        private string CustomScriptsHolder
        {
            set
            {
                this.ViewData["CustomScriptsHolder"] = value;
            }
        }

        private string TitleHolder
        {
            set
            {
                this.ViewData["TitleHolder"] = value;
            }
        }

        public ActionResult Preview()
        {
            if (this.Request.Form.Count > 0)
            {
                string postData = this.Request.Form["postData"];
                if (!StringExtensions.IsNullOrEmptyTrim(postData))
                {
                    NameValueCollection values = HttpUtility.ParseQueryString(postData);
                    this.TitleHolder = HttpUtility.UrlDecode(values["title"]);
                    this.CustomStylesHolder = HttpUtility.UrlDecode(values["cstyles"]);
                    this.CustomScriptsHolder = HttpUtility.UrlDecode(values["script"]);
                }
            }
            return View();
        }

        #endregion

        #region Viewer

        private Cms_CustomerFormValues QueryValuesByUUID(string valueUUID)
        {
            if (StringExtensions.IsNullOrEmptyTrim(valueUUID))
                return null;

            Guid uid = new Guid(valueUUID);
            Cms_CustomerFormValues valuesObj = (from v in dataContext.Cms_CustomerFormValues
                                                where v.UUID == uid
                                                select v).First();
            return valuesObj;
        }

        private string FormValues
        {
            get
            {
                return this.Request.Form["__FormValues"];
            }
            set
            {
                this.ViewData["__FormValues"] = value;
            }
        }

        private string FormValueUUID
        {
            get
            {
                return this.Request.Form["__FormValueUUID"];
            }
            set
            {
                this.ViewData["__FormValueUUID"] = value.ToString();
            }
        }

        private bool IsPostBack
        {
            get
            {
                string key = "__YPostBack";
                bool isPostback = !StringExtensions.IsNullOrEmptyTrim(this.Request.Form[key]);
                this.ViewData[key] = "1";
                return isPostback;
            }
        }

        private string BuildXmlValue(string queryValues)
        {
            //MemoryStream ms = new MemoryStream();
            //using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
            //{
            //    writer.WriteStartDocument(true);
            //    writer.WriteStartElement("FormValues");
            //    NameValueCollection fieldValues = HttpUtility.ParseQueryString(queryValues);
            //    for (int i = 0; i < fieldValues.Keys.Count; i++)
            //    {
            //        string key = fieldValues.Keys[i];
            //        string val = fieldValues[key];
            //        writer.WriteStartElement(key);
            //        writer.WriteString(val);
            //        writer.WriteEndElement();
            //    }
            //    writer.WriteEndElement();
            //    writer.WriteEndDocument();
            //    writer.Flush();
            //}
            //return Encoding.UTF8.GetString(ms.ToArray());

            StringBuilder builder = new StringBuilder();
            builder.Append("<FormValues>");
            NameValueCollection fieldValues = HttpUtility.ParseQueryString(queryValues);
            for (int i = 0; i < fieldValues.Keys.Count; i++)
            {
                string key = fieldValues.Keys[i];
                string val = fieldValues[key];
                if (!StringExtensions.IsNullOrEmptyTrim(val))
                {
                    val = val.Replace("<", "&lt;");
                    val = val.Replace(">", "&gt;");
                    val = val.Replace("&", "&amp;");
                    val = val.Replace("'", "&apos;");
                    val = val.Replace("\"", "&quot;");
                }
                builder.AppendFormat("<{0}>{1}</{0}>", key, val);
            }
            builder.Append("</FormValues>");
            return builder.ToString();
        }

        public ActionResult Viewer()
        {
            string dState = this.Request.QueryString["DataState"];
            string formUUID = this.Request.QueryString["FormUUID"];
            string formValueUUID = this.Request.QueryString["FormValueUUID"];

            Cms_CustomerForm formObj = this.QueryFormByUUID(formUUID);
            if (formObj == null)
                throw new Exception("Can't find the web form: " + formUUID);

            this.TitleHolder = formObj.FormName;
            this.CustomStylesHolder = formObj.CustomStyles;
            if (!StringExtensions.IsNullOrEmptyTrim(formObj.FeedbackScript) && this.IsPostBack)
            {
                this.CustomScriptsHolder = formObj.FeedbackScript;
            }
            else
            {
                this.CustomScriptsHolder = formObj.FormScript;
            }

            if (this.IsPostBack)
            {
                if (dState == "add" && StringExtensions.IsNullOrEmptyTrim(this.FormValueUUID) && !StringExtensions.IsNullOrEmptyTrim(this.FormValues))
                {
                    // save add
                    Cms_CustomerFormValues valuesObj = new Cms_CustomerFormValues();
                    valuesObj.UUID = Guid.NewGuid();
                    valuesObj.PostDate = DateTime.Now;
                    valuesObj.FormValues = this.FormValues;
                    valuesObj.XmlTypeValues = this.BuildXmlValue(valuesObj.FormValues);

                    Cms_CustomerForm form = this.QueryFormByUUID(formUUID);
                    form.Cms_CustomerFormValues.Add(valuesObj);
                    dataContext.SaveChanges();

                    this.FormValues = valuesObj.FormValues;
                    this.FormValueUUID = valuesObj.UUID.ToString();
                }
                // edit data
                if (dState == "edit" || !StringExtensions.IsNullOrEmptyTrim(this.FormValueUUID))
                {
                    if (dState == "add")
                        formValueUUID = this.FormValueUUID;

                    var valuesObj = this.QueryValuesByUUID(formValueUUID);
                    if (valuesObj != null)
                    {
                        valuesObj.PostDate = DateTime.Now;
                        valuesObj.FormValues = this.FormValues;
                        valuesObj.XmlTypeValues = this.BuildXmlValue(valuesObj.FormValues);
                        dataContext.SaveChanges();

                        this.FormValues = valuesObj.FormValues;
                        this.FormValueUUID = valuesObj.UUID.ToString();
                    }
                }
            }
            else
            {
                // view data
                if (dState != "add")
                {
                    var valuesObj = this.QueryValuesByUUID(formValueUUID);
                    if (valuesObj != null)
                    {
                        this.FormValues = valuesObj.FormValues;
                        this.FormValueUUID = valuesObj.UUID.ToString();
                    }
                }
            }

            return View();
        }

        #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.