<?php

/****************************************************
 * This File loads the Groups -> Members Tab
 * It allows to manage the groups members (users) 
 ****************************************************/
$groups = new userGroups();
$mode = $_REQUEST['mode'];
if ($mode == "update") {
    $group_temp = $groups->getGroup($_REQUEST['id']);
    $groups->write("Groups access to <b>" . $group_temp->name . "</b> updated.");
}
if ($_REQUEST['id'] == "" && ($mode == "edit" || $mode == "update")) {
    $groups->write("Invalid group.");
}
$cancel = $_REQUEST['cancel'];
switch ($cancel) {
    case 1:
        $groups->write("Group members edit canceled.");
        break;
    default:
        break;
}
?>

<div class="wrap">
<?php 
//prints a page with its groups and then prints the same for its children
function userGroups_PrintGroupMembers()
{
    $groups = new userGroups();
    $alt = pagesByParent($children, $alt, $groups, $group_id, $level + 1);
    return $alt;
}
function pagesByParent($paginas, $alt, $groups, $group_id, $level = 0)
{
    if (isset($paginas) && count($paginas) > 0) {
        foreach ($paginas as $pagina) {
            $alt = printPage($pagina, $alt, $groups, $group_id, $level);
        }
    }
    return $alt;
}
switch ($mode) {
    case "edit":
        if (isset($_REQUEST['id'])) {
            $group = $groups->getGroup($_REQUEST['id']);
            $paginas = $groups->getAllPagesWithGroup($group->id);
            echo "<h2>Edit Pages Access for '" . $group->name . "'</h2>";
            echo '<form id="readWrite" name="readWrite" action="' . $_SERVER['PHP_SELF'] . '?page=wp-group-restriction/manage_pages&amp;mode=update&amp;id=' . $group->id . '" method="post">';
            echo "<input type=\"hidden\" name=\"groupName\" id=\"groupName\"  value=\"" . $group->name . "\" />";
            echo '<script type="text/javascript"><!--
      
      function select_all(name, value) {
        formblock = document.getElementById("readWrite");
        forminputs = formblock.getElementsByTagName("input");
        for (i = 0; i < forminputs.length; i++) {
          // regex here to check name attribute
          var regex = new RegExp(name, "i");
          if (regex.test(forminputs[i].getAttribute("name"))) {
            forminputs[i].checked = value;
          }
             $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;
     break;
 case "editSubmit":
     //to continue edit
     $group->name = $_POST['groupName'];
     $group->prev_name = $_POST['prevName'];
     $group->id = $_POST['groupID'];
     $group->homepage = $_POST['groupPage'];
     $group->description = $_POST['groupDesc'];
     if (!$groups->isValidName($_POST['groupName']) && $_POST['groupName'] != $_POST['prevName']) {
         if ($_POST['groupName'] == "") {
             $errorMessage = "Please specify a name for the group.";
             $mode = "edit";
         } else {
             $errorMessage = "A group with the name <b>" . $_POST['groupName'] . "</b> already exists.";