<?php
/*
 * $RCSfile: ThumbnailImage.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:56 $
 * @package Thumbnail
 * @author Alan Harder <alan.harder@sun.com>
 */

/**
 * Load the parent class
 */
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryChildEntity.class');

/**
 * Representation of an image used for thumbnails of non-image items.
 *
 * @g2 <class-name>ThumbnailImage</class-name>
 * @g2 <parent-class-name>GalleryChildEntity</parent-class-name>
 * @g2 <schema>
 * @g2   <schema-major>1</schema-major>
 * @g2   <schema-minor>1</schema-minor>
 * @g2 </schema>
 * @g2 <requires-id/>
 *
 * @package Thumbnail
 * @subpackage Classes
 */
class ThumbnailImage_core extends GalleryChildEntity {

    /*
     * ****************************************
     *                 Members
     * ****************************************
     */

    /**
     * The thumbnail image filename.
     *
     * @g2 <member>
     * @g2   <member-name>fileName</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2   <unique/>
     * @g2   <required/>
     * @g2 </member>
     *
     * @var string $_fileName
     * @access private
     */
    var $_fileName;

    /**
     * The mime type of the image.
     *
     * @g2 <member>
     * @g2   <member-name>mimeType</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2 </member>
     *
     * @var string $_mimeType
     * @access private
     */
    var $_mimeType;

    /**
     * The size of the image file.
     *
     * @g2 <member>
     * @g2   <member-name>size</member-name>
     * @g2   <member-type>INTEGER</member-type>
     * @g2 </member>
     *
     * @var int $_size
     * @access private
     */
    var $_size;

    /**
     * The width of the image.
     *
     * @g2 <member>
     * @g2   <member-name>width</member-name>
     * @g2   <member-type>INTEGER</member-type>
     * @g2 </member>
     *
     * @var int $_width
     * @access private
     */
    var $_width;

    /**
     * The height of the image.
     *
     * @g2 <member>
     * @g2   <member-name>height</member-name>
     * @g2   <member-type>INTEGER</member-type>
     * @g2 </member>
     *
     * @var int $_height
     * @access private
     */
    var $_height;

    /**
     * The mime types for which this image is the default thumbnail.
     *
     * @g2 <member>
     * @g2   <member-name>itemMimeTypes</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2 </member>
     */
    var $_itemMimeTypes;

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

    /**
     * Get array of item mime types.
     *
     * @return array(string mime type)
     */
    function getItemMimeTypesList() {
	return explode('|', $this->getitemMimeTypes());
    }

    /**
     * Set array of item mime types.
     *
     * @param array(string mime type)
     */
    function setItemMimeTypesList($mimeTypeList) {
	$this->setitemMimeTypes(implode('|', $mimeTypeList));
    }

    /**
     * Create a new instance of this type in the persistent store.
     *
     * @access public
     * @param string the path to a data file to be contained
     * @param string the mime type of the new item
     * @param string the desired name of the new item
     * @return object GalleryStatus a status code
     */
    function create($inputFileName, $mimeType, $targetName=null) {
	GalleryCoreApi::relativeRequireOnce('modules/thumbnail/classes/ThumbnailHelper.class');
	global $gallery;
	$platform = $gallery->getPlatform();

	/* Validate the input file */
	if (empty($inputFileName)) {
	    return GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__);
	}
	if (!$platform->file_exists($inputFileName)) {
	    return GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__);
	}

	/* Uniquify name */
	if (empty($targetName)) {
	    $targetName = basename($inputFileName);
	}
	list ($ret, $list) = ThumbnailHelper::fetchThumbnails(true);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	while (in_array($targetName, $list)) {
	    $targetName = "_$targetName";
	}

	/* No parent by default; create a root ChildEntity */
	$ret = parent::createRoot();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$this->setFileName($targetName);
	$this->setMimeType($mimeType);
	$this->setWidth(0);
	$this->setHeight(0);

	$platform = $gallery->getPlatform();
	$dir = $this->getThumbnailDir();
	list ($success) = GalleryUtilities::guaranteeDirExists($dir);
	if (!$success) {
	    return GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__);
	}

	$newFileName = $this->getThumbnailDir() . $targetName;
	if (!$platform->copy($inputFileName, $newFileName)) {
	    return GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__);
	}
	$this->setSize($platform->filesize($newFileName));

	list ($ret, $toolkit) = GalleryCoreApi::getToolkitByProperty($mimeType, 'dimensions');
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	if (isset($toolkit)) {
	    list ($ret, $dimensions) = $toolkit->getProperty($mimeType, 'dimensions', $newFileName);
	    if ($ret->isError()) {
		$ret->addErrorCode(ERROR_BAD_DATA_TYPE);
		return $ret->wrap(__FILE__, __LINE__);
	    }
	    $this->setWidth($dimensions[0]);
	    $this->setHeight($dimensions[1]);
	}

	return GalleryStatus::success();
    }

    /**
     * Delete this GalleryEntity
     *
     * @access public
     * @return object GalleryStatus a status code
     */
    function delete() {
	global $gallery;

	/* Delete myself */
	$ret = parent::delete();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/* Delete our source file */
	$path = $this->getThumbnailDir() . $this->getFileName();
	$platform = $gallery->getPlatform();
	if (!$platform->unlink($path)) {
	    return GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__, "Could not delete $path");
	}

	return GalleryStatus::success();
    }

    /**
     * Full path to image file
     *
     * @return array(object GalleryStatus a status code, string path)
     */
    function fetchPath() {
	return array(GalleryStatus::success(),
		     $this->getThumbnailDir() . $this->getFileName());
    }

    /**
     * Thumbnail directory
     *
     * @return string Directory where thumbnail images are stored
     */
    function getThumbnailDir() {
	global $gallery;
	$platform = $gallery->getPlatform();
	$slash = $platform->getDirectorySeparator();
	return $gallery->getConfig('data.gallery.plugins_data') .
	    'modules' . $slash . 'thumbnail' . $slash;
    }

    /**
     * Render image
     *
     * @return string content
     */
    function render($format, $params) {
	global $gallery;

	switch($format) {
	case 'HTML':
	    $urlGenerator =& $gallery->getUrlGenerator();
	    $src = $urlGenerator->generateUrl(array('view' => 'core.DownloadItem',
						    'itemId' => $this->getId(),
						    'serialNumber' => $this->getSerialNumber()));

	    list ($width, $height) = array($this->getWidth(), $this->getHeight());

	    /* Shrink our dimensions if necessary */
	    if (isset($params['maxSize'])) {
		list ($width, $height) =
		    GalleryUtilities::shrinkDimensionsToFit($width, $height, $params['maxSize']);
		unset($params['maxSize']);
	    }

	    $sizeStr = ($width > 0 && $height > 0)
		? sprintf(' width="%s" height="%s"', $width, $height) : '';
	    if (!isset($params['alt'])) {
		$params['alt'] = $this->getFileName();
	    }

	    $html = sprintf('<img src="%s"%s', $src, $sizeStr);
	    unset($params['fallback']);
	    foreach ($params as $attr => $value) {
		if (isset($value)) {
		    $html .= " $attr=\"$value\"";
		}
	    }
	    return $html . '/>';

	default:
	    return null;
	}
    }
}

include(dirname(__FILE__) . '/interfaces/ThumbnailImage.inc');
?>
