<commit_before>using System;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using umbraco.DataLayer;
using umbraco.BusinessLogic;

using umbraco.editorControls;
using umbraco.IO;
using System.Collections.Generic;
using umbraco.cms.businesslogic.datatype;

namespace umbraco.editorControls.userControlGrapper
{
	public class usercontrolPrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
	{
		public ISqlHelper SqlHelper
		{
			get { return Application.SqlHelper; }
		}

		#region IDataPrevalue Members

		// referenced datatype
		private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;

		private DropDownList _dropdownlist;
		private DropDownList _dropdownlistUserControl;
		private PlaceHolder _phSettings;

		private Dictionary<string, DataEditorSettingType> dtSettings = new Dictionary<string, DataEditorSettingType>();

		public usercontrolPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType) 
		{
			// state it knows its datatypedefinitionid
			_datatype = DataType;
			setupChildControls();

		}
		
		private void setupChildControls() 
		{
			_dropdownlist = new DropDownList();
			_dropdownlist.ID = "dbtype";
			_dropdownlist.Items.Add(DBTypes.Date.ToString());
			_dropdownlist.Items.Add(DBTypes.Integer.ToString());
			_dropdownlist.Items.Add(DBTypes.Ntext.ToString());
			_dropdownlist.Items.Add(DBTypes.Nvarchar.ToString());
			
			_dropdownlistUserControl = new DropDownList();
			_dropdownlistUserControl.ID = "usercontrol";

			_phSettings = new PlaceHolder();
			_phSettings.ID = "settings";

			// put the childcontrols in context - ensuring that
			// the viewstate is persisted etc.
			Controls.Add(_dropdownlist);
			Controls.Add(_dropdownlistUserControl);
			Controls.Add(_phSettings);

			// populate the usercontrol dropdown
			_dropdownlistUserControl.Items.Add(new ListItem(ui.Text("choose"), ""));
			populateUserControls( IOHelper.MapPath( SystemDirectories.Usercontrols) );

		   
		}

		private void populateUserControls(string path)
		{
			DirectoryInfo di = new DirectoryInfo(path);

			foreach (FileInfo uc in di.GetFiles("*.ascx"))
			{
                string ucRoot = IOHelper.MapPath(SystemDirectories.Usercontrols);

				_dropdownlistUserControl.Items.Add(
					
					new ListItem( SystemDirectories.Usercontrols +
                            uc.FullName.Substring(ucRoot.Length).Replace(IOHelper.DirSepChar, '/'))

					/*
					new ListItem( 
						uc.FullName.Substring( uc.FullName.IndexOf(root), uc.FullName.Length - uc.FullName.IndexOf(root)).Replace(IOHelper.DirSepChar, '/'))
					  */  
						);

			}
			foreach (DirectoryInfo dir in di.GetDirectories())
				populateUserControls(dir.FullName);
		}

		public Control Editor
		{
			get
			{
				return this;
			}
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!Page.IsPostBack)
			{
				string config = Configuration;
				if (config != "")
				{
					_dropdownlistUserControl.SelectedValue = config;

					
				}
				_dropdownlist.SelectedValue = _datatype.DBType.ToString();


				
			}

			//check for settings
			if (!string.IsNullOrEmpty(Configuration))
				LoadSetttings(Configuration);
		   

		}

		private Dictionary<string, DataEditorSetting> GetSettings(Type t)
		{
			Dictionary<string, DataEditorSetting> settings = new Dictionary<string, DataEditorSetting>();

			foreach (System.Reflection.PropertyInfo p in t.GetProperties())
			{

				object[] o = p.GetCustomAttributes(typeof(DataEditorSetting), true);

				if (o.Length > 0)
					settings.Add(p.Name, (DataEditorSetting)o[0]);
			}
			return settings;
		}

		private bool HasSettings(Type t)
		{
			bool hasSettings = false;
			foreach (System.Reflection.PropertyInfo p in t.GetProperties())
			{
				object[] o = p.GetCustomAttributes(typeof(DataEditorSetting), true);

				if (o.Length > 0)
				{
					hasSettings = true;
					break;
				}
			}

			return hasSettings;
		}

		private void LoadSetttings(string fileName)
		{
			if (System.IO.File.Exists(IOHelper.MapPath("~/" + fileName)))
			{
			  
				UserControl oControl = (UserControl)this.Page.LoadControl(@"~/" + fileName);

				Type type = oControl.GetType();


				Dictionary<string, DataEditorSetting> settings = GetSettings(type);

				foreach (KeyValuePair<string, DataEditorSetting> kv in settings)
				{
					DataEditorSettingType dst = kv.Value.GetDataEditorSettingType();
					dtSettings.Add(kv.Key, dst);

					DataEditorPropertyPanel panel = new DataEditorPropertyPanel();
					panel.Text = kv.Value.GetName();
					panel.Text += "<br/><small>" + kv.Value.description + "</small>";
<commit_msg>Fixed U4-1439 4.11.2 DataEditorSetting not working for UserControl wrapper.<commit_after>using System;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using umbraco.DataLayer;
using umbraco.BusinessLogic;

using umbraco.editorControls;
using umbraco.IO;
using System.Collections.Generic;
using umbraco.cms.businesslogic.datatype;

namespace umbraco.editorControls.userControlGrapper
{
	public class usercontrolPrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
	{
		public ISqlHelper SqlHelper
		{
			get { return Application.SqlHelper; }
		}

		#region IDataPrevalue Members

		// referenced datatype
		private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;

		private DropDownList _dropdownlist;
		private DropDownList _dropdownlistUserControl;
		private PlaceHolder _phSettings;

		private Dictionary<string, DataEditorSettingType> dtSettings = new Dictionary<string, DataEditorSettingType>();

		public usercontrolPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType) 
		{
			// state it knows its datatypedefinitionid
			_datatype = DataType;
			setupChildControls();

		}
		
		private void setupChildControls() 
		{
			_dropdownlist = new DropDownList();
			_dropdownlist.ID = "dbtype";
			_dropdownlist.Items.Add(DBTypes.Date.ToString());
			_dropdownlist.Items.Add(DBTypes.Integer.ToString());
			_dropdownlist.Items.Add(DBTypes.Ntext.ToString());
			_dropdownlist.Items.Add(DBTypes.Nvarchar.ToString());
			
			_dropdownlistUserControl = new DropDownList();
			_dropdownlistUserControl.ID = "usercontrol";

			_phSettings = new PlaceHolder();
			_phSettings.ID = "settings";

			// put the childcontrols in context - ensuring that
			// the viewstate is persisted etc.
			Controls.Add(_dropdownlist);
			Controls.Add(_dropdownlistUserControl);
			Controls.Add(_phSettings);

			// populate the usercontrol dropdown
			_dropdownlistUserControl.Items.Add(new ListItem(ui.Text("choose"), ""));
			populateUserControls( IOHelper.MapPath( SystemDirectories.Usercontrols) );

		   
		}

		private void populateUserControls(string path)
		{
			DirectoryInfo di = new DirectoryInfo(path);

			foreach (FileInfo uc in di.GetFiles("*.ascx"))
			{
                string ucRoot = IOHelper.MapPath(SystemDirectories.Usercontrols);

				_dropdownlistUserControl.Items.Add(
					
					new ListItem( SystemDirectories.Usercontrols +
                            uc.FullName.Substring(ucRoot.Length).Replace(IOHelper.DirSepChar, '/'))

					/*
					new ListItem( 
						uc.FullName.Substring( uc.FullName.IndexOf(root), uc.FullName.Length - uc.FullName.IndexOf(root)).Replace(IOHelper.DirSepChar, '/'))
					  */  
						);

			}
			foreach (DirectoryInfo dir in di.GetDirectories())
				populateUserControls(dir.FullName);
		}

		public Control Editor
		{
			get
			{
				return this;
			}
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!Page.IsPostBack)
			{
				string config = Configuration;
				if (config != "")
				{
					_dropdownlistUserControl.SelectedValue = config;

					
				}
				_dropdownlist.SelectedValue = _datatype.DBType.ToString();


				
			}

			//check for settings
			if (!string.IsNullOrEmpty(Configuration))
				LoadSetttings(Configuration);
		   

		}

		private Dictionary<string, DataEditorSetting> GetSettings(Type t)
		{
			Dictionary<string, DataEditorSetting> settings = new Dictionary<string, DataEditorSetting>();

			foreach (System.Reflection.PropertyInfo p in t.GetProperties())
			{

				object[] o = p.GetCustomAttributes(typeof(DataEditorSetting), true);

				if (o.Length > 0)
					settings.Add(p.Name, (DataEditorSetting)o[0]);
			}
			return settings;
		}

		private bool HasSettings(Type t)
		{
			bool hasSettings = false;
			foreach (System.Reflection.PropertyInfo p in t.GetProperties())
			{
				object[] o = p.GetCustomAttributes(typeof(DataEditorSetting), true);

				if (o.Length > 0)
				{
					hasSettings = true;
					break;
				}
			}

			return hasSettings;
		}

		private void LoadSetttings(string fileName)
		{
            // due to legacy, some user controls may not have the tilde start
            if (fileName.StartsWith("~/"))
                fileName = fileName.Substring(2);

			if (System.IO.File.Exists(IOHelper.MapPath("~/" + fileName)))
			{
			  
				UserControl oControl = (UserControl)this.Page.LoadControl(@"~/" + fileName);

				Type type = oControl.GetType();


				Dictionary<string, DataEditorSetting> settings = GetSettings(type);

				foreach (KeyValuePair<string, DataEditorSetting> kv in settings)
				{
					DataEditorSettingType dst = kv.Value.GetDataEditorSettingType();
					dtSettings.Add(kv.Key, dst);

					DataEditorPropertyPanel panel = new DataEditorPropertyPanel();
					panel.Text = kv.Value.GetName();
					panel.Text += "<br/><small>" + kv.Value.description + "</small>";

{'fraction_non_alphanumeric': 0.1083131067961165, 'fraction_numerical': 0.0016181229773462784, 'mean_word_length': 3.67565011820331, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7252032', 'n_tokens_mistral': 3590, 'n_tokens_neox': 2932, 'n_words': 580}