<?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\elements;

use seekquarry\yioop as B;
use seekquarry\yioop\configs as C;
use seekquarry\yioop\library\CrawlConstants;

/**
 * Element responsible for displaying statistics about recent queries that
 * have been run against the search engine
 *
 * @author Chris Pollett
 */
class QuerystatsElement extends Element
{
    /**
     * Draws statistics about what queries have been recently run against the
     * search engine
     *
     * @param array $data keys are generally the different setting that can
     *     be set in the crawl.ini file
     */
    public function render($data)
    {
        $token = C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')];
        $icon_helper = $this->view->helper('iconlink');
        ?>
        <div class="current-activity">
        <?=$this->view->element('crawlcontrols')->render($data); ?>
        <h2><?=tl('querystats_element_query_statistics') ?></h2>
        <div class="float-opposite"><?php
            $icon_helper->renderLink(B\controllerUrl("admin", true) .
            "a=manageCrawls&amp;$token&amp;arg=deletestats", "delete");
        ?></div>
        <form>
        <div>
        <b><label for='query-filter'><?=
        tl('querystats_element_filter')?></label></b>
        <input type="text" class="narrow-field" name='filter'
            id='query-filter' value="<?=$data['FILTER']?>" >
        <input type="hidden" name='<?=C\p('CSRF_TOKEN')?>'
            value='<?=$data[C\p('CSRF_TOKEN')]?>' >
        <input type="hidden" name='a' value='manageCrawls' >
        <input type="hidden" name='arg' value='querystats' >
        <input type="hidden" name='c' value='admin' >
        <button type='submit'
            class="button-box"><?=tl('querystats_element_go') ?></button>
        </div></form>
        <div class="trending-container">
        <?php
        $time_periods = [
           C\ONE_HOUR => tl('querystats_element_last_hour'),
           C\ONE_DAY => tl('querystats_element_last_day'),
           C\ONE_MONTH => tl('querystats_element_last_month'),
           C\ONE_YEAR => tl('querystats_element_last_year'),
           C\FOREVER => tl('querystats_element_all_time'),
        ];
        foreach ($time_periods as  $time_period => $period_heading) { ?>
            <div class="trending-float">
            <table class="trending-table">
                <tr class="trending-tr">
                <th class="trending-th" ><?=$period_heading ?></th>
                <th class="trend-score trending-th"><?=
                    tl('querystats_element_views');
                ?></th></tr><?php
            if (!empty($data['STATISTICS'][
                $time_period])) {
                $background = "";
                foreach ($data['STATISTICS'][$time_period] as
                    $item_name => $item_data) {
                    if (empty(trim($item_name))) {
                        continue;
                    }
                    $background = empty($background) ? " class='back-gray' " :
                        "";?>
                    <tr <?=$background ?>>
                        <td class="trending-td"><a href="<?=
                        C\SHORT_BASE_URL . '?q=' .
                        $item_name; ?>&amp;<?= $token
                        ?>"><?=urldecode($item_name) ?></a></td>
                        <td class="trend-score trending-td"><?=
                        $item_data[0]['NUM_VIEWS']?></td></tr><?php
                } ?>
                <?php
            } else { ?>
                <tr class='back-gray'><td colspan="2"><?=
                    tl('querystats_element_no_activity')?>
                </td>
                </tr><?php
            }?>
            </table>
            </div><?php
        }
        ?>
        </div>
        </div>
    <?php
    }
}
