2012-10-05 11:03:08 -02:00
using System ;
2013-02-09 10:58:21 -01:00
using System.Collections.Generic ;
2012-12-20 08:53:28 -01:00
using System.Drawing ;
using System.Drawing.Drawing2D ;
using System.Drawing.Imaging ;
2013-01-14 11:02:12 -01:00
using System.Globalization ;
2012-12-20 08:53:28 -01:00
using System.IO ;
2012-10-05 11:03:08 -02:00
using System.Linq ;
2012-12-20 08:53:28 -01:00
using System.Web ;
using System.Xml ;
2012-12-13 13:05:23 -01:00
using System.Xml.Linq ;
2014-05-15 12:49:03 +10:00
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
2012-12-14 11:43:16 -01:00
using Umbraco.Core.Configuration ;
2013-09-13 18:11:20 +10:00
using Umbraco.Core.Configuration.UmbracoSettings ;
2012-12-20 08:53:28 -01:00
using Umbraco.Core.IO ;
2013-02-18 15:46:18 -01:00
using Umbraco.Core.Media ;
2013-08-07 11:39:25 +10:00
using Umbraco.Core.Models.EntityBase ;
2012-11-11 06:53:02 -01:00
using Umbraco.Core.Models.Membership ;
2013-02-18 08:31:00 -01:00
using Umbraco.Core.Strings ;
2012-11-11 06:53:02 -01:00
using Umbraco.Core.Persistence ;
2012-12-10 02:58:23 +05:00
using Umbraco.Core.Persistence.UnitOfWork ;
2013-06-20 15:34:57 +10:00
using Umbraco.Core.Services ;
2012-10-05 11:03:08 -02:00
namespace Umbraco.Core.Models
{
public static class ContentExtensions
{
2013-02-09 11:12:51 -01:00
#region IContent
2013-08-07 11:39:25 +10:00
2014-03-06 17:50:08 +11:00
/// <summary>
/// Returns true if this entity was just published as part of a recent save operation (i.e. it wasn't previously published)
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
/// <remarks>
/// This is helpful for determining if the published event will execute during the saved event for a content item.
/// </remarks>
internal static bool JustPublished ( this IContent entity )
{
var dirty = ( IRememberBeingDirty ) entity ;
return dirty . WasPropertyDirty ( "Published" ) & & entity . Published ;
}
2014-10-21 13:12:31 +10:00
/// <summary>
/// Determines if the item should be persisted at all
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
/// <remarks>
/// In one particular case, a content item shouldn't be persisted:
/// * The item exists and is published
/// * A call to ContentService.Save is made
/// * The item has not been modified whatsoever apart from changing it's published status from published to saved
///
/// In this case, there is no reason to make any database changes at all
/// </remarks>
internal static bool RequiresSaving ( this IContent entity )
{
var publishedState = ( ( Content ) entity ) . PublishedState ;
return RequiresSaving ( entity , publishedState ) ;
}
/// <summary>
/// Determines if the item should be persisted at all
/// </summary>
/// <param name="entity"></param>
/// <param name="publishedState"></param>
/// <returns></returns>
/// <remarks>
/// In one particular case, a content item shouldn't be persisted:
/// * The item exists and is published
/// * A call to ContentService.Save is made
/// * The item has not been modified whatsoever apart from changing it's published status from published to saved
///
/// In this case, there is no reason to make any database changes at all
/// </remarks>
internal static bool RequiresSaving ( this IContent entity , PublishedState publishedState )
{
var dirtyEntity = ( ICanBeDirty ) entity ;
var publishedChanged = dirtyEntity . IsPropertyDirty ( "Published" ) & & publishedState ! = PublishedState . Unpublished ;
//check if any user prop has changed
var propertyValueChanged = ( ( Content ) entity ) . IsAnyUserPropertyDirty ( ) ;
//We need to know if any other property apart from Published was changed here
//don't create a new version if the published state has changed to 'Save' but no data has actually been changed
if ( publishedChanged & & entity . Published = = false & & propertyValueChanged = = false )
{
//at this point we need to check if any non property value has changed that wasn't the published state
var changedProps = ( ( TracksChangesEntityBase ) entity ) . GetDirtyProperties ( ) ;
if ( changedProps . Any ( x = > x ! = "Published" ) = = false )
{
return false ;
}
}
return true ;
}
2013-08-07 11:39:25 +10:00
/// <summary>
/// Determines if a new version should be created
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
/// <remarks>
/// A new version needs to be created when:
/// * The publish status is changed
/// * The language is changed
2013-08-08 10:42:06 +10:00
/// * The item is already published and is being published again and any property value is changed (to enable a rollback)
2013-08-07 11:39:25 +10:00
/// </remarks>
internal static bool ShouldCreateNewVersion ( this IContent entity )
{
var publishedState = ( ( Content ) entity ) . PublishedState ;
return ShouldCreateNewVersion ( entity , publishedState ) ;
}
/// <summary>
/// Determines if a new version should be created
/// </summary>
/// <param name="entity"></param>
/// <param name="publishedState"></param>
/// <returns></returns>
/// <remarks>
/// A new version needs to be created when:
/// * The publish status is changed
/// * The language is changed
2013-08-08 10:42:06 +10:00
/// * The item is already published and is being published again and any property value is changed (to enable a rollback)
2013-08-07 11:39:25 +10:00
/// </remarks>
internal static bool ShouldCreateNewVersion ( this IContent entity , PublishedState publishedState )
{
var dirtyEntity = ( ICanBeDirty ) entity ;
2013-10-04 17:13:57 +10:00
2013-08-08 10:42:06 +10:00
//check if the published state has changed or the language
2014-10-21 13:12:31 +10:00
var publishedChanged = dirtyEntity . IsPropertyDirty ( "Published" ) & & publishedState ! = PublishedState . Unpublished ;
var langChanged = dirtyEntity . IsPropertyDirty ( "Language" ) ;
var contentChanged = publishedChanged | | langChanged ;
//check if any user prop has changed
var propertyValueChanged = ( ( Content ) entity ) . IsAnyUserPropertyDirty ( ) ;
2013-08-07 11:39:25 +10:00
2013-08-08 10:42:06 +10:00
//return true if published or language has changed
if ( contentChanged )
{
return true ;
}
2013-08-07 11:39:25 +10:00
2013-08-08 10:42:06 +10:00
//check if any content prop has changed
2013-10-04 17:13:57 +10:00
var contentDataChanged = ( ( Content ) entity ) . IsEntityDirty ( ) ;
2013-08-08 10:42:06 +10:00
//return true if the item is published and a property has changed or if any content property has changed
return ( propertyValueChanged & & publishedState = = PublishedState . Published ) | | contentDataChanged ;
2013-08-07 11:39:25 +10:00
}
2013-08-13 11:01:49 +10:00
/// <summary>
/// Determines if the published db flag should be set to true for the current entity version and all other db
/// versions should have their flag set to false.
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
/// <remarks>
/// This is determined by:
/// * If a new version is being created and the entity is published
/// * If the published state has changed and the entity is published OR the entity has been un-published.
/// </remarks>
internal static bool ShouldClearPublishedFlagForPreviousVersions ( this IContent entity )
{
var publishedState = ( ( Content ) entity ) . PublishedState ;
return entity . ShouldClearPublishedFlagForPreviousVersions ( publishedState , entity . ShouldCreateNewVersion ( publishedState ) ) ;
}
/// <summary>
/// Determines if the published db flag should be set to true for the current entity version and all other db
/// versions should have their flag set to false.
/// </summary>
/// <param name="entity"></param>
/// <param name="publishedState"></param>
/// <param name="isCreatingNewVersion"></param>
/// <returns></returns>
/// <remarks>
/// This is determined by:
/// * If a new version is being created and the entity is published
/// * If the published state has changed and the entity is published OR the entity has been un-published.
/// </remarks>
internal static bool ShouldClearPublishedFlagForPreviousVersions ( this IContent entity , PublishedState publishedState , bool isCreatingNewVersion )
{
if ( isCreatingNewVersion & & entity . Published )
{
return true ;
}
//If Published state has changed then previous versions should have their publish state reset.
//If state has been changed to unpublished the previous versions publish state should also be reset.
2014-10-21 13:12:31 +10:00
if ( entity . IsPropertyDirty ( "Published" ) & & ( entity . Published | | publishedState = = PublishedState . Unpublished ) )
2013-08-13 11:01:49 +10:00
{
return true ;
}
return false ;
}
2013-02-09 10:58:21 -01:00
/// <summary>
/// Returns a list of the current contents ancestors, not including the content itself.
/// </summary>
/// <param name="content">Current content</param>
/// <returns>An enumerable list of <see cref="IContent"/> objects</returns>
public static IEnumerable < IContent > Ancestors ( this IContent content )
{
return ApplicationContext . Current . Services . ContentService . GetAncestors ( content ) ;
}
/// <summary>
/// Returns a list of the current contents children.
/// </summary>
/// <param name="content">Current content</param>
/// <returns>An enumerable list of <see cref="IContent"/> objects</returns>
public static IEnumerable < IContent > Children ( this IContent content )
{
return ApplicationContext . Current . Services . ContentService . GetChildren ( content . Id ) ;
}
/// <summary>
/// Returns a list of the current contents descendants, not including the content itself.
/// </summary>
/// <param name="content">Current content</param>
/// <returns>An enumerable list of <see cref="IContent"/> objects</returns>
public static IEnumerable < IContent > Descendants ( this IContent content )
{
return ApplicationContext . Current . Services . ContentService . GetDescendants ( content ) ;
}
/// <summary>
/// Returns the parent of the current content.
/// </summary>
/// <param name="content">Current content</param>
/// <returns>An <see cref="IContent"/> object</returns>
public static IContent Parent ( this IContent content )
{
return ApplicationContext . Current . Services . ContentService . GetById ( content . ParentId ) ;
}
2013-02-09 11:12:51 -01:00
#endregion
#region IMedia
/// <summary>
/// Returns a list of the current medias ancestors, not including the media itself.
/// </summary>
/// <param name="media">Current media</param>
/// <returns>An enumerable list of <see cref="IMedia"/> objects</returns>
public static IEnumerable < IMedia > Ancestors ( this IMedia media )
{
return ApplicationContext . Current . Services . MediaService . GetAncestors ( media ) ;
}
/// <summary>
/// Returns a list of the current medias children.
/// </summary>
/// <param name="media">Current media</param>
/// <returns>An enumerable list of <see cref="IMedia"/> objects</returns>
public static IEnumerable < IMedia > Children ( this IMedia media )
{
return ApplicationContext . Current . Services . MediaService . GetChildren ( media . Id ) ;
}
/// <summary>
/// Returns a list of the current medias descendants, not including the media itself.
/// </summary>
/// <param name="media">Current media</param>
/// <returns>An enumerable list of <see cref="IMedia"/> objects</returns>
public static IEnumerable < IMedia > Descendants ( this IMedia media )
{
return ApplicationContext . Current . Services . MediaService . GetDescendants ( media ) ;
}
/// <summary>
/// Returns the parent of the current media.
/// </summary>
/// <param name="media">Current media</param>
/// <returns>An <see cref="IMedia"/> object</returns>
public static IMedia Parent ( this IMedia media )
{
return ApplicationContext . Current . Services . MediaService . GetById ( media . ParentId ) ;
}
#endregion
2013-02-09 10:58:21 -01:00
2013-08-05 16:13:27 +10:00
internal static bool IsInRecycleBin ( this IContent content )
{
return IsInRecycleBin ( content , Constants . System . RecycleBinContent ) ;
}
internal static bool IsInRecycleBin ( this IMedia media )
{
return IsInRecycleBin ( media , Constants . System . RecycleBinMedia ) ;
}
internal static bool IsInRecycleBin ( this IContentBase content , int recycleBinId )
{
return content . Path . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries )
. Contains ( recycleBinId . ToInvariantString ( ) ) ;
}
2014-08-13 09:38:40 +02:00
/// <summary>
/// Removes characters that are not valide XML characters from all entity properties
/// of type string. See: http://stackoverflow.com/a/961504/5018
/// </summary>
/// <returns></returns>
/// <remarks>
/// If this is not done then the xml cache can get corrupt and it will throw YSODs upon reading it.
/// </remarks>
/// <param name="entity"></param>
public static void SanitizeEntityPropertiesForXmlStorage ( this IContentBase entity )
{
entity . Name = entity . Name . ToValidXmlString ( ) ;
foreach ( var property in entity . Properties )
{
if ( property . Value is string )
{
var value = ( string ) property . Value ;
property . Value = value . ToValidXmlString ( ) ;
}
}
}
2013-08-05 16:13:27 +10:00
2013-06-20 15:34:57 +10:00
/// <summary>
/// Checks if the IContentBase has children
/// </summary>
/// <param name="content"></param>
/// <param name="services"></param>
/// <returns></returns>
/// <remarks>
/// This is a bit of a hack because we need to type check!
/// </remarks>
internal static bool HasChildren ( IContentBase content , ServiceContext services )
{
if ( content is IContent )
{
return services . ContentService . HasChildren ( content . Id ) ;
}
if ( content is IMedia )
{
return services . MediaService . HasChildren ( content . Id ) ;
}
return false ;
}
/// <summary>
/// Returns the children for the content base item
/// </summary>
/// <param name="content"></param>
/// <param name="services"></param>
/// <returns></returns>
/// <remarks>
/// This is a bit of a hack because we need to type check!
/// </remarks>
internal static IEnumerable < IContentBase > Children ( IContentBase content , ServiceContext services )
{
if ( content is IContent )
{
return services . ContentService . GetChildren ( content . Id ) ;
}
if ( content is IMedia )
{
return services . MediaService . GetChildren ( content . Id ) ;
}
return null ;
}
2013-05-30 21:21:52 -10:00
/// <summary>
/// Returns properties that do not belong to a group
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static IEnumerable < Property > GetNonGroupedProperties ( this IContentBase content )
{
2013-08-09 12:10:42 +10:00
var propertyIdsInTabs = content . PropertyGroups . SelectMany ( pg = > pg . PropertyTypes ) ;
2013-07-25 15:31:26 +10:00
return content . Properties
2013-08-09 12:10:42 +10:00
. Where ( property = > propertyIdsInTabs . Contains ( property . PropertyType ) = = false )
2013-07-25 15:31:26 +10:00
. OrderBy ( x = > x . PropertyType . SortOrder ) ;
2013-05-30 21:21:52 -10:00
}
/// <summary>
/// Returns the Property object for the given property group
/// </summary>
/// <param name="content"></param>
/// <param name="propertyGroup"></param>
/// <returns></returns>
public static IEnumerable < Property > GetPropertiesForGroup ( this IContentBase content , PropertyGroup propertyGroup )
{
//get the properties for the current tab
return content . Properties
. Where ( property = > propertyGroup . PropertyTypes
. Select ( propertyType = > propertyType . Id )
2013-07-25 15:31:26 +10:00
. Contains ( property . PropertyTypeId ) )
. OrderBy ( x = > x . PropertyType . SortOrder ) ;
2013-10-04 17:13:57 +10:00
}
2013-05-30 21:21:52 -10:00
2012-10-05 11:03:08 -02:00
/// <summary>
/// Set property values by alias with an annonymous object
/// </summary>
2012-11-02 09:11:23 -01:00
public static void PropertyValues ( this IContent content , object value )
{
if ( value = = null )
throw new Exception ( "No properties has been passed in" ) ;
2012-10-05 11:03:08 -02:00
2012-11-02 09:11:23 -01:00
var propertyInfos = value . GetType ( ) . GetProperties ( ) ;
foreach ( var propertyInfo in propertyInfos )
{
//Check if a PropertyType with alias exists thus being a valid property
var propertyType = content . PropertyTypes . FirstOrDefault ( x = > x . Alias = = propertyInfo . Name ) ;
if ( propertyType = = null )
throw new Exception (
string . Format (
"The property alias {0} is not valid, because no PropertyType with this alias exists" ,
propertyInfo . Name ) ) ;
2012-10-05 11:03:08 -02:00
2012-11-02 09:11:23 -01:00
//Check if a Property with the alias already exists in the collection thus being updated or inserted
var item = content . Properties . FirstOrDefault ( x = > x . Alias = = propertyInfo . Name ) ;
if ( item ! = null )
{
item . Value = propertyInfo . GetValue ( value , null ) ;
//Update item with newly added value
content . Properties . Add ( item ) ;
}
else
{
//Create new Property to add to collection
var property = propertyType . CreatePropertyFromValue ( propertyInfo . GetValue ( value , null ) ) ;
content . Properties . Add ( property ) ;
}
}
}
2013-10-04 17:13:57 +10:00
#region SetValue for setting file contents
2012-12-20 08:53:28 -01:00
/// <summary>
/// Sets and uploads the file from a HttpPostedFileBase object as the property value
/// </summary>
2013-02-19 11:06:00 -01:00
/// <param name="content"><see cref="IContentBase"/> to add property value to</param>
2012-12-20 08:53:28 -01:00
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFileBase"/> containing the file that will be uploaded</param>
2013-01-14 11:02:12 -01:00
public static void SetValue ( this IContentBase content , string propertyTypeAlias , HttpPostedFileBase value )
2012-12-20 08:53:28 -01:00
{
2013-02-19 11:06:00 -01:00
// Ensure we get the filename without the path in IE in intranet mode
// http://stackoverflow.com/questions/382464/httppostedfile-filename-different-from-ie
var fileName = value . FileName ;
if ( fileName . LastIndexOf ( @"\" ) > 0 )
fileName = fileName . Substring ( fileName . LastIndexOf ( @"\" ) + 1 ) ;
2012-12-20 08:53:28 -01:00
var name =
IOHelper . SafeFileName (
2013-02-19 11:06:00 -01:00
fileName . Substring ( fileName . LastIndexOf ( IOHelper . DirSepChar ) + 1 ,
fileName . Length - fileName . LastIndexOf ( IOHelper . DirSepChar ) - 1 )
. ToLower ( ) ) ;
2012-12-20 08:53:28 -01:00
if ( string . IsNullOrEmpty ( name ) = = false )
SetFileOnContent ( content , propertyTypeAlias , name , value . InputStream ) ;
}
/// <summary>
/// Sets and uploads the file from a HttpPostedFile object as the property value
/// </summary>
2013-02-19 11:06:00 -01:00
/// <param name="content"><see cref="IContentBase"/> to add property value to</param>
2012-12-20 08:53:28 -01:00
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFile"/> containing the file that will be uploaded</param>
2013-01-14 11:02:12 -01:00
public static void SetValue ( this IContentBase content , string propertyTypeAlias , HttpPostedFile value )
2012-12-20 08:53:28 -01:00
{
2013-09-03 16:35:36 +10:00
SetValue ( content , propertyTypeAlias , ( HttpPostedFileBase ) new HttpPostedFileWrapper ( value ) ) ;
2012-12-20 08:53:28 -01:00
}
/// <summary>
/// Sets and uploads the file from a HttpPostedFileWrapper object as the property value
/// </summary>
2013-02-19 11:06:00 -01:00
/// <param name="content"><see cref="IContentBase"/> to add property value to</param>
2012-12-20 08:53:28 -01:00
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFileWrapper"/> containing the file that will be uploaded</param>
2013-09-03 16:35:36 +10:00
[Obsolete("There is no reason for this overload since HttpPostedFileWrapper inherits from HttpPostedFileBase")]
2013-01-14 11:02:12 -01:00
public static void SetValue ( this IContentBase content , string propertyTypeAlias , HttpPostedFileWrapper value )
2012-12-20 08:53:28 -01:00
{
2013-09-03 16:35:36 +10:00
SetValue ( content , propertyTypeAlias , ( HttpPostedFileBase ) value ) ;
2013-02-19 11:06:00 -01:00
}
2013-10-04 17:13:57 +10:00
2013-02-19 11:06:00 -01:00
/// <summary>
/// Sets and uploads the file from a <see cref="Stream"/> as the property value
/// </summary>
/// <param name="content"><see cref="IContentBase"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="fileName">Name of the file</param>
/// <param name="fileStream"><see cref="Stream"/> to save to disk</param>
2013-08-23 13:41:16 +10:00
public static void SetValue ( this IContentBase content , string propertyTypeAlias , string fileName , Stream fileStream )
2013-02-19 11:06:00 -01:00
{
2013-03-22 14:18:45 -01:00
var name = IOHelper . SafeFileName ( fileName ) ;
if ( string . IsNullOrEmpty ( name ) = = false & & fileStream ! = null )
SetFileOnContent ( content , propertyTypeAlias , name , fileStream ) ;
2012-12-20 08:53:28 -01:00
}
2013-09-25 21:09:59 +10:00
private static void SetFileOnContent ( IContentBase content , string propertyTypeAlias , string filename , Stream fileStream )
2012-12-20 08:53:28 -01:00
{
var property = content . Properties . FirstOrDefault ( x = > x . Alias = = propertyTypeAlias ) ;
2013-01-24 06:16:44 +03:00
if ( property = = null )
2012-12-20 08:53:28 -01:00
return ;
2013-09-25 21:09:59 +10:00
//TODO: ALl of this naming logic needs to be put into the ImageHelper and then we need to change FileUploadPropertyValueEditor to do the same!
2013-02-19 11:06:00 -01:00
var numberedFolder = MediaSubfolderCounter . Current . Increment ( ) ;
2013-09-25 19:23:41 +10:00
var fileName = UmbracoConfig . For . UmbracoSettings ( ) . Content . UploadAllowDirectories
2013-09-25 21:09:59 +10:00
? Path . Combine ( numberedFolder . ToString ( CultureInfo . InvariantCulture ) , filename )
: numberedFolder + "-" + filename ;
2013-02-19 11:06:00 -01:00
2013-09-25 21:09:59 +10:00
var extension = Path . GetExtension ( filename ) . Substring ( 1 ) . ToLowerInvariant ( ) ;
2013-08-23 13:41:16 +10:00
//the file size is the length of the stream in bytes
var fileSize = fileStream . Length ;
2012-12-20 08:53:28 -01:00
var fs = FileSystemProviderManager . Current . GetFileSystemProvider < MediaFileSystem > ( ) ;
fs . AddFile ( fileName , fileStream ) ;
2013-10-04 17:13:57 +10:00
2012-12-20 08:53:28 -01:00
//Check if file supports resizing and create thumbnails
2013-09-25 19:23:41 +10:00
var supportsResizing = UmbracoConfig . For . UmbracoSettings ( ) . Content . ImageFileTypes . InvariantContains ( extension ) ;
2012-12-20 09:06:08 -01:00
2013-08-23 13:41:16 +10:00
//the config section used to auto-fill properties
2013-09-16 17:39:45 +10:00
IImagingAutoFillUploadField uploadFieldConfigNode = null ;
2012-12-20 08:53:28 -01:00
//Check for auto fill of additional properties
2013-09-25 19:23:41 +10:00
if ( UmbracoConfig . For . UmbracoSettings ( ) . Content . ImageAutoFillProperties ! = null )
2012-12-20 08:53:28 -01:00
{
2013-09-25 19:23:41 +10:00
uploadFieldConfigNode = UmbracoConfig . For . UmbracoSettings ( ) . Content . ImageAutoFillProperties
2013-09-16 19:33:21 +10:00
. FirstOrDefault ( x = > x . Alias = = propertyTypeAlias ) ;
2013-09-13 18:11:20 +10:00
2013-08-23 13:41:16 +10:00
}
2013-01-24 06:16:44 +03:00
2013-08-23 13:41:16 +10:00
if ( supportsResizing )
{
//get the original image from the original stream
if ( fileStream . CanSeek ) fileStream . Seek ( 0 , 0 ) ;
using ( var originalImage = Image . FromStream ( fileStream ) )
2013-09-25 21:09:59 +10:00
{
var additionalSizes = new List < int > ( ) ;
2013-08-23 13:41:16 +10:00
2013-09-25 21:09:59 +10:00
//Look up Prevalues for this upload datatype - if it is an upload datatype - get additional configured sizes
2013-09-05 18:38:54 +10:00
if ( property . PropertyType . PropertyEditorAlias = = Constants . PropertyEditors . UploadFieldAlias )
2012-12-20 08:53:28 -01:00
{
2013-08-23 13:41:16 +10:00
//Get Prevalues by the DataType's Id: property.PropertyType.DataTypeId
var values = ApplicationContext . Current . Services . DataTypeService . GetPreValuesByDataTypeId ( property . PropertyType . DataTypeDefinitionId ) ;
var thumbnailSizes = values . FirstOrDefault ( ) ;
//Additional thumbnails configured as prevalues on the DataType
if ( thumbnailSizes ! = null )
{
2013-09-25 21:09:59 +10:00
var sep = ( thumbnailSizes . Contains ( "" ) = = false & & thumbnailSizes . Contains ( "," ) ) ? ',' : ';' ;
2013-08-23 13:41:16 +10:00
foreach ( var thumb in thumbnailSizes . Split ( sep ) )
{
int thumbSize ;
if ( thumb ! = "" & & int . TryParse ( thumb , out thumbSize ) )
{
2013-09-25 21:09:59 +10:00
additionalSizes . Add ( thumbSize ) ;
2013-08-23 13:41:16 +10:00
}
}
}
2012-12-20 08:53:28 -01:00
}
2013-08-23 13:41:16 +10:00
2013-09-25 21:09:59 +10:00
ImageHelper . GenerateMediaThumbnails ( fs , fileName , extension , originalImage , additionalSizes ) ;
2013-10-04 17:13:57 +10:00
2013-08-23 13:41:16 +10:00
//while the image is still open, we'll check if we need to auto-populate the image properties
if ( uploadFieldConfigNode ! = null )
2012-12-20 08:53:28 -01:00
{
2013-09-13 18:11:20 +10:00
content . SetValue ( uploadFieldConfigNode . WidthFieldAlias , originalImage . Width . ToString ( CultureInfo . InvariantCulture ) ) ;
content . SetValue ( uploadFieldConfigNode . HeightFieldAlias , originalImage . Height . ToString ( CultureInfo . InvariantCulture ) ) ;
2012-12-20 08:53:28 -01:00
}
2013-10-04 17:13:57 +10:00
2012-12-20 08:53:28 -01:00
}
}
2013-08-23 13:41:16 +10:00
//if auto-fill is true, then fill the remaining, non-image properties
if ( uploadFieldConfigNode ! = null )
{
2013-09-13 18:11:20 +10:00
content . SetValue ( uploadFieldConfigNode . LengthFieldAlias , fileSize . ToString ( CultureInfo . InvariantCulture ) ) ;
content . SetValue ( uploadFieldConfigNode . ExtensionFieldAlias , extension ) ;
2013-08-23 13:41:16 +10:00
}
2012-12-20 08:53:28 -01:00
//Set the value of the property to that of the uploaded file's url
property . Value = fs . GetUrl ( fileName ) ;
}
2013-10-04 17:13:57 +10:00
#endregion
#region User / Profile methods
2014-09-30 18:46:02 +10:00
2013-10-04 17:13:57 +10:00
/// <summary>
/// Gets the <see cref="IProfile"/> for the Creator of this media item.
/// </summary>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IUserService to use")]
2013-10-04 17:13:57 +10:00
public static IProfile GetCreatorProfile ( this IMedia media )
{
2013-01-28 09:02:28 -01:00
return ApplicationContext . Current . Services . UserService . GetProfileById ( media . CreatorId ) ;
}
2013-01-05 22:46:50 +03:00
2014-09-30 18:46:02 +10:00
/// <summary>
/// Gets the <see cref="IProfile"/> for the Creator of this media item.
/// </summary>
public static IProfile GetCreatorProfile ( this IMedia media , IUserService userService )
{
return userService . GetProfileById ( media . CreatorId ) ;
}
2012-11-06 10:47:14 -01:00
/// <summary>
2013-01-27 16:02:59 -01:00
/// Gets the <see cref="IProfile"/> for the Creator of this content item.
2012-11-06 10:47:14 -01:00
/// </summary>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IUserService to use")]
2013-01-24 12:25:48 -01:00
public static IProfile GetCreatorProfile ( this IContentBase content )
2012-11-06 10:47:14 -01:00
{
2013-01-28 09:02:28 -01:00
return ApplicationContext . Current . Services . UserService . GetProfileById ( content . CreatorId ) ;
2012-11-06 10:47:14 -01:00
}
2012-11-06 15:17:58 -01:00
2014-09-30 18:46:02 +10:00
/// <summary>
/// Gets the <see cref="IProfile"/> for the Creator of this content item.
/// </summary>
public static IProfile GetCreatorProfile ( this IContentBase content , IUserService userService )
{
return userService . GetProfileById ( content . CreatorId ) ;
}
2012-11-06 15:17:58 -01:00
/// <summary>
2012-11-11 06:53:02 -01:00
/// Gets the <see cref="IProfile"/> for the Writer of this content.
2012-11-06 15:17:58 -01:00
/// </summary>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IUserService to use")]
2012-11-11 06:53:02 -01:00
public static IProfile GetWriterProfile ( this IContent content )
2012-11-06 15:17:58 -01:00
{
2013-01-28 09:02:28 -01:00
return ApplicationContext . Current . Services . UserService . GetProfileById ( content . WriterId ) ;
2012-11-06 15:17:58 -01:00
}
2014-09-30 18:46:02 +10:00
/// <summary>
/// Gets the <see cref="IProfile"/> for the Writer of this content.
/// </summary>
public static IProfile GetWriterProfile ( this IContent content , IUserService userService )
{
return userService . GetProfileById ( content . WriterId ) ;
}
2013-10-04 17:13:57 +10:00
#endregion
2012-12-14 15:19:54 -01:00
2012-12-21 09:45:34 -01:00
/// <summary>
/// Checks whether an <see cref="IContent"/> item has any published versions
/// </summary>
/// <param name="content"></param>
/// <returns>True if the content has any published versiom otherwise False</returns>
public static bool HasPublishedVersion ( this IContent content )
{
if ( content . HasIdentity = = false )
return false ;
return ApplicationContext . Current . Services . ContentService . HasPublishedVersion ( content . Id ) ;
}
2014-05-15 12:49:03 +10:00
2013-10-04 17:13:57 +10:00
#region Tag methods
2013-10-24 11:49:09 +11:00
///// <summary>
///// Returns the tags for the given property
///// </summary>
///// <param name="content"></param>
///// <param name="propertyTypeAlias"></param>
///// <param name="tagGroup"></param>
///// <returns></returns>
///// <remarks>
///// The tags returned are only relavent for published content & saved media or members
///// </remarks>
//public static IEnumerable<ITag> GetTags(this IContentBase content, string propertyTypeAlias, string tagGroup = "default")
//{
//}
2013-10-04 17:13:57 +10:00
/// <summary>
/// Sets tags for the property - will add tags to the tags table and set the property value to be the comma delimited value of the tags.
/// </summary>
/// <param name="content">The content item to assign the tags to</param>
2013-10-08 10:58:47 +11:00
/// <param name="propertyTypeAlias">The property alias to assign the tags to</param>
2013-10-04 17:13:57 +10:00
/// <param name="tags">The tags to assign</param>
/// <param name="replaceTags">True to replace the tags on the current property with the tags specified or false to merge them with the currently assigned ones</param>
/// <param name="tagGroup">The group/category to assign the tags, the default value is "default"</param>
/// <returns></returns>
2013-10-08 12:25:03 +11:00
public static void SetTags ( this IContentBase content , string propertyTypeAlias , IEnumerable < string > tags , bool replaceTags , string tagGroup = "default" )
2014-05-15 12:49:03 +10:00
{
content . SetTags ( TagCacheStorageType . Csv , propertyTypeAlias , tags , replaceTags , tagGroup ) ;
}
/// <summary>
/// Sets tags for the property - will add tags to the tags table and set the property value to be the comma delimited value of the tags.
/// </summary>
/// <param name="content">The content item to assign the tags to</param>
/// <param name="storageType">The tag storage type in cache (default is csv)</param>
/// <param name="propertyTypeAlias">The property alias to assign the tags to</param>
/// <param name="tags">The tags to assign</param>
/// <param name="replaceTags">True to replace the tags on the current property with the tags specified or false to merge them with the currently assigned ones</param>
/// <param name="tagGroup">The group/category to assign the tags, the default value is "default"</param>
/// <returns></returns>
public static void SetTags ( this IContentBase content , TagCacheStorageType storageType , string propertyTypeAlias , IEnumerable < string > tags , bool replaceTags , string tagGroup = "default" )
2013-10-04 17:13:57 +10:00
{
2013-10-08 10:58:47 +11:00
var property = content . Properties [ propertyTypeAlias ] ;
2013-10-04 17:13:57 +10:00
if ( property = = null )
{
2013-10-08 10:58:47 +11:00
throw new IndexOutOfRangeException ( "No property exists with name " + propertyTypeAlias ) ;
2013-10-04 17:13:57 +10:00
}
2014-07-01 12:53:07 +10:00
property . SetTags ( storageType , propertyTypeAlias , tags , replaceTags , tagGroup ) ;
}
internal static void SetTags ( this Property property , TagCacheStorageType storageType , string propertyTypeAlias , IEnumerable < string > tags , bool replaceTags , string tagGroup = "default" )
{
if ( property = = null ) throw new ArgumentNullException ( "property" ) ;
2013-10-04 17:13:57 +10:00
var trimmedTags = tags . Select ( x = > x . Trim ( ) ) . ToArray ( ) ;
property . TagSupport . Enable = true ;
property . TagSupport . Tags = trimmedTags . Select ( x = > new Tuple < string , string > ( x , tagGroup ) ) ;
property . TagSupport . Behavior = replaceTags ? PropertyTagBehavior . Replace : PropertyTagBehavior . Merge ;
//ensure the property value is set to the same thing
if ( replaceTags )
{
2014-05-15 12:49:03 +10:00
switch ( storageType )
{
case TagCacheStorageType . Csv :
property . Value = string . Join ( "," , trimmedTags ) ;
break ;
case TagCacheStorageType . Json :
//json array
property . Value = JsonConvert . SerializeObject ( trimmedTags ) ;
break ;
}
2013-10-04 17:13:57 +10:00
}
else
{
2014-05-15 12:49:03 +10:00
switch ( storageType )
{
case TagCacheStorageType . Csv :
var currTags = property . Value . ToString ( ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries )
2013-10-04 17:13:57 +10:00
. Select ( x = > x . Trim ( ) ) ;
2014-05-15 12:49:03 +10:00
property . Value = string . Join ( "," , trimmedTags . Union ( currTags ) ) ;
break ;
case TagCacheStorageType . Json :
var currJson = JsonConvert . DeserializeObject < JArray > ( property . Value . ToString ( ) ) ;
//need to append the new ones
foreach ( var tag in trimmedTags )
{
currJson . Add ( tag ) ;
}
//json array
property . Value = JsonConvert . SerializeObject ( currJson ) ;
break ;
}
2013-10-04 17:13:57 +10:00
}
}
/// <summary>
/// Remove any of the tags specified in the collection from the property if they are currently assigned.
/// </summary>
/// <param name="content"></param>
2013-10-08 10:58:47 +11:00
/// <param name="propertyTypeAlias"></param>
2013-10-04 17:13:57 +10:00
/// <param name="tags"></param>
/// <param name="tagGroup">The group/category that the tags are currently assigned to, the default value is "default"</param>
2013-10-08 12:25:03 +11:00
public static void RemoveTags ( this IContentBase content , string propertyTypeAlias , IEnumerable < string > tags , string tagGroup = "default" )
2013-10-04 17:13:57 +10:00
{
2013-10-08 10:58:47 +11:00
var property = content . Properties [ propertyTypeAlias ] ;
2013-10-04 17:13:57 +10:00
if ( property = = null )
{
2013-10-08 10:58:47 +11:00
throw new IndexOutOfRangeException ( "No property exists with name " + propertyTypeAlias ) ;
2013-10-04 17:13:57 +10:00
}
var trimmedTags = tags . Select ( x = > x . Trim ( ) ) . ToArray ( ) ;
property . TagSupport . Behavior = PropertyTagBehavior . Remove ;
property . TagSupport . Enable = true ;
property . TagSupport . Tags = trimmedTags . Select ( x = > new Tuple < string , string > ( x , tagGroup ) ) ;
//set the property value
var currTags = property . Value . ToString ( ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries )
. Select ( x = > x . Trim ( ) ) ;
property . Value = string . Join ( "," , currTags . Except ( trimmedTags ) ) ;
}
2012-12-14 15:19:54 -01:00
2013-10-04 17:13:57 +10:00
#endregion
#region XML methods
2014-10-01 10:18:51 +10:00
2013-03-01 03:04:29 +06:00
/// <summary>
/// Creates the full xml representation for the <see cref="IContent"/> object and all of it's descendants
/// </summary>
/// <param name="content"><see cref="IContent"/> to generate xml for</param>
2014-10-01 10:18:51 +10:00
/// <param name="packagingService"></param>
2013-03-01 03:04:29 +06:00
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
2014-10-01 10:18:51 +10:00
internal static XElement ToDeepXml ( this IContent content , IPackagingService packagingService )
2013-03-01 03:04:29 +06:00
{
2014-10-01 10:18:51 +10:00
return packagingService . Export ( content , true , raiseEvents : false ) ;
2013-03-01 03:04:29 +06:00
}
2012-12-14 15:19:54 -01:00
/// <summary>
/// Creates the xml representation for the <see cref="IContent"/> object
/// </summary>
/// <param name="content"><see cref="IContent"/> to generate xml for</param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IPackagingService to use")]
2012-12-14 15:19:54 -01:00
public static XElement ToXml ( this IContent content )
{
2014-01-20 10:37:46 +01:00
return ApplicationContext . Current . Services . PackagingService . Export ( content , raiseEvents : false ) ;
2012-12-14 15:19:54 -01:00
}
2014-10-01 10:18:51 +10:00
/// <summary>
/// Creates the xml representation for the <see cref="IContent"/> object
/// </summary>
/// <param name="content"><see cref="IContent"/> to generate xml for</param>
/// <param name="packagingService"></param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
public static XElement ToXml ( this IContent content , IPackagingService packagingService )
{
return packagingService . Export ( content , raiseEvents : false ) ;
}
2013-01-24 06:16:44 +03:00
/// <summary>
/// Creates the xml representation for the <see cref="IMedia"/> object
/// </summary>
/// <param name="media"><see cref="IContent"/> to generate xml for</param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IPackagingService to use")]
2013-01-24 09:50:27 -01:00
public static XElement ToXml ( this IMedia media )
2013-01-24 06:16:44 +03:00
{
2014-01-20 10:37:46 +01:00
return ApplicationContext . Current . Services . PackagingService . Export ( media , raiseEvents : false ) ;
2013-01-24 06:16:44 +03:00
}
2013-01-05 22:46:50 +03:00
2014-10-01 10:18:51 +10:00
/// <summary>
/// Creates the xml representation for the <see cref="IMedia"/> object
/// </summary>
/// <param name="media"><see cref="IContent"/> to generate xml for</param>
/// <param name="packagingService"></param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
public static XElement ToXml ( this IMedia media , IPackagingService packagingService )
{
return packagingService . Export ( media , raiseEvents : false ) ;
}
2013-10-18 16:16:30 +11:00
/// <summary>
2013-12-17 14:53:21 +11:00
/// Creates the full xml representation for the <see cref="IMedia"/> object and all of it's descendants
2013-10-18 16:16:30 +11:00
/// </summary>
2013-12-17 14:53:21 +11:00
/// <param name="media"><see cref="IMedia"/> to generate xml for</param>
2014-10-01 10:18:51 +10:00
/// <param name="packagingService"></param>
2013-12-17 14:53:21 +11:00
/// <returns>Xml representation of the passed in <see cref="IMedia"/></returns>
2014-10-01 10:18:51 +10:00
internal static XElement ToDeepXml ( this IMedia media , IPackagingService packagingService )
2013-10-18 16:16:30 +11:00
{
2014-10-01 10:18:51 +10:00
return packagingService . Export ( media , true , raiseEvents : false ) ;
2013-10-18 16:16:30 +11:00
}
2014-05-15 12:49:03 +10:00
2012-12-14 15:19:54 -01:00
/// <summary>
/// Creates the xml representation for the <see cref="IContent"/> object
/// </summary>
/// <param name="content"><see cref="IContent"/> to generate xml for</param>
/// <param name="isPreview">Boolean indicating whether the xml should be generated for preview</param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IPackagingService to use")]
2012-12-14 15:19:54 -01:00
public static XElement ToXml ( this IContent content , bool isPreview )
{
//TODO Do a proper implementation of this
//If current IContent is published we should get latest unpublished version
return content . ToXml ( ) ;
2012-11-06 15:17:58 -01:00
}
2013-12-17 14:53:21 +11:00
2014-10-01 10:18:51 +10:00
/// <summary>
/// Creates the xml representation for the <see cref="IContent"/> object
/// </summary>
/// <param name="content"><see cref="IContent"/> to generate xml for</param>
/// <param name="packagingService"></param>
/// <param name="isPreview">Boolean indicating whether the xml should be generated for preview</param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
public static XElement ToXml ( this IContent content , IPackagingService packagingService , bool isPreview )
{
//TODO Do a proper implementation of this
//If current IContent is published we should get latest unpublished version
return content . ToXml ( packagingService ) ;
}
2013-12-17 14:53:21 +11:00
/// <summary>
/// Creates the xml representation for the <see cref="IMember"/> object
/// </summary>
/// <param name="member"><see cref="IMember"/> to generate xml for</param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
2014-10-01 10:18:51 +10:00
[Obsolete("Use the overload that declares the IPackagingService to use")]
2013-12-17 14:53:21 +11:00
public static XElement ToXml ( this IMember member )
{
2014-01-08 14:12:39 +01:00
return ( ( PackagingService ) ( ApplicationContext . Current . Services . PackagingService ) ) . Export ( member ) ;
2013-12-17 14:53:21 +11:00
}
2014-10-01 10:18:51 +10:00
/// <summary>
/// Creates the xml representation for the <see cref="IMember"/> object
/// </summary>
/// <param name="member"><see cref="IMember"/> to generate xml for</param>
/// <param name="packagingService"></param>
/// <returns>Xml representation of the passed in <see cref="IContent"/></returns>
public static XElement ToXml ( this IMember member , IPackagingService packagingService )
{
return ( ( PackagingService ) ( packagingService ) ) . Export ( member ) ;
}
2013-10-04 17:13:57 +10:00
#endregion
2012-10-05 11:03:08 -02:00
}
}