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

/**
 *
 * @package GalleryCore
 * @subpackage Helpers
 */
class GalleryItemAttributesHelper_medium {
    /**
     * Get the view counts for many item ids
     * @param array the item ids
     * @return array object GalleryStatus a status code
     *               array (itemId => viewCount, ..)
     * @static
     */
    function fetchViewCounts($itemIds) {
	global $gallery;

	$query = sprintf('
        SELECT
          [GalleryItemAttributesMap::itemId], [GalleryItemAttributesMap::viewCount]
        FROM
          [GalleryItemAttributesMap]
        WHERE
          [GalleryItemAttributesMap::itemId] IN (%s)
        ', GalleryUtilities::makeMarkers($itemIds));
	list ($ret, $searchResults) = $gallery->search($query, $itemIds);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if ($searchResults->resultCount() != sizeof($itemIds)) {
	    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__), null);
	}

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

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

    /**
     * Get the order weight for a given item id
     * @param int the item id
     * @return array object GalleryStatus a status code
     *               int the order weight
     * @static
     */
    function fetchOrderWeight($itemId) {
	list ($ret, $orderWeights) = GalleryItemAttributesHelper_medium::fetchOrderWeights(array($itemId));
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if (!isset($orderWeights[$itemId])) {
	    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__), null);
	}

	return array(GalleryStatus::success(), (int)$orderWeights[$itemId]);
    }

    /**
     * Get the order weight for many item ids
     * @param array the item ids
     * @return array object GalleryStatus a status code
     *               array(itemId1 => orderWeight1,
     *                     itemId2 => orderWeight2, ...)
     * @static
     */
    function fetchOrderWeights($itemIds) {
	global $gallery;

	$query = '
        SELECT
          [GalleryItemAttributesMap::itemId], [GalleryItemAttributesMap::orderWeight]
        FROM
          [GalleryItemAttributesMap]
        WHERE
          [GalleryItemAttributesMap::itemId] IN (' . GalleryUtilities::makeMarkers(sizeof($itemIds)) . ')
        ';
	list ($ret, $searchResults) = $gallery->search($query, $itemIds);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

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

    /**
     * Get the parent sequence for this item id
     * @param int the item id
     * @return array object GalleryStatus a status code
     *               array the parent id sequence
     * @static
     */
    function fetchParentSequence($itemId) {
	global $gallery;

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

	if ($searchResults->resultCount() != 1) {
	    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__,
					      "ParentSequence for $itemId"), null);
	}

	$result = $searchResults->nextResult();
	$parentSequence = preg_split('/\//', $result[0], -1, PREG_SPLIT_NO_EMPTY);

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