2009-06-20 13:49:35 +00:00
using System ;
2012-10-25 10:14:24 -01:00
using System.Text ;
2009-06-20 13:49:35 +00:00
using System.Web.UI ;
using System.Web.UI.WebControls ;
2012-10-25 10:14:24 -01:00
using System.Xml ;
using umbraco.cms.businesslogic ;
using umbraco.cms.businesslogic.media ;
using umbraco.cms.businesslogic.member ;
2009-06-20 13:49:35 +00:00
using umbraco.cms.businesslogic.property ;
using umbraco.cms.businesslogic.web ;
namespace umbraco.editorControls.imagecropper
{
2013-09-25 19:03:05 +10: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")]
2009-06-20 13:49:35 +00:00
public class DataEditor : PlaceHolder , umbraco . interfaces . IDataEditor
{
private umbraco . interfaces . IData data ;
private Config config ;
private XmlDocument _xml ;
public Image imgImage = new Image ();
public HiddenField hdnJson = new HiddenField ();
public HiddenField hdnRaw = new HiddenField ();
public HiddenField hdnSer = new HiddenField ();
public DataEditor ( umbraco . interfaces . IData Data , string Configuration )
{
data = Data ;
config = new Config ( Configuration );
}
public virtual bool TreatAsRichTextEditor { get { return false ; } }
public bool ShowLabel { get { return config . ShowLabel ; } }
public Control Editor { get { return this ; } }
protected override void OnInit ( EventArgs e )
{
this . ID = "ImageCropper" ;
//base.OnInit(e);
2010-06-21 09:46:10 +00:00
int propertyId = (( umbraco . cms . businesslogic . datatype . DefaultData ) data ). PropertyId ;
2009-06-20 13:49:35 +00:00
int currentDocumentId = (( umbraco . cms . businesslogic . datatype . DefaultData ) data ). NodeId ;
2010-06-21 09:46:10 +00:00
Property uploadProperty ;
2009-06-20 13:49:35 +00:00
2010-06-21 09:46:10 +00:00
// we need this ugly code because there's no way to use a base class
CMSNode node = new CMSNode ( currentDocumentId );
if ( node . nodeObjectType == Document . _objectType )
{
2012-10-25 10:14:24 -01:00
uploadProperty = new Document ( currentDocumentId ). getProperty ( config . UploadPropertyAlias );
2010-06-21 09:46:10 +00:00
}
else if ( node . nodeObjectType == umbraco . cms . businesslogic . media . Media . _objectType )
{
2012-10-25 10:14:24 -01:00
uploadProperty = new Media ( currentDocumentId ). getProperty ( config . UploadPropertyAlias );
2010-06-21 09:46:10 +00:00
}
else if ( node . nodeObjectType == Member . _objectType )
{
2012-10-25 10:14:24 -01:00
uploadProperty = new Member ( currentDocumentId ). getProperty ( config . UploadPropertyAlias );
2010-06-21 09:46:10 +00:00
}
else
{
throw new Exception ( "Unsupported Umbraco Node type for Image Cropper (only Document, Media and Members are supported." );
}
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
// upload property could be null here if the property wasn't found
if ( uploadProperty != null )
2009-06-20 13:49:35 +00:00
{
2012-10-25 10:14:24 -01:00
string relativeImagePath = uploadProperty . Value . ToString ();
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
ImageInfo imageInfo = new ImageInfo ( relativeImagePath );
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
imgImage . ImageUrl = relativeImagePath ;
imgImage . ID = String . Format ( "cropBox_{0}" , propertyId );
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
StringBuilder sbJson = new StringBuilder ();
StringBuilder sbRaw = new StringBuilder ();
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
try
2009-06-20 13:49:35 +00:00
{
2012-10-25 10:14:24 -01:00
_xml = new XmlDocument ();
_xml . LoadXml ( data . Value . ToString ());
2009-06-20 13:49:35 +00:00
}
2012-10-25 10:14:24 -01:00
catch
2009-06-20 13:49:35 +00:00
{
2012-10-25 10:14:24 -01:00
_xml = createBaseXmlDocument ();
2009-06-20 13:49:35 +00:00
}
2012-10-25 10:14:24 -01:00
sbJson . Append ( "{ \"current\": 0, \"crops\": [" );
for ( int i = 0 ; i < config . presets . Count ; i ++)
2009-06-20 13:49:35 +00:00
{
2012-10-25 10:14:24 -01:00
Preset preset = ( Preset ) config . presets [ i ];
Crop crop ;
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
sbJson . Append ( "{\"name\":'" + preset . Name + "'" );
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
sbJson . Append ( ",\"config\":{" +
String . Format ( "\"targetWidth\":{0},\"targetHeight\":{1},\"keepAspect\":{2}" ,
preset . TargetWidth , preset . TargetHeight ,
( preset . KeepAspect ? "true" : "false" ) + "}" ));
2009-06-20 13:49:35 +00:00
2012-10-25 10:14:24 -01:00
if ( imageInfo . Exists )
2009-06-20 13:49:35 +00:00
{
2012-10-25 10:14:24 -01:00
crop = preset . Fit ( imageInfo );
}
else
{
crop . X = 0 ;
crop . Y = 0 ;
crop . X2 = preset . TargetWidth ;
crop . Y2 = preset . TargetHeight ;
}
// stored
if ( _xml . DocumentElement != null && _xml . DocumentElement . ChildNodes . Count == config . presets . Count )
{
XmlNode xmlNode = _xml . DocumentElement . ChildNodes [ i ];
int xml_x = Convert . ToInt32 ( xmlNode . Attributes [ "x" ]. Value );
int xml_y = Convert . ToInt32 ( xmlNode . Attributes [ "y" ]. Value );
int xml_x2 = Convert . ToInt32 ( xmlNode . Attributes [ "x2" ]. Value );
int xml_y2 = Convert . ToInt32 ( xmlNode . Attributes [ "y2" ]. Value );
// only use xml values if image is the same and different from defaults (document is stored inbetween image upload and cropping)
//if (xml_x2 - xml_x != preset.TargetWidth || xml_y2 - xml_y != preset.TargetHeight)
//fileDate == imageInfo.DateStamp && (
if ( crop . X != xml_x || crop . X2 != xml_x2 || crop . Y != xml_y || crop . Y2 != xml_y2 )
{
crop . X = xml_x ;
crop . Y = xml_y ;
crop . X2 = xml_x2 ;
crop . Y2 = xml_y2 ;
}
}
sbJson . Append ( ",\"value\":{" + String . Format ( "\"x\":{0},\"y\":{1},\"x2\":{2},\"y2\":{3}" , crop . X , crop . Y , crop . X2 , crop . Y2 ) + "}}" );
sbRaw . Append ( String . Format ( "{0},{1},{2},{3}" , crop . X , crop . Y , crop . X2 , crop . Y2 ));
if ( i < config . presets . Count - 1 )
{
sbJson . Append ( "," );
sbRaw . Append ( ";" );
2009-06-20 13:49:35 +00:00
}
}
2012-10-25 10:14:24 -01:00
sbJson . Append ( "]}" );
hdnJson . Value = sbJson . ToString ();
//hdnJson.ID = String.Format("json_{0}", propertyId);
hdnRaw . Value = sbRaw . ToString ();
//hdnRaw.ID = String.Format("raw_{0}", propertyId);
Controls . Add ( imgImage );
Controls . Add ( hdnJson );
Controls . Add ( hdnRaw );
string imageCropperInitScript =
"initImageCropper('" +
imgImage . ClientID + "', '" +
hdnJson . ClientID + "', '" +
hdnRaw . ClientID +
"');" ;
Page . ClientScript . RegisterStartupScript ( GetType (), ClientID + "_imageCropper" , imageCropperInitScript , true );
Page . ClientScript . RegisterClientScriptBlock ( Resources . json2Script . GetType (), "json2Script" , Resources . json2Script , true );
Page . ClientScript . RegisterClientScriptBlock ( Resources . jCropCSS . GetType (), "jCropCSS" , Resources . jCropCSS );
Page . ClientScript . RegisterClientScriptBlock ( Resources . jCropScript . GetType (), "jCropScript" , Resources . jCropScript , true );
Page . ClientScript . RegisterClientScriptBlock ( Resources . imageCropperScript . GetType (), "imageCropperScript" , Resources . imageCropperScript , true );
2009-06-20 13:49:35 +00:00
2010-06-21 09:46:10 +00:00
}
2009-06-20 13:49:35 +00:00
base . OnInit ( e );
}
/// <summary>
/// Store data as string XML (overridden by ToXMl to store "real" XML
/// XML format:
/// <crops dateStamp="">
/// <crop name="" x="" y="" x2="" y2="" url="" />
/// </crops>
/// </summary>
public void Save ()
{
ImageInfo imageInfo = new ImageInfo ( imgImage . ImageUrl );
if (! imageInfo . Exists )
{
data . Value = "" ;
}
else
{
SaveData saveData = new SaveData ( hdnRaw . Value );
data . Value = saveData . Xml ( config , imageInfo );
imageInfo . GenerateThumbnails ( saveData , config );
}
}
private static XmlDocument createBaseXmlDocument ()
{
XmlDocument doc = new XmlDocument ();
XmlNode root = doc . CreateElement ( "crops" );
doc . AppendChild ( root );
return doc ;
}
}
}