<?php
/**
 * phpaga
 *
 * Projects
 *
 * This is the projects management module
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * 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");

$search = array("prj_status" => PHPAGA_PROJECTSTATUS_ALL);

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

$prjIds = array();
$prjInfo = array("cpn_id" => "",
                 "cpn_id_issuebill" => "",
                 "pe_id" => "",
                 "prj_id" => "",
                 "prj_status" => "",
                 "prj_billabe_type" => "",
                 "prjcat_id" => "",
                 "prj_desc" => "",
                 "prj_startdatefrom" => "",
                 "prj_startdateto" => "");

$search_args = "";
$statusmsg = "";
$action = "";
$prj_id = "";
$offset = 0;
$order = PHPAGA_SORT_PROJECTS;

if (isset($REQUEST_DATA["offset"]) && is_numeric($REQUEST_DATA["offset"]))
    $offset = $REQUEST_DATA["offset"];

if (isset($REQUEST_DATA["order"]) && is_numeric($REQUEST_DATA["order"])) {

    $order = $REQUEST_DATA["order"];

    $r = phpaga_config_usersetitem('SORT_PROJECTS', $order);

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

if (isset($REQUEST_DATA["cpn_id"]) && is_numeric($REQUEST_DATA["cpn_id"]))
    $prjInfo["cpn_id"] = $REQUEST_DATA["cpn_id"];

if (isset($REQUEST_DATA["cpn_id_issuebill"]) && is_numeric($REQUEST_DATA["cpn_id_issuebill"]))
    $prjInfo["cpn_id_issuebill"] = $REQUEST_DATA["cpn_id_issuebill"];

if (isset($REQUEST_DATA["pe_id"]) && is_numeric($REQUEST_DATA["pe_id"]))
    $prjInfo["pe_id"] = $REQUEST_DATA["pe_id"];

if (isset($REQUEST_DATA["prj_id"]) && is_numeric($REQUEST_DATA["prj_id"]))
    $prjInfo["prj_id"] = $REQUEST_DATA["prj_id"];

if (isset($REQUEST_DATA["prj_status"]) && is_numeric($REQUEST_DATA["prj_status"]))
    $prjInfo["prj_status"] = $REQUEST_DATA["prj_status"];

if (isset($REQUEST_DATA["prj_billabletype"]) && is_numeric($REQUEST_DATA["prj_billabletype"]))
    $prjInfo["prj_billabletype"] = $REQUEST_DATA["prj_billabletype"];

if (isset($REQUEST_DATA["prjcat_id"]) && is_numeric($REQUEST_DATA["prjcat_id"]))
    $prjInfo["prjcat_id"] = $REQUEST_DATA["prjcat_id"];

if (isset($REQUEST_DATA["prj_desc"]) && ($REQUEST_DATA["prj_desc"] != ""))
    $prjInfo["prj_desc"] = rawurldecode($REQUEST_DATA["prj_desc"]);

if (isset($REQUEST_DATA["prj_startdatefrom"]) && ($REQUEST_DATA["prj_startdatefrom"] != ""))
    $prjInfo["prj_startdatefrom"] = rawurldecode($REQUEST_DATA["prj_startdatefrom"]);

if (isset($REQUEST_DATA["prj_startdateto"]) && ($REQUEST_DATA["prj_startdateto"] != ""))
    $prjInfo["prj_startdateto"] = rawurldecode($REQUEST_DATA["prj_startdateto"]);

/* build search args to be passed to url */

foreach ($prjInfo as $key =>$value) {
    if (isset($value) && ($value != ""))
        $search_args .= "&$key=".rawurlencode($value);
}

$location = $_SERVER["PHP_SELF"]."?$search_args&amp;order=";

$select_company = phpaga_companies_getCompanyList();
phpaga_arrayAddOption($select_company, PHPAGA_OPTION_ALL);

$select_project = phpaga_projects_getProjectList();
phpaga_arrayAddOption($select_project, PHPAGA_OPTION_ALL);

$select_prjstat = phpaga_projects_getStatusList();
phpaga_arrayAddOption($select_prjstat, PHPAGA_OPTION_ALL);

$select_prjcat = phpaga_operations_getCategoryList();
phpaga_arrayAddOption($select_prjcat, PHPAGA_OPTION_ALL);

$select_prjcat = phpaga_projects_getCategoryList();
phpaga_arrayAddOption($select_prjcat, PHPAGA_OPTION_ALL);

$persons = phpaga_persons_list($persons_count, null, null);

$tmp_projects_billabletype = $phpaga_projects_billabletype;
phpaga_arrayAddOption($tmp_projects_billabletype, PHPAGA_OPTION_ALL);

foreach ($persons as $person) {
    $h_pe[$person["pe_id"]] = $person["pe_person"];
}

$select_person = $h_pe;
phpaga_arrayAddOption($select_person, PHPAGA_OPTION_ALL);

$tpl = new psmarty;
$tpl->assign("project", $prjInfo);
$tpl->assign("select_project", $select_project);
$tpl->assign("select_prjstat", $select_prjstat);
$tpl->assign("select_prjcat", $select_prjcat);
$tpl->assign("select_person", $select_person);
$tpl->assign("select_cpn", $select_company);
$tpl->assign("select_billable", $tmp_projects_billabletype);
$tpl->assign("ORDER", $order);
$tpl->assign("FORM_ACTION",
             basename($_SERVER["PHP_SELF"]));

$prjrows = phpaga_projects_search($count, $duration, $prjIds,
                                   $prjInfo, $order, $offset,
                                   PHPAGA_RECORDS_PERPAGE);

if (PhPagaError::isError($prjrows))
    $statusmsg = $prjrows->getFormattedMessage();
else {

    $sort = phpaga_projects_getSortOrder($order);

    $tpl->assign("search_args", $search_args);
    $tpl->assign("projects", $prjrows);
    $tpl->assign("TOTALHOURS", phpaga_minutes2duration($duration));
    $tpl->assign("perm_projects", phpaga_user_hasperm(PHPAGA_PERM_MANAGE_PROJECTS));

    $tpl->assign("BROWSE",
                 phpaga_navigate($_SERVER["PHP_SELF"].
                                 "?order=$order".$search_args,
                                 $count, $offset, PHPAGA_RECORDS_PERPAGE));

    $tpl->assign("auth_user", $auth_user);
    $tpl->assign("refop",
                 rawurlencode(
                     str_replace("&", "|", "order=$order".
                                 $search_args.
                                 "&offset=$offset")));
}

$tpl->assign("SELF", basename($_SERVER["PHP_SELF"]));
$tpl->assign("STATUSMSG", $statusmsg);
$tpl->assign("location", $location);
$tpl->assign("psort", $sort);
$tpl->assign("DELETE_PRJ_URL",
             "action=".ACTION_PRJ_REMOVE."&order=$order".
             $search_args."&offset=$offset");

$tpl->assign("porder", $order);

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

phpaga_footer();

?>
