<?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 878 2007-10-31 22:11:00Z 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 = '';
$projects = array();
$operations = array();
$prjstat = array();
$prjstat_sel = PHPAGA_MAIN_SHOW_PRJSTAT;
$porder = PHPAGA_MAIN_PRJTAB_SORT;
$num_lastoperations = null;

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 (PhPagaError::isError($r))
        $r->printMessage();
}


if (isset($_REQUEST['porder']) && is_numeric($_REQUEST['porder'])) {

    $porder = $_REQUEST['porder'];

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

    if (PhPagaError::isError($r))
        $r->printMessage();
}

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();
            $opInfo = phpaga_operations_getInfo($op_id);

            if ((!PhPagaError::isError($opInfo)) && isset($opInfo['pe_id']) &&
                ($opInfo['pe_id'] == $_SESSION['auth_user']['pe_id'])) {

                $result2 = phpaga_operations_delete($op_id);

                if (PhPagaError::isError($result2))
                    $result2->printMessage();
            }

            if (PhPagaError::isError($opInfo))
                $opInfo->printMessage();
        }
    }
}

$opInfo = array();

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($count, $duration, $prjIds, $prjInfo, $porder);

if (PhPagaError::isError($projects))
    $projects->printMessage();

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

if (PhPagaError::isError($operations)) {
    $operations->printMessage();
    $operations = null;
} else {
    if (count($operations) < PHPAGA_SHOWLASTOPERATIONS)
        $num_lastoperations = count($operations);
    else
        $num_lastoperations = PHPAGA_SHOWLASTOPERATIONS;
}

$prjstat = phpaga_projects_getPrjstatus(true);

if (PhPagaError::isError($prjstat))
    $prjstat->printMessage();

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;

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

$tpl->assign('curryear', date('Y'));
$tpl->assign('num_lastoperations', $num_lastoperations);
$tpl->assign('projects', $projects);
$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->assign('current_pe_id', $_SESSION['auth_user']['pe_id']);
$tpl->display('main.tpl.html');

phpaga_footer();

?>