<?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 AdminmenuElement extends Element
{
    /**
     * Method responsible for drawing the side menu portion of an admin
     * page. This allows the user to signout or select from among allowed admin
     * activities
     *
     * @param array $data has info draw activity links on page
     */
    public function render($data)
    {
        $logged_in = !empty($data["ADMIN"]);
        $is_submenu = false;
        $admin_prefix = "";
        if (!empty($data['MENU']) && $data["MENU"] != 'adminmenu' &&
            !$logged_in) {
            return;
        } else if (!empty($data['MENU']) && $data["MENU"] != 'adminmenu' &&
            $logged_in) {
            $is_submenu = true;
            $admin_prefix = "admin-";
        }
        $token_string = ($logged_in) ? C\p('CSRF_TOKEN') . "=" .
            $data[C\p('CSRF_TOKEN')]
            : "";
        $logo = $this->view->helper("logo")->renderResponsive($data,
            $this->view->logo_alt_text ?? "");
        ?>
        <nav id="<?=$admin_prefix ?>menu-options" class="menu-options">
            <?php
            if ($is_submenu) {?>
            <div class="align-opposite menu-toggle"><a
                class="gray-link"
                onclick="javascript:setDisplay('admin-menu-options', false);
                setDisplay('menu-options', true);return false;"
                href="#menu-options"><?=
                $data['MENU_NAME']?>&Gt;</a></div><?php
            }
            foreach ($data['COMPONENT_ACTIVITIES'] as
                $component_name => $activities) {
                $count = count($activities);
                /* An activity is passed over further down where the
                   account screen has its own place, or where mail is
                   turned off. A heading whose activities are all passed
                   over would stand with nothing under it, so they are
                   counted before the heading is drawn. */
                $drawn = 0;
                $mail_off = !C\nsdefined("MAIL_MODE") ||
                    C\p('MAIL_MODE') == 'disabled';
                foreach ($activities as $one) {
                    if ($one['METHOD_NAME'] == "manageAccount") {
                        continue;
                    }
                    if ($one['METHOD_NAME'] == "userMail" && $mail_off) {
                        continue;
                    }
                    $drawn++;
                }
                if ($drawn == 0) {
                    continue;
                }
                ?>
                <h2 class="option-heading"><?=$component_name ?><?php
                ?></h2>
                <ul class='square-list'><?php
                for ($i = 0 ; $i < $count; $i++) {
                    $method_name = $activities[$i]['METHOD_NAME'];
                    if ($method_name == "manageAccount") {
                        continue;
                    }
                    if ($method_name == "userMail" &&
                        (!C\nsdefined("MAIL_MODE") ||
                        C\p('MAIL_MODE') == 'disabled')) {
                        continue;
                    }
                    if ($method_name == 'groupFeeds') {
                        $feeds_url = B\controllerUrl("group", true) .
                            $token_string;
                        e("<li><a href='" . $feeds_url . "&amp;v=ungrouped' >"
                            . tl('adminmenu_element_discussions').
                            "</a></li>");
                        e("<li><a href='" . $feeds_url . "&amp;v=grouped' >"
                            . tl('adminmenu_element_my_groups') . "</a></li>");
                    } else if ($method_name == 'userMessages') {
                        $messages_url = B\controllerUrl("user_messages", true) .
                            $token_string;
                        e("<li><a id='messages-activity-link' " .
                            "href='$messages_url'>"
                            . $activities[$i]['ACTIVITY_NAME'] .
                            "</a></li>");
                    } else if ($method_name == 'userMail') {
                        $mail_url = B\controllerUrl("user_mail", true) .
                            $token_string;
                        e("<li><a href='$mail_url'>"
                            . $activities[$i]['ACTIVITY_NAME'] .
                            "</a></li>");
                    }  else if ($method_name == 'manageGroups') {
                        $groups_url = B\controllerUrl("admin", true) .
                            $token_string . "&amp;a=" .
                            $activities[$i]['METHOD_NAME'];
                        e("<li><a href='$groups_url&amp;browse=true'>"
                            . tl('adminmenu_element_join_groups') .
                            "</a></li>");
                    } else if ($method_name == 'manageAccount') {
                        e("<li><a href='"
                            . B\controllerUrl("admin", true)
                            . C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')]
                            . "&amp;a="
                            . $activities[$i]['METHOD_NAME']."'>"
                            . tl('adminmenu_element_account_home') .
                            "</a></li>");
                    } else {
                        e("<li><a href='"
                            . B\controllerUrl("admin", true)
                            . C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')]
                            . "&amp;a="
                            . $activities[$i]['METHOD_NAME']."'>"
                            . $activities[$i]['ACTIVITY_NAME'] .
                            "</a></li>");
                    }
                }
                ?>
                </ul>
                <?php
            }
            ?>
        </nav>
        <?php
    }
}
