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

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

/**
 * A subclass of DataItem for containing Photos
 *
 * A GalleryItem whose source is an image (eg JPEG, PNG, GIF, etc)
 *
 * @g2 <class-name>GalleryPhotoItem</class-name>
 * @g2 <parent-class-name>GalleryDataItem</parent-class-name>
 * @g2 <schema>
 * @g2   <schema-major>1</schema-major>
 * @g2   <schema-minor>0</schema-minor>
 * @g2 </schema>
 * @g2 <requires-id/>
 *
 * @package GalleryCore
 * @subpackage Classes
 */
class GalleryPhotoItem_core extends GalleryDataItem {

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

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

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

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

    /**
     * @see GalleryDataItem::canBeViewedInline()
     */
    function canBeViewedInline() {
	$mimeType = $this->getMimeType();
	return in_array($mimeType, array('image/jpeg', 'image/pjpeg', 'image/png',
					 'image/gif', 'image/vnd.wap.wbmp'));
    }

    /**
     * Create a new GalleryPhotoItem from an image file
     *
     * @param int the id of the parent GalleryItem
     * @param string the path to the source image
     * @param string the mime type
     * @param string the desired name of the new item
     * @param boolean (optional) a boolean true if we should symlink instead
     *        of copy (default is false).
     * @return object GalleryStatus a status code
     */
    function create($parentId, $imageFileName, $mimeType, $targetName=null, $symlink=false) {
	global $gallery;
	$platform = $gallery->getPlatform();

	/* Validate the input filename */
	if (empty($imageFileName)) {
	    return GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__);
	}

	if (!$platform->file_exists($imageFileName)) {
	    return GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__, $imageFileName);
	}

	/* Create our data item */
	$ret = parent::create($parentId, $imageFileName,
			      $mimeType, $targetName, $symlink);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/* We're linkable */
	$this->setIsLinkable(true);

	/* Default to empty dimensions */
	$this->setWidth(0);
	$this->setHeight(0);

	/* Detect our dimensions, if possible */
	$ret = $this->rescan();
	if ($ret->isError()) {
	    /* Cleanup our datafile on failure */
	    list ($ret2, $path) = $this->fetchPath();
	    if ($ret2->isSuccess()) {
		@$platform->unlink($path);
	    }
	    return $ret->wrap(__FILE__, __LINE__);
	}

	return GalleryStatus::success();
    }

    /**
     * @see GalleryDataItem::rescan()
     */
    function rescan() {
	$ret = parent::rescan();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	list ($ret, $path) = $this->fetchPath();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	$mimeType = $this->getMimeType();

	/* Check for CMYK colorspace and alter mime type if detected */
	list ($ret, $toolkit) = GalleryCoreApi::getToolkitByProperty($mimeType, 'colorspace');
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	if (isset($toolkit)) {
	    list ($ret, $colorspace) = $toolkit->getProperty($mimeType, 'colorspace', $path);
	    if ($ret->isError()) {
		$ret->addErrorCode(ERROR_BAD_DATA_TYPE); /* See BAD_DATA_TYPE comment below */
		return $ret->wrap(__FILE__, __LINE__);
	    }
	    if ($colorspace[0] == 'CMYK') {
		$this->setMimeType($mimeType .= '-cmyk');
	    }
	    $toolkit = null;
	}

	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', $path);
	    if ($ret->isError()) {

		/*
		 * If we can't get the dimensions, it may be a bad image
		 * Or our graphics code is broken.  Hard to tell which at this point.
		 */
		$ret->addErrorCode(ERROR_BAD_DATA_TYPE);
		return $ret->wrap(__FILE__, __LINE__);
	    }

	    $this->setWidth($dimensions[0]);
	    $this->setHeight($dimensions[1]);
	}

	return GalleryStatus::success();
    }

    /**
     * @see GalleryEntity::itemTypeName
     */
    function itemTypeName($localized = true) {
	if ($localized) {
	    list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core');
	    if (! $ret->isError()) {
		return array($core->translate('Photo'), $core->translate('photo'));
	    }
	}
	return array('Photo', 'photo');
    }

    /**
     * @see GalleryDataItem::render
     */
    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 = '';
	    if ($width > 0 && $height > 0) {
		$sizeStr = sprintf(' width="%s" height="%s"', $width, $height);
	    }
	    if (!isset($params['alt'])) {
		$params['alt'] = $this->getTitle() ? $this->getTitle() : $this->getPathComponent();
	    }
	    if (!isset($params['longdesc'])) {
		$params['longdesc'] = preg_replace('/[\r\n]+/', ' ', $this->getDescription());
	    }

	    $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/GalleryPhotoItem.inc');
?>
