using System; using System.Data; using System.Web; using CMS.CMSHelper; using CMS.Ecommerce; using CMS.ExtendedControls; using CMS.FormControls; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; public partial class CMSModules_Ecommerce_FormControls_SelectProductImage : FormEngineUserControl { string allowedExtensions = null; private int mSKUID = 0; private int mSiteID = -1; #region "Properties" /// /// Gets or sets the SKU ID. /// public int SKUID { get { if (mSKUID > 0) { return mSKUID; } return 0; } set { mSKUID = value; } } /// /// Gets or sets the site ID. /// Default value is current site ID. /// public int SiteID { get { if (mSiteID >= 0) { return mSiteID; } return CMSContext.CurrentSiteID; } set { mSiteID = value; } } /// /// Gets or sets the product image path as a string. /// public override object Value { get { return UseMetaFile ? MetaFilePath : ImageSelectorPath; } set { if (!UseMetaFile) { ImageSelectorPath = ValidationHelper.GetString(value, null); } } } /// /// Gets or sets the enabled state of the control. /// public override bool Enabled { get { return base.Enabled; } set { base.Enabled = value; metaFileElem.Enabled = value; imageSelectorElem.Enabled = value; } } /// /// Gets the image path from the meta file control. /// private string MetaFilePath { get { string path = null; DataSet metaFiles = MetaFileInfoProvider.GetMetaFiles(SKUID, SKUObjectType, MetaFileInfoProvider.OBJECT_CATEGORY_IMAGE, null, null); if (!DataHelper.DataSourceIsEmpty(metaFiles)) { MetaFileInfo metaFile = new MetaFileInfo(metaFiles.Tables[0].Rows[0]); path = MetaFileInfoProvider.GetMetaFileUrl(metaFile.MetaFileGUID, metaFile.MetaFileName); } return path; } } /// /// Gets or sets the image path of the image selector control. /// private string ImageSelectorPath { get { return imageSelectorElem.Value; } set { imageSelectorElem.Value = value; } } /// /// Gets or sets the value that indicates if the information and error messages are display by the control itself. /// public bool ShowMessages { get { return plcMessages.Visible; } set { plcMessages.Visible = value; } } /// /// Gets a messages placeholder control. /// public override MessagesPlaceHolder MessagesPlaceHolder { get { if ((Form != null) && (Form.MessagesPlaceHolder != null)) { return Form.MessagesPlaceHolder; } return plcMessages; } } /// /// Gets the value that indicates if meta file is used for the product image or not. /// private bool UseMetaFile { get { return ECommerceSettings.UseMetaFileForProductImage; } } /// /// Gets type of ECommerce object (SKU or SKUOption). /// public string SKUObjectType { get; set; } #endregion #region "Lifecycle" protected override void OnLoad(EventArgs e) { base.OnLoad(e); TryInitByForm(); if (SiteID == 0) { allowedExtensions = SettingsKeyProvider.GetStringValue("CMSUploadExtensions"); } else if (SiteID > 0) { allowedExtensions = SettingsKeyProvider.GetStringValue(SiteInfoProvider.GetSiteName(SiteID) + ".CMSUploadExtensions"); } if (UseMetaFile) { InitMetaFileControl(); } else { InitImageSelectorControl(); } } #endregion #region "Initialization" private void TryInitByForm() { if (Form == null) { return; } if (Form.Data.ContainsColumn("SKUSiteID")) { SiteID = ValidationHelper.GetInteger(Form.Data.GetValue("SKUSiteID"), 0); } if (Form.Data.ContainsColumn("SKUID")) { SKUID = ValidationHelper.GetInteger(Form.Data.GetValue("SKUID"), 0); } if (Form.AdditionalData.ContainsKey("IsInCompare")) { Enabled = !ValidationHelper.GetBoolean(Form.AdditionalData["IsInCompare"], true); } if (Form.AdditionalData.ContainsKey("SKUObjectType")) { SKUObjectType = ValidationHelper.GetString(Form.AdditionalData["SKUObjectType"], ECommerceObjectType.SKU); } } private void InitMetaFileControl() { metaFileElem.Visible = true; metaFileElem.ObjectID = SKUID; metaFileElem.ObjectType = SKUObjectType; metaFileElem.Category = MetaFileInfoProvider.OBJECT_CATEGORY_IMAGE; metaFileElem.SiteID = SiteID; metaFileElem.AllowedExtensions = allowedExtensions; metaFileElem.OnAfterUpload += delegate(object sender, EventArgs args) { bool result = UpdateProductImagePath(); if (result) { ShowChangesSaved(); } }; metaFileElem.OnAfterDelete += delegate(object sender, EventArgs args) { bool result = UpdateProductImagePath(); if (result) { ShowChangesSaved(); } }; if (Form != null) { Form.OnUploadFile += delegate(object sender, EventArgs args) { TryInitByForm(); int metaFileId = UploadImageForNewProduct(); if (metaFileId > 0) { UpdateProductImagePath(); ShowChangesSaved(); } }; } } private void InitImageSelectorControl() { imageSelectorElem.Visible = true; imageSelectorElem.Value = ValidationHelper.GetString(Value, null); } #endregion #region "Save" /// /// Validates the form control and returns true if it is valid, otherwise sets the validation error message and returns false. /// public override bool IsValid() { if (UseMetaFile && !metaFileElem.IsValid()) { ValidationError = metaFileElem.ValidationError; return false; } return true; } /// /// Uploads the product image meta file for a new product. /// Returns the ID of the uploaded meta file if it was uploaded successfully, otherwise returns 0. /// private int UploadImageForNewProduct() { if (SKUID == 0) { // SKU does not exist return 0; } HttpPostedFile file = metaFileElem.PostedFile; if ((file == null) || (file.ContentLength == 0)) { // No file was posted return 0; } metaFileElem.ObjectID = SKUID; metaFileElem.UploadFile(); MetaFileInfo metaFile = metaFileElem.CurrentlyHandledMetaFile; if (metaFile == null) { // The file was not uploaded return 0; } return metaFile.MetaFileID; } /// /// Updates the product image path. /// Returns true if the path was updated successfully, otherwise returns false. /// private bool UpdateProductImagePath() { var path = ValidationHelper.GetString(Value, null); if (Form != null) { var node = Form.Data as SKUTreeNode; if (node != null) { node.SetValue("SKUImagePath", path); node.Update(true); return true; } } SKUInfo sku = SKUInfoProvider.GetSKUInfo(SKUID); if (sku != null) { sku.SKUImagePath = path; SKUInfoProvider.SetSKUInfo(sku); return true; } return false; } #endregion }