<?php
/*
 * $RCSfile: ColorPackImpl.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 01:33:57 $
 * @package ColorPack
 * @author Alan Harder <alan.harder@sun.com>
 */

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

/**
 * Implementation of the ColorPackInterface for including colorpack css
 *
 * @package ColorPack
 * @subpackage Classes
 */
class ColorPackImpl extends ColorPackInterface_1_0 {

    /**
     * @see ColorPackInterface_1_0::getColorPacks()
     */
    function getColorPacks() {
	global $gallery;
	$platform = $gallery->getPlatform();

	$colorpacks = array();
	$dir = dirname(dirname(__FILE__)) . '/packs';
	if ($platform->is_dir($dir) && $platform->is_readable($dir)
		&& $fd = $platform->opendir($dir)) {
	    ob_start();
	    while ($file = $platform->readdir($fd)) {
		$subdir = "$dir/$file";
		$inc = "$subdir/color.css";
		if ($platform->is_dir($subdir) && $platform->file_exists($inc)) {
		    $colorpackName = null;
		    include($inc);
		    $colorpacks[$file] =
			isset($colorpackName) ? $this->translate($colorpackName) : $file;
		}
	    }
	    ob_end_clean();
	    $platform->closedir($fd);
	    asort($colorpacks);
	}

	return array(GalleryStatus::success(),
		     array_merge(array('' => $this->translate('None')), $colorpacks));
    }

    /**
     * @see ColorPackInterface_1_0::selectColorPack()
     */
    function selectColorPack(&$template, $colorpack) {
	if (!empty($colorpack)) {
	    $template->style("modules/colorpack/packs/$colorpack/color.css");
	}

	return GalleryStatus::success();
    }

    function translate($string) {
	global $gallery;
	$translator =& $gallery->getTranslator();
	list ($ret, $content) =
	    $translator->translateDomain('modules_colorpack', array('text' => $string));
	if ($ret->isError()) {
	    return $string;
	}
	return $content;
    }
}
?>
