/ src / views / elements / AppearanceElement.php
<?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 drawing the screen used to set up the search engine
 * appearance.
 *
 * @author Chris Pollett
 */
class AppearanceElement extends Element
{
    /**
     * Draws the forms used to modify the search engine appearance.
     *
     * This element has a form to set foreground, background appearance and
     * icons used in the display of the Yioop web app. It can also be used to
     * configure if the main landing page should be the main wiki page
     *
     * @param array $data holds data on the profile elements related to site
     *      appearance
     */
    public function render($data)
    {
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $appearance_url = $admin_url . 'a=appearance&'.
            C\p('CSRF_TOKEN')."=".$data[C\p('CSRF_TOKEN')];
        ?>
        <div class="current-activity bold">
        <form id="appearanceForm" method="post"
            action='<?=$appearance_url ?>'
            enctype='multipart/form-data' <?php
            if (!empty($data['USE_THEME_FORM'])) { ?>
                onsubmit="
                    var caret_pos = elt('caret-pos');
                    var scroll_top = elt('scroll-top');
                    var auxiliary_css = elt('auxiliary-css');
                    if (caret_pos && scroll_top && auxiliary_css) {
                        caret_pos.value =
                            (auxiliary_css.selectionStart) ?
                            auxiliary_css.selectionStart : 0;
                        scroll_top.value = (auxiliary_css.scrollTop) ?
                        auxiliary_css.scrollTop : 0;
                    }" <?php
            } ?> >
        <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="appearance" >
        <input type="hidden" name="arg" value="profile" >
        <input type="hidden" name="APPEARANCE_DOMAIN" value="<?=
            $data['APPEARANCE_DOMAIN'] ?? '' ?>" >
        <input type="hidden" name="caret" id="caret-pos" >
        <input type="hidden" name="scroll_top" id="scroll-top" >
        <div class="top-margin">
        <?php
        if (empty($data['USE_THEME_FORM'])) {
            $this->renderMainAppearanceForm($data);
        } else {
            $this->renderThemeForm($data);
        }
        ?>
        <div class="top-margin center">
        <button class="button-box" type="submit"><?=
            tl('appearance_element_save') ?></button>
        </div>
        </div>
        </form>
        </div>
    <?php
    }
    /**
     * Used to draw general appearance controls for a Yioop site
     * such as for setting the background color, site logo, etc.
     * Has a drop down for setting the current theme, but does not
     * show the edit/add theme forms.
     *
     * @param array $data from the AdminController/SystemComponent with
     *    the current values for appearance settings.
     */
    public function renderMainAppearanceForm($data)
    {
        /* the form shows the scope the admin picked, which the
           component keeps apart from the fields dressing this page */
        $form = $data['APPEARANCE_FORM'] ?? $data;
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $appearance_url = $admin_url . 'a=appearance&'.
            C\p('CSRF_TOKEN')."=".$data[C\p('CSRF_TOKEN')];
        $icon_helper = $this->view->helper('iconlink');
        if (!empty($data['APPEARANCE_DOMAINS']) &&
            count($data['APPEARANCE_DOMAINS']) > 1) { ?>
            <div class="top-margin"><label for="appearance-domain"><?=
                tl('appearance_element_domain') ?></label><?php
                $this->view->helper("options")->render(
                    "appearance-domain", "APPEARANCE_DOMAIN",
                    $data['APPEARANCE_DOMAINS'],
                    $data['APPEARANCE_DOMAIN'] ?? "", false); ?>
            </div><?php
        }
        if (empty($data['APPEARANCE_DOMAIN'])) {
            /* the wiki-main-page landing toggle is a global setting;
               per-domain landing is handled by the domain's route,
               so this only shows for the global scope */
            ?>
            <div class="top-margin"><label for="landing-page"><?=
                tl('appearance_element_use_wiki_landing') ?></label>
            <input type="checkbox" id="landing-page"
                name="LANDING_PAGE" value='true' <?php
                if ($data['LANDING_PAGE'] == true) {
                    e("checked='checked'");} ?> ></div><?php
        }
        ?>
        <div class="top-margin"><?php
            $reset_url = $appearance_url . '&arg=reset';
            if (!empty($data['APPEARANCE_DOMAIN'])) {
                $reset_url .= '&APPEARANCE_DOMAIN=' .
                    $data['APPEARANCE_DOMAIN'];
            }
            ?>
            [<a href="<?= $reset_url ?>"><?=
                tl('appearance_element_reset_customizations') ?></a>]
        </div>
        <div class="top-margin"><label for="site-name"><?=
            tl('appearance_element_site_name') ?></label>
        <input type="text" id="site-name" name="SITE_NAME"
            maxlength="<?= C\NAME_LEN ?>" class="wide-field"
            value="<?= $form['SITE_NAME'] ?? '' ?>" ></div>
        <div class="top-margin"><label for="site-motto"><?=
            tl('appearance_element_site_motto') ?></label>
        <input type="text" id="site-motto" name="SITE_MOTTO"
            maxlength="<?= C\NAME_LEN ?>" class="wide-field"
            value="<?= $form['SITE_MOTTO'] ?? '' ?>" ></div>
        <div class="top-margin theme-row"><label for="theme"><?=
            tl('appearance_element_themes') ?></label><?php
            $this->view->helper("options")->render(
                "theme", "AUXILIARY_CSS_NAME", $data['themes'],
                $form['AUXILIARY_CSS_NAME'] ?? -1, true,
                ["class" => 'theme-dropdown']);
            e(" ");
            if (!empty($form['AUXILIARY_CSS_NAME']) &&
                $form['AUXILIARY_CSS_NAME'] != -1) {
                $icon_helper->renderButton("$appearance_url" .
                    "&edit_theme=true", "edit", "", false,
                    "source-anchor-button");
            }
            e(" ");
            $icon_helper->renderButton("$appearance_url" .
                "&add_theme=true&AUXILIARY_CSS=", "add", "", false,
                "source-anchor-button");?>
        </div>
        <div class="top-margin"><label for="back-color"><?=
            tl('appearance_element_background_color') ?></label>
        <input type="color" id="back-color"
            name="BACKGROUND_COLOR" class="narrow-field" value='<?=
            $form["BACKGROUND_COLOR"] ?>' ></div>
        <div class="top-margin">
        <table><tr>
        <td><label for="back-image"><?=
            tl('appearance_element_background_image')
            ?></label></td><td class="user-icon-td"><?php
            $image = (isset($form['BACKGROUND_IMAGE']) &&
            $form['BACKGROUND_IMAGE']) ? $form['BACKGROUND_IMAGE'] :
                C\SHORT_BASE_URL."/resources/drag.png";
        ?><img id='current-back-image' class="user-icon"
            src="<?= $image ?>" alt="<?=
            tl('appearance_element_background_image') ?>" ><?php
        $this->view->helper("fileupload")->render(
            'current-back-image',
            'BACKGROUND_IMAGE', 'back-image', C\THUMB_SIZE, 'image',
            ['image/png', 'image/gif', 'image/jpeg', 'image/webp',
            'image/x-icon']);
        ?></td></tr></table>
        </div>
        <div class="top-margin"><label for="fore-color"><?=
            tl('appearance_element_foreground_color') ?></label>
        <input type="color" id="fore-color"
            name="FOREGROUND_COLOR" class="narrow-field" value='<?=
            $form["FOREGROUND_COLOR"] ?>' ></div>
        <div class="top-margin"><label for="top-color"><?=
            tl('appearance_element_topbar_color') ?></label>
        <input type="color" id="top-color"
            name="TOPBAR_COLOR" class="narrow-field" value='<?=
            $form["TOPBAR_COLOR"]?>' ></div>
        <div class="top-margin"><label for="side-color"><?=
            tl('appearance_element_sidebar_color') ?></label>
        <input type="color" id="side-color"
            name="SIDEBAR_COLOR" class="narrow-field" value='<?=
            $form["SIDEBAR_COLOR"] ?>' ></div><?php
        foreach (['large' => tl('appearance_element_large_logo'),
            'medium' => tl('appearance_element_medium_logo'),
            'small' => tl('appearance_element_small_logo')] as
            $size => $label_size) {
            $upper_size = mb_strtoupper($size);
            $size_logo = $size . '-logo'; ?>
            <div class="top-margin">
            <table><tr>
            <td><label for="<?=$size_logo ?>"><?=$label_size
                ?></label></td><td class="user-icon-td"><?php
            $logo_field = 'LOGO_' . $upper_size;
            $current_logo = 'current-'. $size_logo;
            $image = (isset($form[$logo_field]) &&
                $form[$logo_field]) ? $form[$logo_field] :
                    C\SHORT_BASE_URL . "/resources/drag.png";
            ?><img id='<?= $current_logo ?>' class="user-icon"
                src="<?=$image ?>" alt="<?=$label_size ?>" ><?php
            $this->view->helper("fileupload")->render(
                $current_logo,
                $logo_field, $size_logo, C\THUMB_SIZE, 'image',
                ['image/png', 'image/gif', 'image/jpeg', 'image/webp',
                'image/x-icon']);
            ?></td></tr></table>
            </div><?php
        } ?>
        <div class="top-margin">
        <table><tr>
        <td><label for="favicon"><?=tl('appearance_element_favicon')
            ?></label></td><td class="user-icon-td"><?php
        $image = (isset($form['FAVICON']) &&
            $form['FAVICON']) ? $form['FAVICON'] :
                C\SHORT_BASE_URL . "/resources/drag.png";
        ?><img id='current-favicon' class="user-icon"
            src="<?=$form['FAVICON'] ?>" alt="<?=
            tl('appearance_element_favicon') ?>" ><?php
        $this->view->helper("fileupload")->render(
            'current-favicon',
            'FAVICON', 'favicon',  C\THUMB_SIZE, 'image',
            ['image/png', 'image/gif', 'image/jpeg', 'image/webp',
            'image/x-icon']);
        ?></td></tr></table>
        </div>
        <?php
    }
    /**
     * Used to draw the create/edit theme forms for the Appearance Acitivity
     *
     * @param array $data from the AdminController/SystemComponent with
     *    the code for the theme being edited
     */
    public function renderThemeForm($data)
    {
        /* the form shows the scope the admin picked, which the
           component keeps apart from the fields dressing this page */
        $form = $data['APPEARANCE_FORM'] ?? $data;
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $appearance_url = $admin_url . 'a=appearance&'.
            C\p('CSRF_TOKEN')."=".$data[C\p('CSRF_TOKEN')];
        $this->view->helper("close")->render($appearance_url);
        $icon_helper = $this->view->helper('iconlink');
        if ($data['LANDING_PAGE'] == true) {?>
            <input type="hidden" name="LANDING_PAGE" value='true' ><?php
        }
        ?>
        <div class="top-margin"><input type="hidden" name="edit_theme"
            value="true" ><label for="auxiliary-css-name"><?=
            tl('appearance_element_auxiliary_css_name') ?></label>
        <input type="text" name="AUXILIARY_CSS_NAME" maxlength="<?=C\NAME_LEN?>"
            placeholder="<?=tl('appearance_element_theme_name') ?>"
            <?=!empty($form['AUXILIARY_CSS_NAME']) ? ' style="' .
            'pointer-events:none; color: gray;" ' .
            'value="'. $form['AUXILIARY_CSS_NAME'] . '" ' :
            "" ?> > <?php
        if (empty($data['ADD_THEME'])) {
            $icon_helper->renderLink("$appearance_url" .
                "&arg=delete&AUXILIARY_CSS_NAME=".
                urlencode($form['AUXILIARY_CSS_NAME']), "delete", "", false);
        }?></div>
        <div class="top-margin"><label for="auxiliary-css"><?=
            tl('appearance_element_css_code') ?></label>
        <textarea class="tall-text-area" id="auxiliary-css"
            name="AUXILIARY_CSS" ><?=
            (!empty($form['AUXILIARY_CSS_NAME']) ||
            !empty($data['ADD_THEME'])) ?
                $form['AUXILIARY_CSS'] : "" ?></textarea>
        </div><?php
    }
}
X