/*
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.IO;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Data.Objects;
using System.Web.Mvc;
using System.Web.Security;
using System.Linq.Dynamic;
using Everest.Library.Data;
using Everest.Library;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Data.Entity;
using Everest.Library.Json;
using Everest.Library.Extjs.Tree;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Everest.CmsServices.Services;
using Everest.CmsServices.Models;
using Everest.Forms;
using Everest.CmsServices.Providers;
namespace Everest.CmsServices.Controllers{
[PermissionFilter(Permission = FolderType.Content)]
public class BinaryContentController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
BinaryContentService binaryContentService = UnityManager.Resolve<BinaryContentService>();
#region IStandardActions Members
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
var folderUUID = new Guid(Request.Form["folderUUID"]);
string app = Request.Form["application"];
string userName = User.Identity.Name;
string title = Request.Form["Title"];
StringBuilder expression = new StringBuilder();
if (!StringExtensions.IsNullOrEmptyTrim(title))
{
expression.Append(" AND Title LIKE '%' + {Title} + '%'");
}
bool? isProcessed = null;
if (Request.Form["Processed"] == "0")
{
isProcessed = false;
}
if (Request.Form["Processed"] == "1")
{
isProcessed = true;
}
string orderStatement = GetOrderStatement();
IEnumerable<IDictionary<string, object>> queryData;
int count;
queryData = binaryContentService.QueryBinaryContents(folderUUID, userName, expression.ToString(), Request.Form, isProcessed, orderStatement, out count, start, limit);
foreach (var item in queryData)
{
item["FileUrl"] = Url.Content(item["FilePath"].ToString());
}
return Json(new ExtJsonReaderObject(queryData, count));
}
public ActionResult GetDetails(Guid uuid)
{
JsonResultData resultData = new JsonResultData();
var queriedBinaryContent = binaryContentService.QueryBinaryContent(uuid, User.Identity.Name);
resultData.data = new
{
FormTitle = queriedBinaryContent.Title,
queriedBinaryContent.Application,
queriedBinaryContent.ContentId,
queriedBinaryContent.ContentStatus,
FilePath = queriedBinaryContent.FilePath,
queriedBinaryContent.Readonly,
queriedBinaryContent.CanUpdateContentStatus,
queriedBinaryContent.Title,
queriedBinaryContent.UUID,
queriedBinaryContent.Published,
FileUrl = Url.Content(queriedBinaryContent.FilePath)
};
return Json(resultData);
}
/// <summary>
/// Submits the specified add.
/// </summary>
/// <param name="add">if set to <c>true</c> [add].</param>
/// <param name="closeForm">The close form.</param>
/// <returns></returns>
public ActionResult Submit(bool add, bool? closeForm)
{
JsonResultData resultData = new JsonResultData();
Cms_Content content = null;
try
{
if (add)
{
content = binaryContentService.AddBinaryContent(Request.Form, User.Identity.Name, Request.Files[0].FileName,
Request.Files[0].InputStream);
}
else
{
var contentUUID = new Guid(Request.Form["oldData.UUID"]);
content = binaryContentService.UpdateBinaryContent(contentUUID, Request.Form, User.Identity.Name, Request.Files[0].FileName, Request.Files[0].InputStream);
}
}
catch (RuleViolationException violationException)
{
violationException.Issues.UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(violationException);
}
catch (Exception e)
{
resultData.AddError("Title", e.Message);
}
if (closeForm.Value == false && resultData.success == true)
{
resultData.closeForm = false;
resultData.data = binaryContentService.QueryBinaryContent(content.UUID, User.Identity.Name);
}
return Json(resultData);
}
/// <summary>
/// Deletes the specified content id.
/// </summary>
/// <param name="contentId">The content id.</param>
/// <returns></returns>
public ActionResult Delete(Guid[] uuid)
{
var resultData = new JsonResultData() { success = true };
foreach (var guid in uuid)
{
binaryContentService.Delete(guid, User.Identity.Name);
}
return Json(resultData);
}
#endregion
#region Combobox
///// <summary>
///// Gets the binary by folder.
///// </summary>
///// <param name="binaryFolderId">The binary folder id.</param>
///// <returns></returns>
//public ActionResult GetBinaryByFolder(int binaryFolderId)
//{
// var binaryContents = from c in dataContext.Cms_Content
// where c.Cms_Folder.FolderId == binaryFolderId
// select new
// {
// c.Title,
// c.ContentId
// };
// var items = binaryContents.ToComboboxItems(c => c.Title, c => c.ContentId.ToString());
// return Json(new ExtJsonReaderObject(items, items.Count));
//}
#endregion
/// <summary>
/// Gets the binary schema.
/// </summary>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public ActionResult GetBinarySchema(Guid folderUUID)
{
JsonResultData resultData = new JsonResultData();
var schema = dataContext.QueryFolder(folderUUID)
.Select(f => new
{
f.Cms_Schema.SchemaId,
f.Cms_Schema.Extensions,
f.Cms_Schema.MaxSize
}).First();
resultData.data = schema;
return Json(resultData);
}
#region Localize
/// <summary>
/// Localizes the specified content id.
/// </summary>
/// <param name="contentId">The content id.</param>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public ActionResult Localize(Guid uuid, Guid folderUUID)
{
JsonResultData resultData = new JsonResultData();
var localizedContent = binaryContentService.Localize(uuid, folderUUID, User.Identity.Name);
//return the localized content to edit
resultData.data = new { localizedContent.UUID, localizedContent.Title, localizedContent.UserName };
return Json(resultData);
}
#endregion
#region UsedBy
/// <summary>
/// Useds the by.
/// </summary>
/// <param name="uuid">The UUID.</param>
/// <returns></returns>
public ActionResult UsedBy(Guid uuid, string dataUrl)
{
IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
#region Included By
var includedByNodes = new TreeNode();
includedByNodes.Text = Resources.IncludedBy;
includedByNodes.IconCls = FolderType.Content.ToString();
includedByNodes.Expanded = true;
treeNodes.Add(includedByNodes.Attributes);
var includedBy = dataContext.QueryIncludedByContents(uuid)
.Select(c => new
{
c.aspnet_Applications.ApplicationName,
c.UUID,
c.Title
});
includedByNodes.children = new List<IDictionary<string, object>>();
foreach (var content in includedBy)
{
var contentNode = new TreeNode();
includedByNodes.AddChild(contentNode);
contentNode.Text = new UniqueName(content.ApplicationName, content.Title).ToString();
contentNode.IconCls = FolderType.TextContent.ToString();
contentNode.AddAttribute("uuid", content.UUID);
contentNode.AddAttribute("dataUrl", "Kooboo_Content/usedBy");
}
#endregion
#region Localized By
var localizedByNodes = new TreeNode();
localizedByNodes.Text = Resources.LocalizedBy;
localizedByNodes.IconCls = FolderType.Content.ToString();
localizedByNodes.Expanded = true;
treeNodes.Add(localizedByNodes.Attributes);
var localizations = dataContext.QueryContentLocalizations(uuid)
.Select(c => new
{
c.aspnet_Applications.ApplicationName,
c.UUID,
c.Title
});
localizedByNodes.children = new List<IDictionary<string, object>>();
foreach (var content in localizations)
{
var contentNode = new TreeNode();
localizedByNodes.AddChild(contentNode);
contentNode.Text = new UniqueName(content.ApplicationName, content.Title).ToString();
contentNode.IconCls = FolderType.BinaryContent.ToString();
contentNode.AddAttribute("uuid", content.UUID);
contentNode.AddAttribute("dataUrl", dataUrl);
}
#endregion
#region Localize From
var localizeFromNodes = new TreeNode();
localizeFromNodes.Text = Resources.LocalizeFrom;
localizeFromNodes.IconCls = FolderType.Content.ToString();
localizeFromNodes.Expanded = true;
treeNodes.Add(localizeFromNodes.Attributes);
var localizeFrom = dataContext.QueryContent(uuid).Where(c => c.Base != null)
.Select(c => new
{
c.Base.aspnet_Applications.ApplicationName,
c.Base.UUID,
c.Base.Title
});
localizeFromNodes.children = new List<IDictionary<string, object>>();
foreach (var content in localizeFrom)
{
var contentNode = new TreeNode();
localizeFromNodes.AddChild(contentNode);
contentNode.Text = new UniqueName(content.ApplicationName, content.Title).ToString();
contentNode.IconCls = FolderType.BinaryContent.ToString();
contentNode.AddAttribute("uuid", content.UUID);
contentNode.AddAttribute("dataUrl", dataUrl);
}
#endregion
return Json(treeNodes);
}
#endregion
}
}
|