Created
April 11, 2023 10:05
-
-
Save frueda1/721f83a9fdb7d687cc40aaa824ebe6c1 to your computer and use it in GitHub Desktop.
Keylookup Extend to add new column
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 Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Shell.Applications.ContentEditor; | |
using Sitecore.Text; | |
using Sitecore.Web.UI.Sheer; | |
using System; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Web; | |
using System.Web.UI; | |
namespace Sitecore.Foundation.SitecoreExtensions.Controls | |
{ | |
public class KeyValueWithDropdownOrdered : NameLookupValue | |
{ | |
protected override void OnLoad(EventArgs e) | |
{ | |
Assert.ArgumentNotNull((object)e, "e"); | |
string origValue = this.Value; | |
base.OnLoad(e); // this will modify this.Value using existing base logic and falsely throw a modified alert | |
this.Value = origValue; // so reset the value back for comparison below | |
Sitecore.Context.ClientPage.Modified = false; // if values have actually changed our code below will set this to true | |
if (Sitecore.Context.ClientPage.IsEvent) | |
this.LoadValue(); | |
else | |
this.BuildControl(); | |
} | |
private void LoadValue() | |
{ | |
if (this.ReadOnly || this.Disabled) | |
return; | |
System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page; | |
NameValueCollection nameValueCollection = page == null ? new NameValueCollection() : page.Request.Form; | |
UrlString urlString = new UrlString(); | |
UrlString urlStringOrder = new UrlString(); | |
int i = 1; | |
foreach (string index1 in nameValueCollection.Keys) | |
{ | |
if (!string.IsNullOrEmpty(index1) && index1.StartsWith(this.ID + "_Param", StringComparison.InvariantCulture) && !index1.EndsWith("_value", StringComparison.InvariantCulture) && !index1.EndsWith("_order", StringComparison.InvariantCulture)) | |
{ | |
string input = nameValueCollection[index1]; | |
string str = nameValueCollection[index1 + "_value"]; | |
string order = nameValueCollection[index1 + "_order"] == null ? (i++).ToString() : nameValueCollection[index1 + "_order"]; | |
if (!string.IsNullOrEmpty(input)) | |
{ | |
// modified here to return the actual GUID | |
urlString[input] = str; | |
urlStringOrder[input] = order; | |
} | |
} | |
} | |
UrlString urlStringOrdered = OrderParameters(urlStringOrder); | |
UrlString urlStringOrderedValues = OrderParametersValues(urlStringOrdered, urlString); | |
string str1 = urlStringOrderedValues.ToString(); | |
string order1 = urlStringOrdered.ToString(); | |
string str2 = str1 + "ORD_PAR" + order1; | |
if (this.Value == str2) | |
return; | |
this.Value = str2; | |
this.SetModified(); | |
} | |
private void BuildControl() | |
{ | |
this.Controls.Clear(); /* clear controls created by base class */ | |
string[] stringSeparators = new string[] { "ORD_PAR" }; | |
UrlString urlString = new UrlString(this.Value); | |
UrlString urlStringOrder = null; | |
UrlString urlStringOrdered = null; | |
this.Controls.Add((System.Web.UI.Control)new LiteralControl(this.BuildHeaderParameters())); | |
int i = 1; | |
if (this.Value.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries).Length > 1) | |
{ | |
urlString = new UrlString(this.Value.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)[0]); | |
urlStringOrder = new UrlString(this.Value.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)[1]); | |
urlStringOrdered = OrderParameters(urlStringOrder); | |
foreach (string key in urlStringOrdered.Parameters.Keys) | |
{ | |
if (key.Length > 0) | |
this.Controls.Add((System.Web.UI.Control)new LiteralControl(this.BuildParameterKeyValue(key, urlString.Parameters[key], urlStringOrdered.Parameters[key]))); | |
} | |
} | |
else | |
{ | |
foreach (string key in urlString.Parameters.Keys) | |
{ | |
if (key.Length > 0) | |
this.Controls.Add((System.Web.UI.Control)new LiteralControl(this.BuildParameterKeyValue(key, urlString.Parameters[key], (i++).ToString()))); | |
} | |
} | |
this.Controls.Add((System.Web.UI.Control)new LiteralControl(this.BuildParameterKeyValue(string.Empty, string.Empty, (i++).ToString()))); | |
} | |
protected new void ParameterChange() | |
{ | |
ClientPage clientPage = Sitecore.Context.ClientPage; | |
if (clientPage.ClientRequest.Source == StringUtil.GetString(clientPage.ServerProperties[this.ID + "_LastParameterID"]) && !string.IsNullOrEmpty(clientPage.ClientRequest.Form[clientPage.ClientRequest.Source])) | |
{ | |
string str = this.BuildParameterKeyValue(string.Empty, string.Empty, "1"); | |
clientPage.ClientResponse.Insert(this.ID, "beforeEnd", str); | |
} | |
NameValueCollection form = (NameValueCollection)null; | |
System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page; | |
if (page != null) | |
form = page.Request.Form; | |
if (form == null) | |
return; | |
clientPage.ClientResponse.SetReturnValue(true); | |
} | |
private string BuildParameterKeyValue(string key, string value, string order) | |
{ | |
Assert.ArgumentNotNull((object)key, "key"); | |
Assert.ArgumentNotNull((object)value, "value"); | |
string uniqueId = GetUniqueID(this.ID + "_Param"); | |
Sitecore.Context.ClientPage.ServerProperties[this.ID + "_LastParameterID"] = (object)uniqueId; | |
string clientEvent = Sitecore.Context.ClientPage.GetClientEvent(this.ID + ".ParameterChange"); | |
string str1 = this.ReadOnly ? " readonly=\"readonly\"" : string.Empty; | |
string str2 = this.Disabled ? " disabled=\"disabled\"" : string.Empty; | |
string str3 = this.IsVertical ? "</tr><tr>" : string.Empty; | |
return | |
string.Format( | |
"<table style=\"margin-bottom:13px;\" width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\"><tr><td style=\"width:30%;\">{0}</td>{2}<td style=\"width:60%;\">{1}</td><td style=\"width:10%;\">{3}</td></tr></table>", | |
GetNameLookupHtmlControl(uniqueId, str1, str2, key, clientEvent), /* This code has been changed from default */ | |
(object)this.GetValueHtmlControl(uniqueId, StringUtil.EscapeQuote(HttpUtility.UrlDecode(value))), | |
(object)str3, GetOrderValueHtmlControl(uniqueId, order)); | |
} | |
private string GetNameLookupHtmlControl(string uniqueId, string readOnly, string disabled, string key, string clientEvent) | |
{ | |
HtmlTextWriter htmlTextWriter = new HtmlTextWriter((TextWriter)new StringWriter()); | |
Item[] items = this.GetItems(Sitecore.Context.ContentDatabase.GetItem(this.ItemID)); | |
htmlTextWriter.Write("<select style=\"width:97%;\" id=\"{0}\" name=\"{0}\" style=\"{1}\" {2}{3} onchange=\"{4}\" >", uniqueId, this.NameStyle, readOnly, disabled, clientEvent); | |
htmlTextWriter.Write("<option" + (string.IsNullOrEmpty(key) ? " selected=\"selected\"" : string.Empty) + " value=\"\"></option>"); | |
foreach (Item obj in items) | |
{ | |
string itemHeader = this.GetItemHeader(obj); | |
bool flag = obj.ID.ToString() == key; | |
htmlTextWriter.Write("<option value=\"" + this.GetItemValue(obj) + "\"" + (flag ? " selected=\"selected\"" : string.Empty) + ">" + itemHeader + "</option>"); | |
} | |
htmlTextWriter.Write("</select>"); | |
return htmlTextWriter.InnerWriter.ToString(); | |
} | |
// Copied back from the original NameValue control that NameLookupValue overrode | |
protected override string GetValueHtmlControl(string id, string value) | |
{ | |
string str1 = this.ReadOnly ? " readonly=\"readonly\"" : string.Empty; | |
string str2 = this.Disabled ? " disabled=\"disabled\"" : string.Empty; | |
return string.Format("<input style=\"width:98%;\" id=\"{0}_value\" name=\"{0}_value\" type=\"text\" style=\"width:100%\" value=\"{1}\"{2}{3}/>", (object)id, (object)value, (object)str1, (object)str2); | |
} | |
protected string GetOrderValueHtmlControl(string id, string value) | |
{ | |
return string.Format("<input id=\"{0}_order\" name=\"{0}_order\" type=\"text\" style=\"width:100%\" value=\"{1}\"/>", (object)id, (object)value); | |
} | |
protected UrlString OrderParameters(UrlString urlStringOrder) | |
{ | |
try | |
{ | |
UrlString urlStringOrdered = new UrlString(); | |
while (urlStringOrder.Parameters.Count > 0) | |
{ | |
int parametersCounter = urlStringOrder.Parameters.Count; | |
int index = 0; | |
int lower = int.Parse(urlStringOrder.Parameters[urlStringOrder.Parameters.GetKey(0)]); | |
for (int x = 0; x < parametersCounter - 1; x++) | |
{ | |
if (int.Parse(urlStringOrder.Parameters[urlStringOrder.Parameters.GetKey(x + 1)]) < lower) | |
{ | |
lower = int.Parse(urlStringOrder.Parameters[urlStringOrder.Parameters.GetKey(x + 1)]); | |
index = x + 1; | |
} | |
} | |
urlStringOrdered.Add(urlStringOrder.Parameters.GetKey(index), lower.ToString()); | |
urlStringOrder.Parameters.Remove(urlStringOrder.Parameters.GetKey(index)); | |
} | |
return urlStringOrdered; | |
} | |
catch | |
{ | |
throw new Exception("Order field must be a number!"); | |
} | |
} | |
protected UrlString OrderParametersValues(UrlString urlStringOrder, UrlString urlStringOriginal) | |
{ | |
int parametersCounter = urlStringOrder.Parameters.Count; | |
UrlString urlStringOrderedValues = new UrlString(); | |
for (int x = 0; x < parametersCounter; x++) | |
{ | |
string value = urlStringOriginal.Parameters[urlStringOrder.Parameters.GetKey(x)]; | |
urlStringOrderedValues[urlStringOrder.Parameters.GetKey(x)] = Uri.UnescapeDataString(value); | |
} | |
return urlStringOrderedValues; | |
} | |
private string BuildHeaderParameters() | |
{ | |
return | |
string.Format( | |
"<table style=\"margin-bottom:13px;\" width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\"><tr><td style=\"width:30%;\">Parameter</td><td style=\"width:60%;\">Value</td><td style=\"width:10%;\">Order</td></tr></table>" | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment