<?php
/*
 * $RCSfile: StorageSetupStep.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 StorageSetupStep extends InstallStep {
    function stepName() {
	return _('Storage Setup');
    }

    function loadTemplateData(&$templateData) {
	global $galleryStub;

	if (empty($this->_dir)) {
	    $this->_dir = $galleryStub->getConfig('data.gallery.base');
	    if (empty($this->_dir)) {
		$this->_dir = $_SESSION['configPath'] . DIRECTORY_SEPARATOR . 'g2data';
	    }
	}

	$templateData['isMultisite'] = $galleryStub->getConfig('isMultisite');

	if (!empty($_POST['action']) && $_POST['action'] == 'save') {
	    if (empty($_POST['dir'])) {
		$templateData['error']['missing_value'] = 1;
	    } else {
		$dir = rtrim($this->sanitize($_POST['dir']));
		if (!is_dir($dir)) {
		    $templateData['error']['missing_dir'] = 1;
		} else if (!is_readable($dir)) {
		    $templateData['error']['inaccessible_dir'] = 1;
		} else if (!is_writeable($dir)) {
		    $templateData['error']['readonly_dir'] = 1;
		} else {
		    /* Populate the dir */
		    if (!populateDataDirectory($dir)) {
			$templateData['error']['creation_error'] = 1;
		    } else {
			$this->_dir = $dir;
			$this->setComplete(true);
		    }
		}
	    }
	    $templateData['dir'] = $dir;
	} else {
	    $templateData['dir'] = $this->_dir;
	}

	if ($this->isComplete()) {
	    $galleryStub->setConfig('data.gallery.base', $this->_dir);
	    $templateData['bodyFile'] = 'StorageSetupSuccess.html';
	} else {
	    $templateData['bodyFile'] = 'StorageSetupRequest.html';
	}

	if (!strncasecmp(PHP_OS, 'win', 3)) {
	    $templateData['OS'] = 'winnt';
	} else {
	    $templateData['OS'] = 'unix';
	}
    }

    function processRequest() {
	if (!empty($_GET['doAutoComplete'])) {
	    processAutoCompleteRequest();
	    return false;
	}

	return true;
    }

    function isRedoable() {
	return true;
    }
}
?>
