Last active
November 30, 2015 14:23
-
-
Save PNergard/4fe250fc8d96062b3ff3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using EPiServer.Core; | |
using EPiServer.Validation; | |
using EPiServer; | |
using AlloyDemoMVC.Models.Media; | |
namespace AlloyDemoMVC.Business | |
{ | |
public class ImagePropertyValidator : IValidate<ContentData> | |
{ | |
/// <summary> | |
/// Validation attribute that checks Imagefile properties for null. | |
/// </summary> | |
/// <param name="page"></param> | |
/// <returns></returns> | |
IEnumerable<ValidationError> IValidate<ContentData>.Validate(ContentData page) | |
{ | |
string errorMessageTemplate = @"The file ""{0}"" is missing Copyright info!"; | |
ValidationErrorSeverity severity = ValidationErrorSeverity.Warning; | |
List<ValidationError> errors = new List<ValidationError>(); | |
PropertyDataCollection pageProperties = page.Property; | |
foreach (PropertyData propertyData in pageProperties) | |
{ | |
//Built-in properties that we want to ignore | |
if (propertyData.OwnerTab == -1) continue; | |
//ContentArea | |
if (propertyData.PropertyValueType == typeof(ContentArea)) | |
{ | |
var contentArea = (ContentArea)propertyData.Value; | |
if (contentArea.Items != null) | |
{ | |
foreach (var item in contentArea.Items) | |
{ | |
var content = item.GetContent(); | |
if (content as ImageFile != null) | |
{ | |
if (string.IsNullOrEmpty(((ImageFile)content).Copyright)) | |
{ | |
errors.Add( | |
new ValidationError | |
{ | |
ErrorMessage = string.Format(errorMessageTemplate, ((ImageFile)content).Name), | |
PropertyName = propertyData.Name, | |
Severity = severity | |
}); | |
} | |
} | |
} | |
} | |
} | |
//ContentReference | |
else if (propertyData.PropertyValueType == typeof(ContentReference)) | |
{ | |
var reference = propertyData.Value as ContentReference;// page.GetValue(propertyData.Name) as ContentReference; | |
if (!ContentReference.IsNullOrEmpty(reference)) | |
{ | |
ImageFile file = null; | |
var locator = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
try | |
{ | |
file = locator.Get<ImageFile>(reference); | |
} | |
catch (Exception) | |
{ | |
file = null; | |
} | |
if (file != null) | |
{ | |
if (string.IsNullOrEmpty(((ImageFile)file).Copyright)) | |
{ | |
errors.Add( | |
new ValidationError | |
{ | |
ErrorMessage = string.Format(errorMessageTemplate, file.Name), | |
PropertyName = propertyData.Name, | |
Severity = severity | |
}); | |
} | |
} | |
} | |
} | |
} | |
return errors; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment