Skip to content

Instantly share code, notes, and snippets.

@hishaamn
Last active November 2, 2021 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hishaamn/8910e189319efd74d5e6697b71367de1 to your computer and use it in GitHub Desktop.
Save hishaamn/8910e189319efd74d5e6697b71367de1 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore;
using Sitecore.Buckets.Extensions;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.JavaScriptServices.AppServices;
using Sitecore.JavaScriptServices.AppServices.Data;
using Sitecore.JavaScriptServices.AppServices.Extensions;
using Sitecore.JavaScriptServices.AppServices.Models;
using Sitecore.JavaScriptServices.AppServices.Pipelines.Import;
using Sitecore.JavaScriptServices.Configuration;
using Convert = System.Convert;
namespace Zerex.Framework.Pipelines.Import
{
public class ProcessTemplateWithValidation : ProcessTemplates
{
protected override void ProcessField(TemplateItem template, Item multilistRoot, FieldDef fieldDef, ref int sortOrderIndex, IdManager idManager, AppConfiguration app)
{
Assert.IsNotNullOrEmpty(fieldDef.Name, "A field on the " + template.Name + " template had no name, or an empty name.");
string str = ItemUtil.ProposeValidItemName(string.IsNullOrWhiteSpace(fieldDef.Section) ? this.DefaultSectionName : fieldDef.Section);
TemplateSectionItem templateSectionItem = template.GetSection(str);
NameUtil.NameResult nameResult = NameUtil.ProposeValidItemNameWithUniqueness(fieldDef.Name, fieldDef.DisplayName);
ID templateFieldId = idManager.GetTemplateFieldId(app.Name, template.ID, fieldDef);
idManager.AssertIdIsUnused(templateFieldId, "template field " + template.Name + "::" + fieldDef.Name);
TemplateFieldItem field = template.GetField(templateFieldId) ?? template.GetField(nameResult.Name);
if (field == null)
{
if (!template.InnerItem.Access.CanCreate() || templateSectionItem != null && !templateSectionItem.InnerItem.Access.CanCreate())
{
Log.Warn("[JSS] - Skipping import of " + fieldDef.Name + " within " + template.InnerItem.Paths.FullPath + " because child creation is not allowed by import user.", (object)this);
return;
}
if (templateSectionItem == null)
{
ID templateSectionId = idManager.GetTemplateSectionId(app.Name, template.ID, str);
idManager.AssertIdIsUnused(templateSectionId, "template section " + template.Name + "::" + str);
Log.Info("[JSS] - Creating template section " + str + " on the " + template.Name + " template.", (object)this);
templateSectionItem = new TemplateSectionItem(template.InnerItem.Add(str, new TemplateID(TemplateIDs.TemplateSection), templateSectionId), template);
}
Log.Info("[JSS] - + field '" + fieldDef.Name + "' to '" + str + "' section on " + template.Name + " template", (object)this);
field = (TemplateFieldItem)templateSectionItem.InnerItem.Add(nameResult.Name, new TemplateID(TemplateIDs.TemplateField), templateFieldId);
template.Database.Engines.TemplateEngine.Reset();
}
if (!field.InnerItem.CanWrite())
return;
field.InnerItem.Editing.BeginEdit();
int? sortOrder = fieldDef.SortOrder;
if (sortOrder.HasValue)
{
Log.Debug("[JSS] - Updating sort order on " + field.InnerItem.Paths.FullPath, (object)this);
TemplateFieldItem templateFieldItem = field;
sortOrder = fieldDef.SortOrder;
int num = sortOrder.Value;
templateFieldItem.Sortorder = num;
}
else
{
Log.Debug("[JSS] - Setting default sort order on " + field.InnerItem.Paths.FullPath, (object)this);
sortOrderIndex += 100;
field.Sortorder = sortOrderIndex;
}
if (fieldDef.Storage.Equals("shared", StringComparison.OrdinalIgnoreCase))
{
Log.Debug("[JSS] - Setting " + field.InnerItem.Paths.FullPath + " to shared", (object)this);
field.InnerItem[TemplateFieldIDs.Shared] = "1";
field.InnerItem[TemplateFieldIDs.Unversioned] = "0";
}
if (fieldDef.Storage.Equals("unversioned", StringComparison.OrdinalIgnoreCase))
{
Log.Debug("[JSS] - Setting " + field.InnerItem.Paths.FullPath + " to unversioned", (object)this);
field.InnerItem[TemplateFieldIDs.Unversioned] = "1";
field.InnerItem[TemplateFieldIDs.Shared] = "0";
}
if (fieldDef.Storage.Equals("versioned", StringComparison.OrdinalIgnoreCase))
{
Log.Debug("[JSS] - Setting " + field.InnerItem.Paths.FullPath + " to versioned", (object)this);
field.InnerItem[TemplateFieldIDs.Unversioned] = "0";
field.InnerItem[TemplateFieldIDs.Shared] = "0";
}
if (!string.IsNullOrWhiteSpace(nameResult.DisplayName))
{
Log.Debug("[JSS] - Updating display name/title on " + field.InnerItem.Paths.FullPath, (object)this);
field.InnerItem.Appearance.DisplayName = nameResult.DisplayName;
field.Title = nameResult.DisplayName;
}
Log.Debug("[JSS] - Updating type on " + field.InnerItem.Paths.FullPath, (object)this);
field.Type = fieldDef.Type;
this.ProcessFieldSource(field, multilistRoot, fieldDef.Source);
Log.Debug(string.Format("[JSS] - Setting required to {0} on {1}", (object)fieldDef.Required, (object)field.InnerItem.Paths.FullPath), (object)this);
this.AssignRequiredValidation(field.InnerItem, fieldDef.Required, "Quick Action Bar");
this.AssignRequiredValidation(field.InnerItem, fieldDef.Required, "Validate Button");
this.AssignRequiredValidation(field.InnerItem, fieldDef.Required, "Validator Bar");
this.AssignRequiredValidation(field.InnerItem, fieldDef.Required, "Workflow");
var isFieldCriticalString = fieldDef.AdditionalData?["IsCriticalField"].ToString();
if (!isFieldCriticalString.IsNullOrEmpty())
{
var isFieldCritical = Convert.ToBoolean(isFieldCriticalString);
this.AssignCustomFieldValidation(field.InnerItem, isFieldCritical, "Quick Action Bar");
this.AssignCustomFieldValidation(field.InnerItem, isFieldCritical, "Validate Button");
this.AssignCustomFieldValidation(field.InnerItem, isFieldCritical, "Validator Bar");
this.AssignCustomFieldValidation(field.InnerItem, isFieldCritical, "Workflow");
}
if (!string.IsNullOrWhiteSpace(fieldDef.ValidationPattern))
{
if (string.IsNullOrWhiteSpace(fieldDef.ValidationMessage))
Log.Warn("[JSS] - validationPattern was specified for " + field.InnerItem.Paths.FullPath + ", but no validationMessage was specified. A generic message will be used.", (object)this);
Log.Debug("[JSS] - Updating regex validation on " + field.InnerItem.Paths.FullPath, (object)this);
field.Validation = fieldDef.ValidationPattern;
field.ValidationText = !string.IsNullOrWhiteSpace(fieldDef.ValidationMessage) ? fieldDef.ValidationMessage : "Field must match regular expression " + fieldDef.ValidationPattern + ".";
}
field.InnerItem.Editing.EndEdit();
}
protected virtual void AssignCustomFieldValidation(Item templateField, bool isCritical, string validationFieldName)
{
MultilistField field = templateField.Fields[validationFieldName];
if (field == null)
{
Log.Warn("[JSS] - Could not find " + validationFieldName + " field on " + templateField.Paths.FullPath + "; validation settings may be incomplete.", (object)this);
}
else if (!isCritical)
{
field.Remove("{0E89EA60-AC6F-4619-8EDF-2057F307F19D}");
}
else
{
if (field.Contains("{0E89EA60-AC6F-4619-8EDF-2057F307F19D}"))
{
return;
}
field.Add("{0E89EA60-AC6F-4619-8EDF-2057F307F19D}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment