2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2013-09-02 15:40:14 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Models.Editors;
|
2018-02-01 14:14:45 +01:00
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2014-07-01 12:53:07 +10:00
|
|
|
|
using Umbraco.Core.Services;
|
2017-05-30 18:13:11 +02:00
|
|
|
|
using Umbraco.Web.Composing;
|
2013-09-02 15:40:14 +02:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2013-09-10 15:37:44 +10:00
|
|
|
|
using Umbraco.Web.WebApi;
|
2013-09-02 15:40:14 +02:00
|
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2015-07-24 11:44:09 +02:00
|
|
|
|
|
2013-09-02 15:40:14 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An abstract base controller used for media/content (and probably members) to try to reduce code replication.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[OutgoingDateTimeFormat]
|
2015-07-24 11:44:09 +02:00
|
|
|
|
public abstract class ContentControllerBase : BackOfficeNotificationsController
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2013-09-27 15:19:39 +10:00
|
|
|
|
protected HttpResponseMessage HandleContentNotFound(object id, bool throwException = true)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2018-04-03 19:25:43 +10:00
|
|
|
|
ModelState.AddModelError("id", $"content with id: {id} was not found");
|
2013-09-02 15:40:14 +02:00
|
|
|
|
var errorResponse = Request.CreateErrorResponse(
|
|
|
|
|
|
HttpStatusCode.NotFound,
|
|
|
|
|
|
ModelState);
|
|
|
|
|
|
if (throwException)
|
|
|
|
|
|
{
|
2016-09-01 19:06:08 +02:00
|
|
|
|
throw new HttpResponseException(errorResponse);
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
return errorResponse;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
protected void UpdateName<TPersisted>(ContentBaseItemSave<TPersisted> contentItem)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
|
{
|
|
|
|
|
|
//Don't update the name if it is empty
|
2017-05-30 10:50:09 +02:00
|
|
|
|
if (contentItem.Name.IsNullOrWhiteSpace() == false)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
|
|
|
|
|
contentItem.PersistedContent.Name = contentItem.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-10-10 17:44:56 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps the dto property values to the persisted model
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TPersisted"></typeparam>
|
2018-04-04 01:59:51 +10:00
|
|
|
|
/// <typeparam name="TSaved"></typeparam>
|
2013-10-10 17:44:56 +11:00
|
|
|
|
/// <param name="contentItem"></param>
|
2018-04-04 01:59:51 +10:00
|
|
|
|
/// <param name="getPropertyValue"></param>
|
|
|
|
|
|
/// <param name="savePropertyValue"></param>
|
|
|
|
|
|
protected void MapPropertyValues<TPersisted, TSaved>(
|
|
|
|
|
|
TSaved contentItem,
|
|
|
|
|
|
Func<TSaved, Property, object> getPropertyValue,
|
|
|
|
|
|
Action<TSaved, Property, object> savePropertyValue)
|
|
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
|
where TSaved : ContentBaseItemSave<TPersisted>
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2018-02-01 14:14:45 +01:00
|
|
|
|
// map the property values
|
|
|
|
|
|
foreach (var propertyDto in contentItem.ContentDto.Properties)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2018-02-01 14:14:45 +01:00
|
|
|
|
// get the property editor
|
|
|
|
|
|
if (propertyDto.PropertyEditor == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Warn<ContentController>("No property editor found for property " + propertyDto.Alias);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2013-09-02 15:40:14 +02:00
|
|
|
|
|
2018-02-01 14:14:45 +01:00
|
|
|
|
// get the value editor
|
|
|
|
|
|
// nothing to save/map if it is readonly
|
2018-03-16 09:06:44 +01:00
|
|
|
|
var valueEditor = propertyDto.PropertyEditor.GetValueEditor();
|
2018-02-01 14:14:45 +01:00
|
|
|
|
if (valueEditor.IsReadOnly) continue;
|
2018-01-15 15:54:06 +01:00
|
|
|
|
|
2018-02-01 14:14:45 +01:00
|
|
|
|
// get the property
|
|
|
|
|
|
var property = contentItem.PersistedContent.Properties[propertyDto.Alias];
|
|
|
|
|
|
|
|
|
|
|
|
// prepare files, if any
|
|
|
|
|
|
var files = contentItem.UploadedFiles.Where(x => x.PropertyAlias == propertyDto.Alias).ToArray();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
foreach (var file in files)
|
|
|
|
|
|
file.FileName = file.FileName.ToSafeFileName();
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
2018-02-01 14:14:45 +01:00
|
|
|
|
// create the property data for the property editor
|
|
|
|
|
|
var data = new ContentPropertyData(propertyDto.Value, propertyDto.DataType.Configuration)
|
2018-01-15 15:54:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
ContentKey = contentItem.PersistedContent.Key,
|
2018-02-01 14:14:45 +01:00
|
|
|
|
PropertyTypeKey = property.PropertyType.Key,
|
2018-01-15 15:54:06 +01:00
|
|
|
|
Files = files
|
|
|
|
|
|
};
|
2013-09-02 15:40:14 +02:00
|
|
|
|
|
2018-02-01 14:14:45 +01:00
|
|
|
|
// let the editor convert the value that was received, deal with files, etc
|
2018-04-04 01:59:51 +10:00
|
|
|
|
var value = valueEditor.FromEditor(data, getPropertyValue(contentItem, property));
|
2018-02-01 14:14:45 +01:00
|
|
|
|
|
|
|
|
|
|
// set the value - tags are special
|
|
|
|
|
|
var tagAttribute = propertyDto.PropertyEditor.GetTagAttribute();
|
|
|
|
|
|
if (tagAttribute != null)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2018-02-01 14:14:45 +01:00
|
|
|
|
var tagConfiguration = ConfigurationEditor.ConfigurationAs<TagConfiguration>(propertyDto.DataType.Configuration);
|
|
|
|
|
|
if (tagConfiguration.Delimiter == default) tagConfiguration.Delimiter = tagAttribute.Delimiter;
|
2018-04-04 01:59:51 +10:00
|
|
|
|
//fixme how is this supposed to work with variants?
|
2018-02-01 14:14:45 +01:00
|
|
|
|
property.SetTagsValue(value, tagConfiguration);
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2018-04-04 01:59:51 +10:00
|
|
|
|
savePropertyValue(contentItem, property, value);
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
protected void HandleInvalidModelState<T, TPersisted>(ContentItemDisplayBase<T, TPersisted> display)
|
|
|
|
|
|
where TPersisted : IContentBase
|
2013-09-02 15:40:14 +02:00
|
|
|
|
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();
|
2013-09-10 15:37:44 +10:00
|
|
|
|
throw new HttpResponseException(Request.CreateValidationErrorResponse(display));
|
2013-09-02 15:40:14 +02: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>
|
2013-10-22 11:24:56 +11:00
|
|
|
|
protected TPersisted GetObjectFromRequest<TPersisted>(Func<TPersisted> getFromService)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2014-04-24 11:58:18 +10:00
|
|
|
|
//checks if the request contains the key and the item is not null, if that is the case, return it from the request, otherwise return
|
|
|
|
|
|
// it from the callback
|
|
|
|
|
|
return Request.Properties.ContainsKey(typeof(TPersisted).ToString()) && Request.Properties[typeof(TPersisted).ToString()] != null
|
|
|
|
|
|
? (TPersisted) Request.Properties[typeof (TPersisted).ToString()]
|
|
|
|
|
|
: getFromService();
|
2016-09-01 19:06:08 +02:00
|
|
|
|
}
|
2013-09-02 15:40:14 +02:00
|
|
|
|
|
2013-10-31 18:17:30 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the action passed in means we need to create something new
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
internal static bool IsCreatingAction(ContentSaveAction action)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (action.ToString().EndsWith("New"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
protected void AddCancelMessage(INotificationModel display,
|
|
|
|
|
|
string header = "speechBubbles/operationCancelledHeader",
|
2015-07-30 11:35:00 +02:00
|
|
|
|
string message = "speechBubbles/operationCancelledText",
|
|
|
|
|
|
bool localizeHeader = true,
|
|
|
|
|
|
bool localizeMessage = true)
|
2015-07-27 18:04:20 +02:00
|
|
|
|
{
|
|
|
|
|
|
//if there's already a default event message, don't add our default one
|
2016-06-30 11:30:52 +02:00
|
|
|
|
var msgs = Current.EventMessages;
|
2015-07-27 18:04:20 +02:00
|
|
|
|
if (msgs != null && msgs.GetAll().Any(x => x.IsDefaultEventMessage)) return;
|
|
|
|
|
|
|
|
|
|
|
|
display.AddWarningNotification(
|
2015-07-30 11:35:00 +02:00
|
|
|
|
localizeHeader ? Services.TextService.Localize(header) : header,
|
|
|
|
|
|
localizeMessage ? Services.TextService.Localize(message): message);
|
2015-07-27 18:04:20 +02:00
|
|
|
|
}
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|