<?php
/**
 * phpaga
 *
 * project
 *
 * View project information
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: project.php 726 2006-08-05 22:34:30Z florian $
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

include_once("./config.php");

$acerror = PHPAGA_ERR_NONE;
$sum_duration = 0;
$opcatresult = true;
$ophide = true;
$opcat_byperson = 0;
$prjmanage = false;
$prjmember = false;
$map = null;
$prj_id = "";

if (isset($_REQUEST["id"]))
{
    $prj_id = $_REQUEST["id"];
}

if (!phpaga_project_canview($prj_id))
{
    phpaga_header(array("menuitem" => "core"));
    phpaga_message(_("No access"),
                   _("You do not have the required permissions to access this project's data."));
    phpaga_footer();
    exit;
}

$prjmanage = phpaga_project_canmanage($prj_id);
phpaga_project_is_member($prj_id, $_SESSION["auth_user"]["pe_id"], $prjmember);

if (isset($_REQUEST["ophide"]) && ($_REQUEST["ophide"] == 1))
    $ophide = false;;

if (isset($_REQUEST["detail"]) && ($_REQUEST["detail"]) == PHPAGA_OPCGRP_PERSON)
    $opcat_byperson = PHPAGA_OPCGRP_PERSON;

if (isset($_REQUEST["action"]) && is_numeric($_REQUEST["action"]))
{
    if (!$prjmanage)
        phpaga_stoppage(_("No permission"),
                        _("You do not have the required permissions to perform this action."));

    $action = $_REQUEST["action"];

    switch($action)
    {

    case ACTION_OP_REMOVE:

        if (isset($_REQUEST["op_id"]) && is_numeric($_REQUEST["op_id"]))
        {
            $op_id = $_REQUEST["op_id"];

            /* only the task owner and users who can manage this project
             * are allowed to remove tasks */

            $opInfo = array();
            $result = phpaga_operations_getInfo($op_id, $opInfo);

            if (($result == PHPAGA_ERR_NONE)
                && ($prjmanage || ($opInfo["pe_id"] == $_SESSION["auth_user"]["pe_id"])))
            {

                $result = phpaga_operations_delete($op_id);

                if ($result != PHPAGA_ERR_NONE)
                    phpaga_error($result);
            }
        }
        break;

    case ACTION_FILE_REMOVE:

        if (isset($_REQUEST["file_id"]) && is_numeric($_REQUEST["file_id"]))
        {
            /* only the file owner and users who can manage this
             * project are allowed to remove files */

            $file = array();
            $result = phpaga_files_getInfo($_REQUEST["file_id"], $file);

            if (($result == PHPAGA_ERR_NONE)
                && ($prjmanage || ($file["pe_id"] == $_SESSION["auth_user"]["pe_id"])))
            {

                $result = phpaga_files_delete($_REQUEST["file_id"]);

                if ($result != PHPAGA_ERR_NONE)
                    phpaga_error($result);
            }
        }
        break;

    case ACTION_MATERIAL_REMOVE:

        if (isset($_REQUEST["mat_id"]) && is_numeric($_REQUEST["mat_id"]))
        {
            /* only users with the permission to manage this project can remove a material entry */

            if ($prjmanage)
            {

                $result = phpaga_materials_delete($_REQUEST["mat_id"]);

                if ($result != PHPAGA_ERR_NONE)
                    phpaga_error($result);
            }
        }
        break;

    case ACTION_PRJ_REMOVE:

        if (!$prjmanage)
            phpaga_stoppage(_("No permission"),
                            _("You do not have the required permissions to perform this action."));

        if (strlen($prj_id))
        {
            $acerror = phpaga_projects_delete($prj_id);

            if ($acerror == PHPAGA_ERR_NONE)
                header("Location: projects.php");
            elseif($acerror == PHPAGA_ERR_DEL_DEPENDENCIES)
            {
                $tpl = new psmarty;
                $tpl->assign("prj_id", $prj_id);
                $tpl->display("delete_project_force.tpl.js");
                $acerror = PHPAGA_ERR_NONE;
            }

        }
        break;

    case ACTION_PRJ_REMOVE_FORCE:

        if (!$prjmanage)
            phpaga_stoppage(_("No permission"),
                            _("You do not have the required permissions to perform this action."));

        if (strlen($prj_id))
        {
            $acerror = phpaga_projects_delete($prj_id, PHPAGA_FORCE);

            if ($acerror == PHPAGA_ERR_NONE)
                header("Location: projects.php");
        }
        break;

    case ACTION_PRJ_MEMBER_REMOVE:

        if (!$prjmanage)
            phpaga_stoppage(_("No permission"),
                            _("You do not have the required permissions to perform this action."));

        if (isset($_REQUEST["pe_id"]) && is_numeric($_REQUEST["pe_id"]))
        {
            $pe_id = $_REQUEST["pe_id"];

            $result = phpaga_projects_deletemember($prj_id, $pe_id);

            if ($result != PHPAGA_ERR_NONE)
                phpaga_error($result);
        }
        break;

    default:
        break;

    }
}

$result = phpaga_projects_getInfo($prj_id, $prjInfo);
if ($result != PHPAGA_ERR_NONE)
{
    phpaga_header(array("menuitem" => "core"));
    phpaga_error($result);
    phpaga_footer();
    exit;
}

phpaga_header(array("menuitem" => "core"));

if ($acerror != PHPAGA_ERR_NONE)
{
    phpaga_error($acerror);
}

$opcatresult = phpaga_operations_getOpcatStats(array("prj_id" => $prj_id),
                                               $prjopcatStats,
                                               $sum_duration,
                                               $opcat_byperson);

if ($opcatresult != PHPAGA_ERR_NONE)
{
    $opcatresult = false;
}

$use_graphviz = (defined("PHPAGA_GRAPHVIZ_ENABLED") && (PHPAGA_GRAPHVIZ_ENABLED == true));

if ($use_graphviz)
{
    $map = phpaga_projects_getRelationGraph($prj_id, true);
}

$tpl = new psmarty;
$tpl->assign("project", $prjInfo);
$tpl->assign("use_graphviz", $use_graphviz);
$tpl->assign("relmap", $map);
$tpl->assign("refop", rawurlencode("id=$prj_id"));
$tpl->assign("SELF", basename($_SERVER["PHP_SELF"]));
$tpl->assign("opcatresult", $opcatresult);
$tpl->assign("opcats", $prjopcatStats);
$tpl->assign("opcatgraphrows", count($prjopcatStats) + 2);
$tpl->assign("opcat_byperson", $opcat_byperson);
$tpl->assign("auth_user", $_SESSION["auth_user"]);
$tpl->assign("ophide", $ophide);
$tpl->assign("prjmanage", $prjmanage);
$tpl->assign("prjmember", $prjmember);
$tpl->assign("file_rel_type", PHPAGA_RELTYPE_PROJECT);
$tpl->assign("file_rel_id", $prjInfo["prj_id"]);
$tpl->assign("DELETE_OP_URL_EXTRA", "&id=$prj_id");
$tpl->assign("returl", basename($_SERVER["PHP_SELF"])."?id=".$prjInfo["prj_id"]);
$tpl->assign("perm_invoice", phpaga_user_hasperm(PHPAGA_PERM_MANAGE_INVOICES));

if ($opcat_byperson == PHPAGA_OPCGRP_PERSON)
{
    $tpl->assign("opcatcols", count($prjopcatStats["persons"]));
}

$tpl->display("project.tpl.html");

phpaga_footer();

?>