<?php

// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: UrlIcon.php,v 1.1.4.3 2026/03/24 15:51:34 qvarin Exp $

namespace Pmb\Common\Helper;

use HtmlHelper;

if (stristr($_SERVER['REQUEST_URI'], basename(__FILE__))) {
    die("no access");
}

class UrlIcon
{
    /**
     * Cache for icon paths
     *
     * @var array
     */
    private static $iconToPatch = [];

    /**
     * Get the path to an icon
     *
     * @param string $icon
     * @param bool $force_opac_url_base
     * @return string
     */
    public static function toPath(string $icon, bool $force_opac_url_base = false)
    {
        $key = $force_opac_url_base ? 'force_opac_' . $icon : $icon;
        if (!isset(self::$iconToPatch[$key])) {
            if (defined('GESTION')) {
                self::$iconToPatch[$key] = self::fetchPath($icon, $force_opac_url_base);
            } else {
                self::$iconToPatch[$key] = self::fetchPathOpac($icon, $force_opac_url_base);
            }
        }
        return self::$iconToPatch[$key];
    }

    /**
     * Fetch the path to an icon
     *
     * @param string $icon
     * @param bool $force_opac_url_base
     * @return string
     */
    private static function fetchPathOpac(string $icon, bool $force_opac_url_base)
    {
        global $base_path;
        global $opac_url_base, $css;
        global $use_opac_url_base;

        if ($force_opac_url_base || !empty($use_opac_url_base)) {
            $url_base = $opac_url_base;
        } else {
            $url_base = $base_path."/";
        }

        $common_folder = HtmlHelper::getInstance()->getCommonFolder();
        $icon_name = str_replace(['.svg', '.png', '.jpg', '.gif'], '', $icon);

        // on cherche celle du style, du common, sinon celle par défaut
        if ($url = search_url_icon_type("styles/".$css."/images/".$icon_name)) {
            return $url_base.$url;
        }
        if ($url = search_url_icon_type("styles/".$common_folder."/images/".$icon_name)) {
            return $url_base.$url;
        }
        if ($url = search_url_icon_type("images/".$icon_name)) {
            return $url_base.$url;
        }
        return "";
    }

    /**
     * Fetch the path to an icon
     *
     * @param string $icon
     * @param bool $force_opac_url_base
     * @return string
     */
    private static function fetchPath(string $icon, bool $force_opac_url_base)
    {
        global $base_path;
        global $opac_url_base;
        global $stylesheet;
        global $opac_default_style;
        global $use_opac_url_base;

        $result = "";
        $tmp_base = $base_path;
        $tmp_stylesheet = $stylesheet;
        //gestion des url OPAC
        if ($force_opac_url_base || !empty($use_opac_url_base)) {
            $url_base = $opac_url_base;
            $base_path .= "/opac_css";
            $stylesheet = $opac_default_style;
            $common_folder = HtmlHelper::getInstance()->getCommonOpacFolder();
        } else {
            $url_base = $base_path."/";
            $common_folder = 'common';
        }

        $icon_name = str_replace(['.svg', '.png', '.jpg', '.gif'], '', $icon);

        if ($url = search_url_icon_type("styles/".$stylesheet."/images/".$icon_name)) {
            $result = $url_base.$url;
        } elseif ($url = search_url_icon_type("styles/".$common_folder."/images/".$icon_name)) {
            $result = $url_base.$url;
        } elseif ($url = search_url_icon_type("images/".$icon_name)) {
            $result = $url_base.$url;
        } elseif ($url = "$url_base/images/$icon") {
            if (file_exists($url)) {
                $result = $url;
            } else {
                $result = '';
            }
        }
        //On resset les globales à leur valeur d'origine
        $base_path = $tmp_base;
        $stylesheet = $tmp_stylesheet;

        return $result;
    }
}
