/*
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.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Library;
using Everest.Library.Data.Entity;
using Everest.Library.Data.Rule;
using Everest.Library.ExtensionMethod;
using Everest.Library.Extjs;
using Everest.Library.Json;
using Everest.Library.Mvc;
namespace Everest.CmsServices.Controllers{
[PermissionFilter(Permission = FolderType.Rss)]
[ValidateInput(false)]
public class RssSettingController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
public System.Web.Mvc.ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
string channelName = Request.Form["ChannelName"];
Expression<Func<Cms_RssChannel, bool>> condition = r => r.aspnet_Applications.ApplicationName == application;
if (!string.IsNullOrEmpty(channelName))
{
condition = it => it.ChannelName.Contains(channelName);
}
IQueryable<Cms_RssChannel> query = dataContext.Cms_RssChannel.Where(condition).OrderByDescending(p => p.ChannelName);
query = OrderByRequest(query);
var queryResult = query.Select<Cms_RssChannel, object>(s => new
{
s.UUID,
s.ChannelDescription,
s.ChannelLink,
s.ChannelName,
//s.ChannelOptionalElements,
s.ChannelTitle,
s.ItemDescriptionField,
s.ItemLink,
//s.ItemOptionalElements,
s.ItemTitleField,
s.ShowGenerator,
Application = s.aspnet_Applications.ApplicationName
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
public System.Web.Mvc.ActionResult GetDetail(Guid uuid)
{
JsonResultData resultData = new JsonResultData() { closeForm = false };
var rssChannel = dataContext.Cms_RssChannel.Where(r => r.UUID == uuid).First();
if (!rssChannel.Cms_DataRule.IsLoaded)
{
rssChannel.Cms_DataRule.Load();
}
resultData.data = new
{
rssChannel.UUID,
rssChannel.ChannelDescription,
rssChannel.ChannelLink,
rssChannel.ChannelName,
rssChannel.ChannelOptionalElements,
rssChannel.ChannelTitle,
rssChannel.ItemDescriptionField,
rssChannel.ItemLink,
rssChannel.ItemOptionalElements,
rssChannel.ItemTitleField,
rssChannel.ShowGenerator,
DataRules = rssChannel.Cms_DataRule.ToDataRuleQuery()
};
return Json(resultData);
}
public System.Web.Mvc.ActionResult Submit(string application, bool add, bool closeForm)
{
JsonResultData resultData = new JsonResultData() { closeForm = closeForm };
Cms_RssChannel cms_RssChannel = null;
try
{
if (add)
{
cms_RssChannel = new Cms_RssChannel();
cms_RssChannel.aspnet_Applications = dataContext.QueryApplication(application).First();
dataContext.AddToCms_RssChannel(cms_RssChannel);
}
else
{
var uuid = new Guid(Request["oldData.UUID"]);
cms_RssChannel = dataContext.Cms_RssChannel.Where(r => r.UUID == uuid).First();
cms_RssChannel.Cms_DataRule.Load();
if (cms_RssChannel.Cms_DataRule.Count > 0)
{
Cms_DataRule item = null;
do
{
item = cms_RssChannel.Cms_DataRule.FirstOrDefault();
if (item != null)
{
cms_RssChannel.Cms_DataRule.Remove(item);
dataContext.DeleteObject(item);
}
} while (item != null);
}
}
cms_RssChannel.ChannelDescription = Request.Form["ChannelDescription"].TryTrim();
cms_RssChannel.ChannelLink = Request.Form["ChannelLink"].TryTrim();
cms_RssChannel.ChannelName = Request.Form["ChannelName"].TryTrim();
cms_RssChannel.ChannelOptionalElements = Request.Form["ChannelOptionalElements"].TryTrim();
cms_RssChannel.ChannelTitle = Request.Form["ChannelTitle"].TryTrim();
cms_RssChannel.ItemDescriptionField = Request.Form["ItemDescriptionField"].TryTrim();
cms_RssChannel.ItemLink = Request.Form["ItemLink"].TryTrim();
cms_RssChannel.ItemOptionalElements = Request.Form["ItemOptionalElements"].TryTrim();
cms_RssChannel.ItemTitleField = Request.Form["ItemTitleField"].TryTrim();
cms_RssChannel.ShowGenerator = StringExtensions.IsTrue(Request.Form["ShowGenerator"]);
AddDataRules(dataContext, Request.Form, cms_RssChannel);
dataContext.SaveChanges();
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
if (closeForm == false && resultData.success)
{
return GetDetail(cms_RssChannel.UUID);
}
return Json(resultData);
}
private static void AddDataRules(IEverestCmsDataContext dataContext, NameValueCollection values, Cms_RssChannel rssChannel)
{
IDictionary<string, string>[] newDataRules = values["DataRules"].DeserializeJSON<Dictionary<string, string>[]>();
int itemCount = 0;
for (int i = 0; i < newDataRules.Length; i++)
{
var dataRule = Cms_DataRule.CreateCms_DataRule(dataContext, newDataRules[i]);
if (dataRule != null)
{
itemCount++;
dataRule.Order = itemCount;
rssChannel.Cms_DataRule.Add(dataRule);
}
}
}
public System.Web.Mvc.ActionResult Delete(Guid[] uuid)
{
JsonResultData resultData = new JsonResultData() { success = true };
Expression<Func<Cms_RssChannel, bool>> expression = EfUtility.BuildContainsExpression<Cms_RssChannel, Guid>(r => r.UUID, uuid);
var rssChannels = dataContext.Cms_RssChannel.Where(expression);
foreach (var rss in rssChannels)
{
dataContext.DeleteObject(rss);
}
dataContext.SaveChanges();
return Json(resultData);
}
}
}
|