<?php
/**
 * phpaga
 *
 * Summary graph
 *
 * View a summary chart
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: summary_graph.php 536 2005-05-23 20:43:56Z worf $
 *
 * Copyright (c) 2005, 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");

$summary = array();

$graphWidth = PHPAGA_GRAPH_WIDTH;
$graphHeight = PHPAGA_GRAPH_HEIGHT;

$result = phpaga_getSummary($summary);

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

    if (isset($_REQUEST["type"]) && ($_REQUEST["type"] == "prj"))
    {
        foreach ($summary["projects"]["rows"] as $tmp_stats)
        {
            $item_arr[] = $tmp_stats["prj_title"];
            $itemcount_arr[] = $tmp_stats["sum_duration"];
        }
    }
    elseif (isset($_REQUEST["type"]) && ($_REQUEST["type"] == "pe"))
    {
        foreach ($summary["persons"]["rows"] as $tmp_stats)
        {
            $item_arr[] = $tmp_stats["pe_person"];
            $itemcount_arr[] = $tmp_stats["sum_duration"];
        }
    }
    elseif (isset($_REQUEST["type"]) && ($_REQUEST["type"] == "cu"))
    {
        if (!isset($_REQUEST["curr_id"]) || !is_numeric($_REQUEST["curr_id"]))
        {
            phpaga_error(_("Illegal currency"));
            exit;
        }

        if (!isset($summary["customers"][$_REQUEST["curr_id"]]))
        {
            phpaga_error(_("Unable to fetch data for currency"));
            exit;
        }

        foreach ($summary["customers"][$_REQUEST["curr_id"]]["rows"] as $tmp_stats)
        {
            $item_arr[] = $tmp_stats["cpn_name"];
            $itemcount_arr[] = $tmp_stats["sum_amount"];
        }
    }

    else
    {
        phpaga_error(_("Illegal type"));
        exit;
    }

    $graph = new PieGraph($graphWidth, $graphHeight);
    $graph->SetFrame(false);

    $graph->legend->Pos(0.001, 0.01);
    $graph->legend->SetShadow(false);
    $graph->legend->SetFillColor("#ffffff");

    if (PHPAGA_GRAPH_OPCAT_STYLE == PHPAGA_GRAPH_OPCAT_STYLE_PIE2D)
        $p1 = new PiePlot ($itemcount_arr);
    else
    {
        $p1 = new PiePlot3d ($itemcount_arr);
        $p1->SetAngle (PHPAGA_GRAPH_OPCAT_ANGLE);
    }

    if (isset($phpaga_piecolors) && is_array($phpaga_piecolors))
        $p1->SetSliceColors($phpaga_piecolors);

    $p1->SetSize(0.3);
    $p1->ExplodeAll();

    $p1->SetTheme('sand');
    $p1->SetCenter(0.3);
    $p1->SetLegends($item_arr);

    $graph->legend->SetColor("#444444", "white");
    $graph->legend->SetFont(FF_FONT1);
    $graph->Add($p1);
    $graph->Stroke();

}

?>