EditContentFiltersBox.ascx.cs :  » Bloggers » dasBlog » newtelligence » DasBlog » Web » 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 » Bloggers » dasBlog 
dasBlog » newtelligence » DasBlog » Web » EditContentFiltersBox.ascx.cs
using System;
using System.IO;
using System.Resources;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using newtelligence.DasBlog.Web.Core;
using newtelligence.DasBlog.Web.Core.WebControls;

namespace newtelligence.DasBlog.Web{
  /// <summary>                                             
  ///    Summary description for EditNavigatorLinksBox.
  /// </summary>
  public partial class EditContentFiltersBox : SharedBaseControl
  {
        [TransientPageState]
        public ContentFilterCollection contentFilters;
        protected RequiredFieldValidator validatorRFname;
        protected Panel Panel1;
        protected ResourceManager resmgr;
        
    private Core.EditControlAdapter editControl;

        private void SaveFilters( )
        {
            SiteConfig siteConfig = SiteConfig.GetSiteConfig(SiteConfig.GetConfigFilePathFromCurrentContext());
            siteConfig.ApplyContentFiltersToWeb = checkFilterHtml.Checked;
            siteConfig.ApplyContentFiltersToRSS = checkFilterRSS.Checked;
            siteConfig.ContentFilters.Clear();
            siteConfig.ContentFilters.AddRange(contentFilters);
          XmlSerializer ser = new XmlSerializer(typeof(SiteConfig));
            using (StreamWriter writer = new StreamWriter(SiteConfig.GetConfigFilePathFromCurrentContext()))
            {
                ser.Serialize(writer, siteConfig);
            }
        }

        protected string TruncateString( string s, int max )
        {
            if ( s == null )
            {
                return "";
            }
            else if ( s.Length < max )
            {
                return s;
            }
            else if ( s.Length >= max )
            {
                return s.Substring(0,max)+"...";
            }
            return s;
        }

        private void LoadFilters( )
        {
            SiteConfig siteConfig = SiteConfig.GetSiteConfig(SiteConfig.GetConfigFilePathFromCurrentContext());
            contentFilters = new ContentFilterCollection(siteConfig.ContentFilters);
            checkFilterHtml.Checked = siteConfig.ApplyContentFiltersToWeb;
            checkFilterRSS.Checked = siteConfig.ApplyContentFiltersToRSS;
        }

        private void BindGrid()
        {
            contentFiltersGrid.DataSource = contentFilters;
            DataBind();
        }

    protected void Page_Load(object sender, EventArgs e)
    {
      SharedBasePage requestPage = this.Page as SharedBasePage;

            if (SiteSecurity.IsInRole("admin") == false) 
            {
                Response.Redirect("~/FormatPage.aspx?path=SiteConfig/accessdenied.format.html");
            }

            resmgr = ((ResourceManager)ApplicationResourceTable.Get());

            if ( !IsPostBack || contentFilters == null )
            {
                LoadFilters( );
                UpdateTestBox();
            }

            BindGrid();
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);

      EditControlProvider editControlProvider = new EditControlProvider();
      this.editControl = editControlProvider.CreateEditControl();
      this.editControlHolder.Controls.Add(editControl.Control);
      this.editControl.Initialize();
      this.editControl.Width = Unit.Percentage(99d);
      this.editControl.Height = Unit.Pixel(200);
    }
    
    /// <summary>
    ///    Required method for Designer support - do not modify
    ///    the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.contentFiltersGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.contentFiltersGrid_ItemCommand);
      this.contentFiltersGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.contentFiltersGrid_PageIndexChanged);
      this.contentFiltersGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.contentFiltersGrid_CancelCommand);
      this.contentFiltersGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.contentFiltersGrid_EditCommand);
      this.contentFiltersGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.contentFiltersGrid_UpdateCommand);
      this.contentFiltersGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.contentFiltersGrid_DeleteCommand);

    }
    #endregion

        private void contentFiltersGrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            contentFiltersGrid.EditItemIndex = e.Item.ItemIndex;
            DataBind();
            UpdateTestBox();
        }

        private void contentFiltersGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            contentFilters.RemoveAt(e.Item.DataSetIndex);
            contentFiltersGrid.EditItemIndex = -1;
            DataBind();
            UpdateTestBox();
            changesAlert.Visible = true;
        }

        private void contentFiltersGrid_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            contentFiltersGrid.EditItemIndex = -1;
            DataBind();
            UpdateTestBox();
        }

        private void contentFiltersGrid_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            ContentFilter filter = contentFilters[e.Item.DataSetIndex];
            filter.Expression = ((TextBox)e.Item.FindControl("textExpression")).Text;
            filter.MapTo = ((TextBox)e.Item.FindControl("textMapTo")).Text;
            filter.IsRegEx = ((CheckBox)e.Item.FindControl("checkRegex")).Checked;
            contentFiltersGrid.EditItemIndex = -1;
            DataBind();
            UpdateTestBox();
            changesAlert.Visible = true;
        }                                          

        private void contentFiltersGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            contentFiltersGrid.CurrentPageIndex = e.NewPageIndex;
            DataBind();
            UpdateTestBox();
        }

        private void contentFiltersGrid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if ( e.CommandName == "AddItem" )
            {
                ContentFilter newFilter = new ContentFilter();
                newFilter.Expression = "New Expression";
                contentFilters.Insert(0,newFilter);
                contentFiltersGrid.CurrentPageIndex = 0;
                contentFiltersGrid.EditItemIndex = 0;
                UpdateTestBox();
                BindGrid();
            }
            else if ( e.CommandName == "MoveUp" && e.Item != null )
            {
                int position = e.Item.DataSetIndex;

                if ( position > 0 )
                {
                    ContentFilter filter = contentFilters[position];
                    contentFilters.RemoveAt( position );
                    contentFilters.Insert( position-1, filter );
                    contentFiltersGrid.CurrentPageIndex = (position-1) / contentFiltersGrid.PageSize;
                    if ( contentFiltersGrid.EditItemIndex == position )
                    {
                        contentFiltersGrid.EditItemIndex = position - 1;
                    }
                    else if ( contentFiltersGrid.EditItemIndex == position - 1)
                    {
                        contentFiltersGrid.EditItemIndex = position;
                    }
                    changesAlert.Visible = true;
                }
                UpdateTestBox();
                BindGrid();
            }
            else if ( e.CommandName == "MoveDown" && e.Item != null )
            {
                int position = e.Item.DataSetIndex;

                if ( position < contentFilters.Count-1 )
                {
                    ContentFilter filter = contentFilters[position];
                    contentFilters.RemoveAt( position );
                    contentFilters.Insert( position+1, filter );
                    contentFiltersGrid.CurrentPageIndex = (position+1) / contentFiltersGrid.PageSize;
                    if ( contentFiltersGrid.EditItemIndex == position )
                    {
                        contentFiltersGrid.EditItemIndex = position + 1;
                    }
                    else if ( contentFiltersGrid.EditItemIndex == position + 1)
                    {
                        contentFiltersGrid.EditItemIndex = position;
                    }
                    changesAlert.Visible = true;
                }
                UpdateTestBox();
                BindGrid();

            }
        }

        private string Filter(  string content )
        {
            if ( contentFilters == null )
                return content;

            foreach ( ContentFilter filter in contentFilters )
            {
                if ( filter.IsRegEx )
                {
                    try
                    {
                        content = Regex.Replace(content, filter.Expression,filter.MapTo,RegexOptions.Singleline);
                    }
                    catch( Exception e )
                    {
                        content += String.Format("<br /><br />Error in filter {0}<br />{1}", HttpUtility.HtmlEncode(filter.Expression),HttpUtility.HtmlEncode(e.Message));
                        break;
                    }
                }
                else
                {
                    content = content.Replace( filter.Expression, filter.MapTo );
                }
            }

            return content;
        }

        private void UpdateTestBox()
        {
            previewPanel.Style.Add("padding","5px");
            previewPanel.Style.Add("margin","5px");

      previewPanel.Controls.Add( new LiteralControl(Filter(editControl.Text)));
      
            sampleContentBox.Visible = true;
        }

        protected void buttonFilterResults_Click(object sender, EventArgs e)
        {
            UpdateTestBox();
        }

        protected void buttonSaveChanges_Click(object sender, EventArgs e)
        {
            SaveFilters();
            UpdateTestBox();
            changesAlert.Visible = false;
        }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.