<?php
/*
* Page Class
*
* Copyright (c) 2003-4 St. Christopher House
*
* Developed by The Working Group Inc.
*
* 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, 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @version $Id: Cln_Group.php,v 1.30 2005/01/20 18:38:01 cbooth7575 Exp $
*
*/
require_once 'PEAR.php';
includeLangFile('lib/CLN/lang/General');
class Cln_Group extends PEAR {
/*
*
* Class Attributes: Cln_Group
*
* The attributes for this class are:
*
* TBD
*
*/
var $groupId;
var $name;
var $users;
/*
*
* Constructor: Cln_Group()
*
* creates an instance of a knowledge object
*
* @access public
* @return reference instance of Cln_Group
*
*/
function Cln_Group($groupId) {
$this->groupId = $groupId;
$this->name = '';
$this->users = Array();
$this->loadGroup($groupId);
}
/*
*
* Function: loadGroup()
*
* gets all data for specific group
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function loadGroup($groupId)
{
$this->groupId = $groupId;
if ($this->groupId != 'NEW') {
// Get the group name
$sql = sprintf('SELECT groupId,name FROM `%s` WHERE groupId = %d',
GROUP_TABLE,$this->groupId);
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$group = $db->getRow($sql);
if (PEAR::isError($group)) {
PEAR::raiseError('Unable to select from '.GROUP_TABLE.' query: '.$sql,E_ERROR);
return FALSE;
} else {
$this->name = stripslashes($group[1]);
}
// Get the users
if ($this->groupId != 'NEW') {
$sql = "SELECT user.userId, CONCAT(user.firstName, ' ', user.lastName)
AS name, user2group.editor
FROM " . USER_TABLE . " AS user, " . USER2GROUP_TABLE . " AS user2group
WHERE user2group.userId = user.userId
AND user2group.groupId = " . $this->groupId . ' ORDER BY name ASC';
$result = $db->query($sql);
while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
$userId = $row->userId;
$this->users[$userId]['name'] = $row->name;
$this->users[$userId]['editor'] = $row->editor;
}
}
}
}
/*
*
* Function: addGroup()
*
* adds a group
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function addGroup($groupName)
{
if(!empty($groupName)) $this->groupName = $groupName;
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$this->groupId = $db->nextId(GROUP_TABLE);
$sql = sprintf("INSERT INTO `%s` SET groupId = %d, groupName = '%s', created = NOW()",
GROUP_TABLE, $this->groupId, addslashes($this->groupName));
$result = $db->query($sql);
if (PEAR::isError($result)) {
PEAR::raiseError('Unable to insert into '.GROUP_TABLE.' table: '.$sql,E_USER_WARNING);
return FALSE;
} else {
return TRUE;
}
}
/*
*
* Function: addUser()
*
* adds a user
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function addUser($userId, $userName, $editor = 0)
{
$thisUserId = $_SESSION['User']->userId;
// Only if the user is an editor of this group, or a superuser
if (in_array(CLN_SUPERUSER_GROUPID,$_SESSION['User']->groupList)
|| (isset($this->users[$thisUserId]) && $this->users[$thisUserId]['editor'] == 1)) {
if (!isset($this->users[$userId])) {
$this->users[$userId]['name'] = $userName;
$this->users[$userId]['editor'] = $editor;
$this->sortUsers();
}
}
else {
PEAR::raiseError('You don\'t have permission to add users to this group', E_USER_WARNING);
}
}
/*
*
* Function: sortUsers()
*
* adds a user
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function sortUsers()
{
// Get all the names
$names = Array();
foreach ($this->users as $userId => $data) {
$names[$data['name']] = $userId;
}
ksort($names, SORT_STRING);
$users = Array();
foreach ($names as $name => $userId) {
$users[$userId] = $this->users[$userId];
}
$this->users = $users;
return TRUE;
}
/*
*
* Function: captureFormData()
*
* Capture the form data
*
* @access public
* @return TBD
*
*/
function captureFormData ()
{
$thisUserId = $_SESSION['User']->userId;
// Only if the user is an editor of this group, or a superuser
if (in_array(CLN_SUPERUSER_GROUPID,$_SESSION['User']->groupList)
|| (isset($this->users[$thisUserId]) && $this->users[$thisUserId]['editor'] == 1)) {
$startingUsers = $this->users;
if (isset($_POST['groupUsers'])) {
foreach ($_POST['groupUsers'] as $userId) {
unset($startingUsers[$userId]);
}
}
foreach ($startingUsers as $userId => $userName) {
unset($this->users[$userId]);
}
// Then get the editors
foreach($this->users as $userId => $userData) {
if (isset($_POST['groupEditors'][$userId])) {
$this->users[$userId]['editor'] = 1;
}
else {
$this->users[$userId]['editor'] = 0;
}
}
$this->name = $_POST['groupEditTitle'];
}
else {
PEAR::raiseError('You don\'t have permission to edit this group', E_USER_WARNING);
}
}
/*
*
* Function: validateData()
*
* Validates the data and returns FALSE with errors, or TRUE
*
* @access public
* @return Boolean TRUE or FALSE
*
*/
function validateData()
{
includeLangFile('lib/CLN/lang/Group-Process');
$status = TRUE;
// Is the name empty?
if ($this->name == '') {
PEAR::raiseError(ADMIN_EM_NO_NAME, E_USER_WARNING);
$status = FALSE;
}
// Else, is that name in use elsewhere?
else {
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$sql = sprintf("SELECT groupId FROM `%s` WHERE name ='%s' AND groupId !='%s'",
GROUP_TABLE, addslashes($this->name), $this->groupId);
$result = $db->query($sql);
if ($result->numRows() > 0) {
PEAR::raiseError(ADMIN_EM_GROUP_NAME_EXISTS, E_USER_WARNING);
$status = FALSE;
}
}
// Are they any members?
if (count($this->users) < 1) {
PEAR::raiseError(ADMIN_EM_NO_MEMBERS, E_USER_WARNING);
$status = FALSE;
}
// Is there at least one editor (unless the user is a super user)
if (!in_array(CLN_SUPERUSER_GROUPID,$_SESSION['User']->groupList)) {
$count = 0;
foreach ($this->users as $userId => $userData) {
if ($userData['editor']) {
$count++;
}
}
if ($count == 0) {
PEAR::raiseError('You must designate at least 1 person to be the Group Editor', E_USER_WARNING);
$status = FALSE;
}
}
return $status;
}
/*
*
* Function: save()
*
* updates the information stored for a group
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function save()
{
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
if ($this->groupId == 'NEW') {
$this->groupId = $db->nextId(GROUP_TABLE);
$sql = sprintf("INSERT INTO `%s` SET groupId = %d, name = '%s', created = NOW()",
GROUP_TABLE, $this->groupId, addslashes($this->name));
}
else {
$sql = sprintf("UPDATE `%s` SET name = '%s' WHERE groupId = %d",
GROUP_TABLE, addslashes($this->name), $this->groupId);
}
$result = $db->query($sql);
if (PEAR::isError($result)) {
PEAR::raiseError('Unable to update '.GROUP_TABLE.' table: '.$sql,E_USER_WARNING);
return FALSE;
}
$this->saveUsers();
// Reload the user's group list
if (isset($_SESSION['User'])) {
$_SESSION['User']->reloadGroupList();
}
return TRUE;
}
/*
*
* Function: saveUsers()
*
* saves the users
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function saveUsers()
{
$this->deleteAllUsers();
// Prepare the SQL
foreach ($this->users as $userId => $userData) {
$valueSets[] = '('.$userId.','.$this->groupId.',' . $userData['editor'] . ', NOW())';
}
$sql = sprintf('INSERT IGNORE INTO %s (userId, groupId, editor, created) VALUES %s',
USER2GROUP_TABLE, join(',',$valueSets));
// Query the DB
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$result = $db->query($sql);
}
/*
*
* Function: deleteAllUsers()
*
* deletes all the users
*
* @access public
* @return BOOLEAN TRUE or FALSE
*
*/
function deleteAllUsers()
{
$sql = 'DELETE FROM ' . USER2GROUP_TABLE . ' WHERE groupId = ' . $this->groupId;
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$result = $db->query($sql);
}
/*
*
* Function: getMembers()
*
* gets a list of all members of a groups
*
* @access public
* @return String (comma separated list)
*
*/
function getMembers($groupId = '')
{
if(!empty($groupId)) $this->groupId = $groupId;
$sql = sprintf('SELECT userId FROM `%s` WHERE groupId = %d',USER2GROUP_TABLE,$this->groupId);
//print $sql;
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$result = $db->query($sql);
if (PEAR::isError($result)) {
PEAR::raiseError('Unable to select from '.USER2GROUP_TABLE.' table: '.$sql, E_WARNING);
return FALSE;
}
while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$groups[] = $row['userId'];
}
return $groups;
}
/*
*
* Function: getGroupName()
*
* returns the name of the group
* stores group names in $_SESSION['groupNames'];
*
* @access public
* @return String name of group
*
*/
function getGroupName($groupId)
{
if(!isset($_SESSION['groupNames'][$groupId])) {
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$sql = sprintf('SELECT groupId,name FROM `%s`',GROUP_TABLE);
$result = $db->query($sql);
if (PEAR::isError($result)) {
PEAR::raiseError('unable to select from '.USER_TABLE.' table: '.$sql,E_USER_WARNING);
return FALSE;
} else {
while($row = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
$_SESSION['groupNames'][($row->groupId)] = stripslashes($row->name);
}
}
}
return $_SESSION['groupNames'][$groupId];
}
/*
*
* Function: getInterface()
*
* returns an admin interface
*
* @access public
* @return String $content
*
*/
function getInterface($file)
{
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/*
*
* Function: loadAllUsers()
*
* Returns an associative array of all available users, with userId as the key
*
* @access public
* @return Boolean TRUE or FALSE
*
*/
function loadAllUsers()
{
// Query the DB
$sql = sprintf("SELECT userId, CONCAT(firstName, ' ', lastName) AS name FROM `%s`",
USER_TABLE);
$db = &Cln_Db::singleton(MAIN_CLN_DSN);
$users = stripslashesArray($db->getAssoc($sql));
// Output errors
if (PEAR::isError($users)) {
PEAR::raiseError('Unable to select all the users '.$sql, E_WARNING);
return FALSE;
} else {
return $users;
}
}
}
?>