From 66629aa676cd8bb63576c90df430df66d897eb14 Mon Sep 17 00:00:00 2001 From: velan99 <36262833+velan99@users.noreply.github.com> Date: Fri, 2 Nov 2018 15:29:07 +0400 Subject: [PATCH] intitial commit --- .gitattributes | 63 +++++++++ .../Sitecore.Scientist.Forms.sln | 25 ++++ .../Properties/AssemblyInfo.cs | 36 +++++ .../Sitecore.Scientist.Forms.csproj | 98 +++++++++++++ .../Validator/EmailExistValidator.cs | 130 ++++++++++++++++++ .../ViewModel/EmailInputViewModel.cs | 53 +++++++ .../Sitecore.Scientist.Forms/app.config | 19 +++ .../Sitecore.Scientist.Forms/packages.config | 20 +++ 8 files changed, 444 insertions(+) create mode 100644 .gitattributes create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.sln create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Properties/AssemblyInfo.cs create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.csproj create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Validator/EmailExistValidator.cs create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/ViewModel/EmailInputViewModel.cs create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/app.config create mode 100644 Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/packages.config diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.sln b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.sln new file mode 100644 index 0000000..2b8888f --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2002 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitecore.Scientist.Forms", "Sitecore.Scientist.Forms\Sitecore.Scientist.Forms.csproj", "{5698549E-8BAF-4616-B649-92F5A72541DA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5698549E-8BAF-4616-B649-92F5A72541DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5698549E-8BAF-4616-B649-92F5A72541DA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5698549E-8BAF-4616-B649-92F5A72541DA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5698549E-8BAF-4616-B649-92F5A72541DA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7D1446E1-0A51-421A-850F-A4CE3C9B93AA} + EndGlobalSection +EndGlobal diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Properties/AssemblyInfo.cs b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1df6228 --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Sitecore.Scientist.Forms")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Sitecore.Scientist.Forms")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("5698549e-8baf-4616-b649-92f5a72541da")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.csproj b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.csproj new file mode 100644 index 0000000..f2b4cc6 --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms.csproj @@ -0,0 +1,98 @@ + + + + + Debug + AnyCPU + {5698549E-8BAF-4616-B649-92F5A72541DA} + Library + Properties + Sitecore.Scientist.Forms + Sitecore.Scientist.Forms + v4.6.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.1.0.0\lib\netstandard1.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + + ..\packages\Sitecore.ExperienceForms.NoReferences.9.0.180604\lib\NET462\Sitecore.ExperienceForms.dll + + + ..\packages\Sitecore.ExperienceForms.Client.NoReferences.9.0.180604\lib\NET462\Sitecore.ExperienceForms.Client.dll + + + ..\packages\Sitecore.ExperienceForms.Mvc.NoReferences.9.0.180604\lib\NET462\Sitecore.ExperienceForms.Mvc.dll + + + ..\packages\Sitecore.Kernel.NoReferences.9.0.180604\lib\NET462\Sitecore.Kernel.dll + + + ..\packages\Sitecore.Mvc.NoReferences.9.0.180604\lib\NET462\Sitecore.Mvc.dll + + + + + + ..\packages\System.Reflection.4.1.0\lib\net462\System.Reflection.dll + + + + ..\packages\Microsoft.AspNet.WebPages.3.2.6\lib\net45\System.Web.Helpers.dll + + + ..\packages\Microsoft.AspNet.Razor.3.2.6\lib\net45\System.Web.Razor.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.6\lib\net45\System.Web.WebPages.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.6\lib\net45\System.Web.WebPages.Deployment.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.6\lib\net45\System.Web.WebPages.Razor.dll + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Validator/EmailExistValidator.cs b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Validator/EmailExistValidator.cs new file mode 100644 index 0000000..d551d56 --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/Validator/EmailExistValidator.cs @@ -0,0 +1,130 @@ +using Microsoft.Extensions.DependencyInjection; +using Sitecore.Data; +using Sitecore.Data.Items; +using Sitecore.DependencyInjection; +using Sitecore.ExperienceForms.Data; +using Sitecore.ExperienceForms.Mvc.Models.Fields; +using Sitecore.ExperienceForms.Mvc.Models.Validation; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web.Mvc; + +namespace Sitecore.Scientist.Forms.Validator +{ + public class EmailExistValidator : ValidationElement + { + public readonly string FormTemplateId = "{6ABEE1F2-4AB4-47F0-AD8B-BDB36F37F64C}"; + + private IFormDataProvider _dataProvider; + + + protected virtual IFormDataProvider FormDataProvider + { + get + { + IFormDataProvider formDataProvider = this._dataProvider; + if (formDataProvider == null) + { + IFormDataProvider service = ServiceLocator.ServiceProvider.GetService(); + IFormDataProvider formDataProvider1 = service; + this._dataProvider = service; + formDataProvider = formDataProvider1; + } + return formDataProvider; + } + } + + public override IEnumerable ClientValidationRules + { + get + { + if (string.IsNullOrEmpty(this.FormId)) + { + yield break; + } + } + } + + protected virtual string FormId + { + get; + set; + } + + protected virtual string Title + { + get; + set; + } + protected virtual string FieldName + { + get; + set; + } + public EmailExistValidator(ValidationDataModel validationItem) : base(validationItem) + { + + } + + public override void Initialize(object validationModel) + { + base.Initialize(validationModel); + StringInputViewModel stringInputViewModel = validationModel as StringInputViewModel; + if (stringInputViewModel != null) + { + var fieldItem = Context.Database.GetItem(ID.Parse(stringInputViewModel.ItemId)); + var formItem = GetFormItem(fieldItem); + if (formItem != null) + { + this.FormId = formItem.ID.ToString(); + } + this.Title = stringInputViewModel.Title; + this.FieldName = stringInputViewModel.Name; + } + } + + public override ValidationResult Validate(object value) + { + if (value == null) + { + return ValidationResult.Success; + } + if (!string.IsNullOrEmpty(this.FormId)) + { + var formId = Guid.Parse(this.FormId); + var data = this.FormDataProvider.GetEntries(formId, null, null); + foreach (var item in data) + { + var emailValue = item.Fields.Where(x => x.FieldName == this.FieldName).FirstOrDefault(); + if (emailValue != null && emailValue.Value.ToLower() == value.ToString().ToLower()) + { + return new ValidationResult(this.FormatMessage(new object[] { this.Title })); + } + + } + return ValidationResult.Success; + } + return new ValidationResult(this.FormatMessage(new object[] { this.Title })); + } + public Item GetFormItem(Item sitecoreItem) + { + if (sitecoreItem == null) + return null; + Item parent = sitecoreItem.Parent; + if (parent.TemplateID == ID.Parse(this.FormTemplateId)) + { + return parent; + } + else if (ItemIDs.RootID == parent.ID) + { + return null; + } + else + { + return GetFormItem(parent); + } + } + } +} diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/ViewModel/EmailInputViewModel.cs b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/ViewModel/EmailInputViewModel.cs new file mode 100644 index 0000000..1b07086 --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/ViewModel/EmailInputViewModel.cs @@ -0,0 +1,53 @@ +using Sitecore.Data; +using Sitecore.Data.Fields; +using Sitecore.Data.Items; +using Sitecore.Diagnostics; +using Sitecore.ExperienceForms.Mvc.Models.Fields; +using System.Globalization; + +namespace Sitecore.Scientist.Forms.ViewModel +{ + public class EmailInputViewModel : StringInputViewModel + { + public string FormId + { + get; + set; + } + public EmailInputViewModel() + { + + } + protected override void InitItemProperties(Item item) + { + var formItem = GetFormItem(item); + if (formItem != null) + { + this.FormId = formItem.ID.ToString(); + } + } + + protected override void UpdateItemFields(Item item) + { + Assert.ArgumentNotNull(item, "item"); + base.UpdateItemFields(item); + } + + public Item GetFormItem(Item sitecoreItem) + { + Item parent = sitecoreItem.Parent; + if (parent.TemplateID == ID.Parse("{6ABEE1F2-4AB4-47F0-AD8B-BDB36F37F64C}")) + { + return parent; + } + else if (ItemIDs.RootID == parent.ID) + { + return null; + } + else + { + return GetFormItem(parent); + } + } + } +} diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/app.config b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/app.config new file mode 100644 index 0000000..7257b83 --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/app.config @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/packages.config b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/packages.config new file mode 100644 index 0000000..6b203f7 --- /dev/null +++ b/Sitecore.Scientist.Forms/Sitecore.Scientist.Forms/packages.config @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file