<?php
/*
 * $RCSfile: GalleryCommentHelper.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.
 */

/**
 * @package Comment
 * @version $Revision: 1.14 $ $Date: 2005/08/23 03:49:02 $
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * A helper class for GalleryComments
 *
 * Utility functions useful in managing GalleryComments
 *
 * @package Comment
 * @subpackage Classes
 */
class GalleryCommentHelper {

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

    /**
     * Return the comments associated with the given item ids
     *
     * @param array int GalleryItem id
     * @return array (object GalleryStatus a status code,
     *                array (item id => array (GalleryComment, ...), ...)
     * @access private
     * @static
     */
    function fetchComments($itemId, $count=null, $orderDirection=ORDER_ASCENDING) {
	global $gallery;

	switch($orderDirection) {
	case ORDER_ASCENDING:
	    $direction = 'ASC';
	    break;

	case ORDER_DESCENDING:
	    $direction = 'DESC';
	    break;

	default:
	    return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__),
			 null);
	}

	$query = '
        SELECT
          [GalleryComment::id]
        FROM
          [GalleryComment], [GalleryChildEntity]
        WHERE
          [GalleryChildEntity::parentId] = ?
          AND
          [GalleryChildEntity::id] = [GalleryComment::id]
        ORDER BY
          [GalleryComment::date] ' . $direction . '
        ';
	list($ret, $searchResults) = $gallery->search($query,
						       array($itemId),
						       array('limit' => array('count' => $count)));

	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	/* Get all of our ids */
	$allIds = array();
	while ($result = $searchResults->nextResult()) {
	    $allIds[] = $result[0];
	}

	/* Load all the comments at once */
	$comments = array();
	if (!empty($allIds)) {
	    list ($ret, $comments) = GalleryCoreApi::loadEntitiesById($allIds);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}

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

    /**
     * Return the number of comments associated with the given item ids
     *
     * @param array int GalleryItem ids
     * @return array object GalleryStatus a status code
     *               int a count
     * @access private
     * @static
     */
    function fetchCommentCounts($itemIds) {
	global $gallery;

	$markers = GalleryUtilities::makeMarkers(sizeof($itemIds));
	$query = '
        SELECT
          [GalleryChildEntity::parentId],
          COUNT([GalleryComment::id])
        FROM
          [GalleryComment], [GalleryChildEntity]
        WHERE
          [GalleryChildEntity::parentId] IN (' . $markers . ')
          AND
          [GalleryChildEntity::id] = [GalleryComment::id]
        GROUP BY
          [GalleryChildEntity::parentId]
        ';

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

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

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