<?php
/*
 * $RCSfile: G1MigrateHelper.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.4 $ $Date: 2005/08/23 03:49:46 $
 * @package Migrate
 * @author Alan Harder <alan.harder@sun.com>
 */

/**
 * A helper class for the Panorama module.
 *
 * @package Migrate
 * @subpackage Classes
 */
class G1MigrateHelper {

    /**
     * Count number of mappings in migration table
     *
     * @return array GalleryStatus a status code
     *               int count
     * @static
     */
    function fetchMapCount() {
	global $gallery;

	$query = 'SELECT COUNT(*) FROM [G1MigrateMap]';

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

	$count = 0;
	if ($result = $searchResults->nextResult()) {
	    $count = (int)$result[0];
	}
	return array(GalleryStatus::success(), $count);
    }

    /**
     * Get mapping for given G1 album/item
     *
     * @param string G1 album name
     * @param string (optional) G1 item name
     * @return array object GalleryStatus a status code
     *               int id of G2 item or null if not found
     * @static
     */
    function fetchMapping($g1album, $g1item=null) {
	global $gallery;

	$query = 'SELECT [G1MigrateMap::itemId] FROM [G1MigrateMap]
	          WHERE [G1MigrateMap::g1album]=? AND [G1MigrateMap::g1item]';
	$data = array($g1album);

	if (isset($g1item)) {
	    $query .= '=?';
	    $data[] = $g1item;
	} else {
	    $query .= ' IS NULL';
	}

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

	$id = null;
	if ($result = $searchResults->nextResult()) {
	    $id = (int)$result[0];
	}
	return array(GalleryStatus::success(), $id);
    }
}
?>
