/ src / views / elements / GroupmenuElement.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;
use seekquarry\yioop\library as L;
use seekquarry\yioop\library\CrawlConstants;
/**
 * Element responsible for drawing the menu side bar for group and
 * wiki pages. These options include recently viewed wiki pages, groups, and
 * threads
 *
 * @author Chris Pollett
 */
class GroupmenuElement extends Element implements CrawlConstants
{
    /**
     * Method responsible for drawing the menu side bar  for group and
     * wiki pages. These item include recently viewed wiki pages, groups, and
     * threads
     *
     * @param array $data needed to populate the links on page
     */
    public function render($data)
    {
        $logged_in = !empty($data["ADMIN"]);
        $is_wiki = isset($data['ELEMENT']) && $data['ELEMENT'] == 'wiki';
        $token_string = ($logged_in && isset($data[C\p('CSRF_TOKEN')])) ?
            C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')] : "";
        $base_query = ($is_wiki) ? htmlentities(B\wikiUrl("", true,
            $data['CONTROLLER'], $data["GROUP"]["GROUP_ID"])) : B\feedsUrl("",
                "", true, $data['CONTROLLER']) . $token_string;
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $register_url = htmlentities(B\controllerUrl('register', true));
        $can_edit = $logged_in && !empty($data["CAN_EDIT"]);
        ?>
        <nav id="menu-options" class="menu-options">
        <?php
            if ($logged_in) {
                $messages_url = B\controllerUrl("user_messages", true) .
                    $token_string;
                $mail_url = B\controllerUrl("user_mail", true) .
                    $token_string;
                $mail_enabled = C\nsdefined("MAIL_MODE") &&
                    C\p('MAIL_MODE') !== 'disabled';
                ?>
                <div class="menu-toggle"><a
                    class="gray-link"
                    onclick="javascript:setDisplay('admin-menu-options', true);
                    setDisplay('menu-options', false); return false;"
                    href="#admin-menu-options" >&Lt;<?=
                    tl('groupmenu_element_admin_menu'); ?></a></div>
                <h2 class="option-heading"><?=tl('groupmenu_element_personal');
                    ?></h2>
                <ul class="square-list">
                    <li><a href="<?= $messages_url ?>"><?=
                        tl('groupmenu_element_messages');?></a></li>
                    <?php if ($mail_enabled) { ?>
                    <li><a href="<?= $mail_url ?>"><?=
                        tl('groupmenu_element_mail');?></a></li>
                    <?php } ?>
                </ul>
                <?php
            }
            if (isset($data['ELEMENT']) && $data['ELEMENT'] == 'wiki') {
                $human_page_name = str_replace("_", " ",
                    $data['PAGE_NAME'] ?? "");
                if (!empty($data['SUB_PATH'])) {
                    $full_human_page_name = $human_page_name ."/".
                        $data['SUB_PATH'];
                } else {
                    $full_human_page_name = $human_page_name;
                }
                $options = [];
                if ($data["MODE"] != 'pages') {
                    $options = [ tl('groupmenu_element_page',
                        $full_human_page_name,
                        $data['GROUP']['GROUP_NAME']) => ""];
                    if ($can_edit) {
                        $modes = [
                            "read" => tl('groupmenu_element_view'),
                            "edit" => tl('groupmenu_element_edit'),
                            "history" => tl('groupmenu_element_history'),
                        ];
                    } else if (!empty($data['NO_HISTORY_SOURCE'])) {
                        $modes = [
                            "read" => tl('groupmenu_element_view'),
                        ];
                    } else {
                        $modes = [
                            "read" => tl('groupmenu_element_view'),
                            "source" => tl('groupmenu_element_source'),
                            "history" => tl('groupmenu_element_history'),
                        ];
                    }
                    if (!empty($data['PAGE_HAS_RELATIONSHIPS']) ) {
                        $relationship_mode = [
                            "relationships" =>
                                tl('groupmenu_element_relationships'),
                        ];
                        $modes = array_merge($modes, $relationship_mode);
                    }
                    /* A repository's talk happens on its issues rather
                       than on a thread of the page itself, so what would
                       be the discussion entry is the issue tracker. */
                    $is_repository = ($data['page_type'] ??
                        $data['HEAD']['page_type'] ?? '') ==
                        'git_repository';
                    if ($is_repository) {
                        $modes["issues"] =
                            tl('groupmenu_element_issue_tracker');
                    } else if (!empty($data['DISCUSS_THREAD']) &&
                        empty($data['NO_HISTORY_SOURCE'])) {
                        $modes[htmlentities(B\feedsUrl("thread",
                            $data['DISCUSS_THREAD'], true,
                            "group")) . $token_string] =
                            tl('groupmenu_element_discussion');
                    }
                }
                $modes[tl('groupmenu_element_group',
                    $data['GROUP']['GROUP_NAME'])] = "";
                $group_page_list_off =
                    isset($data['GROUP']['PAGE_LIST_ALLOWED']) &&
                    !$data['GROUP']['PAGE_LIST_ALLOWED'];
                if ($logged_in || !$group_page_list_off) {
                    $modes["pages"] = tl('groupmenu_element_create_find',
                        $data['GROUP']['GROUP_NAME']);
                }
                $modes[htmlentities(
                    B\feedsUrl("group", $data["GROUP"]["GROUP_ID"],
                    true, 'group')) . $token_string] =
                    tl('groupmenu_element_group_feed');
                $modes[B\controllerUrl('group', true).
                    $token_string] = tl('groupmenu_element_mygroups');
                $selected_url = "";
                foreach ($modes as $name => $translation) {
                    if (empty($translation) && !empty($name)) {
                        $options[$name] = $translation;
                        continue;
                    }
                    $append = "";
                    $page_name = ($name == 'pages') ?
                        'pages' : $data['PAGE_NAME'];
                    $amp = (empty($token_string)) ? "" : "&";
                    if (in_array($name, ['history', 'relationships'])) {
                        $page_id = (empty($data['PAGE_ID'])) ? "" :
                            $data['PAGE_ID'];
                        $append .= "{$amp}arg=$name&page_id=" . $page_id;
                        $amp = "&";
                    }
                    if (in_array($name, ['source', 'edit'])) {
                        $append .= "{$amp}arg=$name";
                        $amp = "&";
                    }
                    /* Which locale of the page is being looked at goes
                       with every way of looking at it, so reading its
                       history or its source stays in the same language
                       rather than falling back to the one the reader has
                       the site in. */
                    if (in_array($name, ['history', 'source', 'edit',
                        'read']) && !empty($data['CURRENT_LOCALE_TAG'])) {
                        $append .= "{$amp}page_locale=" .
                            urlencode($data['CURRENT_LOCALE_TAG']);
                        $amp = "&";
                    }
                    if ($name == 'issues') {
                        $append .= "{$amp}arg=read&repo_view=issues";
                        $amp = "&";
                        $page_name = $data['PAGE_NAME'];
                    }
                    if (!empty($data['SUB_PATH'])) {
                        $append .= "{$amp}sf=".urlencode($data['SUB_PATH']);
                        $amp = "&";
                    }
                    if (isset($_REQUEST['noredirect'])) {
                        $append .= "{$amp}noredirect=true";
                        $amp = "&";
                    }
                    if (isset($data['OTHER_BACK_URL'])) {
                        $append .= $data['OTHER_BACK_URL'];
                    }
                    if (!str_contains($name, C\SHORT_BASE_URL)) {
                        $url = htmlentities(B\wikiUrl(
                            $page_name, true, $data['CONTROLLER'],
                            $data["GROUP"]["GROUP_ID"])) . $token_string .
                            $append;
                    } else {
                        $url = $name . $append;
                    }
                    if ($data["MODE"] == $name) {
                        $selected_url = $url;
                    }
                    $options[$url] = $translation;
                }
                $sub_path = $this->view->element("wiki")->renderPath(
                    'page-path', $data,
                    $options, $selected_url, "",
                    "just_groups_and_pages", true);
            } else if (isset($data['JUST_THREAD']) &&
                !empty($data['PAGES'][0])) {
                if (isset($data['WIKI_PAGE_NAME'])) {
                    $wiki_url = htmlentities(B\wikiUrl(
                        $data['WIKI_PAGE_NAME'], true,
                        $data['CONTROLLER'],$data['PAGES'][0]["GROUP_ID"])).
                        $token_string;
                    $group_base_query = $base_query . $token_string;
                    $group_name = $data['PAGES'][0][self::SOURCE_NAME];
                    $paths = [$group_base_query =>
                        tl('groupmenu_element_page_talk',
                        $data['WIKI_PAGE_NAME']),
                        $wiki_url => tl('groupmenu_element_wiki',
                        $data['WIKI_PAGE_NAME'])
                        ];
                    $groupsfeed_url = htmlentities(B\feedsUrl("group",
                        "", false, $data['CONTROLLER'])).
                        $token_string;
                    $source_name = $data['PAGES'][0][self::SOURCE_NAME] ?? "";
                    $this->view->element("groupfeed")->renderPath($data, $paths,
                         "", $groupsfeed_url,
                         $source_name, "", true);
                } else {
                    $groupfeed_url = htmlentities(B\feedsUrl("group",
                        $data['PAGES'][0]["GROUP_ID"], true,
                        $data['CONTROLLER'])) . $token_string;
                    $groupfeed_group_url = htmlentities(B\feedsUrl("group",
                        $data['PAGES'][0]["GROUP_ID"], true, "group")) .
                        $token_string;
                    $groupwiki_url = htmlentities(B\wikiUrl("Main", true,
                        $data['CONTROLLER'], $data['PAGES'][0]["GROUP_ID"])).
                        $token_string;
                    $thread_url = B\feedsUrl("thread", $data['JUST_THREAD'],
                        true, $data['CONTROLLER']) . $token_string;
                    $group_base_query = B\feedsUrl("", "", true,
                        $data['CONTROLLER']) . $token_string;
                    $paths = [
                        $thread_url => tl("groupmenu_element_thread",
                        $data['SUBTITLE']),
                        $groupfeed_url =>
                        tl("groupmenu_element_group_feed")];
                    $source_name = $data['PAGES'][0][self::SOURCE_NAME] ?? "";
                    $this->view->element('groupfeed')->renderPath($data, $paths,
                         $groupwiki_url, $group_base_query,
                         $source_name, "", true);
                }
            } else if (isset($data['JUST_GROUP_ID'])) {
                $groupfeed_url = htmlentities(B\feedsUrl("group",
                    $data['JUST_GROUP_ID'], true, $data['CONTROLLER'])).
                    $token_string;
                $groupfeed_group_url = htmlentities(B\feedsUrl("group",
                    $data['JUST_GROUP_ID'], true, "group")).
                    $token_string;
                $amp = (empty($token_string)) ? "" : "&";
                $groupwiki_url = htmlentities(B\wikiUrl("Main", true,
                    $data['CONTROLLER'], $data['JUST_GROUP_ID'])).
                    $token_string;
                $group_base_query = B\feedsUrl("", "", true,
                    $data['CONTROLLER']) . $token_string;
                $paths = [
                    $groupfeed_url => tl("groupmenu_element_group_feed")];
                $this->view->element('groupfeed')->renderPath($data, $paths,
                    $groupwiki_url, $group_base_query, $data['SUBTITLE']
                    , "", true);
            } else if (isset($data['JUST_USER_ID'])) {
                if (empty($data['PAGES'][0]["USER_NAME"])) {
                    e(tl("groupmenu_element_no_path_info"));
                } else {
                    $viewed_user_name = $data['PAGES'][0]["USER_NAME"];
                    $userfeed_url = htmlentities(B\feedsUrl("user",
                        $data['JUST_USER_ID'], true, $data['CONTROLLER'])).
                        $token_string;
                    $amp = (empty($token_string)) ? "" : "&";
                    $group_base_all = B\feedsUrl("", "", true,
                        $data['CONTROLLER']) . $token_string;
                    $paths = [
                        $userfeed_url => tl("groupmenu_element_userfeed",
                            $viewed_user_name)];
                    $this->view->element('groupfeed')->renderPath($data, $paths,
                         $group_base_all, $userfeed_url, $viewed_user_name,
                         "user", true);
                }
            } else {
                $paths = [];
                $this->view->element('groupfeed')->renderPath($data, $paths, "",
                    $base_query, tl('groupmenu_element_mygroups'),
                    "just_group_and_thread", true);
            }
            ?>
        </nav>
        <?php
    }
}
X