<?php
if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
	include_once("../../embedded/functions/site_functions.php");
	}
if (!isset($device_id))
{	
$device_id=$_SESSION['device_id'];
}
db_open();
//start to origin data for graph
//period declaration
				if ($period =="day")
					{
					$ptitle=$final_msg;
					$a=database_graph($d_msg,$option=1,$device_id);
					if(empty($a[0][0]))
						{
						$data_fan= $a[5];
						$data_watering= $a[6];
						$data_lighting= $a[7];
						$labels =$a[8];
						$angle="90";
						}
					else
						{
						echo '<span class="day_deactivated">'.$a[0][0].'</span>';
						die();
						}
					}
				else if ($period=="week")
					{
					$ptitle=$final_msg;
					$a=database_graph($d_msg,$option=22,$device_id);
					if(empty($a[0][0]))
						{
						$data_fan= $a[5];
						$data_watering= $a[6];
						$data_lighting= $a[7];
						$labels =$a[8];
						$angle="0";
						}
					else
						{
						echo '<span class="day_deactivated">'.$a[0][0].'</span>';
						die();
						}
					}
				else if ($period=="month")
					{
					$ptitle=$final_msg;
					$a=database_graph($d_msg,$option=32,$device_id);
					if(empty($a[0][0]))
						{
						$data_fan= $a[5];
						$data_watering= $a[6];
						$data_lighting= $a[7];
						}
					else
						{
						echo '<span class="day_deactivated">'.$a[0][0].'</span>';
						die();
						}
					$labels =$a[8];
					$angle="90";
					}
				else
					{
					$ptitle=$final_msg;
					$a=database_graph($d_msg,$option=4,$device_id);
					if(empty($a[0][0]))
						{
						$data_fan= $a[5];
						$data_watering= $a[6];
						$data_lighting= $a[7];
						$labels =$a[8];
						$angle="0";
						}
					else
						{
						echo '<span class="day_deactivated">'.$a[0][0].'</span>';
						die();
						}
					}

	//device check margin for graph
	if ($device=="all")
		{
		$a=30;
		$b=5;
		$c=5;
		$d=80;
		$title="Device_Overview".$ptitle;
		$fileName = $title.".png";	
		}
	else
		{
		$a=40;
		$b=20;
		$c=5;
		$d=0;
		if ($device=="fan")
			{
			$title="Fan_Overview".$ptitle;
			$fileName = $title.".png";	
			}
		else if ($device=="watering")
			{
			$title="Watering_Overview".$ptitle;
			$fileName = $title.".png";	
			
			}	
		else if ($device=="lighting")
			{
			$title="Lighting_Overview".$ptitle;
			$fileName = $title.".png";	
			}
		}
		if(isset($e) && ($e)==1)
			{
			$path = "charts/devices/".$fileName;
			$image="charts/devices/$fileName";
			}
		else
			{
			$path = "../../charts/devices/".$fileName;
			$image="charts/devices/$fileName";
			}	
	
			//check if this plot is already produced
			if (file_exists($path))
					{
					if (preg_match('/\.(png)$/i', $image ))
						{

						echo"<img src='$image' id='device_image' alt='graph' class='chart_graph' title='$title' onclick='bigger(2);' onmouseover='notice(1)'; onmouseout='notice(2)'; ></img>";
						}
					else
						{
						echo'<span class="day_deactivated">Problem during image uploading!</span>';
						}
					}
				else
					{
				//here we make the bar graph		
				if ($period=="day")
				{
					if(isset($e) && $e==1)
						{
						require_once ("jpgraph/jpgraph.php");
						require_once ("jpgraph/jpgraph_bar.php");
						}
					else
						{
						require_once ("../../jpgraph/jpgraph.php");
						require_once ("../../jpgraph/jpgraph_bar.php");
						}
						
						// Setup the graph
						$graph = new Graph(600,450);
						$graph->SetMargin($a,$b,$c,$d); 
						$graph->SetScale('textlin');
						
						$graph->img->SetAntiAliasing(true);
						$graph->img->SetQuality(100);
						
						$graph->yaxis->HideZeroLabel();
						$graph->yaxis->HideLine(false);
						$graph->yaxis->HideTicks(false,false);
						$graph->ygrid->SetLineStyle("solid");
						$graph->ygrid->SetColor('#E3E3E3');
														
						$graph->xgrid->Show();
						$graph->xgrid->SetLineStyle("solid");
						$graph->xaxis->SetLabelAngle($angle);
						$graph->xaxis->SetTickLabels($labels);
						$graph->xgrid->SetColor('#E3E3E3');

						if ($device =="fan")
						{
						// Create the fan_bar
						$bplot = new BarPlot($data_fan);
						$graph->Add($bplot);
						$bplot->SetFillColor('red');
						$bplot->SetLegend('Fan');
						$bplot->SetWeight(30);
						$graph->title->Set($title);
						}
					else if ($device =="watering")
						{
						//create the watering bar
						$bplot = new BarPlot($data_watering);
						$graph->Add($bplot);
						$bplot->SetFillColor('blue');
						$bplot->SetLegend('Watering');
						$bplot->SetWeight(30);
						$graph->title->Set($title);
						}
					else if ($device =="lighting")
						{
						//create the watering bar
						$bplot = new BarPlot($data_lighting);
						$graph->Add($bplot);
						$bplot->SetFillColor("#DAA520");
						$bplot->SetLegend('Lighting');
						$bplot->SetWeight(30);
						$graph->title->Set($title);
						}
					else
						{
						$b1plot = new BarPlot($data_fan);
						$b1plot->SetFillColor("red");
						$b2plot = new BarPlot($data_watering);
						$b2plot->SetFillColor("blue");
						$b3plot = new BarPlot($data_lighting);
						$b3plot->SetFillColor("#DAA520");
						$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
						$graph->Add($gbplot);
						$b1plot->SetLegend('Fan');
						$b2plot->SetLegend('Watering');
						$b3plot->SetLegend('Lighting');
						
						//end of lines
						$graph->title->Set($title);
						}
				}
				//end of day period
			else
				{	
				if($period=="month")
					{
					for($i=0;$i<=count($data_lighting)-1;$i++)
								{
								$labels_light[$i]=$labels[$i];
								if ($data_lighting[$i]==0)
									{
									$labels_light[$i]="-\n(%.1f%%)";
									}
								}
					for($i=0;$i<=count($data_fan)-1;$i++)
								{
								$labels_fan[$i]=$labels[$i];
								if ($data_fan[$i]==0)
									{
									$labels_fan[$i]="-\n(%.1f%%)";
									}
								}
					for($i=0;$i<=count($data_watering)-1;$i++)
								{
								$labels_water[$i]=$labels[$i];
								if ($data_watering[$i]==0)
									{
									$labels_water[$i]="-\n(%.1f%%)";
									}
								}			
					}
				if ($period==="week" && $device!='all')
					{					
				if ($data_fan[0]==0 && $data_fan[1]==0 &&$data_fan[2]==0 && $data_fan[3]==0 && $data_fan[4]==0 && $data_fan[5]==0 && $data_fan[6]==0)
							{
							die("$headdd");
							}
						if ($data_watering[0]==0 && $data_watering[1]==0 &&$data_watering[2]==0 && $data_watering[3]==0 && $data_watering[4]==0 && $data_watering[5]==0 && $data_fan[6]==0)
							{
							die("$headdd"); 
							}
						if ($data_lighting[0]==0 && $data_lighting[1]==0 &&$data_lighting[2]==0 && $data_lighting[3]==0 && $data_lighting[4]==0 && $data_lighting[5]==0 && $data_lighting[6]==0)
							{
							die("$headdd");
							}
					}
				//here is produced the pies
					if(isset($e) && ($e)==1)
						{
						require_once ("jpgraph/jpgraph.php");
						require_once ("jpgraph/jpgraph_pie.php");
						require_once ("jpgraph/jpgraph_pie3d.php");
						}
					else
						{
						require_once ("../../jpgraph/jpgraph.php");
						require_once ("../../jpgraph/jpgraph_pie.php");
						require_once ("../../jpgraph/jpgraph_pie3d.php");
						}
					
					$graph = new PieGraph(650,500,'auto');
					$graph->SetShadow();
					$graph->img->SetAntiAliasing(true);
					$graph->img->SetQuality(100);
					if($device=="fan")
						{
						if ($period=="month")
							{
							$labels=$labels_fan;
							}
						$p1 = new PiePlot3D($data_fan);
						$p1->SetAngle(60);
						$p1->SetSize(0.5);
						$p1->SetCenter(0.5);
						if ($period=="month")
							{
							$p1->SetLabels($labels);
							$p1->SetLabelPos(1);
							$p1->SetLabelType(PIE_VALUE_PER);
							}
						else
							{
							$p1->SetLegends($labels);
							}
						$graph->Add($p1);
						$graph->title->Set($title);
						}
					else if($device=="watering")
						{
						if ($period=="month")
							{
							$labels=$labels_water;
							}
						$p1 = new PiePlot3D($data_watering);
						$p1->SetAngle(60);
						$p1->SetSize(0.5);
						$p1->SetCenter(0.5);
						if ($period=="month")
							{
							$p1->SetLabels($labels);
							$p1->SetLabelPos(1);
							$p1->SetLabelType(PIE_VALUE_PER);
							}
						else
							{
							$p1->SetLegends($labels);
							}
						$graph->Add($p1);
						$graph->title->Set($title);
						
						}
					else if($device=="lighting")
						{
						if ($period=="month")
							{
							$labels=$labels_light;
							}
						$p1 = new PiePlot3D($data_lighting);
						$p1->SetAngle(60);
						$p1->SetSize(0.5);
						$p1->SetCenter(0.55);
						if ($period=="month")
							{
							$p1->SetLabels($labels);
							$p1->SetLabelPos(1);
							$p1->SetLabelType(PIE_VALUE_PER);
							}
						else
							{
							$p1->SetLegends($labels);
							}
						$graph->Add($p1);
						$graph->title->Set($title);
						}
					else
						{
							$data = array($data_fan,$data_watering,$data_lighting);
 							$piepos = array(0.25,0.25,0.75,0.25,0.25,0.7);
							$titles = array('Fan','Watering','Lighting');
						if ($data_fan[0]==0 && $data_fan[1]==0 &&$data_fan[2]==0 && $data_fan[3]==0 && $data_fan[4]==0 && $data_fan[5]==0 && $data_fan[6]==0)
							{
							$data = array($data_watering,$data_lighting);
							$titles = array('Watering','Lighting');
							$piepos = array(0.2,0.3,0.6,0.3);
							}
						if ($data_watering[0]==0 && $data_watering[1]==0 &&$data_watering[2]==0 && $data_watering[3]==0 && $data_watering[4]==0 && $data_watering[5]==0 && $data_fan[6]==0)
							{
							$data = array($data_fan,$data_lighting);
							$titles = array('Fan','Lighting');
							$piepos = array(0.2,0.3,0.6,0.3);
							}
						if ($data_lighting[0]==0 && $data_lighting[1]==0 &&$data_lighting[2]==0 && $data_lighting[3]==0 && $data_lighting[4]==0 && $data_lighting[5]==0 && $data_lighting[6]==0)
							{
							$data = array($data_fan,$data_watering);
							$titles = array('Fan','Watering');
							$piepos = array(0.2,0.3,0.6,0.3);
							}
						
							 
							$n = count($piepos)/2;
							 // Specify margins since we put the image in the plot area
							$graph->SetMargin(0,0,0,0);
							// Setup title
							$graph->title->Set($title);
							 
							$p = array();
							// Create the plots
							for( $i=0; $i < $n; ++$i ) {
								$d = "data$i";
								$p[] = new PiePlot3D($data[$i]);
								$p[$i]->SetAngle(60);
								$p[$i]->SetSize(0.5);
								$p[$i]->SetCenter(0.3);
							}
							 
							// Position the four pies
							for( $i=0; $i < $n; ++$i ) {
								$p[$i]->SetCenter($piepos[2*$i],$piepos[2*$i+1]);
							}
							 
							// Set the titles
							for( $i=0; $i < $n; ++$i ) {
								$p[$i]->title->Set($titles[$i]);
								}
							 
							// Label font and color setup
							for( $i=0; $i < $n; ++$i ) {
								$p[$i]->value->SetFont(FF_ARIAL,FS_BOLD);
								$p[$i]->value->SetColor('blue');
							}
							 
							// Show the percetages for each slice
							for( $i=0; $i < $n; ++$i ) {
								$p[$i]->value->Show();
							}
							if ($period=="month")
								{
								$p[0]->SetLabels($labels_fan);
								$p[1]->SetLabels($labels_water);
								$p[2]->SetLabels($labels_light);
								// Label format
								for( $i=0; $i < $n; ++$i ) {
									$p[$i]->SetLabelPos(1);
									$p[$i]->SetLabelType(PIE_VALUE_PER); 
									$p[$i]->value->SetFormat("%01.1f%%");
								}
								// Size of pie in fraction of the width of the graph
								for( $i=0; $i < $n; ++$i ) {
									$p[$i]->SetSize(0.2);
								} 
								}
							else
								{							
								// Label format
								for( $i=0; $i < $n; ++$i ) {
									$p[$i]->value->SetFormat("%01.1f%%");
								}
								 
								// Size of pie in fraction of the width of the graph
								for( $i=0; $i < $n; ++$i ) {
									$p[$i]->SetSize(0.15);
								} 
								// Use one legend for the whole graph
								$p[0]->SetLegends($labels);
								$graph->legend->Pos(0.2,0.5);
								}
							
							
								for( $i=0; $i < $n; ++$i ) {
								$graph->Add($p[$i]);
							}
						
						
						
						
						}
				}
				//graph  is being produced
				$graph->legend->SetFrameWeight(2);
				$graph->legend->Pos(0.5,0.98,"center","bottom");
				$graph->legend->SetLayout(LEGEND_HOR);
				//r Output line
				$gdImgHandler = $graph->Stroke(_IMG_HANDLER);
				
				// Default is PNG so use ".png" as suffix
				$graph->img->Stream($path);
				if (file_exists($path))
					{					
					if (preg_match('/\.(png)$/i', $image ))
						{
						
						$date=date("Y-m-d");
						$sql="INSERT INTO charts (chart_id,device_id,dater,filename,folder) VALUES (null, '$device_id', '$date', '$title', '2')";
						@ $result=mysql_query($sql);
						
						if ($result!=0)
							{
							echo"<img src='$image' id='device_image' alt='graph' class='chart_graph' title='$title' onclick='bigger(2);' onmouseover='notice(1)'; onmouseout='notice(2)'; ></img>";
							}
						else
							{
							echo'<span class="day_deactivated">Database Troubleshooting!</span>';
							}
						}
					else
						{
						echo'<span class="day_deactivated">Problem during image uploading!</span>';
						}
					}
				else
					{
					echo'<span class="day_deactivated">Problem during image uploading!</span>';
					}
		}
db_close();
?>

