<?php
/**
 * Functions to deal with project.
 *
 * 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     Include
 * @version     $Id: FuncPRJ.php,v 1.5 2006/08/16 15:36:26 wangcs Exp $
 */
/**
 * Get projects index file name.
 *
 * @author                      wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @return  string              the full path of the index file.
 */
function prjGetIndexFile($UserName)
{
    global $_CFG;
    return genGetUserHome($UserName) . $_CFG["IndexPRJ"];
}

/**
 * Save projects indexes to file.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   string  $ArrayString    Array string created by array2String function.
 */
function prjSaveIndexFile($UserHome, $ArrayString)
{
    $IndexPRJ = prjGetIndexFile($UserName);
    $FileID   = fopen($IndexPRJ, "w+");
    fwrite($FileID, $ArrayString);
    fclose($FileID);
}

/**
 * Get all projects of a user.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @return  array                   Projects index array.
 */
function prjGetList($UserName)
{
    global $_CFG;
    $IndexPRJ = prjGetIndexFile($UserName);
    if(file_exists($IndexPRJ))
    {
        require($IndexPRJ);    // Note: don't use require_once.
        return $Projects;
    }
    else
    {
        return false;
    }
}
/**
 * Get a project's working dir.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 * @return  string                  the full path of the project's home
 */
function prjGetHome($UserName, $ProjectID)
{
    $Projects = prjGetList($UserName);
    return genGetUserHome($UserName) . $Projects[$ProjectID]["WorkingDir"] . "/";
}

/**
 * Get a project's defination.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 * @return  array                   the defination array of the project.
 */
function prjGetInfo($UserName, $ProjectID)
{
    $Projects = prjGetList($UserName);
    return $Projects[$ProjectID];
}

/**
 * Get a project's code style.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 * @return  string                  the code style of the project.
 */
function prjGetCodeStyle($UserName, $ProjectID)
{
    $Project = prjGetInfo($UserName, $ProjectID);
    return $Project["CodeStyle"];
}
/**
 * Get a project's DSN.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 * @return  array                   the DSN of the project.
 */
function prjGetDSN($UserName, $ProjectID)
{
    $Project = prjGetInfo($UserName, $ProjectID);
    return $Project["DSN"];
}
/**
 * Init the project dir: make userhome, projecthome, tablehome, pagehome and codehome.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 */
function prjInitDir($UserName, $ProjectID)
{
    global $_CFG;
    $DirList["UserHome"]    = genGetUserHome($UserName);
    $DirList["ProjectHome"] = prjGetHome($UserName, $ProjectID);
    $DirList["TableHome"]   = tblGetHome($UserName, $ProjectID);
    $DirList["PageHome"]    = pageGetHome($UserName, $ProjectID);
    $DirList["CodeHome"]    = codeGetHome($UserName, $ProjectID);

    /* Create dirs. */
    foreach($DirList as $Dir)
    {
        sysMkdir($Dir);
    }
    return $DirList;
}

/**
 * Copy basic file to projecthome.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 * @return  array                   Copied file list.
 */
function prjCopyBasicFrame($UserName, $ProjectID)
{
    global $_CFG;
    return sysXcopy($_CFG["GEN"]["FilesToCopy"], codeGetHome($UserName, $ProjectID), 'CVS');
}

/**
 * Connect the the database server of one project.
 *
 * @author                          wwccss <wwccss@263.net>
 * @param   string  $UserName
 * @param   int     $ProjectID
 * @return  object                  ADO object.
 */
function prjConnectDB($UserName, $ProjectID)
{
    $Project = prjGetInfo($UserName, $ProjectID);
    $DB = &ADONewConnection($Project["DSN"]["Type"]);
    $DB->Connect($Project["DSN"]["Server"], $Project["DSN"]["User"], $Project["DSN"]["Password"], $Project["DSN"]["Database"]);
    return $DB;
}

/**
 * Judge whether can modify project info.
 *
 * @author                          wwccss <wwccss@263.net>
 * @global  array                   Config array of PHPGEN system.
 */
function prjJudgeUser()
{
    if(!$_CFG["CommonCanModifyPRJ"] and !$_SESSION["IsAdmin"])
    {
        die("Sorry, this is a demo version, you have no privelege to modify projects");
    }
}
?>