//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// CSModule to remove the HTML formatting from the Body and Excerpt of Gallery Image Posts
	/// </summary>
	public class HtmlScrubberModule : ICSModule
	{
		public HtmlScrubberModule() { }

		public void Init(CSApplication csa, XmlNode node)
		{
			csa.PrePostUpdate +=new CSPostEventHandler(csa_BeforeProcessPost);
		}

		private void csa_BeforeProcessPost(IContent content, CSPostEventArgs e)
		{
			if(e.ApplicationType == ApplicationType.Gallery && (e.State == ObjectState.Create || e.State == ObjectState.Update))
			{
				GalleryPost pic = content as GalleryPost;
				if(pic != null)
				{
					if(pic.GalleryPostType == GalleryPostType.Image)
					{
						pic.FormattedBody = HtmlScrubber.Clean(pic.FormattedBody,true,true);
						pic.Excerpt = Globals.HtmlEncode(pic.Excerpt);
					}
				}
			}
		}
	}
}
