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

/**
 * This is a helper class that provides an interface to the GalleryToolkit
 * api.  Modules that implement a GalleryToolkit interface can register their
 * various operations and properties using this class, and then classes that
 * want to use a toolkit operation or property can locate the appropriate
 * toolkit by operation/property and mime type.
 *
 * @package GalleryCore
 * @subpackage Helpers
 * @abstract
 */
class GalleryToolkitHelper_simple {

    /**
     * Get a toolkit that can perform the given operation
     *
     * @access public
     * @static
     * @param string a mime type
     * @param string the operation name
     * @return array object GalleryStatus a status code
     *               object GalleryToolkit a toolkit
     *               string a result mime type
     */
    function getToolkitByOperation($mimeType, $operationName) {
	global $gallery;

	$cacheKey = "GalleryToolkitHelper::getToolkitByOperation($mimeType, $operationName)";
	if (!GalleryDataCache::containsKey($cacheKey)) {
	    $query = '
            SELECT
              [GalleryToolkitOperationMimeTypeMap::toolkitId],
              [GalleryToolkitOperationMap::outputMimeType]
            FROM
              [GalleryToolkitOperationMap],
              [GalleryToolkitOperationMimeTypeMap]
            WHERE
              [GalleryToolkitOperationMap::name] = [GalleryToolkitOperationMimeTypeMap::operationName]
              AND
              [GalleryToolkitOperationMimeTypeMap::mimeType] = ?
              AND
              [GalleryToolkitOperationMap::name] = ?
            ORDER BY
              [GalleryToolkitOperationMimeTypeMap::priority] ASC
            ';

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

	    $toolkit = null;
	    $outputMimeType = null;
	    while ($result = $searchResults->nextResult()) {
		$toolkitId = $result[0];
		$outputMimeType = empty($result[1]) ? $mimeType : $result[1];

		list ($ret, $toolkit) =
		    GalleryCoreApi::newFactoryInstanceById('GalleryToolkit', $toolkitId);
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null, null);
		}

		if (isset($toolkit)) {
		    break;
		}
	    }
	    GalleryDataCache::put($cacheKey, array($toolkit, $outputMimeType));
	} else {
	    list ($toolkit, $outputMimeType) = GalleryDataCache::get($cacheKey);
	}

	return array(GalleryStatus::success(), $toolkit, $outputMimeType);
    }

    /**
     * Get a toolkit that can retrieve the given property
     *
     * @access public
     * @static
     * @param string a mime type
     * @param string the property name
     * @param int how many toolkits to we want (0 = all available)
     * @return array object GalleryStatus a status code
     *               object GalleryToolkit a toolkit
     */
    function getToolkitByProperty($mimeType, $propertyName) {
	$cacheKey = "GalleryToolkitHelper::getToolkitByProperty($mimeType, $propertyName)";
	if (!GalleryDataCache::containsKey($cacheKey)) {
	    list ($ret, $toolkits) = 
		GalleryToolkitHelper_simple::getToolkitsByProperty($mimeType, $propertyName);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    $toolkit = $toolkits[0];
	} else {
	    $toolkit = GalleryDataCache::get($cacheKey);
	}
	return array(GalleryStatus::success(), $toolkit);
    }

    /**
     * Get the toolkits that can retrieve the given property
     *
     * @access public
     * @static
     * @param string a mime type
     * @param string the property name
     * @return array object GalleryStatus a status code
     *               array of GalleryToolkit objects
     */
    function getToolkitsByProperty($mimeType, $propertyName) {
	global $gallery;

	$cacheKey = "GalleryToolkitHelper::getToolkitsByProperty($mimeType, $propertyName)";
	if (!GalleryDataCache::containsKey($cacheKey)) {
	    $query = '
            SELECT
              [GalleryToolkitPropertyMimeTypeMap::toolkitId]
            FROM
              [GalleryToolkitPropertyMap],
              [GalleryToolkitPropertyMimeTypeMap]
            WHERE
              [GalleryToolkitPropertyMap::name] = [GalleryToolkitPropertyMimeTypeMap::propertyName]
              AND
              [GalleryToolkitPropertyMimeTypeMap::mimeType] = ?
              AND
              [GalleryToolkitPropertyMap::name] = ?
            ';

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

	    $toolkits = null;
	    while ($result = $searchResults->nextResult()) {
		list ($ret, $toolkit) =
		    GalleryCoreApi::newFactoryInstanceById('GalleryToolkit', $result[0]);
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
		$toolkits[] = $toolkit;
	    }
	    GalleryDataCache::put($cacheKey, $toolkits);
	} else {
	    $toolkits = GalleryDataCache::get($cacheKey);
	}

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

    /**
     * Get maximum priority value (lowest priority) in managed priority range (20-40)
     *
     * @return array object GalleryStatus a status code
     *               int priority
     * @static
     */
    function getMaximumManagedPriority() {
	global $gallery;

	$query = 
	    'SELECT
		MAX([GalleryToolkitOperationMimeTypeMap::priority])
	     FROM 
		[GalleryToolkitOperationMimeTypeMap]
	     WHERE 
		[GalleryToolkitOperationMimeTypeMap::priority] >= 20
		AND 
		[GalleryToolkitOperationMimeTypeMap::priority] <= 40';

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

	if ($result = $results->nextResult()) {
	    $priority = $result[0];
	}
	if (empty($priority)) {
	    $priority = 20;
	}

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

    /**
     * Get priority for toolkit by toolkit id
     *
     * @param string Toolkit Id
     * @return array object GalleryStatus a status code
     *               int priority (null if not found in db)
     * @static
     */
    function getToolkitPriorityById($toolkitId) {
	global $gallery;

	$query = 
	    'SELECT 
		[GalleryToolkitOperationMimeTypeMap::priority]
	     FROM 
		[GalleryToolkitOperationMimeTypeMap]
	     WHERE 
		[GalleryToolkitOperationMimeTypeMap::toolkitId] = ?';

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

	if ($result = $results->nextResult()) {
	    $priority = (int)$result[0];
	}
	if (empty($priority)) {
	    $priority = null;
	}

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

}
?>
