Skip to content

Instantly share code, notes, and snippets.

@Wesley-Lomax
Last active October 21, 2016 10:34
Show Gist options
  • Save Wesley-Lomax/4577ce5f3a821e46adc67521a55e6748 to your computer and use it in GitHub Desktop.
Save Wesley-Lomax/4577ce5f3a821e46adc67521a55e6748 to your computer and use it in GitHub Desktop.
WFFM - Sequential Number Custom Field
using System.ComponentModel;
using System.ComponentModel.Design;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Form.Core.Attributes;
using Sitecore.Form.Web.UI.Controls;
using Sitecore.SecurityModel;
namespace WesleyLomax.WFFM
{
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class SequentialNumber : SingleLineText
{
private bool _isHidden = true;
private string _sequentialNumberId;
private int _numberPadding;
[VisualCategory("Sequence")]
[VisualFieldType(typeof(Fields.SequentialNumberField))]
[VisualProperty("Sequence:", 100)]
public string SequentialNumberId
{
get { return _sequentialNumberId; }
set { _sequentialNumberId = value; }
}
[VisualCategory("Sequence")]
[VisualProperty("Length:", 200), DefaultValue("6")]
public int NumberPadding
{
get { return _numberPadding; }
set { _numberPadding = value; }
}
[VisualCategory("Sequence")]
[VisualFieldType(typeof(Sitecore.Form.Core.Visual.BooleanField))]
[VisualProperty("Hidden:", 300), DefaultValue("Yes")]
public string IsHidden
{
get { return _isHidden ? "Yes" : "No"; }
set { _isHidden = value == "Yes"; }
}
}
}
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Form.Core.Configuration;
using Sitecore.Form.Core.Visual;
namespace WesleyLomax.WFFM.Fields
{
public class SequentialNumberField : ValidationField
{
private static readonly ID SequentialFormNumbers = new ID("{C7FB3754-A0B1-469C-9591-7CE3DC075272}");
public SequentialNumberField() : base(HtmlTextWriterTag.Select.ToString())
{
}
public override bool IsCacheable => false;
private static Item SequentialFormNumbersRoot => StaticSettings.ContextDatabase.GetItem(SequentialFormNumbers);
protected override void OnPreRender(object sender, EventArgs ev)
{
Controls.Clear();
base.OnPreRender(sender, ev);
Controls.Add(new Literal
{
Text = string.Format("<option {0} value='{1}'>{1}</option>", DefaultValue == EmptyValue ? "selected='selected'" : string.Empty, EmptyValue)
});
foreach (Item child in SequentialFormNumbersRoot.Children)
Controls.Add(new Literal
{
Text =
string.Format("<option {0} value='{1}'>{2}</option>",
DefaultValue == child.Fields[FieldIDs.MetaDataListItemValue].Value ? "selected='selected'" : string.Empty,
child.ID.ToShortID(),
child.DisplayName)
});
}
}
}
using System;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Forms.Mvc.Models.Fields;
using Sitecore.SecurityModel;
namespace WesleyLomax.WFFM.MVCModels
{
public class SequentialNumberField : SingleLineTextField
{
public SequentialNumberField(Item item) : base(item)
{
Initialize();
}
public override object Value { get; set; }
private void Initialize()
{
string isHidden = Sitecore.Forms.Mvc.Extensions.DictionaryExtensions.GetValue(ParametersDictionary, "isHidden");
if (!string.IsNullOrWhiteSpace(isHidden) && isHidden.ToUpper() == "YES")
{
if (!string.IsNullOrWhiteSpace(CssClass))
{
CssClass += " hidden";
}
else
{
CssClass = "hidden";
}
}
else
{
if (!string.IsNullOrWhiteSpace(CssClass))
{
CssClass += " disabled";
}
else
{
CssClass = "disabled";
}
}
string sequentialNumberId = Sitecore.Forms.Mvc.Extensions.DictionaryExtensions.GetValue(ParametersDictionary, "sequentialNumberId");
if (!string.IsNullOrWhiteSpace(sequentialNumberId) && ShortID.IsShortID(sequentialNumberId))
{
ID id = ShortID.DecodeID(sequentialNumberId);
if (!id.IsNull)
{
Database master = Factory.GetDatabase("master");
if (master != null)
{
Item sequenceItem = master.GetItem(id);
string number = sequenceItem.Fields[Sitecore.Form.Core.Configuration.FieldIDs.MetaDataListItemValue].Value;
int padding = Convert.ToInt32(Sitecore.Forms.Mvc.Extensions.DictionaryExtensions.GetValue(ParametersDictionary, "numberPadding"));
int currentNumber;
if (int.TryParse(number, out currentNumber))
{
currentNumber++;
string sequence = currentNumber.ToString().PadLeft(padding, '0');
Value = sequence;
using (new SecurityDisabler())
{
sequenceItem.Editing.BeginEdit();
sequenceItem.Fields[Sitecore.Form.Core.Configuration.FieldIDs.MetaDataListItemValue].Value = sequence;
sequenceItem.Editing.EndEdit();
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment