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

/**
 * Element responsible for displaying the queue_server or fetcher log
 * of a machine
 *
 * @author Chris Pollett
 */
class MachinelogElement extends Element
{
    /**
     * Draws the log file of a queue_server or a fetcher
     *
     * @param array $data LOG_FILE_DATA has the log data
     */
    public function render($data)
    {
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $close_activity = (($data['LOG_MACHINE_TYPE'] ?? '') ===
            'Mail') ? "servers" : "manageMachines";
        ?>
        <div class="current-activity">
        <div class="<?=$data['leftorright'] ?>">
            <?=$this->view->helper("close")->render($admin_url .
            "a=$close_activity&amp;". C\p('CSRF_TOKEN') . "=" .
            $data[C\p('CSRF_TOKEN')]) ?>
            <form method="get"  >
                <div class="clear top-space">
                <div class="filter-container">
                <input type="hidden" name="<?=C\p('CSRF_TOKEN') ?>"
                    value="<?=$data[C\p('CSRF_TOKEN')] ?>" >
                <input type="hidden" name="c" value="admin" >
                <input type="hidden" name="a" value="manageMachines" >
                <input type="hidden" name="arg" value="log" >
                <input type="hidden" name="time" value="<?=$data['time'] ?>" >
                <input type="hidden" name="name" value="<?=
                    $data['LOG_NAME'] ?>" >
                <input type="hidden" name="id" value="<?=
                    $data['LOG_ID'] ?? "" ?>" >
                <input type="hidden" name="channel" value="<?=
                    $data['LOG_CHANNEL'] ?? 0 ?>" >
                <input type="hidden" name="type" value="<?=
                    $data['LOG_MACHINE_TYPE'] ?>" >
                <?php
                if (!empty($data['NO_REFRESH'])) { ?>
                    <input type="hidden" name="NO_REFRESH" value="true" ><?php
                }?>
                <input type="search" class="narrow-field" name='f'
                    placeholder="<?=tl('machinelog_element_filter_log') ?>"
                    id='set-filter' value="<?=$data['filter']?>" >
                </div>
                <span class="top-space-px">
                <button type="submit" class="button-box" ><?=
                    tl('machinelog_element_go')?></button>
                </span>
                </div>
            </form>
        </div>
        <h2><?= tl('machinelog_element_log_file', $data['LOG_TYPE']) ?></h2>
        <?php if (!$data['NO_REFRESH']) {?>
        <p>[<a href="<?=$admin_url .
            C\p('CSRF_TOKEN')."=".
                $data[C\p('CSRF_TOKEN')]?>&amp;a=manageMachines<?=
                $data['REFRESH_LOG'] ?>&amp;NO_REFRESH=true&amp;f=<?=
                $data['filter'] ?>" ><?=
                tl('machinelog_element_refresh_off')  ?></a>]</p>
        <?php } else { ?>
        <p>[<a href="<?=$admin_url .
            C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')]
                ?>&amp;a=manageMachines<?=$data['REFRESH_LOG'] ?>&amp;f=<?=
                $data['filter'] ?>"><?=
                tl('machinelog_element_refresh_on') ?></a>]</p>
        <?php } ?>
        <pre><?= wordwrap($data["LOG_FILE_DATA"], 60, "\n", true)
        ?></pre><?php
        if (!$data['NO_REFRESH']) { ?>
            <script>
            var updateId;
            /*
               called to refresh the log file data
             */
            function logUpdate()
            {
                var filter = elt('set-filter').value;
                var refreshUrl= "<?=B\controllerUrl('admin', true)
                    ?>f=" + filter + "&<?=
                    C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')]
                    ?>&a=manageMachines<?= $data['REFRESH_LOG'] ?>";
                document.location = refreshUrl;
            }
            /*
               sets up the callback to periodically update log
             */
            function doUpdate()
            {
                 var sec = 1000;
                 updateId = setInterval("logUpdate()", 30*sec);
            }
            </script><?php
        } else { ?>
            <script>
            function doUpdate() {}
            </script><?php
        }
    }
}
