<?php
/**
 * phpaga
 *
 * manhours graph
 *
 * View a manhours graph
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: manhours_graph.php 857 2007-09-19 20:12:30Z 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.'graphs.php');

/* Callback function */
function _removeZero($v) {
    return $v != 0;
}

$type = '';
$data = array();
$avg = array();

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

if (isset($_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;
    $title = _('Weekly manhours');
    $title_xaxis = _("calendar week"). " [". date ("Y", strtotime($opInfo["op_startdatefrom"])). "]";

    for ($i = 1; $i <= 53; $i++) {
        $data[$i] = 0;
        $avg[$i] = PHPAGA_MHRSGRAPH_WEEKLY_AVG_HOURS;
    }

} else {
    $type = PHPAGA_MANHOURS_DISPLAY_MONTH;
    $title = _('Daily manhours');
    $title_xaxis = _("day"). " [". date("m/Y", strtotime($opInfo["op_startdatefrom"])). "]";

    for ($i = 1; $i <= date("t", strtotime($opInfo["op_startdatefrom"])); $i++)
        $data[sprintf("%02s", $i)] = 0;
}

$rows = phpaga_operations_getManhours($opInfo, $type);
if (PhPagaError::isError($rows)) {
    $rows->printMessage();
    exit;
}

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 = sprintf("%02s", date("d", strtotime($tmp_stats["mhrs_date"])));

    $data[$h_date] = sprintf('%.1f', (float)$tmp_stats["mhrs_duration"]);
}

$graph = new PhPagaGraphBarChart();

if (strlen($title))
    $graph->title = $title;

$graph->legend = false;
$graph->xAxis = new ezcGraphChartElementLabeledAxis();
$graph->xAxis->label = $title_xaxis;
$graph->xAxis->axisLabelRenderer->showZeroValue = true;
$graph->yAxis->label = _('man-hours');
$graph->yAxis->axisSpace = .05;

$graph->renderer->options->barPadding = .7;
$graph->renderer->options->dataBorder = .2;

$graph->options->highlightSize = 10;
$graph->options->highlightFont->background = '#EEEEEC88';
$graph->options->highlightFont->border = '#cccccc';
$graph->options->highlightFont->borderWidth = 1;

$graph->palette = new PhPagaManHoursGraphPalette();

$graph->data[_('man-hours')] = new ezcGraphArrayDataSet($data);

if (is_array($avg) && count($avg)) {
    $graph->data['avg'] = new ezcGraphArrayDataSet($avg);
    $graph->data['avg']->displayType = ezcGraph::LINE;
}

if ($type == PHPAGA_MANHOURS_DISPLAY_MONTH) {
    $graph->xAxis->labelCount = count($graph->data[_("man-hours")]);
    $graph->renderer->options->barMargin = .1;

    foreach ($data as $idx => $val)
        if ($val != 0)
            $graph->data[_('man-hours')]->highlight[$idx] = true;
} else {
    $graph->xAxis->labelCount = 14;
    $graph->renderer->options->barMargin = .2;

    $max = max(array_values($data));
    $min = min(array_filter(array_values($data), '_removeZero'));

    foreach ($data as $idx => $val)
        if (($val == $max) || ($val == $min))
            $graph->data[_('man-hours')]->highlight[$idx] = true;
}

$graph->renderToOutput($graphWidth, $graphHeight);

?>