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

/**
 * Element responsible for drawing the side menu portion of an admin
 * page. This allows the user to signout or select from among allowed admin
 * activities
 *
 * @author Chris Pollett
 */
class WelcomemenuElement extends Element
{
    /**
     * Method responsible for drawing the side menu portion of welcome
     * part of an admin  page. This allows the user to signout or go to
     * the account home
     *
     * @param array $data has info draw activity links on page
     */
    public function render($data)
    {
        $logged_in = !empty($data["ADMIN"]);
        $token_string = ($logged_in) ? C\p('CSRF_TOKEN') . "=" .
            $data[C\p('CSRF_TOKEN')]
            : "";
        $register_url = htmlentities(B\controllerUrl('register', true));
        $logo = $this->view->helper("logo")->renderResponsive($data,
            $this->view->logo_alt_text ?? "");?>
        <?php
        if (!$logged_in) {?>
            <nav id="welcome-menu-options" class="menu-options">
            <?php $this->view->helper("hamburger")->render(
                $logged_in, false); ?>
            <h2><a href="<?=C\SHORT_BASE_URL ?><?php
            ?>"><?php e($logo); ?></a></h2>
            <h2 class="menu-option-heading"><?=
                tl('welcomehmenu_element_welcome') ?></h2>
                <ul class='square-list'>
                <li><a href="<?= B\controllerUrl('admin') ?>"><?=
                tl('welcomemenu_element_signin') ?></a></li><?php
                if (in_array(C\p('REGISTRATION_TYPE'), ['no_activation',
                    'email_registration', 'admin_activation'])) { ?>
                    <li><a href="<?=rtrim($register_url .
                        "a=createAccount&amp;" .
                        $token_string, "?&amp;") ?>"><?=
                        tl('welcomemenu_element_create_account')
                        ?></a></li>
                    <?php
                }?>
                </ul>
            </nav><?php
            return;
        }?>
        <nav id="welcome-menu-options" class="menu-options">
            <?php $this->view->helper("hamburger")->render(
                $logged_in, false); ?>
            <h2><a href="<?=C\SHORT_BASE_URL ?><?php
            if ($logged_in) {
                e("?$token_string");
            }
            ?>"><?php e($logo); ?></a></h2>
            <?php
            /* The account screen is what this menu offers, together
               with the link that signs a user out. It is looked for by
               name rather than taken from the front of the list,
               because a component added later can sort ahead of it. */
            $activities = [];
            foreach ($data['COMPONENT_ACTIVITIES'] as $group) {
                if (!empty($group[0]['METHOD_NAME']) &&
                    $group[0]['METHOD_NAME'] == 'manageAccount') {
                    $activities = $group;
                    break;
                }
            }
            if (empty($activities)) {
                return;
            }
            $component_name = tl('welcomemenu_element_welcome_user',
                $_SESSION['USER_NAME']);
            $count = count($activities);
            ?>
            <h2 class="menu-option-heading"><?=$component_name ?></h2>
            <ul class='square-list'>
            <li><a href='<?= B\controllerUrl("admin", true) .
                C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')] . "&amp;a=" .
                $activities[0]['METHOD_NAME']?>' ><?=
                tl('welcomemenu_element_account_home');
            ?></a></li>
            <li><b><a href="<?=C\SHORT_BASE_URL ?>?a=signout"><?=
                tl('welcomemenu_element_signout') ?></a></b></li>
            </ul>
        </nav>
        <?php
    }
}
