/*
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("<", "<");
val = val.Replace(">", ">");
val = val.Replace("&", "&");
val = val.Replace("'", "'");
val = val.Replace("\"", """);
}
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
}
}
|