<?php
/**
 * Manage project.
 *
 * Todo: 1. abstrac the error info into language file.
         2. Connect to the database server, create database.
         3. Judge the workingdir exists or not, Create the working dir.
 *
 * PHPGEN 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.
 *
 * PHPGEN 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 PHPGEN; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @copyright   EasySoft R&D Team (C) 2006
 * @author      Chunsheng Wang <wwccss@263.net>
 * @link        http://www.phpgen.com
 * @package     Project
 * @version     $Id: ManageProject.php,v 1.5 2006/08/15 15:53:51 wangcs Exp $
 */
require_once("../Include/Init.php");
prjJudgeUser();

if($_REQUEST["Mode"] == "Add" or $_REQUEST["Mode"] == "Edit")
{
    /* Judge the project param. */
    empty($_POST["Name"])            ? $ErrorMSG[] = "Empty project name"    : "";
    empty($_POST["DSN"]["Server"])   ? $ErrorMSG[] = "Empty database server" : "";
    empty($_POST["DSN"]["Type"])     ? $ErrorMSG[] = "Empty database type."  : "";
    empty($_POST["DSN"]["User"])     ? $ErrorMSG[] = "Empty datbase user. "  : "";
    empty($_POST["DSN"]["Database"]) ? $ErrorMSG[] = "Empty database name"   : "";
    !eregi("^[a-zA-Z0-9_]{1,}$", $_POST["WorkingDir"]) ? $ErrorMSG[] = "WorkingDir wrong. " : "";

    if(!empty($ErrorMSG))
    {
        $MyJS->alert(join("\\n", $ErrorMSG));
        $MyJS->goto("Back");
        exit;
    }

    /* Remove unused variable and assign to session. */
    $ProjectID = ($_POST["Mode"] == "Add") ? max(array_keys($_SESSION["Projects"])) + 1 : $_POST["ProjectID"];
    unset($_POST["Submit"]);
    unset($_POST["Mode"]);
    unset($_POST["ProjectID"]);

    $_SESSION["Projects"][$ProjectID] = $_POST;
}
elseif($_REQUEST["Mode"] == "Delete")
{
    if($_REQUEST["DeleteIt"] == "Yes")
    {
        unset($_SESSION["Projects"][$_GET["ProjectID"]]);
        sysRM(prjGetHome($_SESSION["UserName"], $_SESSION["ProjectID"]));
    }
    else
    {
        $SureURL   = "?ProjectID=$_GET[ProjectID]&Mode=Delete&DeleteIt=Yes";
        $CancleURL = "EditProject.php?ProjectID=$_GET[ProjectID]&Mode=Edit";
        $MyJS->confirm("Are you sure to delete this project?\\n Press ok to deleteit, cancle go back", $SureURL, $CancleURL);
    }
}

/* Save to file. */
genInitUserHome($_SESSION["UserName"]);
prjSaveIndexFile($_SESSION["UserName"], array2String($_SESSION["Projects"], "_SESSION[Projects]", "Projects"));

/* Reload the project tree. */
$MyJS->goto("ListProject.php", "parent.leftfrm");
if($_REQUEST["Mode"] != "Delete")
{
    $MyJS->goto("EditProject.php?ProjectID=$ProjectID&Mode=Edit");
}
else
{
    $MyJS->goto("../index.php?TplFile=Help");
}
?>