<?php
/*
 * $RCSfile: SizeLimitHelper.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:54 $
 * @package SizeLimit
 * @author Felix Rabinovich
 */
/**
 *
 * @package SizeLimit
 * @subpackage Helpers
 * @static
 */
class SizeLimitHelper {

    /**
     * Reduce the item file size without preserving the original
     *
     * @param item GalleryPhotoItem
     * @param operation ('scale' to reduce dimensions
     *                   'compress' to reduce filesize)
     * @param args operation arguments to pass through to the toolkit
     * @return array (object GalleryStatus a status code,
     *                array (errors),
     *                array (warnings)
     */
    function applyLimits(&$item, $operation, $args) {

	/* Get the path of the source */
	list($ret, $sourcePath) = $item->fetchPath();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/* Get the appropriate toolkit */
	list ($ret, $toolkit) =
	    GalleryCoreApi::getToolkitByOperation($item->getMimeType(), $operation);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	if (!isset($toolkit)) {
	    return GalleryStatus::error(ERROR_UNSUPPORTED_FILE_TYPE, __FILE__, __LINE__);
	}

	/* Perform the operation */
	list ($ret, $outputMimeType) =
	    $toolkit->performOperation($item->getMimeType(), $operation,
	    $sourcePath, $sourcePath, $args);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	$item->setMimeType($outputMimeType);

	/* Get the item to rescan its data object */
	$ret = $item->rescan();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($item->getId());
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/* Save the item */
	$ret = $item->save();
	if ($ret->isError()) {
	    GalleryCoreApi::releaseLocks($lockId);
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$ret = GalleryCoreApi::releaseLocks($lockId);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	return GalleryStatus::success();
    }

    /**
     * Set up a preferred derivative that conforms to size limits
     *
     * @param item GalleryPhotoItem
     * @param operation ('scale' to reduce dimensions
     *                   'compress' to reduce filesize)
     * @args  passthru to the toolkit
     * @return array (object GalleryStatus a status code,
     *                array (errors),
     *                array (warnings)
     */
    function buildDerivativeWithLimits($item, $operation, $args) {
	/* Check to see if we have a preferred source */
	list ($ret, $preferred) =
	    GalleryCoreApi::fetchPreferredSource($item);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/* Make sure we support the given operation */
	list ($ret, $toolkit) =
	    GalleryCoreApi::getToolkitByOperation($item->getMimeType(), $operation);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	if (!isset($toolkit)) {
	    return GalleryStatus::error(ERROR_UNSUPPORTED_FILE_TYPE, __FILE__, __LINE__);
	}

	/*
	 * If we don't have derivative preferred (for example, from resize), create one
	 * Otherwise, just merge operations
	 */
	if ($preferred->getId() == $item->getId()) {
	    list ($ret, $preferred) = GalleryCoreApi::newFactoryInstanceByHint(
		'GalleryDerivative', $item->getEntityType());
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }

	    if (!isset($preferred)) {
		return $ret->wrap(__FILE__, __LINE__);
	    }

	    $ret = $preferred->create($item->getId(), DERIVATIVE_TYPE_IMAGE_PREFERRED);
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }

	    $preferred->setDerivativeSourceId($item->getId());
	    $preferred->setMimeType($item->getMimeType());

	    $ret = GalleryCoreApi::remapSourceIds($item->getId(), $preferred->getId());
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }
	}

	$operationString = $operation . '|' . join(',', $args);
	list ($ret, $operations) = GalleryCoreApi::mergeDerivativeOperations(
	    $preferred->getDerivativeOperations(), $operationString);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	$preferred->setDerivativeOperations($operations);

	list ($ret, $lockIds) = GalleryCoreApi::acquireWriteLock($preferred->getId());
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	$ret = $preferred->save();
	if ($ret->isError()) {
	    GalleryCoreApi::releaseLocks($lockIds);
	    return $ret->wrap(__FILE__, __LINE__);
	}

	if (!empty($lockIds)) {
	    $ret = GalleryCoreApi::releaseLocks($lockIds);
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }
	}
	return GalleryStatus::success();
    }
}
?>
