<?php
/*
 * $RCSfile: GalleryGroupHelper_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.7 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * A helper class for GalleryGroups
 *
 * Utility functions useful in managing GalleryGroups
 *
 * @package GalleryCore
 * @subpackage Helpers
 */
class GalleryGroupHelper_simple {

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

    /**
     * Return a map of groupNames => groupids
     *
     * You can specify how many groupnames to list, and where the windows is in
     * the list of all groups.
     *
     * @param int [optional] the number of groupnames desired
     * @param int [optional] the start of the range
     * @param string [optional] a substring to match
     * @return array object GalleryStatus a status code
     *               array (groupname, groupname, ...)
     * @static
     */
    function fetchGroupNames($count=null, $offset=null, $substring=null) {
	global $gallery;

	$data = array();
	$query = '
        SELECT
          [GalleryGroup::id],
          [GalleryGroup::groupName]
        FROM
          [GalleryGroup]
        ';

	if (!empty($substring)) {
	    $query .= '
        WHERE
	  [GalleryGroup::groupName] LIKE ?
            ';
	    $data[] = "%$substring%";
	}

	$query .= '
        ORDER BY
          [GalleryGroup::groupName] ASC
        ';

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

	$groupnames = array();
	while ($result = $searchResults->nextResult()) {
	    $groupnames[$result[0]] = $result[1];
	}

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

    /**
     * Return a count of groups, optionally matching a search string
     *
     * @param string the substring to match
     * @return array object GalleryStatus a status code
     *               int group count
     * @static
     */
    function fetchGroupCount($substring=null) {
	global $gallery;

	$data = array();
	$query = '
        SELECT
          COUNT([GalleryGroup::id])
        FROM
          [GalleryGroup]
        ';

	if (!empty($substring)) {
	    $query .= '
        WHERE
	  [GalleryGroup::groupName] LIKE ?
            ';
	    $data[] = "%$substring%";
	}

	list ($ret, $searchResults) = $gallery->search($query, $data);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$groupnames = array();
	$result = $searchResults->nextResult();
	return array(GalleryStatus::success(), (int)$result[0]);
    }

    /**
     * Lookup a group by name
     *
     * @param string the groupname
     * @return array object GalleryStatus a status code
     *               object GalleryGroup a group
     * @static
     */
    function fetchGroupByGroupName($groupName=null) {
	global $gallery;

        $query = '
        SELECT
          [GalleryGroup::id]
        FROM
          [GalleryGroup]
        WHERE
          [GalleryGroup::groupName] = ?
        ';
	list ($ret, $searchResults) = $gallery->search($query, array($groupName));
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if ($searchResults->resultCount() == 0) {
	    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__), null);
	} else {
	    $result = $searchResults->nextResult();
	    $id = $result[0];
	    list ($ret, $group) = GalleryCoreApi::loadEntitiesById($id);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }

	    return array(GalleryStatus::success(), $group);
	}
    }
}
?>
