<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/********************************************************/
/* NSN Groups                                           */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright  2000-2005 by NukeScripts Network         */
/********************************************************/
defined('MAXIMUS') or Header("Location: ../../index.php");

$module_name = "Groups";
$index = 1;
get_lang($module_name);

switch ($op) {

    default:
        incoh();
        title(_GR_PUBLICGROUPS);
        Max_OpenTable();
        echo "<center><table border='0' cellpadding='2' cellspacing='2' bgcolor='$bgcolor2'>\n";
        echo "<tr bgcolor='$bgcolor2'>\n";
        echo "<td align='center' width='200'><b>"._GR_GRPNAME."</b></td>\n";
        echo "<td align='center' width='100'><b>"._GR_NUMUSERS."</b></td>\n";
        echo "<td align='center' width='100'><b>"._GR_LIMIT."</b></td>\n";
        echo "<td align='center' width='100'><b>&nbsp;</b></td>\n";
        echo "</tr>\n";
        $result = $GLOBALS['db']->sql_query("SELECT `gid`, `gname`, `glimit` FROM `".TABLE_NSNGR_GROUPS."` WHERE `gpublic`>'0' ORDER BY `gname`");
        if ($GLOBALS['db']->sql_numrows($result) > 0) {
            while(list($gid, $gname, $glimit) = $GLOBALS['db']->sql_fetchrow($result)) {
                $numusers = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `uid` FROM ".TABLE_NSNGR_USERS." WHERE `gid`='$gid'"));
                echo "<tr bgcolor='$bgcolor1' onmouseover='this.style.backgroundColor='$bgcolor2'' onmouseout='this.style.backgroundColor='$bgcolor1''>\n";
                echo "<td align='center'>$gname</td>\n";
                echo "<td align='center'>$numusers</td>\n";
                if ($glimit == 0) {
                $gmlimit = _GR_NOLIMIT;
                } elseif ($glimit <= $numusers) {
                $gmlimit = _GR_FILLED;
                } else {
                $gmlimit = $glimit;
                }
                echo "<td align='center'>$gmlimit</td>\n";
                echo "<td align='center'>\n";
                echo "<a href='modules.php?name=$module_name&amp;op=info&amp;gid=$gid'><img src='modules/$module_name/images/info.png' height='16' width='36' border='0' alt='"._GR_INFO."' title='"._GR_INFO."'></a>\n";
                if (($glimit == 0 OR $numusers < $glimit) AND !in_group($gid)) {
                    echo "<a href='modules.php?name=$module_name&amp;op=join&amp;gid=$gid'><img src='modules/$module_name/images/join.png' height='16' width='36' border='0' alt='"._GR_JOIN."' title='"._GR_JOIN."' /></a>\n";
                } else {
                    echo "<img src='modules/$module_name/images/blank.png' height='16' width='36' border='0' alt='' />\n";
                }
                echo "</td>\n";
                echo "</tr>\n";
            }
        } else {
            echo "<tr bgcolor='$bgcolor1'><td align='center' colspan='4'>"._GR_NOPUBLICGROUPS."</td></tr>\n";
        }
        echo "</table></center>\n";
        Max_CloseTable();
        incof();
    break;

    case "info":
        $gid = $GLOBALS['db']->XMFnum($gid);
        incoh();
        title(_GR_GROUPINFO."");
        Max_OpenTable();
        echo "<center><table border='0' cellpadding='2' cellspacing='2' bgcolor='$bgcolor1'>\n";
        $result = $GLOBALS['db']->sql_query("SELECT `gid`, `gname`, `gdesc`, `glimit` FROM `".TABLE_NSNGR_GROUPS."` WHERE `gpublic`>'0' AND `gid`='$gid'");
        if ($GLOBALS['db']->sql_numrows($result) > 0) {
            while(list($gid, $gname, $gdesc, $glimit) = $GLOBALS['db']->sql_fetchrow($result)) {
                echo "<tr><td bgcolor='$bgcolor2'><b>"._GR_GRPNAME.":</b></td><td>$gname ";
                if (($glimit == 0 OR $glimit <= $numusers) AND !in_group($gid)) {
                    echo "<a href='modules.php?name=$module_name&amp;op=join&amp;gid=$gid'><img src='modules/$module_name/images/join.png' height='16' width='36' border='0' alt='"._GR_JOIN."' title='"._GR_JOIN."' /></a>\n";
                } else {
                    echo "<img src='modules/$module_name/images/blank.png' height='16' width='36' border='0' alt='' />\n";
                }
                echo "</td></tr>\n";
                $numusers = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `uid` FROM `".TABLE_NSNGR_USERS."` WHERE `gid`='$gid'"));
                echo "<tr><td bgcolor='$bgcolor2'><b>"._GR_NUMUSERS.":</b></td><td>$numusers</td></tr>\n";
                if ($glimit == 0) {
                $gmlimit = _GR_NOLIMIT;
                } elseif ($glimit <= $numusers) {
                $gmlimit = _GR_FILLED;
                } else {
                $gmlimit = $glimit;
                }
                echo "<tr><td bgcolor='$bgcolor2'><b>"._GR_LIMIT.":</b></td><td>$gmlimit</td></tr>\n";
                echo "<tr><td bgcolor='$bgcolor2' valign='top'><b>"._GR_DESCRIPTION.":</b></td><td>".nl2br($gdesc)."</td>\n";
                echo "</tr>\n";
            }
        } else {
            echo "<tr bgcolor='$bgcolor1'><td align='center' colspan='2'>"._GR_NOPUBLICGROUP."</td></tr>\n";
        }
        echo "</table></center><br />\n";
        echo "<center>"._GOBACK."</center>\n";
        Max_CloseTable();
        incof();
    break;

    case "join":
        incoh();
        $result = $GLOBALS['db']->sql_query("SELECT `glimit`, `gpublic` FROM `".TABLE_NSNGR_GROUPS."` WHERE `gid`='$gid'");
        list($glimit, $gpublic) = $GLOBALS['db']->sql_fetchrow($result);
        $numusers = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `uid` FROM `".TABLE_NSNGR_USERS."` WHERE `gid`='$gid'"));
        cookiedecode($user);
        $uid = $cookie[0];
        $gid = $GLOBALS['db']->XMFnum($gid);
        title(_GR_GROUPJOIN);
        Max_OpenTable();
        if (is_ingroup($uid,$gid)) {
            echo "<center><b>"._GR_INGROUP."</b></center><br />\n";
            echo "<center>"._GOBACK."</center>\n";
        } elseif ($gpublic == 0) {
            echo "<center><b>"._GR_NOTPUBLIC."</b></center><br />\n";
            echo "<center>"._GOBACK."</center>\n";
        } elseif ($glimit <= $numusers AND $glimit != 0) {
            echo "<center><b>"._GR_GROUPFILLED."</b></center><br />\n";
            echo "<center>"._GOBACK."</center>\n";
        } else {
            $xdate = time();
            $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNGR_USERS."` (`gid`, `uid`, `sdate`) VALUES ('$gid', '$uid', '$xdate')");
            echo "<center><b>"._GR_ADDGROUP."</b></center><br />\n";
            echo "<center>"._GOBACK."</center>\n";
        }
        Max_CloseTable();
        incof();
    break;

}