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

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

phpaga_header(array("calendar" => true, "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["offset"]) && is_numeric($_REQUEST["offset"]))
    $offset = $_REQUEST["offset"];

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

    $order = $_REQUEST["order"];

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

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

/* get search criteria */

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

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

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

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

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

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

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

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

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

if (isset($_REQUEST["prj_startdateto"]) && ($_REQUEST["prj_startdateto"] != ""))
    $prjInfo["prj_startdateto"] = rawurldecode($_REQUEST["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=";

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

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

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

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

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

phpaga_jobcat_getList($select_jcat);
phpaga_arrayAddOption($select_jcat, PHPAGA_OPTION_ALL);

phpaga_persons_list($persons, $persons_count, "", "");

$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"]));

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

if (($projects != PHPAGA_ERR_NONE) && ($projects != PHPAGA_ERR_DB_NO_ROWS)) {
    $statusmsg = phpaga_returnmessage(_("Information"),
                                      $phpaga_error[$projects]);
} 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();

?>