<?php
/*
 * $RCSfile: GalleryFileSystemEntityHelper_simple.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:04 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * Helper class for GalleryFileSystemEntities
 *
 * @package GalleryCore
 * @subpackage Helpers
 * @abstract
 */
class GalleryFileSystemEntityHelper_simple {

    /**
     * Given a complete logical path, return the item id that it refers to.
     *
     * @param string the path
     * @return array object GalleryStatus a status code
     *               int the item id
     * @static
     */
    function fetchItemIdByPath($path) {
	global $gallery;
	if (empty($path)) {
	    return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__), null);
	}

	list ($ret, $rootId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.rootAlbum');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if ($path == '/') {
	    return array(GalleryStatus::success(), $rootId);
	}

	$currentId = $rootId;
	foreach (preg_split('|/|', $path, -1, PREG_SPLIT_NO_EMPTY) as $pathComponent) {
	    list ($ret, $currentId) =
		GalleryCoreApi::fetchChildIdByPathComponent($currentId, $pathComponent);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}

	return array(GalleryStatus::success(), $currentId);
    }

    /**
     * Returns the id of the child filesystem entity that matches the given
     * path component.
     *
     * Note: this call ignores permissions so it must be used very carefully!
     *
     * @access public
     * @param id of the parent
     * @param path component of the target item
     * @return array object GalleryStatus a status code
     *               int an id
     * @static
     */
    function fetchChildIdByPathComponent($parentId, $pathComponent) {
	global $gallery;

	if (empty($parentId) || empty($pathComponent)) {
	    return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__),
			 null);
	}

	$storage =& $gallery->getStorage();

	$query = '
	SELECT
	  [GalleryFileSystemEntity::id]
	FROM
	  [GalleryFileSystemEntity], [GalleryChildEntity]
	WHERE
	  [GalleryChildEntity::parentId] = ?
	  AND
	  [GalleryChildEntity::id] = [GalleryFileSystemEntity::id]
	  AND
	  [GalleryFileSystemEntity::pathComponent] = ?
	';
	$data = array();
	$data[] = $parentId;
	$data[] = $pathComponent;

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

	if ($searchResults->resultCount() == 0) {
	    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__,
					      "Parent $parentId path $pathComponent"), null);
	}

	if ($searchResults->resultCount() > 1) {
	    return array(GalleryStatus::error(ERROR_COLLISION, __FILE__, __LINE__), null);
	}

	$data = array();
	$result = $searchResults->nextResult();
	$targetId = (int)$result[0];

	return array(GalleryStatus::success(), $targetId);
    }
}
?>
