<?php

/*************************************************************
 * This File loads the "Groups -> Groups" Tab
 * It allows to manage Groups by editing, adding or deleting
 ************************************************************/
$groups = new userGroups();
$mode = $_REQUEST['mode'];
$cancel = $_REQUEST['cancel'];
$errorMessage = "";
switch ($mode) {
    case "add":
        if (!$groups->isValidName($_POST['groupName'])) {
            if ($_POST['groupName'] == "") {
                $errorMessage = "Please specify a name for the group.";
            } else {
                $errorMessage = "A group with the name <b>" . $_POST['groupName'] . "</b> already exists.";
            }
        } else {
            if ($groups->createGroup($_POST['groupName'], $_POST['groupPage'], $_POST['groupDesc'])) {
                $message = "Group <b>" . $_POST['groupName'] . "</b> created successfuly. ";
                $group = $groups->getGroupByName($_POST['groupName']);
                $message .= "<a href=\"?page=wp-group-restriction/group_members&mode=edit&id={$group->id}\">Add users to the group &raquo;</a>";
                $groups->write($message);
            }
        }
        break;
    case "edit":
        $id = $_REQUEST['id'];
        $group = $groups->getGroup($id);
        $group->prev_name = $group->name;