2013-08-12 15:26:31 +02:00
|
|
|
using System;
|
2013-08-09 18:04:44 +10:00
|
|
|
using System.Collections.Generic;
|
2013-07-24 14:59:49 +10:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Web.Http;
|
2013-08-12 18:09:16 +10:00
|
|
|
using System.Web.Mvc;
|
2013-07-24 14:59:49 +10:00
|
|
|
using Umbraco.Core;
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
using Umbraco.Core.Models.Editors;
|
2013-08-12 18:09:16 +10:00
|
|
|
using Umbraco.Core.PropertyEditors;
|
2013-07-24 14:59:49 +10:00
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2013-08-08 16:31:55 +10:00
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2013-07-24 14:59:49 +10:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An abstract base controller used for media/content (and probably members) to try to reduce code replication.
|
|
|
|
|
/// </summary>
|
2013-08-08 16:31:55 +10:00
|
|
|
[OutgoingDateTimeFormat]
|
2013-07-24 14:59:49 +10:00
|
|
|
public abstract class ContentControllerBase : UmbracoAuthorizedJsonController
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ContentControllerBase()
|
|
|
|
|
: this(UmbracoContext.Current)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="umbracoContext"></param>
|
|
|
|
|
protected ContentControllerBase(UmbracoContext umbracoContext)
|
|
|
|
|
: base(umbracoContext)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 16:13:27 +10:00
|
|
|
protected HttpResponseMessage HandleContentNotFound(int id, bool throwException = true)
|
2013-07-24 14:59:49 +10:00
|
|
|
{
|
|
|
|
|
ModelState.AddModelError("id", string.Format("content with id: {0} was not found", id));
|
|
|
|
|
var errorResponse = Request.CreateErrorResponse(
|
|
|
|
|
HttpStatusCode.NotFound,
|
|
|
|
|
ModelState);
|
2013-08-05 16:13:27 +10:00
|
|
|
if (throwException)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(errorResponse);
|
|
|
|
|
}
|
|
|
|
|
return errorResponse;
|
2013-07-24 14:59:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void UpdateName<TPersisted>(ContentItemSave<TPersisted> contentItem)
|
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
{
|
|
|
|
|
//Don't update the name if it is empty
|
|
|
|
|
if (!contentItem.Name.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
contentItem.PersistedContent.Name = contentItem.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 19:28:32 +10:00
|
|
|
protected HttpResponseMessage PerformSort(ContentSortOrder sorted)
|
|
|
|
|
{
|
|
|
|
|
if (sorted == null)
|
|
|
|
|
{
|
|
|
|
|
return Request.CreateResponse(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if there's nothing to sort just return ok
|
|
|
|
|
if (sorted.IdSortOrder.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-24 14:59:49 +10:00
|
|
|
protected void MapPropertyValues<TPersisted>(ContentItemSave<TPersisted> contentItem)
|
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
{
|
|
|
|
|
//Map the property values
|
|
|
|
|
foreach (var p in contentItem.ContentDto.Properties)
|
|
|
|
|
{
|
|
|
|
|
//get the dbo property
|
|
|
|
|
var dboProperty = contentItem.PersistedContent.Properties[p.Alias];
|
|
|
|
|
|
|
|
|
|
//create the property data to send to the property editor
|
|
|
|
|
var d = new Dictionary<string, object>();
|
|
|
|
|
//add the files if any
|
|
|
|
|
var files = contentItem.UploadedFiles.Where(x => x.PropertyId == p.Id).ToArray();
|
|
|
|
|
if (files.Any())
|
|
|
|
|
{
|
|
|
|
|
d.Add("files", files);
|
|
|
|
|
}
|
2013-08-28 17:53:31 +10:00
|
|
|
var data = new ContentPropertyData(p.Value, p.PreValues, d);
|
2013-07-24 14:59:49 +10:00
|
|
|
|
|
|
|
|
//get the deserialized value from the property editor
|
|
|
|
|
if (p.PropertyEditor == null)
|
|
|
|
|
{
|
|
|
|
|
LogHelper.Warn<ContentController>("No property editor found for property " + p.Alias);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-08-12 18:09:16 +10:00
|
|
|
var valueEditor = p.PropertyEditor.ValueEditor;
|
|
|
|
|
//don't persist any bound value if the editor is readonly
|
|
|
|
|
if (valueEditor.IsReadOnly == false)
|
|
|
|
|
{
|
2013-08-22 18:38:57 +10:00
|
|
|
dboProperty.Value = p.PropertyEditor.ValueEditor.FormatDataForPersistence(data, dboProperty.Value);
|
2013-08-12 18:09:16 +10:00
|
|
|
}
|
|
|
|
|
|
2013-07-24 14:59:49 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void HandleInvalidModelState<T, TPersisted>(ContentItemDisplayBase<T, TPersisted> display)
|
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
where T : ContentPropertyBasic
|
|
|
|
|
{
|
|
|
|
|
//lasty, if it is not valid, add the modelstate to the outgoing object and throw a 403
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
display.Errors = ModelState.ToErrorDictionary();
|
|
|
|
|
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Forbidden, display));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-09 18:04:44 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// A helper method to attempt to get the instance from the request storage if it can be found there,
|
|
|
|
|
/// otherwise gets it from the callback specified
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TPersisted"></typeparam>
|
|
|
|
|
/// <param name="getFromService"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This is useful for when filters have alraedy looked up a persisted entity and we don't want to have
|
|
|
|
|
/// to look it up again.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
protected TPersisted GetEntityFromRequest<TPersisted>(Func<TPersisted> getFromService)
|
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
{
|
|
|
|
|
return Request.Properties.ContainsKey(typeof (TPersisted).ToString()) == false
|
|
|
|
|
? getFromService()
|
|
|
|
|
: (TPersisted) Request.Properties[typeof (TPersisted).ToString()];
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-24 14:59:49 +10:00
|
|
|
}
|
2013-08-12 15:26:31 +02:00
|
|
|
}
|