<?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_temperature= $a[1];
						$data_humidity= $a[2];
						$data_soilmoisture= $a[3];
						$data_indoorlighting= $a[4];
						$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=2,$device_id);
					if(empty($a[0][0]))
						{
						$data_temperature= $a[1];
						$data_humidity= $a[2];
						$data_soilmoisture= $a[3];
						$data_indoorlighting= $a[4];
						$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=3,$device_id);
					if(empty($a[0][0]))
						{
						$data_temperature= $a[1];
						$data_humidity= $a[2];
						$data_soilmoisture= $a[3];
						$data_indoorlighting= $a[4];
						}
					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_temperature= $a[1];
						$data_humidity= $a[2];
						$data_soilmoisture= $a[3];
						$data_indoorlighting= $a[4];
						$labels =$a[8];
						$angle="0";
						}
					else
						{
						echo '<span class="day_deactivated">'.$a[0][0].'</span>';
						die();
						}
					}

	//sensor check margin for graph
	if ($sensor=="all")
		{
		$a=40;
		$b=140;
		$c=5;
		$d=0;
		$title="Sensor_Overview".$ptitle;
		$fileName = $title.".png";	
		}
	else
		{
		$a=40;
		$b=20;
		$c=5;
		$d=0;
		if ($sensor=="temperature")
			{
			$title="Temperature_Overview".$ptitle;
			$fileName = $title.".png";	
			}
		else if ($sensor=="humidity")
			{
			$title="Humidity_Overview".$ptitle;
			$fileName = $title.".png";	
			}	
		else if ($sensor=="indoorlighting")
			{
			$title="Indoor_Lighting_Overview".$ptitle;
			$fileName = $title.".png";
			}
		else if ($sensor=="soilmoisture")
			{
			$title="Soil_Moisture_Overview".$ptitle;
			$fileName = $title.".png";	
			}			
		}
		if(isset($e) && $e==1)
			{
			$path = "charts/sensors/".$fileName;
			$image="charts/sensors/$fileName";
			}
		else
			{
			$path = "../../charts/sensors/".$fileName;
			$image="charts/sensors/$fileName";
			}
			if (file_exists($path))
					{
					if (preg_match('/\.(png)$/i', $image ))
						{
						echo"<img src='$image' id='sensor_image' alt='graph' class='chart_graph' title='$title' onclick='bigger(1);' onmouseover='notice(1)'; onmouseout='notice(2)'; ></img>";
						}
					else
						{
						echo"Problem during image uploding!";
						}
					}
				else
					{
				if(isset($e) && $e==1)
						{
						require_once ("jpgraph/jpgraph.php");
						require_once ("jpgraph/jpgraph_line.php");
						}
					else
						{
						require_once ("../../jpgraph/jpgraph.php");
						require_once ("../../jpgraph/jpgraph_line.php");
						}	
				
				// Setup the graph
				$graph = new Graph(600,450);
				$graph->SetMargin($a,$b,$c,$d); 
				if ($sensor=="temperature")
					{
					$graph->SetScale('textlin',14,30);
					}
				else
					{
					$graph->SetScale('textlin');
					}
				if($sensor=="all")
					{
					$graph->SetScale('textlin',14,30);
					$graph->SetYScale(0,"lin"); 
					$graph->SetYScale(1,"lin"); 
					$graph->SetYScale(2,"lin"); 
					}
				$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 ($sensor =="temperature")
				{
				// Create the temperature line
				$p1 = new LinePlot($data_temperature);
				$graph->Add($p1);
				$p1->SetColor("red");
				$p1->SetLegend('Temperature');
				$p1->SetWeight(30);
				$graph->title->Set($title);
				$graph->yaxis->title->Set("Celsious");
				}
			else if ($sensor =="humidity")
				{
				// Create the humidity line
				$p1 = new LinePlot($data_humidity);
				$graph->Add($p1);
				$p1->SetColor("blue");
				$p1->SetLegend('Humidity');
				$graph->title->Set($title);
				$graph->yaxis->title->Set("%");
				}
			else if ($sensor =="indoorlighting")
				{
				// Create the indoorligthing line
				$p1 = new LinePlot($data_indoorlighting);
				$graph->Add($p1);
				$p1->SetColor("#DAA520");
				$p1->SetLegend('Indoor Lighting');
				$graph->title->Set($title);
				$graph->yaxis->title->Set("Brightness");
				}
			else if ($sensor =="soilmoisture")
				{
				// Create the soilmoisture line
				$p1 = new LinePlot($data_soilmoisture);
				$graph->Add($p1);
				$p1->SetColor("brown");
				$p1->SetLegend('Soil Moisture');
				$graph->title->Set($title);
				$graph->yaxis->title->Set("Dryness");
				}
			else
				{
				// Create the temperature line
				$p1 = new LinePlot($data_temperature);
				$graph->Add($p1);
				$p1->SetColor("red");				
				$graph->yaxis->SetColor('red'); 
				$graph->yaxis->title->Set("Celsious");
				$p1->SetLegend('Temperature');
				
				// Create the humidity line
				$p2 = new LinePlot($data_humidity);
				$graph->AddY(0,$p2);
				$p2->SetColor("blue");
				$graph->ynaxis[0]->SetColor('blue'); 
				$graph->ynaxis[0]->title->Set("%");
				$p2->SetLegend('Humidity');
				
				// Create the indoorligthing line
				$p3 = new LinePlot($data_indoorlighting);
				$graph->AddY(1,$p3);
				$p3->SetColor("#DAA520");				
				$graph->ynaxis[1]->SetColor('#DAA520'); 
				$graph->ynaxis[1]->title->Set("Brightness");
				$p3->SetLegend('Indoor Lighting'); 
				
				// Create the soilmoisture line
				$p4 = new LinePlot($data_soilmoisture);
				$graph->AddY(2,$p4);
				$p4->SetColor("brown");
				$graph->ynaxis[2]->SetColor('brown'); 
				$graph->ynaxis[2]->title->Set("Dryness");
				$p4->SetLegend('Soil Moisture');
				
				//end of lines
				$graph->title->Set($title);
				}

				$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', '1')";
						@ $result=mysql_query($sql);
						
						if ($result!=0)
							{
							echo"<img src='$image' id='sensor_image' alt='graph' class='chart_graph' title='$title' onclick='bigger(1);' 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();
?>

