Created
September 17, 2019 20:20
-
-
Save yansklyarenko/dd6e0cab47dd93bc8d1f9a1c80c035e5 to your computer and use it in GitHub Desktop.
The source code of the TreeListSourceValidator
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; | |
using System.Collections.Generic; | |
using System.Runtime.Serialization; | |
using Sitecore.Collections; | |
using Sitecore.Data; | |
using Sitecore.Data.Fields; | |
using Sitecore.Data.Items; | |
using Sitecore.Data.Query; | |
using Sitecore.Data.Validators; | |
namespace Validators | |
{ | |
public class Constants | |
{ | |
public const string EXCLUDE_TEMPLATES_FOR_SELECTION = "excludetemplatesforselection"; | |
public const string INCLUDE_TEMPLATES_FOR_SELECTION = "includetemplatesforselection"; | |
public const string EXCLUDE_TEMPLATES_FOR_DISPLAY = "excludetemplatesfordisplay"; | |
public const string INCLUDE_TEMPLATES_FOR_DISPLAY = "includetemplatesfordisplay"; | |
public const string EXCLUDE_ITEMS_FOR_DISPLAY = "excludeitemsfordisplay"; | |
public const string INCLUDE_ITEMS_FOR_DISPLAY = "includeitemsfordisplay"; | |
public const string ALLOW_MULTIPLE_SELECTION = "allowmultipleselection"; | |
public const string DATA_SOURCE = "datasource"; | |
public const string DATABASE_NAME = "databasename"; | |
} | |
[Serializable] | |
public class TreeListSourceValidator : StandardValidator | |
{ | |
readonly List<string> sourceParameters = new List<string>(new string[] { Constants.EXCLUDE_TEMPLATES_FOR_SELECTION, | |
Constants.INCLUDE_TEMPLATES_FOR_SELECTION, | |
Constants.EXCLUDE_TEMPLATES_FOR_DISPLAY, | |
Constants.INCLUDE_TEMPLATES_FOR_DISPLAY, | |
Constants.EXCLUDE_ITEMS_FOR_DISPLAY, | |
Constants.INCLUDE_ITEMS_FOR_DISPLAY, | |
Constants.ALLOW_MULTIPLE_SELECTION, | |
Constants.DATA_SOURCE, | |
Constants.DATABASE_NAME }); | |
public TreeListSourceValidator() | |
{ } | |
public TreeListSourceValidator(SerializationInfo info, StreamingContext context) | |
: base(info, context) | |
{ } | |
protected override ValidatorResult Evaluate() | |
{ | |
ValidatorResult result = ValidatorResult.Valid; | |
Field field = GetField(); | |
if (field != null) | |
{ | |
string fieldSource = field.Source; | |
if (!string.IsNullOrEmpty(fieldSource) && !ID.IsID(fieldSource) | |
&& !fieldSource.StartsWith("/", StringComparison.InvariantCulture)) | |
{ | |
result = EvaluateSourceParameters(fieldSource); | |
} | |
} | |
return result; | |
} | |
ValidatorResult EvaluateSourceParameters(string fieldSource) | |
{ | |
SafeDictionary<string> parameters = new SafeDictionary<string>(); | |
string[] sourceParts = fieldSource.Split('&'); | |
foreach (string part in sourceParts) | |
{ | |
if (string.IsNullOrEmpty(part)) | |
{ | |
continue; | |
} | |
if (!part.Contains("=") || part.EndsWith("=")) | |
{ | |
Text = string.Format("The value is not set for source parameter '{0}'", part.TrimEnd('=')); | |
return GetFailedResult(ValidatorResult.Error); | |
} | |
else | |
{ | |
string parameterName = part.Substring(0, part.IndexOf('=')).ToLower(); | |
if (!sourceParameters.Contains(parameterName)) | |
{ | |
Text = string.Format("Unknown source parameter '{0}'", parameterName); | |
return GetFailedResult(ValidatorResult.Error); | |
} | |
else | |
{ | |
string parameterValue = part.Substring(part.IndexOf('=') + 1); | |
parameters.Add(parameterName, parameterValue); | |
} | |
} | |
} | |
return EvaluateWellFormedParameters(parameters); | |
} | |
ValidatorResult EvaluateWellFormedParameters(SafeDictionary<string> parameters) | |
{ | |
Database db = Sitecore.Context.ContentDatabase; | |
if (parameters.ContainsKey(Constants.DATABASE_NAME)) | |
{ | |
db = Database.GetDatabase(parameters[Constants.DATABASE_NAME]); | |
} | |
ValidatorResult result = ValidatorResult.Valid; | |
foreach (string parameter in parameters.Keys) | |
{ | |
switch (parameter) | |
{ | |
case Constants.EXCLUDE_TEMPLATES_FOR_SELECTION: | |
case Constants.INCLUDE_TEMPLATES_FOR_SELECTION: | |
case Constants.EXCLUDE_TEMPLATES_FOR_DISPLAY: | |
case Constants.INCLUDE_TEMPLATES_FOR_DISPLAY: | |
result = EvaluateTemplates(parameters[parameter], db); | |
break; | |
case Constants.EXCLUDE_ITEMS_FOR_DISPLAY: | |
case Constants.INCLUDE_ITEMS_FOR_DISPLAY: | |
result = EvaluateItems(parameters[parameter], db); | |
break; | |
case Constants.ALLOW_MULTIPLE_SELECTION: | |
result = EvaluateMultipleSelection(parameters[parameter]); | |
break; | |
case Constants.DATA_SOURCE: | |
result = EvaluateDataSource(parameters[parameter], db); | |
break; | |
default: | |
break; | |
} | |
if (result != ValidatorResult.Valid) | |
{ | |
return GetFailedResult(result); | |
} | |
} | |
return result; | |
} | |
ValidatorResult EvaluateDataSource(string value, Database database) | |
{ | |
Item[] items = Query.SelectItems(value, database); | |
if ((items == null) || (items.Length == 0)) | |
{ | |
Text = "Querying the specified datasource didn't bring any single item. The query might be invalid"; | |
return ValidatorResult.Warning; | |
} | |
return ValidatorResult.Valid; | |
} | |
ValidatorResult EvaluateMultipleSelection(string value) | |
{ | |
if (string.Compare(value, "yes", StringComparison.OrdinalIgnoreCase) != 0) | |
{ | |
Text = string.Format("{0}: The values other than 'yes' are treated as 'no'. You can avoid specifying the value in this case, since it is a default choice", Constants.ALLOW_MULTIPLE_SELECTION); | |
return ValidatorResult.Warning; | |
} | |
return ValidatorResult.Valid; | |
} | |
ValidatorResult EvaluateItems(string value, Database database) | |
{ | |
string[] items = value.Split(new char[] { ',' }); | |
foreach (string item in items) | |
{ | |
if (!string.IsNullOrEmpty(item) && Query.SelectSingleItem(string.Format("/sitecore/content//*[@@key='{0}']", item.ToLower()), database) == null) | |
{ | |
Text = string.Format("The item '{0}' doesn't exist in the '{1}' database", item, database.Name); | |
return ValidatorResult.Warning; | |
} | |
} | |
return ValidatorResult.Valid; | |
} | |
ValidatorResult EvaluateTemplates(string value, Database database) | |
{ | |
string[] templates = value.Split(new char[] { ',' }); | |
foreach (string template in templates) | |
{ | |
if (!string.IsNullOrEmpty(template) && Query.SelectSingleItem(string.Format("/sitecore/templates//*[@@key='{0}']", template.ToLower()), database) == null) | |
{ | |
Text = string.Format("The template '{0}' doesn't exist in the '{1}' database", template, database.Name); | |
return ValidatorResult.Warning; | |
} | |
} | |
return ValidatorResult.Valid; | |
} | |
protected override ValidatorResult GetMaxValidatorResult() | |
{ | |
return ValidatorResult.Error; | |
} | |
public override string Name | |
{ | |
get | |
{ | |
return "TreeList source validator"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment