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/App_Code/Pux/Tools.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Text;
using CMS.Helpers;
using CMS.SiteProvider;

namespace Pux
{
    public class Tools
    {
        public static readonly Regex VimeoVideoRegex = new Regex(@"vimeo\.com/(?:.*#|.*/videos/)?([0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        public static readonly Regex YoutubeVideoRegex = new Regex(@"^(?:https?\:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v\=))([\w-]{10,12})(?:$|\&|\?\#).*", RegexOptions.IgnoreCase);

        public static string GetYoutubeEmbedCode(string url, int width = 560, int height = 315)
        {
            if (!String.IsNullOrEmpty(url))
            {
                Match youtubeMatch = YoutubeVideoRegex.Match(url);
                string videoId = String.Empty;
                if (youtubeMatch.Success)
                {
                    videoId = youtubeMatch.Groups[1].Value;
                }
                string code = String.Format("<iframe src='//www.youtube.com/embed/{0}?rel=0' width='{1}' height='{2}' frameborder='0' allowfullscreen></iframe>", videoId, width, height);
                return code;
            }
            return String.Empty;
        }

        public static string GetYoutubeID(string url)
        {
            if (!String.IsNullOrEmpty(url))
            {
                Match youtubeMatch = YoutubeVideoRegex.Match(url);
                string videoId = String.Empty;
                if (youtubeMatch.Success)
                {
                    videoId = youtubeMatch.Groups[1].Value;
                }
                return videoId;
            }
            return String.Empty;
        }

        public static string ConstructWhereCondition(string strList, string columnName, char delimiter = '|', string sqloperator = "OR")
        {
            StringBuilder result = new StringBuilder();
            if (!String.IsNullOrEmpty(strList))
            {
                string[] samples = strList.Split(delimiter);
                bool firstUse = true;
                foreach (string sample in samples)
                {
                    if (!firstUse)
                    {
                        result.Append(String.Format(" {0} ", sqloperator));
                    }
                    result.Append(columnName + " = '" + sample + "'");
                    firstUse = false;
                }
                return result.ToString();
            }
            return "1 = 2";
        }

        public static string GetLinkByNodeGUID(Guid guid)
        {
            CMS.DocumentEngine.TreeNode node = null;
            CMS.Membership.UserInfo ui = CMS.Membership.MembershipContext.AuthenticatedUser;
            CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(ui);
            node = tree.SelectSingleNode(guid, CMS.DocumentEngine.DocumentContext.CurrentDocument.DocumentCulture, CMS.SiteProvider.SiteContext.CurrentSiteName);
            if (node != null)
            {
                return String.Format("<a href='{0}'>{1}</a>", node.NodeAliasPath, node.NodeName);
            }

            return String.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="cssClass"></param>
        /// <param name="level">Level of parent. Count from actual document</param>
        /// <returns></returns>
        public static string GetLinkByNodeId(int nodeId, string cssClass = null, int level = 1)
        {
            if (level < 1) level = 1;
            CMS.DocumentEngine.TreeNode node = null;
            CMS.DocumentEngine.TreeNode nodeTwo = null;
            CMS.Membership.UserInfo ui = CMS.Membership.UserInfoProvider.GetUserInfo(CMS.Membership.MembershipContext.AuthenticatedUser.UserName);
            CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(ui);
            int i = 1;
            while (i <= level)
            {
                node = tree.SelectSingleNode(nodeId, CMS.DocumentEngine.DocumentContext.CurrentDocumentCulture.CultureCode);
                if (node != null)
                {
                    nodeId = node.NodeParentID;
                }
                else
                {
                    break;
                }
                i++;
            }
            if (node != null)
            {
                return String.Format("<a class='{0}' href='{1}'>{2}</a>", cssClass, node.NodeAliasPath, node.DocumentName);
            }

            return String.Empty;
        }
        
        public static string GetNameByNodeId(int nodeId, int level = 1)
        {
            if (level < 1) level = 1;
            CMS.DocumentEngine.TreeNode node = null;
            CMS.DocumentEngine.TreeNode nodeTwo = null;
            CMS.Membership.UserInfo ui = CMS.Membership.UserInfoProvider.GetUserInfo(CMS.Membership.MembershipContext.AuthenticatedUser.UserName);
            CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(ui);
            int i = 1;
            while (i <= level)
            {
                node = tree.SelectSingleNode(nodeId, CMS.DocumentEngine.DocumentContext.CurrentDocumentCulture.CultureCode);
                if (node != null)
                {
                    nodeId = node.NodeParentID;
                }
                else
                {
                    break;
                }
                i++;
            }
            if (node != null)
            {
                return String.Format("{0}", node.DocumentName);
            }

            return String.Empty;
        }

        public static string GetNodeAliasByNodeId(int nodeId, int level = 1)
        {
            if (level < 1) level = 1;
            CMS.DocumentEngine.TreeNode node = null;
            CMS.DocumentEngine.TreeNode nodeTwo = null;
            CMS.Membership.UserInfo ui = CMS.Membership.UserInfoProvider.GetUserInfo(CMS.Membership.MembershipContext.AuthenticatedUser.UserName);
            CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(ui);
            int i = 1;
            while (i <= level)
            {
                node = tree.SelectSingleNode(nodeId, CMS.DocumentEngine.DocumentContext.CurrentDocumentCulture.CultureCode);
                if (node != null)
                {
                    nodeId = node.NodeParentID;
                }
                else
                {
                    break;
                }
                i++;
            }
            if (node != null)
            {
                return String.Format("{0}", node.NodeAlias);
            }

            return String.Empty;
        }


        public static string GetMeta(string title, string og)
        {
            return String.Format("<meta property='{0}' content='{1}'/>", og, title);
        }

        public static string GetSizeImage(object path, string width = "460")
        {
            if (path != null)
            {
                string strPath = path.ToString();
                return URLHelper.RemoveParameterFromUrl(
                        URLHelper.AddParameterToUrl(strPath.ToString(), "width", width),
                    "height");
            }
            return String.Empty;
        }

        public static string GetValueFromListOfOptionsField(object fieldName, object nodeClassId, object id)
        {
            CMS.DataEngine.DataClassInfo classInfo = CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo(ValidationHelper.GetInteger(nodeClassId, 0));

            if (classInfo != null)
            {
                CMS.FormEngine.FormInfo formInfo = new CMS.FormEngine.FormInfo(classInfo.ClassFormDefinition);
                CMS.FormEngine.FormFieldInfo ffi = formInfo.GetFormField(ValidationHelper.GetString(fieldName, ""));

                if (ffi != null)
                {
                    List<string> optionsList = ffi.Settings["Options"].ToString().Split('\n').ToList();

                    optionsList = optionsList.Select(s => s = s.Trim()).ToList();

                    System.Collections.Hashtable options = new System.Collections.Hashtable();
                    optionsList.ForEach(s => options.Add(s.Split(';')[0], s.Split(';')[1]));

                    if (options[id] != null)
                    {
                        return options[id].ToString();
                    }
                }
            }

            return "";
        }

        public string GetLinksFromGUIDs(string regionsNodeGUIDs, string linkUrl, string className, string delimiter = ", ")
        {
            if (!String.IsNullOrEmpty(regionsNodeGUIDs))
            {
                string[] regionsArr = regionsNodeGUIDs.Split('|');
                CMS.DocumentEngine.DocumentQuery regions = CMS.DocumentEngine.DocumentHelper.GetDocuments(className)
                    .WhereIn("NodeGUID", regionsArr);

                IEnumerable<string> links = regions.ToList().Select(a => String.Format("<a href='{0}{1}'>{2}</a>", linkUrl, a.NodeGUID, a.NodeName));
                return links.Join(delimiter);
            }

            return String.Empty;
        }

        public static int GetNumberForResourceString(int n)
        {
            if ((n >= 5) || (n < 1)) return 5;
            else if (n > 1) return 2;
            return 1;
        }

        public static Guid GetAttachmentColumnGUID(string pageType, string attachmentColumn)
        {
            CMS.DataEngine.DataClassInfo dci = CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo(pageType);
            if (dci != null)
            {
                CMS.FormEngine.FormInfo fi = new CMS.FormEngine.FormInfo(dci.ClassFormDefinition);
                CMS.FormEngine.FormFieldInfo ffi = fi.GetFormField(attachmentColumn);
                return ffi == null ? Guid.Empty : ffi.Guid;
            }

            return Guid.Empty;
        }

        public static int GetDocumentAttachmentsCount(int documentID, string pageType, string attachmentColumn)
        {
            int attachmentCount = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachments(documentID, false)
                .WhereEquals("AttachmentGroupGUID", GetAttachmentColumnGUID(pageType, attachmentColumn))
                .Count;
            return attachmentCount;
        }

        public static string FormatAttachmentFileSize(Guid guid)
        {
            return FormatAttachmentFileSize(guid, SiteContext.CurrentSiteName);
        }

        public static string FormatAttachmentFileSize(Guid guid, string sitename)
        {
            CMS.DocumentEngine.AttachmentInfo ai = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(guid, sitename);
            if (ai != null)
            {
                return CMS.Helpers.DataHelper.FileSizeFormat(ai.AttachmentSize);
            }
            return String.Empty;
        }

        public static string FormatMediaFileSize(string mediaFilePath)
        {
            mediaFilePath = URLHelper.RemoveQuery(mediaFilePath);
            var physicalPath = URLHelper.GetPhysicalPath(mediaFilePath);
            if (System.IO.File.Exists(physicalPath))
            {
                return CMS.Helpers.DataHelper.FileSizeFormat(new System.IO.FileInfo(physicalPath).Length);
            }
            return String.Empty;
        }

        public static string GetAttachmentFileExtension(Guid guid)
        {
            return GetAttachmentFileExtension(guid, SiteContext.CurrentSiteName);
        }

        public static string GetAttachmentFileExtension(Guid guid, string sitename)
        {
            CMS.DocumentEngine.AttachmentInfo ai = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(guid, sitename);
            if (ai != null)
            {
                return ai.AttachmentExtension;
            }
            return String.Empty;
        }

        public static string GetMediaFileExtension(string mediaFilePath)
        {
            mediaFilePath = URLHelper.RemoveQuery(mediaFilePath);
            var physicalPath = URLHelper.GetPhysicalPath(mediaFilePath);
            if (System.IO.File.Exists(physicalPath))
            {
                return System.IO.Path.GetExtension(physicalPath);
            }
            return String.Empty;
        }
    }
}