using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using CMS.UIControls; using CMS.Controls; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.CMSHelper; using CMS.ExtendedControls; using CMS.PortalEngine; using CMS.FormEngine; using CMS.PortalControls; using CMS.DocumentEngine; public partial class CMSModules_PortalEngine_Controls_Editable_EditableImage : CMSUserControl { #region "Variables" protected const int NOT_KOWN = -1; protected XmlData mImageAutoResize = null; protected int mResizeToWidth = 0; protected int mResizeToHeight = 0; protected int mResizeToMaxSideSize = 0; protected bool mDimensionsLoaded = false; protected ISimpleDataContainer mIDataControl = null; protected IPageManager mIPageManager = null; protected ViewModeEnum? mViewMode = null; protected PageInfo mCurrentPageInfo = null; private string mEditPageUrl = "~/CMSModules/PortalEngine/UI/OnSiteEdit/EditImage.aspx"; private string mEditDialogWidth = "80%"; #endregion #region "Controls" /// /// Image. /// protected Image imgImage = null; /// /// Region title. /// protected Label lblTitle = null; /// /// Error label. /// protected Label lblError = null; /// /// Region panel. /// protected Panel pnlEditor = null; /// /// Image selector. /// protected ImageSelector selPath = null; /// /// Image title. /// protected string mImageTitle = null; #endregion #region "Public properties" /// /// ID of the control content /// public string ContentID { get; set; } /// /// Gets the url of the page which ensures editing of the web part's editable content in the On-Site editing mode. /// public string EditPageUrl { get { return URLHelper.ResolveUrl(mEditPageUrl); } } /// /// Gets the width of the edit dialog in the On-Site editing mode. /// public string EditDialogWidth { get { return mEditDialogWidth; } } /// /// If set, only published content is displayed on a live site. /// public bool SelectOnlyPublished { get { return ValidationHelper.GetBoolean(GetValue("SelectOnlyPublished"), false); } set { SetValue("SelectOnlyPublished", value); } } /// /// Page mode of the current web part. /// public ViewModeEnum ViewMode { get { if (mViewMode == null) { if (PageManager != null) { return PageManager.ViewMode; } return CMSContext.ViewMode; } return mViewMode.Value; } set { mViewMode = value; } } /// /// Configuration of the dialog for inserting Images. /// public DialogConfiguration DialogConfig { get { return selPath.DialogConfig; } set { selPath.DialogConfig = value; } } /// /// Gets or sets the title of the image region which is displayed in the EDIT mode. /// public string ImageTitle { get { string title = ValidationHelper.GetString(GetValue("ImageTitle"), String.Empty); if (String.IsNullOrEmpty(title)) { title = ValidationHelper.GetString(GetValue("WebPartTitle"), String.Empty); if (String.IsNullOrEmpty(title)) { title = ValidationHelper.GetString(GetValue("ControlID"), String.Empty); } } return title; } set { SetValue("ImageTitle", value); } } /// /// Gets or sets the image width. /// public int ImageWidth { get { return ValidationHelper.GetInteger(GetValue("ImageWidth"), 0); } set { SetValue("ImageWidth", value); } } /// /// Gets or sets the image height. /// public int ImageHeight { get { return ValidationHelper.GetInteger(GetValue("ImageHeight"), 0); } set { SetValue("ImageHeight", value); } } /// /// Gets or sets the alternative image text (ALT tag of the image). /// public string AlternateText { get { return ValidationHelper.GetString(GetValue("AlternateText"), ""); } set { SetValue("AlternateText", value); } } /// /// Gets or sets the value that indicates whether the path to file is displayed in EDIT mode. /// public bool DisplaySelectorTextBox { get { return ValidationHelper.GetBoolean(GetValue("DisplaySelectorTextBox"), selPath.ImagePathTextBox.Visible); } set { SetValue("DisplaySelectorTextBox", value); } } /// /// Gets or sets the name of the css class applied to the image. /// public string ImageCssClass { get { return ValidationHelper.GetString(GetValue("ImageCssClass"), ""); } set { SetValue("ImageCssClass", value); } } /// /// Gets or sets the style tag of the image. /// public string ImageStyle { get { return ValidationHelper.GetString(GetValue("ImageStyle"), ""); } set { SetValue("ImageStyle", value); } } /// /// Gets or sets the value that indicates whether the permissions are checked. /// public bool CheckPermissions { get { return ValidationHelper.GetBoolean(GetValue("CheckPermissions"), false); } set { SetValue("CheckPermissions", value); } } /// /// Width the image should be automatically resized to after it is uploaded. /// public int ResizeToWidth { get { if (!mDimensionsLoaded) { // Use image auto resize settings Hashtable settings = ImageAutoResize.ConvertToHashtable(); ImageHelper.GetAutoResizeDimensions(settings, CMSContext.CurrentSiteName, out mResizeToWidth, out mResizeToHeight, out mResizeToMaxSideSize); mDimensionsLoaded = true; } return mResizeToWidth; } set { mResizeToWidth = value; mDimensionsLoaded = true; } } /// /// Height the image should be automatically resized to after it is uploaded. /// public int ResizeToHeight { get { if (!mDimensionsLoaded) { // Use image auto resize settings Hashtable settings = ImageAutoResize.ConvertToHashtable(); ImageHelper.GetAutoResizeDimensions(settings, CMSContext.CurrentSiteName, out mResizeToWidth, out mResizeToHeight, out mResizeToMaxSideSize); mDimensionsLoaded = true; } return mResizeToHeight; } set { mResizeToHeight = value; mDimensionsLoaded = true; } } /// /// Max side size the image should be automatically resized to after it is uploaded. /// public int ResizeToMaxSideSize { get { if (!mDimensionsLoaded) { // Use image auto resize settings Hashtable settings = ImageAutoResize.ConvertToHashtable(); ImageHelper.GetAutoResizeDimensions(settings, CMSContext.CurrentSiteName, out mResizeToWidth, out mResizeToHeight, out mResizeToMaxSideSize); mDimensionsLoaded = true; } return mResizeToMaxSideSize; } set { mResizeToMaxSideSize = value; mDimensionsLoaded = true; } } /// /// Default image displayed when no image is selected. /// public string DefaultImage { get { return ValidationHelper.GetString(GetValue("DefaultImage"), ""); } set { SetValue("DefaultImage", value); } } /// /// Gets or sets the IDataControl to get/set values /// public ISimpleDataContainer DataControl { get { if (mIDataControl == null) { mIDataControl = ControlsHelper.GetParentControl(this, typeof(ISimpleDataContainer)) as ISimpleDataContainer; if (mIDataControl == null) { mIDataControl = this as ISimpleDataContainer; } } return mIDataControl; } set { mIDataControl = value; } } /// /// Parent page manager. /// public IPageManager PageManager { get { return mIPageManager; } set { mIPageManager = value; } } /// /// Current page info /// public new PageInfo CurrentPageInfo { get { if ((mCurrentPageInfo == null) && (PagePlaceholder != null)) { mCurrentPageInfo = PagePlaceholder.PageInfo; } return mCurrentPageInfo; } set { mCurrentPageInfo = value; } } /// /// Page place holder /// public CMSPagePlaceholder PagePlaceholder { get; set; } #endregion #region "Private properties" /// /// Auto resize configuration for the image. /// private XmlData ImageAutoResize { get { if (mImageAutoResize == null) { mImageAutoResize = new XmlData("AutoResize"); mImageAutoResize.LoadData(ValidationHelper.GetString(GetValue("ImageAutoResize"), "")); } return mImageAutoResize; } } #endregion /// /// Initializes the control properties. /// public void SetupControl() { // Do not hide for roles in edit or preview mode switch (ViewMode) { case ViewModeEnum.Edit: case ViewModeEnum.EditLive: case ViewModeEnum.EditDisabled: case ViewModeEnum.Design: case ViewModeEnum.DesignDisabled: case ViewModeEnum.EditNotCurrent: case ViewModeEnum.Preview: SetValue("DisplayToRoles", String.Empty); break; } } /// /// Overridden CreateChildControls method. /// protected override void CreateChildControls() { Controls.Clear(); base.CreateChildControls(); if (!StopProcessing) { if (!CMSAbstractEditableControl.RequestEditViewMode(ViewMode, ContentID)) { ViewMode = ViewModeEnum.Preview; } // Create controls by actual page mode switch (ViewMode) { case ViewModeEnum.Edit: case ViewModeEnum.EditDisabled: { // Main editor panel pnlEditor = new Panel(); pnlEditor.ID = "pnlEditor"; pnlEditor.CssClass = "EditableImageEdit EditableImage_" + ContentID; if (ImageWidth > 0) { pnlEditor.Style.Add(HtmlTextWriterStyle.Width, ImageWidth.ToString() + "px;"); //this.pnlEditor.Width = new Unit(this.DialogWidth); // Causes Invalid cast on Render } Controls.Add(pnlEditor); // Title label lblTitle = new Label(); lblTitle.EnableViewState = false; lblTitle.CssClass = "EditableTextTitle"; pnlEditor.Controls.Add(lblTitle); // Error label lblError = new Label(); lblError.EnableViewState = false; lblError.CssClass = "EditableTextError"; pnlEditor.Controls.Add(lblError); // Add image selector selPath = new ImageSelector(null, true); selPath.Culture = CMSContext.CurrentUser.PreferredUICultureCode; selPath.EnableOpenInFull = false; selPath.ID = "selPath"; selPath.UseImagePath = true; selPath.ImageCssClass = ImageCssClass; selPath.ImageStyle = ImageStyle; selPath.ShowTextBox = DisplaySelectorTextBox; selPath.DefaultValue = DefaultImage; // Dialog configuration selPath.DialogConfig.ResizeToHeight = ResizeToHeight; selPath.DialogConfig.ResizeToWidth = ResizeToWidth; selPath.DialogConfig.ResizeToMaxSideSize = ResizeToMaxSideSize; pnlEditor.Controls.Add(selPath); selPath.Enabled = (ViewMode == ViewModeEnum.Edit); selPath.IsLiveSite = (ViewMode == ViewModeEnum.LiveSite); } break; default: { // Display content in non editing modes imgImage = new Image(); imgImage.ID = "imgImage"; imgImage.GenerateEmptyAlternateText = true; if (ImageCssClass != "") { imgImage.CssClass = ImageCssClass; } if (ImageStyle != "") { imgImage.Attributes.Add("style", ImageStyle); } imgImage.AlternateText = AlternateText; imgImage.ToolTip = AlternateText; imgImage.EnableViewState = false; Controls.Add(imgImage); } break; } } } /// /// Loads the control content. /// /// Content to load /// If true, the content is forced to reload public void LoadContent(string content, bool forceReload) { if (!StopProcessing) { // Load the properties EnsureChildControls(); string path = null; string altText = null; // Load the image data if (!string.IsNullOrEmpty(content)) { XmlDocument xml = new XmlDocument(); xml.LoadXml(content); XmlNodeList properties = xml.SelectNodes("image/property"); if (properties != null) { foreach (XmlNode node in properties) { if (node.Attributes["name"] != null) { switch (node.Attributes["name"].Value.ToLowerCSafe()) { case "imagepath": path = ResolveUrl(node.InnerText.Trim()); break; case "alttext": altText = node.InnerText.Trim(); break; } } } } } else { // Ensure correct url from media selector path = Server.HtmlDecode(DefaultImage); } switch (ViewMode) { case ViewModeEnum.Edit: case ViewModeEnum.EditDisabled: // Force image width if (ImageWidth > 0) { selPath.ImageWidth = ImageWidth; selPath.ImagePreviewControl.Width = ImageWidth; } // Force image height if (ImageHeight > 0) { selPath.ImageHeight = ImageHeight; selPath.ImagePreviewControl.Height = ImageHeight; } // Initialize selected value if (forceReload || !RequestHelper.IsPostBack()) { selPath.Value = path; selPath.AlternateText = altText; } // Set image title if (ImageTitle != "") { lblTitle.Text = HTMLHelper.HTMLEncode(ImageTitle); } else { lblTitle.Visible = false; } break; default: Visible = true; if (string.IsNullOrEmpty(path)) { Visible = false; return; } // Force image width if (ImageWidth > 0) { imgImage.Width = ImageWidth; path = URLHelper.AddParameterToUrl(path, "width", ImageWidth.ToString()); } // Force image height if (ImageHeight > 0) { imgImage.Height = ImageHeight; path = URLHelper.AddParameterToUrl(path, "height", ImageHeight.ToString()); } // Use specific alternate text or default alternate text imgImage.AlternateText = String.IsNullOrEmpty(altText) ? AlternateText : altText; // Check authorization bool isAuthorized = true; if (CheckPermissions) { isAuthorized = PageManager.IsAuthorized; } // Only published if ((CMSContext.ViewMode != ViewModeEnum.LiveSite) || !SelectOnlyPublished || ((CurrentPageInfo != null) && CurrentPageInfo.IsPublished)) { if (isAuthorized) { imgImage.ImageUrl = path; } else { imgImage.Visible = false; } } else { imgImage.Visible = false; } break; } } } /// /// Gets the current control content. /// public string GetContent() { if (!StopProcessing) { EnsureChildControls(); switch (ViewMode) { case ViewModeEnum.Edit: case ViewModeEnum.EditDisabled: string path = URLHelper.UnResolveUrl(selPath.Value.Trim(), URLHelper.ApplicationPath); if (!string.IsNullOrEmpty(path)) { XmlDocument xml = new XmlDocument(); xml.LoadXml(""); // Add path XmlNode newNode = xml.CreateElement("property"); XmlAttribute attr = xml.CreateAttribute("name"); attr.Value = "imagepath"; newNode.Attributes.Append(attr); newNode.InnerText = path; if (xml.DocumentElement != null) { xml.DocumentElement.AppendChild(newNode); } // Image specific alternate text string altText = selPath.AlternateText; if (!String.IsNullOrEmpty(altText)) { // Add alternate text if defined newNode = xml.CreateElement("property"); attr = xml.CreateAttribute("name"); attr.Value = "alttext"; newNode.Attributes.Append(attr); newNode.InnerText = altText; if (xml.DocumentElement != null) { xml.DocumentElement.AppendChild(newNode); } } return xml.OuterXml; } else { return string.Empty; } } } return null; } /// /// PreRender event handler. /// private void Page_PreRender(object sender, EventArgs e) { if (!StopProcessing) { ViewModeEnum viewMode = (CMSContext.ViewMode == ViewModeEnum.Preview) ? ViewModeEnum.Preview : ViewMode; switch (ViewMode) { case ViewModeEnum.Edit: case ViewModeEnum.EditDisabled: // Set enabled if (selPath != null) { selPath.Enabled = (ViewMode == ViewModeEnum.Edit); } if (lblError != null) { lblError.Visible = (lblError.Text != ""); } lblTitle.Text = HTMLHelper.HTMLEncode(ImageTitle); break; } } } /// /// Returns the value of the given web part property property. /// /// Property name public override object GetValue(string propertyName) { if (DataControl != null) { return DataControl.GetValue(propertyName); } return base.GetValue(propertyName); } /// /// Sets the property value of the control, setting the value affects only local property value. /// /// Property name to set /// New property value public override void SetValue(string propertyName, object value) { if (DataControl != null) { DataControl.SetValue(propertyName, value); } base.SetValue(propertyName, value); } }