<?php
/*
 * $RCSfile: ItemEditThumbPage.inc,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.7 $ $Date: 2005/08/23 03:49:56 $
 * @package ThumbPage
 * @subpackage UserInterface
 * @author Alan Harder <alan.harder@sun.com>
 */

/**
 * This plugin handles selecting the page to use for thumbnails
 *
 * @package ThumbPage
 * @subpackage UserInterface
 *
 */
class ItemEditThumbPage extends ItemEditPlugin {

    /**
     * @see ItemEditPlugin::handleRequest
     */
    function handleRequest($form, &$item, &$preferred) {
	$status = null;
	$error = array();
	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'thumbpage');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}

	if (isset($form['action']['save'])) {
	    list ($ret, $thumbnails) =
		GalleryCoreApi::fetchThumbnailsByItemIds(array($item->getId()));
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null, null);
	    }
	    $thumb = array_shift($thumbnails);
	    $operations = $thumb->getDerivativeOperations();
	    if ($form['page'] == 1) {
		$operations = preg_replace('/(^|;)select-page\|\d+(;|$)/', '$1', $operations);
	    } else if (preg_match('/(((^|;)select-page\|)(\d+))(;|$)/', $operations, $regs)) {
		if ($form['page'] != $regs[4]) {
		    $operations = str_replace($regs[1], $regs[2] . $form['page'], $operations);
		}
	    } else {
		$operations = 'select-page|' . $form['page'] . ';' . $operations;
	    }
	    list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($thumb->getId());
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null, null);
	    }
	    $thumb->setDerivativeOperations($operations);
	    $ret = $thumb->save();
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null, null);
	    }
	    $ret = GalleryCoreApi::releaseLocks($lockId);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null, null);
	    }

	    $status = $module->translate('Changes saved successfully');
	} /* else $form['action']['reset']) */

	return array(GalleryStatus::success(), $error, $status);
    }

    /**
     * @see ItemEditPlugin::loadTemplate
     */
    function loadTemplate(&$template, &$form, $item, $thumbnail) {
	if ($form['formName'] != 'ItemEditThumbPage') {
	    $form['formName'] = 'ItemEditThumbPage';
	    $form['page'] = 1;
	    if (preg_match('/(^|;)select-page\|(\d+)/',
			   $thumbnail->getDerivativeOperations(), $regs)) {
		$form['page'] = $regs[2];
	    }
	}
	list ($ret, $count) = $this->_getPageCount($item);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}
	$ItemEditThumbPage = array('pageList' => array());
	for ($i = 1; $i <= $count; $i++) {
	    $ItemEditThumbPage['pageList'][$i] = $i;
	}
	$template->setVariable('ItemEditThumbPage', $ItemEditThumbPage);
	$template->setVariable('controller', 'thumbpage.ItemEditThumbPage');
	return array(GalleryStatus::success(),
		     'modules/thumbpage/templates/Page.tpl', 'modules_thumbpage');
    }

    /**
     * @see ItemEditPlugin::isSupported
     */
    function isSupported($item, $thumbnail) {
	if (GalleryUtilities::isA($item, 'GalleryDataItem') && isset($thumbnail)) {
	    list ($ret, $mimeTypes) =
		GalleryCoreApi::getPluginParameter('module', 'thumbpage', 'pageTypes');
	    if ($ret->isSuccess()) {
		$list = explode('|', $mimeTypes);
		if (in_array($item->getMimeType(), $list)) {
		    list ($ret, $count) = $this->_getPageCount($item);
		    return ($ret->isSuccess() && $count > 1);
		}
	    }
	}
	return false;
    }

    /**
     * @see ItemEditPlugin::getTitle
     */
    function getTitle() {
	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'thumbpage');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	return array(GalleryStatus::success(), $module->translate('Thumbnail'));
    }

    /**
     * Get the page-count property..
     *
     * @param object GalleryDataItem
     * @return array object GalleryStatus a status code
     *               int page count
     * @access private
     */
    function _getPageCount(&$item) {
	list ($ret, $path) = $item->fetchPath();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	list ($ret, $toolkit) =
	    GalleryCoreApi::getToolkitByProperty($item->getMimeType(), 'page-count');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	if (!isset($toolkit)) {
	    return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
	}
	list ($ret, $result) = $toolkit->getProperty($item->getMimeType(), 'page-count', $path);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	return array(GalleryStatus::success(), $result[0]);
    }
}
?>
