<?php
/*
 * $RCSfile: GalleryFactoryHelper_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.17 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */
GalleryCoreApi::relativeRequireOnce(
    'modules/core/classes/helpers/GalleryFactoryHelper_simple.class');

/**
 * A factory for creating all different kinds of objects
 *
 * @package GalleryCore
 * @subpackage Helpers
 * @abstract
 */
class GalleryFactoryHelper_medium {

    /**
     * Create a new instance of the given type based on the id provided
     *
     * @param string the class type (eg. 'GalleryGraphics')
     * @param string the class name (eg. 'NetPBM')
     * @return array object GalleryStatus a status code,
     *               object an instance
     * @static
     */
    function newInstanceById($classType, $id) {
	list ($ret, $registry) = GalleryFactoryHelper_simple::_getFactoryData();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if (empty($registry['ids'][$classType][$id])) {
	    return array(GalleryStatus::success(), null);
	}

	$className = $registry['ids'][$classType][$id];
	list ($ret, $instance) = GalleryFactoryHelper_simple::newInstance($classType, $className);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

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

    /**
     * Return the ids of all the available implementations for a class
     *
     * @return array object GalleryStatus a status code
     *               array (id => className, ...)
     * @static
     */
    function getAllImplementationIds($classType) {
	list ($ret, $registry) = GalleryFactoryHelper_simple::_getFactoryData();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if (isset($registry['ids'][$classType])) {
	    $result = $registry['ids'][$classType];
	} else {
	    $result = array();
	}
	return array(GalleryStatus::success(), $result);
    }

    /**
     * Return the ids of all the available implementations for a class
     *
     * @return array object GalleryStatus a status code
     *               array (id => className, ...)
     * @static
     */
    function getAllImplementationIdsWithHint($classType, $hint) {
	list ($ret, $registry) = GalleryFactoryHelper_simple::_getFactoryData();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$hint = strtolower($hint);
	if (isset($registry['hints'][$classType][$hint])) {
	    $result = $registry['hints'][$classType][$hint];
	} else {
	    $result = array();
	}
	return array(GalleryStatus::success(), $result);
    }

    /**
     * Unregister an implementation from the factory by id
     *
     * @param string an id (eg. 'netpbm')
     * @return object GalleryStatus a status code
     * @static
     */
    function unregisterImplementationsByModuleId($moduleId) {
	GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryFactoryMap.class');
	$ret = GalleryFactoryMap::removeMapEntry(array('implModuleId' => $moduleId));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	GalleryFactoryHelper_simple::deleteCache();

	return GalleryStatus::success();
    }

    /**
     * Unregister an implementation from the factory by id
     *
     * @param string a class type (eg. 'GalleryToolkit')
     * @param string an implementation id (eg. 'NetPBM')
     * @return object GalleryStatus a status code
     * @static
     */
    function unregisterImplementation($classType, $implId) {
	GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryFactoryMap.class');
	$ret = GalleryFactoryMap::removeMapEntry(array('classType' => $classType,
						       'implId' => $implId));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	GalleryFactoryHelper_simple::deleteCache();
	
	return GalleryStatus::success();
    }

    /**
     * @param string the class type (eg. 'GalleryGraphics')
     * @param string the class name (eg. 'NetPbmGraphics')
     * @param string an implementation id (eg. 'NetPBM')
     * @param string the relative path to the implementation file
     *               (eg. 'modules/netpbm/classes/NetPbmGraphics.class')
     * @param string the id of the module containing the implementation (eg. 'netpbm')
     * @param array hints that can be used to locate this
     *              implementation (eg. array('image/jpeg', 'image/gif'))
     * @param int the priority of this implementation (lower number == higher priority)
     * @return object GalleryStatus a status class
     * @static
     */
    function registerImplementation($classType, $className, $implId, $implPath,
				    $implModuleId, $hints, $orderWeight) {
	global $gallery;
	$platform = $gallery->getPlatform();
	if (!$platform->file_exists(dirname(__FILE__) . '/../../../../' . $implPath)) {
	    return GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__,
					"Bad implementation path: $implPath");
	}
	
	if (empty($hints)) {
	    $hints = null;
	} else {
	    /* Lowercase hints for easier lookups */
	    for ($i = 0; $i < sizeof($hints); $i++) {
		$hints[$i] = strtolower($hints[$i]);
	    }
	}

	GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryFactoryMap.class');
	$ret = GalleryFactoryMap::addMapEntry(array('classType' => $classType,
						    'className' => $className,
						    'implId' => $implId,
						    'implPath' => $implPath,
						    'implModuleId' => $implModuleId,
						    'orderWeight' => $orderWeight,
						    'hints' => serialize($hints)));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	GalleryFactoryHelper_simple::deleteCache();

	return GalleryStatus::success();
    }
}
?>
