<?php
/*
 * $RCSfile: MultiLangHelper.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.5 $ $Date: 2005/09/07 02:33:16 $
 * @package MultiLang
 * @author Alan Harder <alan.harder@sun.com>
 */

/**
 * A helper class for the MultiLang module.
 *
 * @package MultiLang
 * @subpackage Classes
 */
class MultiLangHelper {

    /**
     * Get language data for an item
     *
     * @param int item id
     * @return array object GalleryStatus a status code
     *               array (language => array data, ..)
     * @static
     */
    function getItemData($itemId) {
	global $gallery;

	$query = '
	SELECT [MultiLangItemMap::language], [MultiLangItemMap::title],
	       [MultiLangItemMap::summary], [MultiLangItemMap::description]
	FROM [MultiLangItemMap]
	WHERE [MultiLangItemMap::itemId] = ?
	';

	list ($ret, $searchResults) = $gallery->search($query, array($itemId));
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$data = array();
	while ($result = $searchResults->nextResult()) {
	    $data[$result[0]] = array('title' => $result[1], 'summary' => $result[2],
				      'description' => $result[3]);
	}
	return array(GalleryStatus::success(), $data);
    }

    /**
     * Set language data for an item, or remove data if all text parameters are empty
     *
     * @param object GalleryItem item
     * @param string language
     * @param string (optional) title
     * @param string (optional) summary
     * @param string (optional) description
     * @return object GalleryStatus a status code
     * @static
     */
    function setItemData($item, $language, $title=null, $summary=null, $description=null) {
	GalleryCoreApi::relativeRequireOnce('modules/multilang/classes/MultiLangItemMap.class');

	$itemId = $item->getId();
	list ($ret, $data) = MultiLangHelper::getItemData($itemId);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	if (empty($title) && empty($summary) && empty($description)) {
	    /* Remove data for this item/language (if there is any to remove) */
	    if (isset($data[$language])) {
		$ret = MultiLangItemMap::removeMapEntry(
					 array('itemId' => $itemId, 'language' => $language));
		if ($ret->isError()) {
		    return $ret->wrap(__FILE__, __LINE__);
		}
		if (count($data) == 1) {
		    /* Last multilang data deleted; remove onLoadHandler */
		    list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($itemId);
		    if ($ret->isError()) {
			return $ret->wrap(__FILE__, __LINE__);
		    }
		    $item->removeOnLoadHandler('MultiLang');
		    $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__);
		    }
		}
	    }
	} else {
	    /* Update or insert new data */
	    if (isset($data[$language])) {
		$ret = MultiLangItemMap::updateMapEntry(
		    array('itemId' => $itemId, 'language' => $language),
		    array('title' => $title, 'summary' => $summary, 'description' => $description));
		if ($ret->isError()) {
		    return $ret->wrap(__FILE__, __LINE__);
		}
	    } else {
		$ret = MultiLangItemMap::addMapEntry(
		    array('itemId' => $itemId, 'language' => $language,
			  'title' => $title, 'summary' => $summary, 'description' => $description));
		if ($ret->isError()) {
		    return $ret->wrap(__FILE__, __LINE__);
		}
		if (empty($data)) {
		    /* First multilang data for this item; add onLoadHandler */
		    list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($itemId);
		    if ($ret->isError()) {
			return $ret->wrap(__FILE__, __LINE__);
		    }
		    $item->addOnLoadHandler('MultiLang');
		    $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__);
		    }
		}
	    }
	}

	if ($item->getParentId()) {
	    global $gallery;
	    $event = GalleryCoreApi::newEvent('Gallery::ViewableTreeChange');
	    $event->setData(array('userId' => null, 'itemId' => $item->getParentId()));
	    list ($ret) = GalleryCoreApi::postEvent($event);
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }
	}

	return GalleryStatus::success();
    }

    /**
     * Update item with data for the current language, if available
     *
     * @param GalleryItem item
     * @return object GalleryStatus a status code
     * @static
     */
    function onLoad(&$item) {
	global $gallery;
	list ($ret, $language) = $gallery->getActiveLanguageCode();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	list ($ret, $data) = MultiLangHelper::getItemData($item->getId());
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	if (isset($data[$language])) {
	    $item->setTitle($data[$language]['title']);
	    $item->setSummary($data[$language]['summary']);
	    $item->setDescription($data[$language]['description']);
	}

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