EditField.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » MvcHelper » 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 » MvcHelper » EditField.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.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
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.