<?php
  /**
   * phpaga
   *
   * task category stats
   *
   * View task category statistics
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: opcat_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');

$status = "";
$opInfo = array ();
$sum_duration = 0;

$data = array();
$palette = array();
$palette_defined = false;

$opInfo = array('cpn_id' => '',
                'prj_id' => '',
                'pe_id' => '',
                'opcat_id' => '',
                'op_desc' => '',
                'op_startdatefrom' => '',
                'op_startdateto' => '');

if (isset($_REQUEST["pdf"]) && is_numeric($_REQUEST["pdf"])
    && ($_REQUEST["pdf"] == 1)) {
    $graphWidth = PHPAGA_GRAPH_PDF_WIDTH;
    $graphHeight = PHPAGA_GRAPH_PDF_HEIGHT;
} else {
    $graphWidth = PHPAGA_GRAPH_WIDTH;
    $graphHeight = PHPAGA_GRAPH_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["op_desc"]) && strlen($_REQUEST["op_desc"]))
    $opInfo["op_desc"] = $_REQUEST["op_desc"];

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

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


$opcatStats = phpaga_operations_getOpcatStats($opInfo, $sum_duration);

if (PhPagaError::isError($opcatStats))
    $opcatStats->printMessage();
else {

    $p = 0;

    foreach ($opcatStats as $t) {

        $p++;

        $data[$t["opcat_title"]] = (float)$t["opcat_duration"] / 60.0;

        if (isset($t["opcat_color"]) && strlen($t["opcat_color"])) {
            $palette[$p] = $t["opcat_color"];

            $palette_defined = true;
        } else
            $palette[$p] = "#ffffff";
    }

    $graph = new PhPagaGraphPieChart();

    if (isset($palette) && is_array($palette)
        && (count($palette) > 0) && $palette_defined) {

        $graph->palette = new PhPagaGraphPalette();
        $graph->palette->setColors($palette);
    }

    $graph->title = _('Task categories');

    $graph->data['opcat'] = new ezcGraphArrayDataSet($data);

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

}

?>