Beispiel #1
0
    /// <summary>
    /// Returns SKU image URL including dimension's modifiers (width, height or maxsidesize) and site name parameter if product is from different site than current. If image URL is not specified, SKU default image URL is used.
    /// </summary>
    /// <param name="imageUrl">SKU image URL</param>
    /// <param name="width">Image requested width, has no effect if maxsidesize is specified</param>
    /// <param name="height">Image requested height, has no effect if maxsidesize is specified</param>
    /// <param name="maxsidesize">Image requested maximum side size</param>
    /// <param name="siteId">SKU site ID. If empty, current site ID is used.</param>
    public static string GetSKUImageUrl(object imageUrl, object width, object height, object maxsidesize, object siteId)
    {
        int    iSiteId        = ValidationHelper.GetInteger(siteId, 0);
        bool   notCurrentSite = ((iSiteId > 0) && (iSiteId != CMSContext.CurrentSiteID));
        string siteName       = null;

        // Get site name
        if (notCurrentSite)
        {
            siteName = SiteInfoProvider.GetSiteName(iSiteId);
        }
        else
        {
            siteName = CMSContext.CurrentSiteName;
        }

        // Get product image URL
        string url = ValidationHelper.GetString(imageUrl, null);

        if (String.IsNullOrEmpty(url))
        {
            // Get default product image URL
            url = ECommerceSettings.DefaultProductImageURL(siteName);
        }

        if (!String.IsNullOrEmpty(url))
        {
            // Resolve URL
            url = URLHelper.ResolveUrl(url);

            int slashIndex = url.LastIndexOfCSafe('/');
            if (slashIndex >= 0)
            {
                string urlStartPart = url.Substring(0, slashIndex);
                string urlEndPart   = url.Substring(slashIndex);

                url = urlStartPart + HttpUtility.UrlPathEncode(urlEndPart);

                // Add site name if not current
                if (notCurrentSite)
                {
                    url = URLHelper.AddParameterToUrl(url, "siteName", siteName);
                }

                // Add max side size
                int iMaxSideSize = ValidationHelper.GetInteger(maxsidesize, 0);
                if (iMaxSideSize > 0)
                {
                    url = URLHelper.AddParameterToUrl(url, "maxsidesize", iMaxSideSize.ToString());
                }
                else
                {
                    // Add width
                    int iWidth = ValidationHelper.GetInteger(width, 0);
                    if (iWidth > 0)
                    {
                        url = URLHelper.AddParameterToUrl(url, "width", iWidth.ToString());
                    }

                    // Add height
                    int iHeight = ValidationHelper.GetInteger(height, 0);
                    if (iHeight > 0)
                    {
                        url = URLHelper.AddParameterToUrl(url, "height", iHeight.ToString());
                    }
                }

                // Encode URL
                url = HTMLHelper.HTMLEncode(url);
            }
        }

        return(url);
    }