/*
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;
using System.Web.UI;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Everest.CmsServices.Models;
using Everest.CmsServices.DataRule;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.MvcHelper{
/// <summary>
///
/// </summary>
public class CmsUserControl : UserControl, ICmsView
{
public CmsUserControl()
{
PrivateViewData = new ViewDataDictionary();
}
/// <summary>
/// Gets or sets the content template.
/// </summary>
/// <value>The content template.</value>
public Cms_ContentTemplate ContentTemplate { get; set; }
#region IMVCUserControl Members
/// <summary>
/// Gets the ajax.
/// </summary>
/// <value>The ajax.</value>
public System.Web.Mvc.AjaxHelper<object> Ajax
{
get { return this.ViewPage.Ajax; }
}
/// <summary>
/// Gets the HTML.
/// </summary>
/// <value>The HTML.</value>
public System.Web.Mvc.HtmlHelper<object> Html
{
get { return this.ViewPage.Html; }
}
/// <summary>
/// Gets the temp data.
/// </summary>
/// <value>The temp data.</value>
public System.Web.Mvc.TempDataDictionary TempData
{
get { return this.ViewPage.TempData; }
}
/// <summary>
/// Gets the URL.
/// </summary>
/// <value>The URL.</value>
public System.Web.Mvc.UrlHelper Url
{
get { return this.ViewPage.Url; }
}
/// <summary>
/// Gets the view context.
/// </summary>
/// <value>The view context.</value>
public System.Web.Mvc.ViewContext ViewContext
{
get { return this.ViewPage.ViewContext; }
}
/// <summary>
/// Gets the view data.
/// </summary>
/// <value>The view data.</value>
public System.Web.Mvc.ViewDataDictionary ViewData
{
get { return CmsContext.DataRuleContext.ViewData; }
}
/// <summary>
/// Gets the view page.
/// </summary>
/// <value>The view page.</value>
public ViewPage ViewPage
{
get
{
return (ViewPage)this.Page;
}
}
/// <summary>
/// Gets the data context.
/// </summary>
/// <value>The data context.</value>
public IEverestCmsDataContext DataContext
{
get
{
return CmsContext.DataContext;
}
}
/// <summary>
/// Gets the CMS_ application.
/// </summary>
/// <value>The CMS_ application.</value>
public aspnet_Applications Cms_Application
{
get { return CmsContext.Cms_Application; }
}
/// <summary>
/// Gets the CMS_ page.
/// </summary>
/// <value>The CMS_ page.</value>
public Cms_Page Cms_Page
{
get { return CmsContext.Cms_Page; }
}
/// <summary>
/// Gets the search service.
/// </summary>
/// <value>The search service.</value>
public Search.SearchService SearchService
{
get { return CmsContext.SearchService; }
}
/// <summary>
/// Gets the CMS context.
/// </summary>
/// <value>The CMS context.</value>
public CmsContext CmsContext
{
get { return HttpContext.Current.GetCmsContext(); }
}
/// <summary>
/// Gets the page helper.
/// </summary>
/// <value>The page helper.</value>
public PageHelper PageHelper
{
get
{
return CmsContext.PageHelper;
}
}
public ViewDataDictionary PrivateViewData
{
get;
set;
}
public ContentService ContentService
{
get { return this.CmsContext.ContentService; }
}
public FolderService FolderService
{
get
{
return this.CmsContext.FolderService;
}
}
#endregion
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (CmsContext.DataRuleContext != null)
{
//store the view data.
var storedViewData = CmsContext.DataRuleContext.ViewData;
CmsContext.DataRuleContext.ViewData = PrivateViewData;
CmsContext.DataRuleContext.UserControl = this;
var bContinue = PreDataRuleExecute(CmsContext.DataRuleContext);
if (bContinue)
{
//query the data by the data rule
DataRuleEvaluator.Evaluate(CmsContext.DataRuleContext, CachedData.GetDataRulesByContentTemplate(ContentTemplate.UUID));
//restore the viewdata.
CmsContext.DataRuleContext.ViewData = storedViewData;
CmsContext.DataRuleContext.UserControl = null;
this.OnDataRuleExecuted();
}
}
}
/// <summary>
/// Pres the data rule execute.
/// </summary>
/// <param name="dataRuleContext">The data rule context.</param>
/// <returns>return false to skip evaluating datarule</returns>
protected virtual bool PreDataRuleExecute(DataRuleContext dataRuleContext)
{
return true;
}
protected virtual void OnDataRuleExecuted()
{
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
protected override void Render(HtmlTextWriter writer)
{
//writer.WriteLine("<!-- {0} -->", ContentTemplate.Name);
base.Render(writer);
//writer.WriteLine("<!-- end {0} -->", ContentTemplate.Name);
}
#region Paging Settings
private int pageSize = 20;
/// <summary>
/// Gets the size of the page.
/// default 20
/// </summary>
/// <value>The size of the page.</value>
public virtual int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
private int pageIndex = -1;
/// <summary>
/// Gets the index of the page.
/// default get from the querystring key 'pageIndex'
/// </summary>
/// <value>The index of the page.</value>
public virtual int PageIndex
{
get
{
if (pageIndex == -1)
{
if (!StringExtensions.IsNullOrEmptyTrim(Request.QueryString["pageIndex"]))
{
return int.Parse(Request.QueryString["pageIndex"]);
}
return 1;
}
else
return pageIndex;
}
set
{
pageIndex = value;
}
}
private int totalCount = -1;
/// <summary>
/// Gets the total count.
/// default:(int)PrivateViewData["TotalCount"]
/// </summary>
/// <value>The total count.</value>
public virtual int TotalCount
{
get
{
if (this.GetViewDataValue("TotalCount") != null && totalCount == -1)
return (int)this.GetViewDataValue("TotalCount");
else
{
if (totalCount == -1)
{
return 0;
}
return totalCount;
}
}
set
{
totalCount = value;
}
}
/// <summary>
/// Gets the start index of the row.
/// </summary>
/// <value>The start index of the row.</value>
public virtual int StartRowIndex
{
get
{
return (PageIndex - 1) * PageSize + 1;
}
}
#endregion
#region ICmsView Members
public string CmsTheme
{
get
{
return ((CmsViewPage)this.ViewPage).CmsTheme;
}
set
{
((CmsViewPage)this.ViewPage).CmsTheme = value;
}
}
#endregion
}
}
|