<?php
/*
 * $RCSfile: GalleryUserGroupHelper_simple.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.8 $ $Date: 2005/09/02 09:37:46 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 *
 *
 * @package GalleryCore
 * @subpackage Helpers
 */
class GalleryUserGroupHelper_simple {

    /**
     * Is the active user in the admin group?
     *
     * @return array object GalleryStatus a status code
     *               bool true if yes
     * @static
     */
    function isUserInSiteAdminGroup() {
	global $gallery;
	list ($ret, $adminGroupId) =
	  GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), false);
	}

	list ($ret, $result) = GalleryUserGroupHelper_simple::isUserInGroup(
	  $gallery->getActiveUserId(), $adminGroupId);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), false);
	}

	return array(GalleryStatus::success(), $result);
    }

    /**
     * Is the given user id in the given group?
     *
     * @param int the id of the user
     * @return array object GalleryStatus a status code
     *               bool true if yes
     * @static
     */
    function isUserInGroup($userId, $groupId) {
	global $gallery;

	$cacheKey = "GalleryUserGroupHelper::isUserInGroup($userId,$groupId)";
	if (GalleryDataCache::containsKey($cacheKey)) {
	    return array(GalleryStatus::success(), GalleryDataCache::get($cacheKey));
	}

	$query = '
        SELECT
          [GalleryUserGroupMap::userId]
        FROM
          [GalleryUserGroupMap]
        WHERE
          [GalleryUserGroupMap::userId] = ?
          AND
          [GalleryUserGroupMap::groupId] = ?
        ';

	list ($ret, $searchResults) =
	    $gallery->search($query,
			     array($userId, $groupId),
			     array('limit' => array('count' => 1)));
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$result = $searchResults->resultCount() ? true : false;
	GalleryDataCache::put($cacheKey, $result);
	return array(GalleryStatus::success(), $result);
    }
}
?>
