HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/RMourik/bassol.nl/CMS/CMSModules/Ecommerce/FormControls/SelectProductImage.ascx.cs
using System;
using System.Data;
using System.Web;

using CMS.Ecommerce;
using CMS.ExtendedControls;
using CMS.FormControls;
using CMS.Helpers;
using CMS.Base;
using CMS.SiteProvider;
using CMS.Membership;
using CMS.DataEngine;

public partial class CMSModules_Ecommerce_FormControls_SelectProductImage : FormEngineUserControl
{
    string allowedExtensions = null;

    private int mSKUID = 0;
    private int mSiteID = -1;


    #region "Properties"

    /// <summary>
    /// Gets or sets the SKU ID.
    /// </summary>
    public int SKUID
    {
        get
        {
            return (mSKUID > 0) ? mSKUID : 0;
        }
        set
        {
            mSKUID = value;
        }
    }


    /// <summary>
    /// Gets or sets the site ID.
    /// Default value is current site ID.
    /// </summary>
    public int SiteID
    {
        get
        {
            return (mSiteID >= 0) ? mSiteID : SiteContext.CurrentSiteID;
        }
        set
        {
            mSiteID = value;
        }
    }


    /// <summary>
    /// Gets type of ECommerce object (SKU or SKUOption)
    /// </summary>
    public string SKUObjectType
    {
        get;
        set;
    }


    /// <summary>
    /// Gets or sets the product image path as a string.
    /// </summary>
    public override object Value
    {
        get
        {
            return UseMetaFile ? MetaFilePath : ImageSelectorPath;
        }
        set
        {
            if (!UseMetaFile)
            {
                ImageSelectorPath = ValidationHelper.GetString(value, null);
            }
        }
    }


    /// <summary>
    /// Gets or sets the enabled state of the control.
    /// </summary>
    public override bool Enabled
    {
        get
        {
            return base.Enabled;
        }
        set
        {
            base.Enabled = value;
            metaFileElem.Enabled = value;
            imageSelectorElem.Enabled = value;
        }
    }


    /// <summary>
    /// Gets the image path from the meta file control.
    /// </summary>
    private string MetaFilePath
    {
        get
        {
            string path = null;
            DataSet metaFiles = MetaFileInfoProvider.GetMetaFiles(SKUID, SKUObjectType, ObjectAttachmentsCategories.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;
        }
    }


    /// <summary>
    /// Gets or sets the image path of the image selector control.
    /// </summary>
    private string ImageSelectorPath
    {
        get
        {
            return imageSelectorElem.Value;
        }
        set
        {
            imageSelectorElem.Value = value;
        }
    }


    /// <summary>
    /// Gets or sets the value that indicates if the information and error messages are display by the control itself.
    /// </summary>
    public bool ShowMessages
    {
        get
        {
            return plcMessages.Visible;
        }
        set
        {
            plcMessages.Visible = value;
        }
    }


    /// <summary>
    /// Gets a messages placeholder control.
    /// </summary>
    public override MessagesPlaceHolder MessagesPlaceHolder
    {
        get
        {
            if ((Form != null) && (Form.MessagesPlaceHolder != null))
            {
                return Form.MessagesPlaceHolder;
            }

            return plcMessages;
        }
    }


    /// <summary>
    /// Gets the value that indicates if meta file is used for the product image or not.
    /// </summary>
    private bool UseMetaFile
    {
        get
        {
            return ECommerceSettings.UseMetaFileForProductImage;
        }
    }

    #endregion


    #region "Lifecycle"

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        TryInitByForm();

        if (SiteID == 0)
        {
            allowedExtensions = SettingsKeyInfoProvider.GetValue("CMSUploadExtensions");
        }
        else if (SiteID > 0)
        {
            allowedExtensions = SettingsKeyInfoProvider.GetValue(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"], SKUInfo.OBJECT_TYPE_SKU);
        }
    }


    private void InitMetaFileControl()
    {
        metaFileElem.Visible = true;
        metaFileElem.ObjectID = SKUID;
        metaFileElem.ObjectType = SKUObjectType;
        metaFileElem.Category = ObjectAttachmentsCategories.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"

    /// <summary>
    /// Validates the form control and returns true if it is valid, otherwise sets the validation error message and returns false.
    /// </summary>
    public override bool IsValid()
    {
        if (UseMetaFile && !metaFileElem.IsValid())
        {
            ValidationError = metaFileElem.ValidationError;
            return false;
        }

        return true;
    }


    /// <summary>
    /// 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.
    /// </summary>
    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;

        return (metaFile == null) ? 0 : metaFile.MetaFileID;
    }


    /// <summary>
    /// Updates the product image path.
    /// Returns true if the path was updated successfully, otherwise returns false.
    /// </summary>
    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();
                return true;
            }
        }

        SKUInfo sku = SKUInfoProvider.GetSKUInfo(SKUID);
        if (sku != null)
        {
            sku.SKUImagePath = path;
            SKUInfoProvider.SetSKUInfo(sku);
            return true;
        }

        return false;
    }

    #endregion
}