// Personal Webpage Engine
// Copyright(c) 2008 Mihail Kochanov
// You can contact me at StilgarAD@yahoo.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  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.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
//
// Personal Web Page Engine
//
//------------------------------------------------------------------------------

using System;
using System.Configuration;
using System.Data.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Admin
{
    public partial class EditNewsItem : System.Web.UI.Page
    {

        #region Properties

        private NewsItem NewsItem { get; set; }
        private BlogNETDataContext dc;

        #endregion

        #region PageEvents

        protected void Page_PreInit(object sender, EventArgs e)
        {
            int newsItemID;

            if (!Int32.TryParse(Request.QueryString[Constants.QueryString.NewsItemID], out newsItemID))
            {
                Session[Constants.Session.ErrorMessage] = Constants.Error.InvalidQueryString;
                Server.Transfer("~/Error.aspx");
            }

            dc = new BlogNETDataContext(ConfigurationManager.ConnectionStrings["BlogNETDB"].ConnectionString);
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<NewsItem>(ni => ni.Tags);
            dc.LoadOptions = dlo;

            var item = from ni in dc.NewsItems
                       where ni.NewsItemID == newsItemID
                       select ni;

            NewsItem = item.Single<NewsItem>();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            nieEditor.NewsItem = this.NewsItem;
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            if (dc != null)
            {
                dc.Dispose();
            }
        }

        #endregion

        #region ControlEvents

        protected void nieEditor_Save(object sender, EventArgs e)
        {
            //clear tags cache because the tags may have changed making the cache invalid
            Cache.Remove(Constants.Cache.Tags);
            //clear latest comments cache because the title may have changed
            Cache.Remove(Constants.Cache.LatestComments);

            dc.SubmitChanges();

            //dc.Dispose();

            Response.Redirect("~/ViewNewsItem.aspx?" + Constants.QueryString.NewsItemID + "=" + NewsItem.NewsItemID);
        }

        #endregion

    }
}