<?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;
use seekquarry\yioop\library\CrawlConstants;
use seekquarry\yioop\library\UrlParser;

/**
 * Helper used to draw thumbnails strips for images
 *
 * @author Chris Pollett
 */
class ImagesHelper extends Helper implements CrawlConstants
{
    /**
     * Takes page summaries for image pages and the current query
     * and draw a thumbnail strip so that clicking on an image goes to
     * the cache of that image.
     *
     * @param array $image_pages page data and thumbnails for images
     * @param string $query the current search query
     * @param string $subsearch name of subsearch page this image group on
     * @param bool $image_subsearch whether the image subsearch is
     *  enabled on this Yioop instance
     * @param boolean $open_in_tabs whether new links should be opened in
     *    tabs
     */
    public function render($image_pages, $query, $subsearch,
        $image_subsearch = true, $open_in_tabs = false)
    {
        if ($subsearch != 'images' && !stristr($query, "media%3Aimage")) {
            if ($image_subsearch) {
                $query .= '&s=images';
            } else {
                if (!stristr($query, "media%3Aimage")) {
                    $query .= '%20media%3Aimage';
                }
            }
            ?>
            <h2><a href="<?= $query ?>"
                ><?=tl('images_helper_view_image_results') ?></a></h2>
                <?php
        }
        ?><div class="image-list"><div class="sub-image-list" >
        <?php
        foreach ($image_pages as $page) {
            if (C\p('CACHE_LINK') && (!isset($page[self::ROBOT_METAS]) ||
                !(in_array("NOARCHIVE", $page[self::ROBOT_METAS]) ||
                  in_array("NONE", $page[self::ROBOT_METAS])))) {
                $link = $query."&amp;a=cache&amp;arg=" .
                    urlencode($page[self::URL]) .
                    "&amp;its=" . $page[self::CRAWL_TIME];
            } else {
                $link = htmlentities($page[self::URL]);
            }
            $title = strip_tags(explode("\n", $page[self::TITLE])[0]);
            ?><figure class="image-detail align-opposite" >
            <a href="<?= $link
            ?>" rel="noopener nofollow noreferer" <?php
            if ($open_in_tabs) { ?>
                target="_blank" <?php
            }?>><img src="<?=
            $page[self::THUMB] ?>" alt="<?= $title ?>" ></a><?php
            if (!empty($page[self::WIDTH]) && !empty($page[self::HEIGHT])) {?>
                <div class="image-dim"><?=
                $page[self::WIDTH]?>x<?=$page[self::HEIGHT]?></div>
            <?php
            }
            ?>
            <figcaption class="align-same"><?php
            if (!empty($page[self::URL_PARENT])) {
                ?><div><a href="<?=$page[self::URL_PARENT];
                ?>" rel="noopener nofollow noreferer" <?php
                if ($open_in_tabs) { ?>
                    target="_blank" <?php
                }?>><b><?=
                $title ?></b></div><div><?=
                UrlParser::simplifyUrl(UrlParser::getHost($page[self::URL]));
                ?></a></div><?php
            } else {?>
                <div><b><?=
                $title ?></b></div><div><?=
                UrlParser::simplifyUrl(UrlParser::getHost($page[self::URL]));
                ?></div><?php
            }?>
            </figcaption>
            </figure><?php
        }
        ?>
    </div></div>
        <?php
    }
}
