wiclear-2007-07-19/admin/inc/lib/groups.lib.php

<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of WiClear.
# Copyright (c) 2004-2007 David Jobet. All rights
# reserved.
#
# WiClear 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, or
# (at your option) any later version.
#
# WiClear 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 DotClear; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
#
# ***** END LICENSE BLOCK *****

$adminBoxHandle['groups']          = 'groupsDisplay';
$adminBoxHandle['group_view']      = 'groupDisplay';

$adminActionHandle['group_delete'] = 'onGroupDelete';
$adminActionHandle['group_add']    = 'onGroupAdd';

function groupsDisplay()
{
  global $wiki;

  $groups = $wiki->getGroups();

  $form = new form(createAdminURL(array('mode'=>'group_add')), 'post');

  if (existVarSession('error'))
  {
    $error = varSession('error');
    $form->addHtml('<h1>'.$error.'</h1>');
    unsetVarSession('error');
  }

  $form->addFieldset();
  $form->addLegend(tr('Groups'));

  if (empty($groups))
  {
    $form->addHtml('<h1>'.tr('No groups yet').'</h1>');
  }
  else
  {
    $blob =
    '<table>'.
    '<tr><th>'.tr('Group').'</th><th>'.tr('Delete').'</th></tr>';

    foreach ($groups as $group)
    {
      $blob .=
      '<tr>'.
        '<td><a href="'.createAdminURL(array('mode'=>'group_view', 'id'=>$group->id)).'">'.$group->name.'</a></td>'.
        '<td><a href="'.createAdminURL(array('mode'=>'group_delete', 'id'=>$group->id)).'">'.tr('delete').'</a></td>'.
      '</tr>';
    }

    $blob .=
    '</table>';

    $form->addHtml($blob);
  }

  $form->addHtml('<p class="field">');
  $form->addHidden('check', '1');
  $form->addHtml('</p>');
  $form->addHtml('<p class="field">');
  $form->addButton('AddGroup', tr('Add group'), 'action');
  $form->addInput('group_name', 'Group name');
  $form->addHtml('</p>');
  $form->addHtml('<p class="field"/>');

  return $form->toHtml();
}

function onGroupAdd()
{
  if (!isset($_POST['check']))
  {
    return;
  }

  global $wiki;

  $groupName = varPost('group_name');

  if ($wiki->groupExist($groupName))
  {
    setVarSession('error', tr('Group').' '.$groupName.' '.tr('already exists'));
    session_write_close();
    header('Location: '.createHeaderAdminURL(array('mode'=>'groups')));
    return;
  }

  $id = $wiki->addGroup($groupName);
  header('Location: '.createHeaderAdminURL(array('mode'=>'group_view', 'id'=>$id)));
}

function onGroupDelete()
{
  global $wiki;

  $id = varGet('id');
  $wiki->delGroup($id);
  header('Location: '.createHeaderAdminURL(array('mode'=>'groups')));
}

function groupDisplay()
{
  global $wiki;

  $id    = varGet('id');

  if (isset($_POST['check']))
  {
    if (isset($_POST['add']) && isset($_POST['available_users']))
    {
      $wiki->addUsersToGroup($id, varPost('available_users'));
    }
    else
    if (isset($_POST['remove']) && isset($_POST['group_users']))
    {
      $wiki->removeUsersFromGroup($id, varPost('group_users'));
    }
  }

  $group = $wiki->getGroup($id);

  $form = new form(createAdminURL(array('mode'=>'group_view', 'id'=>$id)), 'post');
  $form->addFieldset();
  $form->addLegend($group->name);

  //id, login, admin, temp_key
  $availableUsers = array();
  $groupUsers = array();

  $users = $wiki->getAllUsersRights();
  foreach ($users as $r)
  {
    $user = new user($r[0], $r[1], '', 0, '', '', '', '', '', 0);
    if (!$group->validates($user))
    {
      $availableUsers[$r[1]] = $r[0];
    }
    else
    {
      $groupUsers[$r[1]] = $r[0];
    }
  }
  $form->addListBox($availableUsers, array(), 'available_users', 10);
  $form->addButton('add', '&gt;&gt;');
  $form->addButton('remove', '&lt;&lt;');
  $form->addListBox($groupUsers,   array(), 'group_users', 10);
  $form->addHidden('check', '1');

  return $form->toHtml();
}
?>