1. <?php
  2. Class RelationshipHandler
  3. {
  4. public $xml_data; //this is an array thats passed and exists as the <relationship> field
  5. 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
  6. public $feeder; //feeder is the name of the object that's page called the relationship handler, e.g. client or lead
  7.  
  8. public function __construct($feeder, $xml_data, $page_from_data, $view, $chartType)
  9. {
  10. $this->feeder = $feeder;
  11. $this->xml_data = $xml_data;
  12. $this->page_from_data = $page_from_data;
  13.  
  14. if ($xml_data['type']=="recip")
  15. {
  16. $this->handleRecip($view, $chartType);
  17. }
  18. }
  19.  
  20. public function handleRecip($view, $chartType)
  21. {
  22. $feeder = $this->feeder;
  23. $xml = $this->xml_data;
  24. $data = $this->page_from_data;
  25.  
  26. if ($view == "receivedComparisonGraph")
  27. {
  28. //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
  29.  
  30. $typeReceived = (string)$xml['receive'];
  31. $typeGiven = (string)$xml['give'];
  32. $givingObject = (string)$xml['with'];
  33.  
  34. //Lets start by seeing the total number of objects received
  35. $query = mysql_query("SELECT * FROM `".$typeReceived."`");
  36. $totalNumberReceived = mysql_affected_rows();
  37.  
  38. //Now we get the number received by the instance that gave the feeder_instance
  39. $query = mysql_query("SELECT * FROM `".$typeReceived."` WHERE `".$givingObject."_id`='".$data[$givingObject."_id"]."'");
  40. $instanceNumberReceived = mysql_affected_rows();
  41.  
  42. //Now what we have to do is get the top X of the givingObjects type for giving this kind of object
  43. $query = mysql_query("SELECT * FROM `".$givingObject."`");
  44. $otherGivingObjects = array();
  45. $otherGivingObjectsGiven = array();
  46.  
  47. while ($d = mysql_fetch_assoc($query))
  48. {
  49. $otherGivingObjects[$d['id']] = $d;
  50. $query2 = mysql_query("SELECT * FROM `".$typeReceived."` WHERE `".$givingObject."_id`='".$d['id']."'");
  51. $otherGivingObjectsGiven[$d['id']] = mysql_affected_rows();
  52. }
  53.  
  54. //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
  55. array_multisort($otherGivingObjectsGiven, SORT_DESC, $otherGivingObjects);
  56.  
  57. //now we shall slice it by amount desired
  58. $amount_desired_to_show = 5;
  59. $otherGivingObjects = array_slice($otherGivingObjects, 0, $amount_desired_to_show);
  60. $otherGivingObjectsGiven = array_slice($otherGivingObjectsGiven, 0, $amount_desired_to_show);
  61.  
  62. //we also need to include the object that gave the feeder object
  63. $query = mysql_query("SELECT * FROM `".$givingObject."` WHERE `id`='".$data[$givingObject."_id"]."'");
  64. $otherGivingObjects[] = mysql_fetch_assoc($query);
  65.  
  66. $query = mysql_query("SELECT * FROM `".$typeReceived."` WHERE `".$givingObject."_id`='".$data[$givingObject."_id"]."'");
  67. $otherGivingObjectsGiven[] = mysql_affected_rows();
  68.  
  69. //now we need to find out the secondary display type so that can work out how to view that type
  70. $givingObjectXML = simplexml_load_file($_SERVER['DOCUMENT_ROOT']."/EMS/data/lead.xml");
  71. $toShow = (string)$givingObjectXML->basic[0]->secondary[0]['name'];
  72.  
  73. //we also need to include a none object
  74. $otherGivingObjects[] = array ($toShow => "None");
  75. $query = mysql_query("SELECT * FROM `".$typeReceived."` WHERE `".$givingObject."_id`=0");
  76. $otherGivingObjectsGiven[] = mysql_affected_rows();
  77.  
  78.  
  79. $dataLine = "";
  80.  
  81. //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.
  82. //$delim = $totalNumberReceived/100;
  83.  
  84. foreach ($otherGivingObjects as $id => $givingObject)
  85. {
  86. $dataLine .= $otherGivingObjectsGiven[$id].",";
  87. $labelLine .= $givingObject[$toShow]."|";
  88. }
  89. $dataLine = substr($dataLine, 0, strlen($dataLine)-1);
  90. $labelLine = substr($labelLine, 0, strlen($labelLine)-1);
  91.  
  92. //lets show the graph
  93. if ($chartType=="vbar")
  94. {
  95.  
  96. $DataSet = new pData;
  97. foreach ($otherGivingObjectsGiven as $id => $ogog)
  98. {
  99. $DataSet->AddPoint(array($ogog), "Serie".$id);
  100. }
  101. $DataSet->AddAllSeries();
  102. $DataSet->SetAbsciseLabelSerie();
  103. foreach ($otherGivingObjectsGiven as $id => $ogog)
  104. {
  105. $DataSet->SetSerieName($otherGivingObjects[$id][$toShow], "Serie".$id);
  106. }
  107.  
  108. // Initialise the graph
  109. $Test = new pChart(700,230);
  110. $Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",8);
  111. $Test->setGraphArea(50,30,680,200);
  112. $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
  113. $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
  114. $Test->drawGraphArea(255,255,255,TRUE);
  115. $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
  116. $Test->drawGrid(4,TRUE,230,230,230,50);
  117.  
  118. // Draw the 0 line
  119. $Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",6);
  120. $Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  121.  
  122. // Draw the bar graph
  123. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  124.  
  125. // Finish the graph
  126. $Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",8);
  127. $Test->drawLegend(596,150,$DataSet->GetDataDescription(),255,255,255);
  128. $Test->setFontProperties($_SERVER['DOCUMENT_ROOT']."/EMS/lib/pChart/Fonts/tahoma.ttf",10);
  129. $Test->drawTitle(50,22,"Example 12",50,50,50,585);
  130. $key = md5(rand(1, 1000)).md5(rand(1, 1000));
  131. $Test->Render("img/graph_".$key.".png");
  132. ?>
  133. <img src="/EMS/img/graph_<?=$key?>.png" alt="Graph" />
  134. ` <?
  135. //unlink("img/graph_".$key.".png");
  136. }
  137. }
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.