<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\views\helpers;

use seekquarry\yioop\configs as C;

/**
 * Resolves the effective logo or favicon URL for the page,
 * preferring a per-domain appearance override supplied in the
 * view $data over the global profile constant. A custom-route
 * domain's appearance overlay (see Controller::applyDomainAppearance)
 * stores already-complete resource URLs in $data, so when one is
 * present it is used as-is; otherwise the global constant is used,
 * with the site's short base url prepended so the relative
 * resources path resolves.
 *
 * @author Chris Pollett
 */
class LogoHelper extends Helper
{
    /**
     * Returns the effective URL for a logo or favicon field,
     * using the per-domain override from $data when present
     * (already a complete resource URL) and otherwise the global
     * value the caller already resolved for that field.
     *
     * @param array $data view data possibly carrying a per-domain
     *      appearance override under $field
     * @param string $field appearance field name, one of
     *      LOGO_SMALL, LOGO_MEDIUM, LOGO_LARGE, FAVICON
     * @param string $global_value value to use when there is no
     *      per-domain override; the caller resolves this exactly as
     *      it did before (some sites prefix the short base url, some
     *      do not), so the global look is unchanged
     * @return string URL to use as the image source
     */
    public function render($data, $field, $global_value)
    {
        $url = (!empty($data[$field])) ? $data[$field] : $global_value;
        return $this->absoluteResourceUrl($url);
    }
    /**
     * Emits the site logo as two images that the stylesheet shows one
     * of by viewport width, so the choice tracks the layout rather than
     * a server-side user-agent guess. The wider medium logo carries the
     * hide-narrow class and the small logo the show-narrow class, so at
     * or below the 600px breakpoint only the small one is shown and above
     * it only the medium. Both are marked loading="lazy" so the hidden
     * one is not fetched until the width crosses the breakpoint and it
     * becomes visible, keeping a single download in the common case.
     *
     * @param array $data view data, possibly with per-domain overrides
     *      for the two logo fields
     * @param string $alt alt text for both images
     * @param string $wide_field appearance field for the wider screen,
     *      shown above the 600px breakpoint (default LOGO_MEDIUM)
     * @param string $narrow_field appearance field for the phone,
     *      shown at or below the breakpoint (default LOGO_SMALL)
     * @return string two img tags, one shown at each width by the
     *      stylesheet's 600px breakpoint
     */
    public function renderResponsive($data, $alt, $wide_field = "LOGO_MEDIUM",
        $narrow_field = "LOGO_SMALL")
    {
        $wide = $this->render($data, $wide_field,
            C\SHORT_BASE_URL . C\p($wide_field));
        $narrow = $this->render($data, $narrow_field,
            C\SHORT_BASE_URL . C\p($narrow_field));
        $alt = htmlentities($alt, ENT_QUOTES);
        return "<img class='hide-narrow' loading='lazy' src='$wide' " .
            "alt='$alt' ><img class='show-narrow' loading='lazy' " .
            "src='$narrow' alt='$alt' >";
    }
    /**
     * Makes a logo or favicon URL resolve from the site root rather
     * than the current page path. A bare value such as
     * "wd/resources/name.png" or "?c=resource&a=get&n=name.png" is
     * relative, so on a deep path like /group/4/Main the browser
     * would look under /group/4/ and miss the image; prepending the
     * site's short base url (a leading slash on a root install)
     * anchors it at the root. Values that are already absolute -- an
     * empty value, one beginning with a slash, a protocol-relative
     * "//host" url, or one carrying a scheme -- are returned
     * unchanged so an already-resolved or off-site url is never
     * double-prefixed.
     *
     * @param string $url logo or favicon url to normalize
     * @return string url anchored at the site root when it was
     *      relative, otherwise the url unchanged
     */
    private function absoluteResourceUrl($url)
    {
        if ($url === '' || $url[0] === '/'
            || str_starts_with($url, '//')
            || preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*:/', $url)) {
            return $url;
        }
        return C\SHORT_BASE_URL . $url;
    }
}
