/*
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.Text;
using System.Web.Mvc;
using System.Web;
using System.IO;
using System.Configuration;
using Everest.CmsServices.Services;
using Everest.Library.ExtensionMethod;
using Everest.Library.Providers.Caching;
using Everest.Library;
using InteSoft.Web.ExternalResourceLoader;
namespace Everest.CmsServices.MvcHelper{
#region Editor
public enum Editor
{
Text,
Number,
Date,
Time,
DateTime,
Textarea,
ComboBox,
HtmlEditor
}
#endregion
#region EditFieldContextClass
public class EditFieldContextClass
{
private Dictionary<Guid, bool> _contentValid;
public EditFieldContextClass()
{
_contentValid = new Dictionary<Guid, bool>();
}
public bool ValidUserRight(Guid uuid)
{
if (!_contentValid.ContainsKey(uuid))
{
var isValid = false;
var userName = HttpContext.Current.User.Identity.Name;
if (!string.IsNullOrEmpty(userName))
{
var workflow = UnityManager.Resolve<WorkflowService>();
isValid = workflow.CanEditContentByStatus(uuid, 1, userName);
}
_contentValid.Add(uuid, isValid);
}
return _contentValid[uuid];
}
public bool ResourceRegistered
{
get;
internal set;
}
private int _idCounter = -1;
public int IDCounter
{
get
{
_idCounter++;
return _idCounter;
}
}
}
#endregion
public class EditField
{
#region Consts
public const string AttributeMark = "etype";
public const string ComboItemsSpliter = "-|-";
private const string StyleTags = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
private const string ScriptTags = "<script type=\"text/javascript\" src=\"{0}\"></script>";
#endregion
#region Constructor
private CmsContext _cmsContext;
private CmsContext CmsContext
{
get
{
if (_cmsContext == null)
_cmsContext = this.HtmlHelper.ViewContext.HttpContext.GetCmsContext();
return _cmsContext;
}
}
private HtmlHelper HtmlHelper
{
get;
set;
}
public EditField(HtmlHelper htmlHelper, IDictionary<string, object> contentValues, string fieldName, Editor fieldType)
{
string msg = "contentValues must contain the {0} key !";
if (contentValues == null)
throw new Exception("contentValues can not be null !");
if (!contentValues.ContainsKey("UUID"))
throw new Exception(string.Format(msg, "UUID"));
if (!contentValues.ContainsKey("ContentId"))
throw new Exception(string.Format(msg, "ContentId"));
if (!contentValues.ContainsKey(fieldName))
throw new Exception(string.Format(msg, fieldName));
if (!contentValues.ContainsKey("Application"))
throw new Exception(string.Format(msg, "Application"));
if (!contentValues.ContainsKey("FolderUUID"))
throw new Exception(string.Format(msg, "FolderUUID"));
// set parameters
this.HtmlHelper = htmlHelper;
this.ContentId = Convert.ToInt32(contentValues["ContentId"]);
this.ContentUUID = (Guid)contentValues["UUID"];
this.FolderUUID = (Guid)contentValues["FolderUUID"];
this.ContentApplication = (string)contentValues["Application"];
this.RuntimeApplication = this.CmsContext.Cms_Application.ApplicationName;
this.FieldName = fieldName;
this.FieldType = fieldType;
this.ClientID = "eidtfield_" + this.CmsContext.EditFieldContext.IDCounter;
if (contentValues[fieldName] != null)
this.Value = contentValues[fieldName].ToString();
// user valid
this.IsValid = this.CmsContext.EditFieldContext.ValidUserRight(this.ContentUUID);
}
#endregion
#region Client Parameters
public string Value
{
get;
private set;
}
public int ContentId
{
get;
private set;
}
public Guid ContentUUID
{
get;
private set;
}
public string FieldName
{
get;
private set;
}
public Editor FieldType
{
get;
private set;
}
public string ClientID
{
get;
private set;
}
public string EditCssName
{
get;
set;
}
public string ToolTip
{
get;
set;
}
public string[] ComboItems
{
get;
set;
}
public bool IsValid
{
get;
private set;
}
public string ContentApplication
{
get;
private set;
}
public string RuntimeApplication
{
get;
private set;
}
public Guid FolderUUID
{
get;
private set;
}
#endregion
#region RegisterResources
private void RegisterResources(StringBuilder builder)
{
builder.Append(Extensions.ExternalResources(this.HtmlHelper, "editfieldCss"));
builder.Append(Extensions.ExternalResources(this.HtmlHelper, "editfieldJs"));
}
#endregion
#region RenderHtml
public string RenderHtml()
{
// validation
if (!this.IsValid)
{
return this.Value;
}
// register resources
StringBuilder builder = new StringBuilder();
if (!this.CmsContext.EditFieldContext.ResourceRegistered)
{
this.CmsContext.EditFieldContext.ResourceRegistered = true;
this.RegisterResources(builder);
}
// build html
builder.AppendFormat("<div style=\"display:inline;\" id=\"{0}\"", this.ClientID);
builder.AppendFormat(" {0}=\"{1}\"", AttributeMark, this.FieldType.ToString());
builder.AppendFormat(" capp=\"{0}\"", this.ContentApplication);
builder.AppendFormat(" rapp=\"{0}\"", this.RuntimeApplication);
builder.AppendFormat(" field=\"{0}\"", this.FieldName);
builder.AppendFormat(" cid=\"{0}\"", this.ContentId);
builder.AppendFormat(" cuuid=\"{0}\"", this.ContentUUID.ToString());
builder.AppendFormat(" fuuid=\"{0}\"", this.FolderUUID.ToString());
if (!StringExtensions.IsNullOrEmptyTrim(this.EditCssName))
builder.AppendFormat(" editcss=\"{0}\"", this.EditCssName);
if (!StringExtensions.IsNullOrEmptyTrim(this.ToolTip))
builder.AppendFormat(" title=\"{0}\"", this.ToolTip);
if (this.ComboItems != null && this.FieldType == Editor.ComboBox)
{
int count = this.ComboItems.Length;
builder.Append(" comboItems=\"");
for (int i = 0; i < count; i++)
{
builder.Append(HttpUtility.UrlEncode(this.ComboItems[i]));
if (i != count - 1)
builder.Append(ComboItemsSpliter);
}
builder.Append("\"");
}
builder.AppendFormat(" applicationPath='{0}'", this.CmsContext.UrlHelper.RequestContext.HttpContext.Request.ApplicationPath);
builder.Append(">");
builder.Append(this.Value);
builder.Append("</div>");
return builder.ToString();
}
#endregion
}
}
|