/*
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.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Everest.Library;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Library.ExtensionMethod;
using System.Web.Compilation;
namespace Everest.CmsServices.MvcHelper{
public static class CmsViewExtensions
{
#region GetContent
/// <summary>
/// Gets the contents from the ViewData returned by data rule.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <returns></returns>
public static IEnumerable<IDictionary<string, object>> GetContents(this ICmsView cmsView, string viewDataKey)
{
var contents = cmsView.GetViewDataValue(viewDataKey);
return ContentExtension.ToList(contents);
}
/// <summary>
/// Gets the content from the ViewData returned by data rule.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="viewDataKey">The ViewData key.</param>
/// <returns></returns>
public static IDictionary<string, object> GetContent(this ICmsView cmsView, string viewDataKey)
{
var content = cmsView.GetViewDataValue(viewDataKey);
return ContentExtension.ToContent(content);
}
/// <summary>
/// Gets the sub contents from ViewData. The Data Rule which return the ViewData must have "include child" checked. Otherwise subcontent will not be queried.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="subContentSchemaName">Name of the sub content schema.</param>
/// <returns></returns>
public static IEnumerable<IDictionary<string, object>> GetSubContents(this ICmsView cmsView, string viewDataKey, string subContentSchemaName)
{
return (IEnumerable<IDictionary<string, object>>)cmsView.GetContent(viewDataKey)[subContentSchemaName];
}
/// <summary>
/// Gets the view data value.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <returns></returns>
public static object GetViewDataValue(this ICmsView cmsView, string viewDataKey)
{
var value = cmsView.PrivateViewData[viewDataKey];
if (value == null)
{
value = cmsView.ViewData[viewDataKey];
}
return value;
}
#endregion
#region GetResource
/// <summary>
/// Gets all the resources.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <returns></returns>
public static IDictionary<string, string> GetResources(this ICmsView cmsView)
{
return UnityManager.Resolve<TextResourceService>().GetTextResources(cmsView.Cms_Application.ApplicationName);
}
/// <summary>
/// Gets the resource by key.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="key">The key. If key value is not available, the key name will be returned as value.</param>
/// <returns></returns>
public static string GetResource(this ICmsView cmsView, string key)
{
return cmsView.GetResource(key, key);
}
/// <summary>
/// Gets the resource by key
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns></returns>
public static string GetResource(this ICmsView cmsView, string key, string defaultValue)
{
key = StringExtensions.TryTrim(key);
if (cmsView.GetResources().ContainsKey(key))
{
return cmsView.GetResources()[key];
}
else
{
UnityManager.Resolve<TextResourceService>().AddTextResource(cmsView.Cms_Application.ApplicationName, key, defaultValue);
return defaultValue;
}
}
/// <summary>
/// Gets the resource.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="strNamespace">The namespace of this resource key.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns></returns>
public static string GetResource(this ICmsView cmsView, string strNamespace, string key, string defaultValue)
{
return cmsView.GetResource(Cms_TextResource.GetFullName(strNamespace, key), defaultValue);
}
#endregion
#region Render ContentTemplate
/// <summary>
/// Renders the content template by name.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateName">Name of the content template.</param>
/// <returns></returns>
[Obsolete("Please use RenderContentTemplate instead.")]
public static string RenderContentTemplateByName(this ICmsView cmsView, string contentTemplateName)
{
return cmsView.RenderContentTemplateByName(contentTemplateName, null);
}
/// <summary>
/// Renders the content template by name
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateName">Name of the content template.</param>
/// <param name="propertySettings">The property settings. For example: new {property1=value1, property2=value2}</param>
/// <returns></returns>
[Obsolete("Please use RenderContentTemplate instead.")]
public static string RenderContentTemplateByName(this ICmsView cmsView, string contentTemplateName, object propertySettings)
{
aspnet_Applications application = cmsView.Cms_Application;
var contentTemplate = CachedData.GetContentTemplate(application.ApplicationName, contentTemplateName);
return RenderContentTemplate(cmsView, contentTemplate, propertySettings);
}
/// <summary>
/// Renders the content template by UUID.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateUUID">The content template UUID.</param>
/// <returns></returns>
[Obsolete("Please use RenderContentTemplate instead.")]
public static string RenderContentTemplateByUUID(this ICmsView cmsView, Guid contentTemplateUUID)
{
return cmsView.RenderContentTemplateByUUID(contentTemplateUUID, null);
}
/// <summary>
/// Renders the content template by UUID.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateUUID">The content template UUID.</param>
/// <param name="propertySettings">The property settings. For example: new {property1=value1, property2=value2}</param>
/// <returns></returns>
[Obsolete("Please use RenderContentTemplate instead.")]
public static string RenderContentTemplateByUUID(this ICmsView cmsView, Guid contentTemplateUUID, object propertySettings)
{
aspnet_Applications application = cmsView.Cms_Application;
var contentTemplate = CachedData.GetContentTemplate(application.ApplicationName, contentTemplateUUID);
return RenderContentTemplate(cmsView, contentTemplate, propertySettings);
}
private static string RenderContentTemplate(ICmsView cmsView, Cms_ContentTemplate contentTemplate, object propertySettings)
{
Hashtable props = new Hashtable();
if (propertySettings != null)
{
props = HtmlExtensionUtility.GetPropertyHash(propertySettings);
}
props.Add("ContentTemplate", contentTemplate);
var virtualPath = contentTemplate.GetVirtualPath();
return cmsView.Html.RenderUserControl(virtualPath, props);
}
/// <summary>
/// Renders the content template by name.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateName">Name of the content template.</param>
/// <returns></returns>
public static string RenderContentTemplate(this ICmsView cmsView, string contentTemplateName)
{
return cmsView.RenderContentTemplate(contentTemplateName, null);
}
/// <summary>
/// Renders the content template by name
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateName">Name of the content template.</param>
/// <param name="propertySettings">The property settings. For example: new {property1=value1, property2=value2}</param>
/// <returns></returns>
public static string RenderContentTemplate(this ICmsView cmsView, string contentTemplateName, object propertySettings)
{
aspnet_Applications application = cmsView.Cms_Application;
var contentTemplate = CachedData.GetContentTemplate(application.ApplicationName, contentTemplateName);
return RenderContentTemplate(cmsView, contentTemplate, propertySettings);
}
/// <summary>
/// Renders the content template by UUID.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateUUID">The content template UUID.</param>
/// <returns></returns>
public static string RenderContentTemplate(this ICmsView cmsView, Guid contentTemplateUUID)
{
return cmsView.RenderContentTemplate(contentTemplateUUID, null);
}
/// <summary>
/// Renders the content template by UUID.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="contentTemplateUUID">The content template UUID.</param>
/// <param name="propertySettings">The property settings. For example: new {property1=value1, property2=value2}</param>
/// <returns></returns>
public static string RenderContentTemplate(this ICmsView cmsView, Guid contentTemplateUUID, object propertySettings)
{
aspnet_Applications application = cmsView.Cms_Application;
var contentTemplate = CachedData.GetContentTemplate(application.ApplicationName, contentTemplateUUID);
return RenderContentTemplate(cmsView, contentTemplate, propertySettings);
}
#endregion
/// <summary>
/// Executes the method on static code.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="codeName">Name of the code including application path. For example: application1.codename1</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public static object ExecuteMethod(this ICmsView cmsView, string codeName, string methodName, params object[] parameters)
{
var uniqueName = new UniqueName(codeName);
string application = uniqueName.ApplicationName;
string name = uniqueName.ItemName;
if (string.IsNullOrEmpty(application))
{
StaticCodeService staticCodeService = UnityManager.Resolve<StaticCodeService>();
var staticCodes = staticCodeService.GetStaticCodes(cmsView.Cms_Application.ApplicationName, codeName, false);
if (staticCodes.Count() > 0)
{
var staticCode = staticCodes.First();
application = staticCode.Application;
name = staticCode.Name;
}
}
var virtualPath = StaticCodeService.GetStaticCodeVirtualPath(application, name);
var codeType = BuildManager.GetCompiledType(virtualPath);
MethodInfo methodInfo = codeType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
return methodInfo.Invoke(null, parameters);
}
#region Set html meta
/// <summary>
/// Sets the HTML meta.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="name">The name.</param>
/// <param name="content">The content.</param>
public static void SetHtmlMeta(this ICmsView cmsView, string name, string content)
{
SetHtmlMeta(cmsView, name, content, "", "");
}
/// <summary>
/// Sets the HTML meta.
/// </summary>
/// <param name="cmsView">The CMS view.</param>
/// <param name="name">The name.</param>
/// <param name="content">The content.</param>
/// <param name="httpEquiv">The HTTP equiv.</param>
/// <param name="schema">The schema.</param>
public static void SetHtmlMeta(this ICmsView cmsView, string name, string content, string httpEquiv, string schema)
{
HtmlMeta meta = new HtmlMeta();
meta.Name = name;
meta.Content = content;
meta.HttpEquiv = httpEquiv;
meta.Scheme = schema;
cmsView.ViewPage.Header.Controls.Add(meta);
}
#endregion
}
}
|