<?php
/**
 * phpaga
 *
 * manhours graph
 *
 * View a manhours graph
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: manhours_graph.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");
include_once(PHPAGA_LIBPATH."jpgraph_wrapper.php");

$status = "";
$type = "";
$h_numdates = 0;
$h_xtickint = 0;
$manhours_arr = array ();
$manhourscount_arr = array ();
$opInfo = array ();

$opInfo = array('cpn_id' => '',
                'prj_id' => '',
                'pe_id' => '',
                'opcat_id' => '',
                'bill_id' => '',
#                'op_startdatefrom' => '');
                'op_startdatefrom' => date("Y-m-d"));

if (isset($_REQUEST["size"]) && is_numeric($_REQUEST["size"])
    && ($_REQUEST["size"] == PHPAGA_MANHOURS_GRAPH_SMALL)) {
    $graphWidth = PHPAGA_MHRSGRAPH_SMALL_WIDTH;
    $graphHeight = PHPAGA_MHRSGRAPH_SMALL_HEIGHT;
} else {
    $graphWidth = PHPAGA_MHRSGRAPH_WIDTH;
    $graphHeight = PHPAGA_MHRSGRAPH_HEIGHT;
}

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

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

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

if (isset($_REQUEST["opcat_id"]) && is_numeric($_REQUEST["opcat_id"]))
    $opInfo["opcat_id"] = $_REQUEST["opcat_id"];

if (isset($_REQUEST["bill_id"]) && is_numeric($_REQUEST["bill_id"]))
    $opInfo["bill_id"] = $_REQUEST["bill_id"];

if (isset($_REQUEST["date"]) && strlen($_REQUEST["date"]))
    $opInfo["op_startdatefrom"] = $_REQUEST["date"];

if (isset($_REQUEST["type"]) && ($_REQUEST["type"] == PHPAGA_MANHOURS_DISPLAY_YEAR)) {
    $type = PHPAGA_MANHOURS_DISPLAY_YEAR;
    $h_numdates = 54;
    $h_xtickint = 3;
    $title_xaxis = _("calendar week").
        " [".
        date ("Y", strtotime($opInfo["op_startdatefrom"])).
        "]";
} else {
    $type = PHPAGA_MANHOURS_DISPLAY_MONTH;
    $h_numdates = date("t", strtotime($opInfo["op_startdatefrom"]));
    $h_xtickint = 2;
    $title_xaxis = _("day").
        " [".
        date("m/Y", strtotime($opInfo["op_startdatefrom"])).
        "]";
}

for ($i = 0; $i < $h_numdates; $i++) {
    $manhours_arr[$i] = ($i + 1).".";
    $manhourscount_arr[$i] = 0;
}

$result = phpaga_operations_getManhours($rows, $opInfo, $type);

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

    foreach ($rows as $tmp_stats) {
        if ($type == PHPAGA_MANHOURS_DISPLAY_YEAR) {
            list($h_date,) = split(" ", $tmp_stats["mhrs_date"]);
            $h_date = (int) $h_date;
        } else
            $h_date = (int) date ("d", strtotime ($tmp_stats["mhrs_date"]));

        $manhours_arr[$h_date - 1] = "$h_date.";
        $manhourscount_arr[$h_date - 1] = $tmp_stats["mhrs_duration"];
    }

    $graph = new JpGraph($graphWidth, $graphHeight);
    $graph->SetScale("textlin");
    $graph->SetFrame(false);
    $graph->img->SetMargin(40, 30, 20, 35);

    $graph->xaxis->SetTickLabels($manhours_arr);
    $graph->xaxis->SetTextTickInterval($h_xtickint);
    $graph->xaxis->title->Set($title_xaxis);
    $graph->yaxis->title->Set(_("man-hours"));

    $graph->xaxis->SetColor("#888888");
    $graph->yaxis->SetColor("#888888");

    $graph->xaxis->title->SetColor("#666666");
    $graph->yaxis->title->SetColor("#666666");

    $graph->ygrid->SetFill(true,'#efefef@0.5','#ddddff@0.5');

    $p1 = new BarPlot($manhourscount_arr);
    $p1->SetFillGradient("#eeeeee", "#dddddd", GRAD_HOR);
    $p1->SetColor("#666666");

    $p1->value->Show();

    $p1->value->SetAngle(90);
    $p1->value->SetFont(FF_FONT0);
    $p1->value->SetColor("#666666");

    if (($type == PHPAGA_MANHOURS_DISPLAY_YEAR)
        && defined("PHPAGA_MHRSGRAPH_WEEKLY_AVG_HOURS")
        && (PHPAGA_MHRSGRAPH_WEEKLY_AVG_HOURS > 0)
        && defined("PHPAGA_MHRSGRAPH_WEEKLY_AVG_LINECOLOR")
        && strlen(PHPAGA_MHRSGRAPH_WEEKLY_AVG_LINECOLOR))

        $graph->AddLine(new PlotLine(HORIZONTAL,
                                     PHPAGA_MHRSGRAPH_WEEKLY_AVG_HOURS,
                                     PHPAGA_MHRSGRAPH_WEEKLY_AVG_LINECOLOR,
                                     1));

    $graph->Add($p1);
    $graph->Stroke();

}

?>
