<?php
/**
 * phpaga
 *
 * Main
 *
 * This is the main file that displays a user's open projects and her last x tasks.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: main.php 753 2006-10-25 21:52:28Z 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");

phpaga_header(array("calendar" => true));

$prjInfo = array();
$prjIds = array();
$opInfo = array();
$opIds = array();
$action = "";
$op_id = "";
$openprojects = array();
$operations = array();
$prjstat = array();

if (isset($_REQUEST["prj_status"])) {

    if (is_numeric($_REQUEST["prj_status"]))
        $prjstat_sel = $_REQUEST["prj_status"];
    else
        $prjstat_sel = '';

    $r = phpaga_config_usersetitem("MAIN_SHOW_PRJSTAT", $prjstat_sel);

    if ($r != PHPAGA_ERR_NONE)
        phpaga_error($r);

}
else
    $prjstat_sel = PHPAGA_MAIN_SHOW_PRJSTAT;


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

    $r = phpaga_config_usersetitem("MAIN_PRJTAB_SORT", $porder);

    if ($r != PHPAGA_ERR_NONE)
        phpaga_error($r);

}
else
    $porder = PHPAGA_MAIN_PRJTAB_SORT;

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

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

        if ($action == ACTION_OP_REMOVE)
        {

            /* only the task owner is allowed to remove tasks from
             * this page */

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

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

                $result2 = phpaga_operations_delete($op_id);

                if ($result2 != PHPAGA_ERR_NONE)
                    phpaga_error($result2);
            }

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

$opInfo = array();

/* set search criteria */
if (isset($_SESSION["auth_user"]["pe_id"]))
{
    $opInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];
    $prjInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];
}

if (strlen($prjstat_sel))
    $prjInfo["prj_status"] = $prjstat_sel;

$psort = phpaga_projects_getSortOrder($porder);

$projects = phpaga_projects_search($openprojects, $count, $duration, $prjIds,
                                   $prjInfo, $porder);

if (($projects != PHPAGA_ERR_NONE) && ($projects != PHPAGA_ERR_DB_NO_ROWS))
    phpaga_error($projects);

$opresult = phpaga_operations_search($operations, $count, $duration,
                                     $opIds, $opInfo,
                                     ORDER_OP_TIMESTAMP_START_DESC, 0,
                                     PHPAGA_SHOWLASTOPERATIONS);

/* how many operations are really shown? */
if (count($operations) < PHPAGA_SHOWLASTOPERATIONS)
    $num_lastoperations = count($operations);
else
    $num_lastoperations = PHPAGA_SHOWLASTOPERATIONS;

$psres = phpaga_projects_getPrjstatus($prjstat, true);

if (($psres != PHPAGA_ERR_NONE) && ($psres != PHPAGA_ERR_DB_NO_ROWS))
    phpaga_error($psres);

if (isset($prjstat[$prjstat_sel]))
    $prjstat_sel_name = $prjstat[$prjstat_sel];
else
    $prjstat_sel_name = null;

phpaga_arrayAddOption($prjstat, PHPAGA_OPTION_ALL);

$tpl = new psmarty;

$tpl->assign ("MYMANHOURS", sprintf(_("My manhours [%s]"), date("Y")));

if (isset($_SESSION["auth_user"]["pe_id"]))
    $tpl->assign ("PE_ID", $_SESSION["auth_user"]["pe_id"]);

$tpl->assign ("LAST_OPERATIONS",
              sprintf (_("My last %d tasks"), $num_lastoperations));

$tpl->assign("projects", $openprojects);
$tpl->assign("operations", $operations);
$tpl->assign("prjstat", $prjstat);
$tpl->assign("prjstat_sel", $prjstat_sel);
$tpl->assign("prjstat_sel_name", $prjstat_sel_name);
$tpl->assign("location", $_SERVER["PHP_SELF"]."?porder=");
$tpl->assign("perm_view_invoices",phpaga_user_hasperm(PHPAGA_PERM_VIEW_INVOICES));
$tpl->assign("psort", $psort);
$tpl->assign("porder", $porder);
$tpl->display("main.tpl.html");

if (($opresult != PHPAGA_ERR_NONE) && ($opresult != PHPAGA_ERR_DB_NO_ROWS))
    phpaga_error ($opresult);

phpaga_footer ();

?>