<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 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.
 */

/**
 * Utility functions useful in managing GalleryGroups
 * @package GalleryCore
 * @subpackage Helpers
 * @author Bharat Mediratta <bharat@menalto.com>
 * @version $Revision: 17580 $
 * @static
 */
class GalleryGroupHelper_simple {

    /**
     * @see GalleryCoreApi::fetchGroupNames
     */
    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) {
	    return array($ret, null);
	}

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

	return array(null, $groupnames);
    }

    /**
     * @see GalleryCoreApi::fetchGroupCount
     */
    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) {
	    return array($ret, null);
	}

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

    /**
     * @see GalleryCoreApi::fetchGroupByGroupName
     */
    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) {
	    return array($ret, null);
	}

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

	    return array(null, $group);
	}
    }
}
?>
