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

use seekquarry\yioop as B;
use seekquarry\yioop\configs as C;
/**
 * This is a helper class is used to draw grouped view discussions for
 * group feeds on a variety on elements such as
 * ManageAccountElement, GroupfeedElement, ManagegroupElement.
 *
 * @author Chris Pollett
 */
class GrouplistHelper extends Helper
{
    /**
     * Method used to render grouped view discussions for group feeds on a
     * variety on elements such as  ManageAccountElement, GroupfeedElement,
     * ManagegroupElement.
     * @param array $groups groups to draw infor about their discussion feeds
     * @param string $token CSRF_TOKEN used for links
     * @param object $icon_helper helper class for drawing buttons
     * @param string $controller controller that view is being rendered on
     * @param string $context name of context element will be rendered in
     *   can be groups, manageaccount, admin, etc
     * @param bool $is_exact_group_query whether being used to draw
     *   info arising from a search query looking for exactly one group
     *  (i.e., a query of form =group_name)
     * @param array $modifiers optional flags that alter the render
     *   (e.g. column visibility, role-display toggles); empty array is
     *   the default no-modifier case
     */
    public function render($groups, $token, $icon_helper, $controller,
        $context = "groups", $is_exact_group_query = false, $modifiers = [])
    {
        $is_mobile = !empty($_SERVER['MOBILE']);
        $user_id = intval($_SESSION['USER_ID']) ?? C\PUBLIC_USER_ID;
        $is_root = $user_id === C\ROOT_ID;
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $pre_manage_group_url = $admin_url . "context=$context&amp;" .
            "$token&amp;a=manageGroups&amp;arg=groupsettings&amp;group_id=" ;
        $pre_info_group_url = $admin_url . "context=$context&amp;" .
            "$token&amp;a=manageGroups&amp;arg=infogroup&amp;group_id=" ;
        $group_url = "{$admin_url}a=manageGroups&amp;$token&amp;".
            "context=$context&amp;";
        $delete_url = $group_url . "arg=deletegroup&amp;";
        $unsubscribe_url = $group_url . "arg=unsubscribe&amp;";
        $add_url = $group_url . "arg=addgroup&amp;";
        $join_url = $group_url . "arg=joingroup&amp;";
        foreach ($groups as $group) {
            $group_name = $group['GROUP_NAME'] ?? "";
            $is_owner = !empty($group['OWNER_ID']) &&
                intval($group['OWNER_ID']) === $user_id;
            $manage_group = $icon_helper->renderButton(
                $pre_info_group_url . $group['GROUP_ID'],
                'information', "", true,
                "media-buttons-container small-margin",
                "media-anchor-button small-font", true);
            if ($is_owner || $is_root ) {
                $statistics_url = $group_url . "arg=statistics&amp;" .
                    'group_id='. $group['GROUP_ID'].'&amp;user_id=' .
                     $user_id;
                $manage_group = $icon_helper->renderButton(
                    $pre_manage_group_url . $group['GROUP_ID'], 'settings',
                    "", true, "media-buttons-container small-margin",
                    "media-anchor-button small-font", true);
                $statistics_link = $icon_helper->renderButton(
                    $statistics_url, 'statistics', "", true,
                    "media-buttons-container" ,
                    "media-anchor-button small-font", true);
            } else {
                $statistics_link = "";
            }
            $main_wiki_url = htmlentities(B\wikiUrl("", true,
                $controller, $group['GROUP_ID'])) . $token;
            $mobile_br = ($is_mobile) ? "<br>": "";
            $desk_b = ($is_mobile) ? "": "<b>";
            $desk_bend = ($is_mobile) ? "": "</b>";
            if ($context === "join_groups" &&
                in_array($group['REGISTER_TYPE'], [C\INVITE_ONLY_JOIN,
                C\REQUEST_JOIN]) && $user_id  != C\ROOT_ID &&
                !$is_exact_group_query) {
                continue;
            }
            ?>
            <div class="access-result"><?php
            $is_private = ($context === "join_groups") &&
                ($group['REGISTER_TYPE'] >= C\LOW_JOIN_FEE ||
                in_array($group['REGISTER_TYPE'],
                [C\INVITE_ONLY_JOIN, C\REQUEST_JOIN]));

            if ($is_owner && !in_array("cannot_delete_groups", $modifiers)) {
                $icon_helper->renderLink(
                    $delete_url . "group_id={$group['GROUP_ID']}", "delete",
                    "", false, "float-opposite medium-font");
            } else if ($context === "join_groups") {
                $register_type = $group['REGISTER_TYPE'];
                if ($register_type >= C\LOW_JOIN_FEE) {
                    $icon_helper->renderLink([
                        $add_url . "name={$group['GROUP_NAME']}&amp;".
                        "user_id=" . $user_id, $register_type],
                        "join_group_fee", "", false,
                        "float-opposite medium-font");
                } else if ($group['MEMBER_STATUS'] == C\INVITED_STATUS) {
                    $icon_helper->renderLink(
                        $join_url . "name={$group['GROUP_NAME']}&amp;".
                        "user_id=" . $user_id,
                        "join_invited_group", "", false,
                        "float-opposite medium-font");
                } else if (in_array($register_type, [C\REQUEST_JOIN,
                        C\PUBLIC_BROWSE_REQUEST_JOIN])) {
                    $icon_helper->renderLink(
                        $add_url . "name={$group['GROUP_NAME']}&amp;".
                        "user_id=" . $user_id,
                        "ask_join_group", "", false,
                        "float-opposite medium-font");
                } else if ($register_type  != C\INVITE_ONLY_JOIN ||
                    $user_id  == C\ROOT_ID) {
                    $icon_helper->renderLink(
                        $add_url . "name={$group['GROUP_NAME']}&amp;".
                        "user_id=" . $user_id,
                        "join_group", "", false,
                        "float-opposite medium-font");
                }
            } else {
                if ($group['MEMBER_STATUS'] == C\ACTIVE_STATUS) {
                    $icon_helper->renderLink(
                        $unsubscribe_url . "group_id={$group['GROUP_ID']}&amp;".
                            "user_id=" . $user_id, "leave_group",
                            "", false, "float-opposite medium-font");
                } else if ($group['MEMBER_STATUS'] == C\INVITED_STATUS) {
                    $icon_helper->renderLink(
                        $join_url . "name={$group['GROUP_NAME']}&amp;".
                        "user_id=" . $user_id,
                        "join_invited_group", "", false,
                        "float-opposite medium-font");
                } else {
                    $icon_helper->renderLink(
                        $unsubscribe_url . "group_id={$group['GROUP_ID']}&amp;".
                            "user_id=" . $user_id, "pending",
                            "", false, "float-opposite medium-font");
                }
            } ?>
            <div><b class="group-title-item"><?= $group_name ?></b><?php
            if (!$is_private) {
                $icon_helper->renderButton(
                    htmlentities(B\wikiUrl("Main", true,
                    "group", $group['GROUP_ID'])) . $token, 'wiki', "", false,
                    "media-buttons-container small-margin" ,
                    "media-anchor-button small-font", true);
                e(" ");
                $icon_helper->renderButton(
                    htmlentities(B\feedsUrl("group", $group['GROUP_ID'], true,
                    $controller)) . $token, 'group_feed',
                    $group['NEW_POSTS'] ?? 0,
                    false, "media-buttons-container small-margin" ,
                    "media-anchor-button small-font", true);
                e(" ");
                $icon_helper->renderButton(htmlentities(B\wikiUrl(
                    "pages", true, "group", $group['GROUP_ID'])) . $token,
                    'pages', "", false, "media-buttons-container small-margin" ,
                    "media-anchor-button small-font", true);
                e(" ". $statistics_link . " ");
            } ?><?php
            echo $manage_group;
            if (!$is_private) {
                 ?><?=$mobile_br ?><?=$desk_b?> (<?=
                tl('grouplist_helper_group_stats',
                $group['NUM_POSTS'], $group['NUM_THREADS']) ?>)<?=
                $desk_bend?><?php
             }?>
             </div><?php
             if (!$is_private) {?>
                <div class="slight-pad" >
                <b><?= tl('groupfeed_element_last_post')?></b><?php
                if ($group['THREAD_ID'] >= 0 || $group['NUM_THREADS'] > 0) {?>
                    <a href="<?= B\feedsUrl("thread", $group['THREAD_ID'],
                        true, $controller) . $token ?>" ><?=
                        $group['ITEM_TITLE'] ?></a><?php
                } else {
                    e($group['ITEM_TITLE']);
                } ?>
                </div><?php
            }
            ?>
            </div><?php
        }
    }
}
