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/CMSWebParts/Pux/Documents/MarkDocumentManager.ascx.cs
using CMS.DocumentEngine;
using CMS.Ecommerce;
using CMS.Helpers;
using CMS.SiteProvider;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CMSWebParts_Pux_Documents_MarkDocumentManager : System.Web.UI.UserControl
{
    private List<int> _nodeIds = null;
    public List<int> NodeIDs
    {
        get
        {
            if (_nodeIds == null)
            {
                _nodeIds = LoadState();
            }

            return _nodeIds;
        }
        set
        {
            _nodeIds = value;
        }
    }

    public string Mark { get; set; }

    private string _cookieName = null;
    public string CookieName
    {
        get
        {
            if (string.IsNullOrEmpty(_cookieName))
            {
                _cookieName = "Products" + Mark;
            }

            return _cookieName;
        }
        set
        {
            _cookieName = value;
        }
    }

    private string _sessionName = null;
    public string SessionName
    {
        get
        {
            if (string.IsNullOrEmpty(_sessionName))
            {
                _sessionName = "Pux.Ecommerce.Products.Products" + Mark;
            }

            return _sessionName;
        }
        set
        {
            _sessionName = value;
        }
    }

    private char _valueSeparator = '|';
    public char ValueSeparator
    {
        get
        {
            return _valueSeparator;
        }
        set
        {
            _valueSeparator = value;
        }
    }

    protected enum Operation { Add, Remove };
    public enum StorageType { Cookie, Session, CustomTable };

    public StorageType Storage { get; set; }

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

        var add = QueryHelper.GetInteger("add", 0);
        var remove = QueryHelper.GetInteger("remove", 0);

        var ajaxResponse = string.Empty;

        if (add > 0)
        {
            ChangeProduct(add, Operation.Add);

            ajaxResponse = "add";
        }
        else if (remove > 0)
        {
            ChangeProduct(remove, Operation.Remove);

            ajaxResponse = "remove";
        }

        if (RequestHelper.IsAJAXRequest())
        {
            Response.Write(JsonConvert.SerializeObject(new { result = ajaxResponse}));
            Response.ContentType = "application/json; charset=utf-8";
            RequestHelper.EndResponse();
        }
    }

    private void ChangeProduct(int nodeId, Operation operation)
    {
        var currentState = NodeIDs;
        if (operation == Operation.Remove)
        {
            if (currentState.Contains(nodeId))
            {
                currentState.Remove(nodeId);

                SaveState(currentState);
            }

            return;
        }

        var treeNode = TreeHelper.SelectSingleNode(nodeId);
        if (treeNode != null && treeNode.NodeSiteID == SiteContext.CurrentSiteID && !currentState.Contains(nodeId))
        {
            currentState.Add(nodeId);

            SaveState(currentState);
        }
    }

    private void SaveState(List<int> currentState)
    {
        var joinedValues = string.Join(ValueSeparator.ToString(), currentState);

        if (Storage == StorageType.Cookie)
        {
            CookieHelper.SetValue(CookieName, joinedValues, "/", DateTime.Now.AddMonths(1), false);
        }
        else if(Storage == StorageType.Session)
        {
            SessionHelper.SetValue(SessionName, joinedValues);
        }
        else if (Storage == StorageType.CustomTable)
        {
            // TODO
        }
    }

    private List<int> LoadState()
    {
        if (Storage == StorageType.Cookie || Storage == StorageType.Session)
        {
            string value = value = Storage == StorageType.Cookie ? CookieHelper.GetValue(CookieName) : SessionHelper.GetValue(SessionName) as string;

            if (!string.IsNullOrEmpty(value))
            {
                // value from cookie
                return value.Split(ValueSeparator).Select(p => ValidationHelper.GetInteger(p, 0)).Where(p => p > 0).ToList();
            }

            // session
            return new List<int>();
        }
        else
        {
            // custom table
            return new List<int>();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}