<?php
Class RelationshipHandler
{
public $xml_data; //this is an array thats passed and exists as the <relationship> field
public $page_from_data; //this is the data that the page has come from, im not sure why this is neccesary but it might be
public $feeder; //feeder is the name of the object that's page called the relationship handler, e.g. client or lead
public function __construct($feeder, $xml_data, $page_from_data, $view, $chartType)
{
$this->feeder = $feeder;
$this->xml_data = $xml_data;
$this->page_from_data = $page_from_data;
if ($xml_data['type']=="recip")
{
$this->handleRecip($view, $chartType);
}
}
public function handleRecip($view, $chartType)
{
$feeder = $this->feeder;
$xml = $this->xml_data;
$data = $this->page_from_data;
if ($view == "receivedComparisonGraph")
{
//We are going to create a graph (or until that point at least a little chart) that compares the amount of received OBJECTS from the recip object instance to the other top X (where X is your desired number) givers of the same recip object type
$typeReceived = (string)$xml['receive'];
$typeGiven = (string)$xml['give'];
$givingObject = (string)$xml['with'];
//Lets start by seeing the total number of objects received
$query =
mysql_query("SELECT * FROM `".
$typeReceived.
"`");
//Now we get the number received by the instance that gave the feeder_instance
$query =
mysql_query("SELECT * FROM `".
$typeReceived.
"` WHERE `".
$givingObject.
"_id`='".
$data[$givingObject.
"_id"].
"'");
//Now what we have to do is get the top X of the givingObjects type for giving this kind of object
$query =
mysql_query("SELECT * FROM `".
$givingObject.
"`");
$otherGivingObjects =
array();
$otherGivingObjectsGiven =
array();
{
$otherGivingObjects[$d['id']] = $d;
$query2 =
mysql_query("SELECT * FROM `".
$typeReceived.
"` WHERE `".
$givingObject.
"_id`='".
$d['id'].
"'");
}
//So now we have 2 arrays, each with corresponding IDs that we have to sort so that they ascend by the value of Objects Given
//now we shall slice it by amount desired
$amount_desired_to_show = 5;
$otherGivingObjects =
array_slice($otherGivingObjects,
0,
$amount_desired_to_show);
$otherGivingObjectsGiven =
array_slice($otherGivingObjectsGiven,
0,
$amount_desired_to_show);
//we also need to include the object that gave the feeder object
$query =
mysql_query("SELECT * FROM `".
$givingObject.
"` WHERE `id`='".
$data[$givingObject.
"_id"].
"'");
$query =
mysql_query("SELECT * FROM `".
$typeReceived.
"` WHERE `".
$givingObject.
"_id`='".
$data[$givingObject.
"_id"].
"'");
//now we need to find out the secondary display type so that can work out how to view that type
$givingObjectXML = simplexml_load_file($_SERVER['DOCUMENT_ROOT']."/EMS/data/lead.xml");
$toShow = (string)$givingObjectXML->basic[0]->secondary[0]['name'];
//we also need to include a none object
$otherGivingObjects[] =
array ($toShow =>
"None");
$query =
mysql_query("SELECT * FROM `".
$typeReceived.
"` WHERE `".
$givingObject.
"_id`=0");
$dataLine = "";
//we need to work out the scale since GoogleGraph works on the basis of 0-100 if we change the scale to higher than 100 it scales everything as if it were that.
//$delim = $totalNumberReceived/100;
foreach ($otherGivingObjects as $id => $givingObject)
{
$dataLine .= $otherGivingObjectsGiven[$id].",";
$labelLine .= $givingObject[$toShow]."|";
}
//lets show the graph
if ($chartType=="vbar")
{
$DataSet = new pData;
foreach ($otherGivingObjectsGiven as $id => $ogog)
{
$DataSet->
AddPoint(array($ogog),
"Serie".
$id);
}
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie();
foreach ($otherGivingObjectsGiven as $id => $ogog)
{
$DataSet->SetSerieName($otherGivingObjects[$id][$toShow], "Serie".$id);
}
// Initialise the graph
$Test = new pChart(700,230);
$Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",8);
$Test->setGraphArea(50,30,680,200);
$Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
$Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
$Test->drawGraphArea(255,255,255,TRUE);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
$Test->drawGrid(4,TRUE,230,230,230,50);
// Draw the 0 line
$Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
// Draw the bar graph
$Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
// Finish the graph
$Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",8);
$Test->drawLegend(596,150,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",10);
$Test->drawTitle(50,22,"Example 12",50,50,50,585);
$Test->Render("img/graph_".$key.".png");
?>
<img src="/EMS/img/graph_<?=$key?>.png" alt="Graph" />
` <?
//unlink("img/graph_".$key.".png");
}
}
}
}