<?php
/*
 * $RCSfile: InstallOtherModulesStep.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.
 */

class InstallOtherModulesStep extends InstallStep {
    function stepName() {
	return _('Install More Modules');
    }

    function loadTemplateData(&$templateData) {
	/*
	 * Disable the data cache before initializing.  If we don't do this
	 * *first* then some data (like factory registrations) will get cached
	 * now, and then the modules won't be able to update it later on.
	 */
	require(dirname(__FILE__) . '/../../modules/core/classes/GalleryDataCache.class');
	GalleryDataCache::setFileCachingEnabled(false);

	define('GALLERY_CONFIG_DIR', $_SESSION['configPath']);
	require(dirname(__FILE__) . '/../../bootstrap.inc');
	require(dirname(__FILE__) . '/../../init.inc');
	$ret = GalleryInitFirstPass(array('debug' => 'buffered',
					  'activeLanguage' => $_SESSION['language']));
	if ($ret->isError()) {
	    $templateData['errors'][] = _('Unable to initialize our Gallery data');
	    $templateData['stackTrace'] = $ret->getAsHtml();
	}

	$groupedModules = $groupLabel = $moduleInstalled = $moduleInvalid = array();

	if (empty($templateData['errors'])) {
	    list ($ret, $moduleList) = GalleryCoreApi::fetchPluginStatus('module', true);
	    if ($ret->isError()) {
		$templateData['errors'][] = _('Unable to get the module list');
		$templateData['stackTrace'] = $ret->getAsHtml();
	    }
	}

	if (empty($templateData['errors'])) {
	    foreach (array_keys($moduleList) as $moduleId) {
		list ($ret, $modulePlugin) = GalleryCoreApi::loadPlugin('module', $moduleId);
		if ($ret->isError()) {
		    $this->resetL10Domain();
		    $templateData['warnings'][] =
			sprintf(_('Unable to load the %s module'), $moduleId);
		    $templateData['stackTrace'] = $ret->getAsHtml();
		} else {
		    $modulePlugin->setName($modulePlugin->translate($modulePlugin->getName()));
		    $modulePlugin->setDescription(
			$modulePlugin->translate($modulePlugin->getDescription()));
		    $modules[$moduleId] = $modulePlugin;
		    $this->resetL10Domain();
		}
	    }

	    uksort($modules, array($this, '_sortModules'));
	    foreach (array_keys($modules) as $moduleId) {
		$moduleInstalled[$moduleId] = !empty($moduleList[$moduleId]['version']);
		$module =& $modules[$moduleId];
		$moduleInvalid[$moduleId] = !GalleryCoreApi::isPluginCompatibleWithApis($module);
		if (isset($_REQUEST['activate']) &&
		      isset($_REQUEST['module'][$moduleId]) &&
		      !$moduleInstalled[$moduleId]) {
		    $ret = $module->installOrUpgrade();
		    if ($ret->isError()) {
			$this->resetL10Domain();
			$templateData['errors'][] =
			    sprintf(_('Unable to install the %s module'), $module->getName());
			$templateData['stackTrace'] = $ret->getAsHtml();
			continue;
		    }
		    $moduleInstalled[$moduleId] = true;

		    list ($ret, $success) = $module->autoConfigure();
		    if ($ret->isError()) {
			$this->resetL10Domain();
			$templateData['errors'][] =
			    sprintf(_('Unable to activate the %s module'), $module->getName());
			$templateData['stackTrace'] = $ret->getAsHtml();
			continue;
		    }

		    if (!$success) {
			/* This is not really an error, just a warning */
			$templateData['needsConfiguration'][$moduleId] = $module->getName();
			$this->resetL10Domain();
			continue;
		    }

		    list ($ret, $redirect) = $module->activate();
		    $this->resetL10Domain();
		    if ($ret->isError()) {
			$templateData['errors'][] =
			    sprintf(_('Unable to activate the %s module'), $module->getName());
			$templateData['stackTrace'] = $ret->getAsHtml();
			continue;
		    }
		    unset($modules[$moduleId]);
		    $templateData['activated'][$moduleId] = $module->getName();
		    continue;
		}

		$group = $module->getGroup();
		if (empty($group)) {
		    $group = array('group' => 'others', 'groupLabel' => _('Other'));
		}
		$groupedModules[$group['group']][$moduleId] =& $module;
		$groupLabel[$group['group']] = $group['groupLabel'];
	    }
	}

	if (isset($_REQUEST['activate'])) {
	    global $gallery;
	    $storage =& $gallery->getStorage();
	    $ret = $storage->commitTransaction();
	    if ($ret->isError()) {
		$templateData['errors'][] = _('Unable to commit database transaction');
		$templateData['stackTrace'] = $ret->getAsHtml();
	    }
	    $templateData['bodyFile'] = 'InstallOtherModulesSuccess.html';
	} else {
	    $templateData['groupedModules'] =& $groupedModules;
	    $templateData['groupLabel'] = $groupLabel;
	    $templateData['moduleInstalled'] = $moduleInstalled;
	    $templateData['moduleInvalid'] = $moduleInvalid;
	    $templateData['bodyFile'] = 'InstallOtherModulesRequest.html';
	}
	$templateData['anyInstallable'] = false;
	foreach ($moduleInstalled as $moduleId => $tmp) {
	    if (!$tmp) {
		$templateData['anyInstallable'] = true;
		break;
	    }
	}
	if (!$templateData['anyInstallable']) {
	    $this->setComplete(true);
	}
    }

    function isRedoable() {
	return false;
    }

    function isOptional() {
	return true;
    }

    /**
     * Put GD at the end of the list so that it's the lowest priority toolkit.  This is a hack,
     * that we should replace with a more sophisticated system in the future.
     *
     * Note: we do the same in the upgrader
     */
    function _sortModules($a, $b) {
	if ($a == 'gd' && $b == 'gd') {
	    return 0;
	} else if ($a == 'gd') {
	    return 1;
	} else if ($b == 'gd') {
	    return -1;
	} else {
	    return strcmp($a, $b);
	}
    }
}
?>
