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/TDijk1/erp-apps.eu/wwwroot/ERPApps/ERPWebParts/Documents/AdsRepeater.ascx.cs
using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using CMS.GlobalHelper;
using CMS.PortalControls;
using CMS.TreeEngine;
using CMS.DocumentEngine;
using CMS.Helpers;
using CMS.DataEngine;
using ERP.Application;

public partial class ERPApps_ERPWebParts_Documents_AdsRepeater : CMSAbstractWebPart
{

    #region "Properties"

    /// <summary>
    /// Document type name for Ads documents.
    /// Default value is ERP.Ad
    /// </summary>
    public string AdsDocumentType
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("AdsDocumentType"), ERPConfig.DOCTYPE_AD);
        }
        set
        {
            this.SetValue("AdsDocumentType", value);
        }
    }

    /// <summary>
    /// Number of ads to be displayed. Default is five (5).
    /// </summary>
    public int Limit
    {
        get
        {
            return ValidationHelper.GetInteger(this.GetValue("Limit"), 5);
        }
        set
        {
            this.SetValue("Limit", value);
        }
    }

    ///// <summary>
    ///// Type of ads to be displayed. Can contain one of values "home, industry, process, platform".
    ///// Default is "home".
    ///// </summary>
    //public int AdsLocation
    //{
    //    get
    //    {
    //        return ValidationHelper.GetInteger(this.GetValue("AdsLocation"), 0);
    //    }
    //    set
    //    {
    //        this.SetValue("AdsLocation", value);
    //    }
    //}

    /// <summary>
    /// Selected industry, from which the ads should be selected.
    /// Default is 0.
    /// </summary>
    public int WhereIndustry
    {
        get
        {
            return ValidationHelper.GetInteger(this.GetValue("WhereIndustry"), 0);
        }
        set
        {
            this.SetValue("WhereIndustry", value);
        }
    }

    /// <summary>
    /// Selected process, from which the ads should be selected.
    /// Default is 0.
    /// </summary>
    public int WhereProcess
    {
        get
        {
            return ValidationHelper.GetInteger(this.GetValue("WhereProcess"), 0);
        }
        set
        {
            this.SetValue("WhereProcess", value);
        }
    }

    /// <summary>
    /// Selected platform, from which the ads should be selected.
    /// Default is 0.
    /// </summary>
    public int WherePlatform
    {
        get
        {
            return ValidationHelper.GetInteger(this.GetValue("WherePlatform"), 0);
        }
        set
        {
            this.SetValue("WherePlatform", value);
        }
    }

    /// <summary>
    /// Application which should be used as a source for determining platform, process and industry.
    /// Random ads from these categories are displayed.
    /// Default is 0.
    /// </summary>
    public int WhereApplication
    {
        get
        {
            return ValidationHelper.GetInteger(this.GetValue("WhereApplication"), 0);
        }
        set
        {
            this.SetValue("WhereApplication", value);
        }
    }

    /// <summary>
    /// Order of ads.
    /// Default is random "NEWID()".
    /// </summary>
    public string OrderBy
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("OrderBy"), "NEWID()");
        }
        set
        {
            this.SetValue("OrderBy", value);
        }
    }

    /// <summary>
    /// Path in the document tree where the documents should be selected from.
    /// Default is "/%".
    /// </summary>
    public string Path
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("Path"), "/%");
        }
        set
        {
            this.SetValue("Path", value);
        }
    }

    /// <summary>
    /// Name of transformation to be used to display the ads.
    /// Default is "ERP.Ad.Preview".
    /// </summary>
    public string TransformationName
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("TransformationName"), ERPConfig.DOCTYPE_AD + ".Preview");
        }
        set
        {
            this.SetValue("TransformationName", value);
        }
    }

    /// <summary>
    /// Content displayed if no ad is found.
    /// </summary>
    public string ZeroRowText
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("ZeroRowText"), "");
        }
        set
        {
            this.SetValue("ZeroRowText", value);
        }
    }

    #endregion

    #region "Methods"
    protected void SetupControl()
    {
        // context
        repItems.ControlContext = ControlContext;

        // documents filter
        repItems.ClassNames = AdsDocumentType;
        repItems.TransformationName = TransformationName;

        // query filter
        repItems.TopN = Limit;
        repItems.Path = Path.Trim();
        repItems.OrderBy = OrderBy;
        repItems.SelectOnlyPublished = true;
        repItems.WhereCondition = GetWhereCondition();

        repItems.HideControlForZeroRows = DataHelper.IsEmpty(ZeroRowText);
        repItems.ZeroRowsText = ZeroRowText;
    }

    /// <summary>
    /// Prepares SQL WHERE condition for selecting ads based on set properties.
    /// </summary>
    /// <returns></returns>
    protected string GetWhereCondition()
    {
        var where = new WhereCondition().WhereTrue("AdIsPublished");

        if ( WhereIndustry > 0 )
        {
            where.WhereEquals("AdIndustrySector", WhereIndustry);
        }

        if ( WhereProcess > 0 )
        {
            where.WhereEquals("AdProcessGroup", WhereProcess);
        }

        if ( WherePlatform > 0 )
        {
            where.WhereEquals("AdPlatformType", WherePlatform);
        }

        if ( WhereApplication > 0 )
        {
            var appWhere = GetWhereConditionByApplication();
            if ( !appWhere.WhereIsEmpty )
            {
                where.And(appWhere);
            }
        }

        return where.ToString(true);
    }

    /// <summary>
    /// Prepares SQL WHERE condition in case of application condition
    /// </summary>
    /// <returns></returns>
    private WhereCondition GetWhereConditionByApplication()
    {
        // filter by application = mix of all combinations     
        
        var appWhere = new WhereCondition();

        // select app details
        var appNode = ApplicationTreeNodeProvider.GetApplications().WhereEquals("Appid", WhereApplication).FirstObject;
        if ( appNode != null )
        {
            // industries - convert string value of "|X|Y|Z|" to array of [X,Y,Z]
            if (!string.IsNullOrEmpty(appNode.AppIndustrySectors))
            {
                string[] industries = appNode.AppIndustrySectors.Split(new[] { "|" },StringSplitOptions.RemoveEmptyEntries);
                if ( industries.Length > 0 )
                {
                    appWhere.WhereIn("AdIndustrySector", industries);
                }
            }

            // processes
            if (!string.IsNullOrEmpty(appNode.AppProcessGroups))
            {
                string[] processes = appNode.AppProcessGroups.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                if ( processes.Length > 0 )
                {
                    if (appWhere.WhereIsEmpty)
                    {
                        appWhere.WhereIn("AdProcessGroup", processes);
                    }
                    else
                    {
                        appWhere.Or(new WhereCondition().WhereIn("AdProcessGroup", processes));
                    }
                   
                }
            }

            // processes
            if (!string.IsNullOrEmpty(appNode.AppPlatformTypes))
            {
                string[] platforms = appNode.AppPlatformTypes.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                if (platforms.Length > 0)
                {
                    if (appWhere.WhereIsEmpty)
                    {
                        appWhere.WhereIn("AdPlatformType", platforms); ;
                    }
                    else
                    {
                        appWhere.Or(new WhereCondition().WhereIn("AdPlatformType", platforms));
                    }
                   
                }
            }
        }

        return appWhere;
    }

    #endregion

    #region "On events"
    /// <summary>
    /// Content loaded event handler.
    /// </summary>
    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
    }


    /// <summary>
    /// Reloads data.
    /// </summary>
    public override void ReloadData()
    {
        base.ReloadData();
        SetupControl();
        repItems.ReloadData(true);
    }

    #endregion
}