2009-06-19 07:39:16 +00:00
using System ;
using System.Collections ;
using System.Web.UI ;
using System.Web.UI.WebControls ;
2011-04-26 10:00:41 -02:00
using ClientDependency.Core ;
2009-06-19 07:39:16 +00:00
using umbraco.BusinessLogic ;
using umbraco.DataLayer ;
2010-11-18 14:56:47 +00:00
using System.Collections.Generic ;
2010-11-19 09:24:28 +00:00
using System.Web.UI.HtmlControls ;
2009-06-19 07:39:16 +00:00
2010-11-18 14:56:47 +00:00
2011-04-26 10:00:41 -02:00
2010-11-18 14:56:47 +00:00
[assembly: System.Web.UI.WebResource("umbraco.editorControls.KeyValuePrevalueEditor.js", "text/js")]
2010-11-19 09:24:28 +00:00
[assembly: System.Web.UI.WebResource("umbraco.editorControls.KeyValuePrevalueEditor.css", "text/css")]
2009-06-19 07:39:16 +00:00
namespace umbraco.editorControls
{
2016-11-25 22:18:35 +01:00
/// <summary>
/// Summary description for KeyValuePrevalueEditor.
/// </summary>
2011-04-26 10:00:41 -02:00
[ClientDependency(ClientDependencyType.Javascript, "Jeditable/jquery.jeditable.js", "UmbracoClient")]
2013-10-21 18:36:46 +11:00
[Obsolete("IDataType and all other references to the legacy property editors are no longer used this will be removed from the codebase in future versions")]
public class KeyValuePrevalueEditor : System . Web . UI . WebControls . PlaceHolder , interfaces . IDataPrevalue
2016-11-25 22:18:35 +01:00
{
// UI controls
public System . Web . UI . WebControls . DropDownList _dropdownlist ;
public TextBox _textbox ;
2010-11-18 14:56:47 +00:00
private TextBox _tbhidden ;
2009-06-19 07:39:16 +00:00
public umbraco . uicontrols . PropertyPanel pp1 = new umbraco . uicontrols . PropertyPanel ();
public umbraco . uicontrols . PropertyPanel pp2 = new umbraco . uicontrols . PropertyPanel ();
2016-11-25 22:18:35 +01:00
private Hashtable DeleteButtons = new Hashtable ();
2009-06-19 07:39:16 +00:00
2016-11-25 22:18:35 +01:00
// referenced datatype
private cms . businesslogic . datatype . BaseDataType _datatype ;
/// <summary>
/// Unused, please do not use
/// </summary>
[Obsolete("Obsolete, For querying the database use the new UmbracoDatabase object ApplicationContext.Current.DatabaseContext.Database", false)]
2009-06-19 07:39:16 +00:00
protected static ISqlHelper SqlHelper
{
get { return Application . SqlHelper ; }
}
2016-11-25 22:18:35 +01:00
public KeyValuePrevalueEditor ( cms . businesslogic . datatype . BaseDataType DataType )
{
// state it knows its datatypedefinitionid
_datatype = DataType ;
2009-06-19 07:39:16 +00:00
setupChildControls ();
2016-11-25 22:18:35 +01:00
2009-06-19 07:39:16 +00:00
// Bootstrap delete.
if ( System . Web . HttpContext . Current . Request [ "delete" ] != null ) {
2016-11-25 22:18:35 +01:00
DeletePrevalue ( int . Parse ( System . Web . HttpContext . Current . Request [ "delete" ]));
}
2009-06-19 07:39:16 +00:00
2016-11-25 22:18:35 +01:00
}
private void DeletePrevalue ( int id ) {
using ( var sqlHelper = Application . SqlHelper )
sqlHelper . ExecuteNonQuery ( "delete from cmsDataTypePreValues where id = " + id );
}
private void setupChildControls ()
{
_dropdownlist = new DropDownList ();
_dropdownlist . ID = "dbtype" ;
_textbox = new TextBox ();
_textbox . ID = "AddValue" ;
2009-06-19 07:39:16 +00:00
2010-11-18 14:56:47 +00:00
_tbhidden = new TextBox ();
_tbhidden . Attributes . Add ( "style" , "display:none;" );
_tbhidden . CssClass = "valuesHiddenInput" ;
2016-11-25 22:18:35 +01:00
// put the childcontrols in context - ensuring that
// the viewstate is persisted etc.
this . Controls . Add ( _dropdownlist );
this . Controls . Add ( _textbox );
2010-11-18 14:56:47 +00:00
this . Controls . Add ( _tbhidden );
2009-06-19 07:39:16 +00:00
2016-11-25 22:18:35 +01:00
_dropdownlist . Items . Add ( DBTypes . Date . ToString ());
_dropdownlist . Items . Add ( DBTypes . Integer . ToString ());
_dropdownlist . Items . Add ( DBTypes . Ntext . ToString ());
_dropdownlist . Items . Add ( DBTypes . Nvarchar . ToString ());
2009-06-19 07:39:16 +00:00
}
protected override void OnInit ( EventArgs e ) {
base . OnInit ( e );
2010-11-18 14:56:47 +00:00
System . Web . UI . Page page = ( System . Web . UI . Page ) System . Web . HttpContext . Current . Handler ;
page . ClientScript . RegisterClientScriptInclude (
"umbraco.editorControls.KeyValuePrevalueEditor.js" ,
page . ClientScript . GetWebResourceUrl ( typeof ( KeyValuePrevalueEditor ), "umbraco.editorControls.KeyValuePrevalueEditor.js" ));
2010-11-19 09:24:28 +00:00
HtmlHead head = ( HtmlHead ) page . Header ;
HtmlLink link = new HtmlLink ();
link . Attributes . Add ( "href" , page . ClientScript . GetWebResourceUrl ( typeof ( KeyValuePrevalueEditor ), "umbraco.editorControls.KeyValuePrevalueEditor.css" ));
link . Attributes . Add ( "type" , "text/css" );
link . Attributes . Add ( "rel" , "stylesheet" );
head . Controls . Add ( link );
2009-06-19 07:39:16 +00:00
}
2016-11-25 22:18:35 +01:00
protected override void OnLoad ( EventArgs e )
{
base . OnLoad ( e );
2009-06-19 07:39:16 +00:00
if (! Page . IsPostBack )
2016-11-25 22:18:35 +01:00
{
_dropdownlist . SelectedValue = _datatype . DBType . ToString ();
2009-06-19 07:39:16 +00:00
2016-11-25 22:18:35 +01:00
}
2009-06-19 07:39:16 +00:00
2016-11-25 22:18:35 +01:00
}
public Control Editor
{
get
{
return this ;
}
}
2009-06-19 07:39:16 +00:00
public void Save ()
{
_datatype . DBType = ( cms . businesslogic . datatype . DBTypes ) Enum . Parse ( typeof ( cms . businesslogic . datatype . DBTypes ), _dropdownlist . SelectedValue , true );
2010-11-18 14:56:47 +00:00
//changes in name and sortorder
if (! string . IsNullOrEmpty ( _tbhidden . Text ))
{
int so = 0 ;
2012-04-24 09:59:16 -02:00
foreach ( string row in _tbhidden . Text . Split ( '¶' ))
2010-11-18 14:56:47 +00:00
{
if (! string . IsNullOrEmpty ( row ))
{
int id = 0 ;
if ( row . Split ( '|' ). Length == 2 && int . TryParse ( row . Split ( '|' )[ 0 ], out id ) && row . Split ( '|' )[ 1 ]. Length > 0 )
{
2016-11-25 22:18:35 +01:00
using ( var sqlHelper = Application . SqlHelper )
{
IParameter [] SqlParams = new IParameter [] {
sqlHelper . CreateParameter ( "@value" , row . Split ( '|' )[ 1 ]),
sqlHelper . CreateParameter ( "@sortorder" , so ),
sqlHelper . CreateParameter ( "@id" , id )};
sqlHelper . ExecuteNonQuery ( "update cmsDataTypePreValues set [value] = @value, sortorder = @sortorder where id = @id" , SqlParams );
}
2010-11-18 14:56:47 +00:00
}
so ++;
}
}
_tbhidden . Text = "" ;
}
// If the add new prevalue textbox is filled out - add the value to the collection.
2009-06-19 07:39:16 +00:00
if ( _textbox . Text != "" )
{
2010-11-19 09:24:28 +00:00
int so = - 1 ;
try
{
2016-11-25 22:18:35 +01:00
using ( var sqlHelper = Application . SqlHelper )
so = sqlHelper . ExecuteScalar < int >( "select max(sortorder) from cmsDataTypePreValues where datatypenodeid = @dtdefid" ,
sqlHelper . CreateParameter ( "@dtdefid" , _datatype . DataTypeDefinitionId ));
2010-11-19 09:24:28 +00:00
so ++;
}
catch { }
2016-11-25 22:18:35 +01:00
using ( var sqlHelper = Application . SqlHelper )
{
IParameter [] SqlParams = new IParameter [] {
sqlHelper . CreateParameter ( "@value" , _textbox . Text ),
sqlHelper . CreateParameter ( "@dtdefid" , _datatype . DataTypeDefinitionId ),
sqlHelper . CreateParameter ( "@so" , so )};
sqlHelper . ExecuteNonQuery ( "insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,@so,'')" , SqlParams );
}
2009-06-19 07:39:16 +00:00
_textbox . Text = "" ;
ScriptManager . GetCurrent ( Page ). SetFocus ( _textbox );
}
}
2016-11-25 22:18:35 +01:00
protected override void Render ( HtmlTextWriter writer )
{
2009-06-19 07:39:16 +00:00
writer . Write ( "<div class='propertyItem'><div class='propertyItemheader'>" + ui . Text ( "dataBaseDatatype" ) + "</div>" );
_dropdownlist . RenderControl ( writer );
writer . Write ( "<br style='clear: both'/></div>" );
2010-11-18 14:56:47 +00:00
List < KeyValuePair < int , String >> _prevalues = PrevaluesAsKeyValuePairList ;
2009-06-19 07:39:16 +00:00
if ( _prevalues . Count > 0 ) {
2010-11-18 14:56:47 +00:00
writer . Write ( "<div class='propertyItem'><table style='width: 100%' id=\"prevalues\">" );
2010-11-19 09:24:28 +00:00
writer . Write ( "<tr class='header'><th style='width: 15%'>Text</th><td colspan='2'>Value</td></tr>" );
2009-06-19 07:39:16 +00:00
2010-11-18 14:56:47 +00:00
foreach ( KeyValuePair < int , String > item in _prevalues )
{
2011-08-03 07:49:08 -02:00
writer . Write ( "<tr class=\"row\"><td class=\"text\">" + item . Value + "</td><td class=\"value\"> " + item . Key . ToString () + "</td><td><a onclick='javascript:return ConfirmPrevalueDelete();' href='?id=" + _datatype . DataTypeDefinitionId + "&delete=" + item . Key . ToString () + "'>" + ui . Text ( "delete" ) + "</a> <span class=\"handle\" style=\"cursor:move\">sort<span></td></tr>" );
2009-06-19 07:39:16 +00:00
}
writer . Write ( "</table><br style='clear: both'/></div>" );
}
writer . Write ( "<div class='propertyItem'><div class='propertyItemheader'>" + ui . Text ( "addPrevalue" ) + "</div>" );
_textbox . RenderControl ( writer );
writer . Write ( "<br style='clear: both'/></div>" );
2010-11-18 14:56:47 +00:00
_tbhidden . RenderControl ( writer );
2016-11-25 22:18:35 +01:00
2009-06-19 07:39:16 +00:00
}
2016-11-25 22:18:35 +01:00
public SortedList Prevalues {
get
2009-06-19 07:39:16 +00:00
{
SortedList retval = new SortedList ();
2016-11-25 22:18:35 +01:00
using ( var sqlHelper = Application . SqlHelper )
using ( IRecordsReader dr = sqlHelper . ExecuteReader (
"Select id, [value] from cmsDataTypePreValues where DataTypeNodeId = "
+ _datatype . DataTypeDefinitionId + " order by sortorder" ))
{
while ( dr . Read ())
retval . Add ( dr . GetInt ( "id" ), dr . GetString ( "value" ));
return retval ;
}
}
}
2010-11-18 14:56:47 +00:00
public List < KeyValuePair < int , String >> PrevaluesAsKeyValuePairList
{
get
{
List < KeyValuePair < int , String >> items = new List < KeyValuePair < int , String >>();
2016-11-25 22:18:35 +01:00
using ( var sqlHelper = Application . SqlHelper )
{
using ( IRecordsReader dr = sqlHelper . ExecuteReader (
"Select id, [value] from cmsDataTypePreValues where DataTypeNodeId = "
+ _datatype . DataTypeDefinitionId + " order by sortorder" ))
{
while ( dr . Read ())
items . Add ( new KeyValuePair < int , string >( dr . GetInt ( "id" ), dr . GetString ( "value" )));
}
}
2010-11-18 14:56:47 +00:00
return items ;
}
}
2016-11-25 22:18:35 +01:00
}
2009-06-19 07:39:16 +00:00
}