<?php
/*
 * $RCSfile: GalleryGroup.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * 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., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.36 $ $Date: 2005/08/23 03:49:03 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * Load the parent class
 */
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryEntity.class');

/**
 * Representation of a group of users
 *
 * A group is a set of GalleryUsers that can be treated as if they were a
 * single user.  This is very useful for managing permissions.
 *
 * @g2 <class-name>GalleryGroup</class-name>
 * @g2 <parent-class-name>GalleryEntity</parent-class-name>
 * @g2 <schema>
 * @g2   <schema-major>1</schema-major>
 * @g2   <schema-minor>1</schema-minor>
 * @g2 </schema>
 * @g2 <requires-id/>
 *
 * @package GalleryCore
 * @subpackage Classes
 */
class GalleryGroup_core extends GalleryEntity {

    /*
     * ****************************************
     *                 Members
     * ****************************************
     */

    /**
     * The group type
     *
     * @g2 <member>
     * @g2   <member-name>groupType</member-name>
     * @g2   <member-type>INTEGER</member-type>
     * @g2   <required/>
     * @g2 </member>
     *
     * @var int $_groupType
     * @access private
     */
    var $_groupType;

    /**
     * The group name
     *
     * @g2 <member>
     * @g2   <member-name>groupName</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2   <unique/>
     * @g2 </member>
     *
     * @var string $_groupName
     * @access private
     */
    var $_groupName;

    /*
     * ****************************************
     *                 Methods
     * ****************************************
     */

    /**
     * Create a new instance of this GalleryGroup in the persistent store
     *
     * @param string the name of the new group
     * @param int the type of group
     * @return object GalleryStatus a status code
     */
    function create($groupName, $groupType=GROUP_NORMAL) {
	global $gallery;

	/* Check to see if we have a collision */
	$query = '
	SELECT
	  [GalleryGroup::id]
	FROM
	  [GalleryGroup]
	WHERE
	  [GalleryGroup::groupName] = ?
	';
	list($ret, $results) =
	    $gallery->search($query, array($groupName),
			     array('limit' => array('count' => 1)));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$result = $results->nextResult();
	if ($result[0] > 0) {
	    return GalleryStatus::error(ERROR_COLLISION, __FILE__, __LINE__);
	}

	$ret = parent::create();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$this->setGroupName($groupName);
	$this->setGroupType($groupType);

	return GalleryStatus::success();
    }

    /**
     * Delete this GalleryGroup
     *
     * Do some bookkeeping, like removing any user/group mappings
     *
     * @access public
     * @return object GalleryStatus a status code
     */
    function delete() {
	$ret = GalleryCoreApi::removeAllUsersFromGroup($this->getId());
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/* Delete all permissions from the permissions map table */
	GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryAccessMap.class');
	$ret = GalleryAccessMap::removeMapEntry(array('groupId' => $this->getId()));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$ret = parent::delete();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	return GalleryStatus::success();
    }

    /**
     * @see GalleryEntity::itemTypeName
     */
    function itemTypeName($localized = true) {
	if ($localized) {
	    list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core');
	    if (! $ret->isError()) {
		return array($core->translate('Group'), $core->translate('group'));
	    }
	}
	return array('Group', 'group');
    }
}

include(dirname(__FILE__) . '/interfaces/GalleryGroup.inc');
?>
