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

/**
 * Draws the page used to edit one domain's robots.txt and humans.txt.
 * The top of the page has a close box that returns to Server Settings.
 * A single checkbox keeps all crawlers out; when it is ticked the rest
 * of the robots choices are hidden by page script and only the
 * humans.txt box remains. Otherwise the administrator picks a crawl
 * delay and ticks which kinds of pages crawlers may visit (with extra
 * wiki choices appearing only when wiki pages are allowed), and may add
 * extra robots directives by hand. Save stores the choices, Cancel
 * returns without saving, and Preview opens the robots.txt that would
 * be produced in a new tab.
 *
 * @author Chris Pollett
 */
class RobotssettingsElement extends Element
{
    /**
     * Draws the robots.txt / humans.txt edit form for one domain.
     *
     * @param array $data fields ROBOTS (the saved choices), HUMANS (the
     *      humans.txt text), and ROBOTS_DOMAIN (the domain being
     *      edited) prepared by SystemComponent::servers
     */
    public function render($data)
    {
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $close_url = $admin_url . "a=servers&amp;" .
            C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')];
        $robots = $data['ROBOTS'];
        $delays = [1, 3, 5, 10, 30, 60];
        ?>
        <div class="current-activity"><?php
        $this->view->helper("close")->render($close_url);
        ?>
        <h2><?= tl('robotssettings_element_edit_robots',
            $data['ROBOTS_DOMAIN']) ?></h2>
        <form id="robotsSettingsForm" method="post">
        <input type="hidden" name="c" value="admin" >
        <input type="hidden" name="<?= C\p('CSRF_TOKEN') ?>" value="<?=
            $data[C\p('CSRF_TOKEN')] ?>" >
        <input type="hidden" name="a" value="servers" >
        <input type="hidden" name="arg" value="robots" >
        <input type="hidden" name="domain" value="<?=
            $data['ROBOTS_DOMAIN'] ?>" >
        <div class="top-margin"><label for="disallow-all"><b><?=
            tl('robotssettings_element_disallow_all')
            ?></b></label><input type="checkbox" id="disallow-all"
            name="disallow_all" value="1" <?=
            !empty($robots['disallow_all']) ? 'checked="checked"' : ''
            ?> ></div>
        <div id="robots-inputs">
            <div class="top-margin"><label for="crawl-delay"><b><?=
                tl('robotssettings_element_crawl_delay')
                ?></b></label> <select id="crawl-delay"
                name="crawl_delay"><option value="0" <?=
                    (intval($robots['crawl_delay']) <= 0) ?
                    'selected="selected"' : '' ?>><?=
                    tl('robotssettings_element_crawl_delay_none')
                    ?></option><?php
                foreach ($delays as $delay) {
                    $selected = (intval($robots['crawl_delay']) == $delay)
                        ? 'selected="selected"' : '';
                    ?><option value="<?= $delay ?>" <?= $selected ?>><?=
                        $delay ?></option><?php
                }
                ?></select></div>
            <div class="top-margin"><label for="search-crawlable"><b><?=
                tl('robotssettings_element_search_crawlable')
                ?></b></label><input type="checkbox" id="search-crawlable"
                name="search" value="1" <?=
                !empty($robots['search']) ? 'checked="checked"' : ''
                ?> ></div>
            <div class="top-margin"><label for="feeds-crawlable"><b><?=
                tl('robotssettings_element_feeds_crawlable')
                ?></b></label><input type="checkbox" id="feeds-crawlable"
                name="feeds" value="1" <?=
                !empty($robots['feeds']) ? 'checked="checked"' : ''
                ?> ></div>
            <div class="top-margin"><label for="wiki-crawlable"><b><?=
                tl('robotssettings_element_wiki_crawlable')
                ?></b></label><input type="checkbox" id="wiki-crawlable"
                name="wiki" value="1" <?=
                !empty($robots['wiki']) ? 'checked="checked"' : ''
                ?> ></div>
            <div id="wiki-sub" class="indent">
                <div class="top-margin"><label
                    for="page-source-crawlable"><?=
                    tl('robotssettings_element_page_source_crawlable')
                    ?></label><input type="checkbox"
                    id="page-source-crawlable" name="page_source"
                    value="1" <?= !empty($robots['page_source']) ?
                    'checked="checked"' : '' ?> ></div>
                <div class="top-margin"><label
                    for="page-feed-crawlable"><?=
                    tl('robotssettings_element_page_feed_crawlable')
                    ?></label><input type="checkbox"
                    id="page-feed-crawlable" name="page_feed"
                    value="1" <?= !empty($robots['page_feed']) ?
                    'checked="checked"' : '' ?> ></div>
                <div class="top-margin"><label
                    for="page-list-crawlable"><?=
                    tl('robotssettings_element_page_list_crawlable')
                    ?></label><input type="checkbox"
                    id="page-list-crawlable" name="page_list"
                    value="1" <?= !empty($robots['page_list']) ?
                    'checked="checked"' : '' ?> ></div>
            </div>
            <div class="top-margin"><b><label for="robots-additional"><?=
                tl('robotssettings_element_additional')
                ?></label></b></div>
            <textarea id="robots-additional" class="short-text-area"
                name="additional"><?= $robots['additional'] ?? ''
                ?></textarea>
        </div>
        <div class="top-margin"><b><label for="humans-text"><?=
            tl('robotssettings_element_humans')
            ?></label></b></div>
        <textarea id="humans-text" class="short-text-area"
            name="humans"><?= $data['HUMANS'] ?? '' ?></textarea>
        <div class="top-margin">
            <button class="button-box" type="submit" name="save"
                value="1"><?= tl('robotssettings_element_save')
                ?></button>
            <button class="button-box" type="submit" name="cancel"
                value="1"><?= tl('robotssettings_element_cancel')
                ?></button>
            <button class="button-box" type="submit" name="preview"
                value="1" formtarget="_blank"><?=
                tl('robotssettings_element_preview') ?></button>
        </div>
        </form>
        </div>
        <?php
    }
}
