<?php

/**
 * Smarty plugin.
 */

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\System\UrlFinder;

/**
 * Smarty {url} plugin.
 *
 * Type:     function<br>
 * Name:     photo<br>
 * Purpose:  insert <img> tag
 *
 * @param array                    $params   parameters
 * @param Smarty_Internal_Template $template template object
 *
 * @return string
 */
function smarty_function_photo($params, $template)
{
    $photo = null;
    $link = false;
    $type = null;
    $id = null;
    $image = null;
    $alt = '';

    extract($params);

    if (!$photo) {
        return null;
    }

    static $urlFinder = null;

    if (!$urlFinder) {
        $urlFinder = ServiceContainer::getService(UrlFinder::class);
    }

    if ($alt !== '' && $alt) {
        $alt = ' alt="'.htmlspecialchars($alt).'"';
    }

    $res = "<img src='{$urlFinder->staticUrl($photo['src'])}'{$alt}>";

    if ($link) {
        $res = "<a href='{$urlFinder->staticUrl($photo['src_big'])}'>{$res}</a>'";
    }

    return $res;
}
