<?php
//ini_set( 'post_max_size', '50M' );
//ini_set( 'upload_max_filesize', '50M' );
class classImages
{
	private $imageID = NULL;
	private $imageName = NULL;
	private $imageType = NULL;
	private $imageSize = NULL;
	private $patientAutoID = NULL;
	private $visitID = NULL;
	private $diagItemID=NULL;
	
	function setDiagItemID($diagItemID)
	{
		if($diagItemID!='')
		{
			$this->diagItemID = $diagItemID;
		}
		else
		{
			throw new MyException(107112);
		}
	}
	function getDiagItemID()
	{
		if($this->diagItemID!='')
		{
			return $this->diagItemID;
		}
		else
		{
			throw new MyException(107113);
		}
	}
	
	function setPatientAutoID($patientAutoID)
	{
		if($patientAutoID!="")
		{
			$this->patientAutoID = $patientAutoID;
		}
		else
		{
			throw new MyException(107100);
		}
	}
	function getPatientAutoID()
	{
		if($this->patientAutoID!="")
		{
			return $this->patientAutoID;
		}
		else
		{
			throw new MyException(107105);
		}
	}
	
	function setVisitID($visitID)
	{
		if($visitID!="")
		{
			$this->visitID = $visitID;
		}
		else
		{
			throw new MyException(107114);
		}
	}
	function getVisitID()
	{
		if($this->visitID!="")
		{
			return $this->visitID;
		}
		else
		{
			throw new MyException(107115);
		}
	}
	
	
	function setImageName($imageName)
	{
		if($imageName!="")
		{
			$this->imageName = $imageName;
		}
		else
		{
			throw new MyException(107101);
		}
		
	}
	function getImageName()
	{
		if($this->imageName!='')
		{
			return $this->imageName;
		}
		else
		{
			throw new MyException(107106);
		}
		
	}
	
	
	function setImageID($imageID)
	{
		if($imageID!='')
		{
			$this->imageID = $imageID;
		}
		else
		{
			throw new MyException(107102);
		}
	}
	function getImageID()
	{
		if($this->imageID!='')
		{
			return $this->imageID;
		}
		else
		{
			throw new MyException(107107);
		}
	}
	
	
	function setImageType($imageType)
	{
		if($imageType!='')
		{
			if($this->checkImageType($imageType))
			{
				$this->imageType = $imageType;
			}
		}
		else
		{
			throw new MyException(107103);
		}
	}
	function getImageType()
	{
		if($this->imageType!='')
		{
			return $this->imageType;
		}
		else
		{
			throw new MyException(107108);
		}
	}
	
	
	function setImageSize($imageSize)
	{
		if($imageSize!='')
		{
			if($this->checkImageSize($imageSize))
			{
				$this->imageSize = $imageSize;
			}
		}
		else
		{
			throw new MyException(107104);
		}
	}
	function getImageSize()
	{
		if($this->imageSize!='')
		{
			return $this->imageSize;
		}
		else
		{
			throw new MyException(107109);
		}
	}
	
	######################### MAIN CHECK FUNCTION ###########################################################
	
	function checkImageType($imageType)
	{
		
		if($imageType == 'image/jpeg' || $imageType == 'image/pjpeg' || $imageType == 'image/png' || $imageType == 'image/gif')
		{
			return 1;
		}
		else
		{
			throw new MyException(107110);
		}
	}
	
	function checkImageSize($imageSize)
	{
		if(number_format($imageSize / 1048576,2)<=4)
		{
			return 1;
		}
		else
		{
			throw new MyException(107111);
		}
	}
	
	/*function makeThumbnail($filename,$name)
	{
		$percent = 0.3;
		
		// Content type
		//header('Content-Type: image/jpeg');
		
		// Get new sizes
		list($width, $height) = getimagesize($filename);
		$newwidth = $width * $percent;
		$newheight = $height * $percent;
		
		// Load
		$thumb = imagecreatetruecolor($newwidth, $newheight);
		$source = imagecreatefromjpeg($filename);
		
		// Resize
		imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		
		// Output
		imagejpeg($thumb,'../images/patient_images/'.$name.'.jpg');
	}*/
	
	######################### MAIN INSERT FUNCTION ###########################################################
	
	function execute()
	{
		
		$image_name = $this->getImageName();
		$image_type = $this->getImageType();
		$image_size = $this->getImageSize();
		$time_stamp = time();
	
		$query = "INSERT  INTO images 
		(
			image_name,
			image_type,
			image_size,
			time_stamp
		) 
		VALUES
		(
			'$image_name',
			'$image_type',
			'$image_size',
			'$time_stamp'
		)"; 
		$queryOb = new classQueryGlobal;
		$this->imageID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
	}
	
	function executeForPatient()
	{
		
		$image_name = $this->getImageName();
		$image_type = $this->getImageType();
		$image_size = $this->getImageSize();
		$time_stamp = time();
		$patient_auto_id = $this->getPatientAutoID();
	
		$query = "INSERT  INTO images 
		(
			image_name,
			image_type,
			image_size,
			time_stamp,
			patient_auto_id
		) 
		VALUES
		(
			'$image_name',
			'$image_type',
			'$image_size',
			'$time_stamp',
			'$patient_auto_id'
		)"; 
		$queryOb = new classQueryGlobal;
		$this->imageID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
	}
	
	
	function executeForVisitor()
	{
		
		$image_name = $this->getImageName();
		$image_type = $this->getImageType();
		$image_size = $this->getImageSize();
		$time_stamp = time();
		$visit_id = $this->getVisitID();
	
		$query = "INSERT  INTO images 
		(
			image_name,
			image_type,
			image_size,
			time_stamp,
			visit_id
		) 
		VALUES
		(
			'$image_name',
			'$image_type',
			'$image_size',
			'$time_stamp',
			'$visit_id'
		)"; 
		$queryOb = new classQueryGlobal;
		$this->imageID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
	}
	
	######################### MAIN UPDATE FUNCTION ###########################################################
	
	function updatePatientAutoID()
	{
		$patient_auto_id = $this->getPatientAutoID();
		$image_id = $this->getImageID();
		
		$sql = " UPDATE images SET patient_auto_id = '$patient_auto_id' WHERE image_id = '$image_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($sql,'update',$server="",'off');
	}
	
	function updateDiagItemID()
	{
		$diagitems_id = $this->getDiagItemID();
		$image_id = $this->getImageID();
		
		$sql = " UPDATE images SET diagitems_id = '$diagitems_id' WHERE image_id = '$image_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($sql,'update',$server="",'off');
	}
	
	######################### MAIN DELETE FUNCTION ###########################################################
	
	function deleteImages()
	{
		$patient_auto_id = $this->getPatientAutoID();
		$image_id = $this->fetchImageOfPatient();
		
		if(file_exists('../images/patient_images/'.$image_id.'.jpg'))
		{
			chmod('../images/patient_images/' . $image_id . '.jpg',0777);
			unlink('../images/patient_images/' . $image_id . '.jpg'); 
		}
		
		$query = "DELETE FROM images WHERE patient_auto_id = '$patient_auto_id'"; 
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
			
			
	}
	
	function deleteImageByImageID()
	{
		$image_id = $this->getImageID();
		
		if(file_exists('../images/patient_images/'.$image_id.'.jpg'))
		{
			chmod('../images/patient_images/' . $image_id . '.jpg',0777);
			unlink('../images/patient_images/' . $image_id . '.jpg'); 
		}
		
		$query = "DELETE FROM images WHERE image_id = '$image_id'"; 
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
			
			
	}
	
	function deleteVisitorImageByImageID()
	{
		$image_id = $this->getImageID();
		
		if(file_exists('../images/visitor_images/'.$image_id.'.jpg'))
		{
			chmod('../images/visitor_images/' . $image_id . '.jpg',0777);
			unlink('../images/visitor_images/' . $image_id . '.jpg'); 
		}
		
		$query = "DELETE FROM images WHERE image_id = '$image_id'"; 
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
			
			
	}
	
	function deleteImagesOfDiagnosis()
	{
		$diagitems_id = $this->getDiagItemID();
		$image_id = $this->fetchImageOfDiagnosis();
		
		if(file_exists('../images/diagnosis_images/'.$image_id.'.jpg'))
		{
			chmod('../images/diagnosis_images/' . $image_id . '.jpg',0777);
			unlink('../images/diagnosis_images/' . $image_id . '.jpg'); 
		}
		
		$query = "DELETE FROM images WHERE diagitems_id = '$diagitems_id'"; 
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
			
			
	}
	
	######################### MAIN RETRIEVE FUNCTION ###########################################################
	
	function fetchImageOfPatient()
	{
		$patient_auto_id = $this->getPatientAutoID();
		$sql = " SELECT * FROM images WHERE patient_auto_id = '$patient_auto_id' ORDER BY image_id DESC";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($sql,'select',$server="",'off');
		if(isset($array[0]['image_id'])&&$array[0]['image_id']!='')
		{
			return $array[0]['image_id'];
		}
	}
	
	function fetchAllImageOfPatient()
	{
		$patient_auto_id = $this->getPatientAutoID();
		$sql = " SELECT * FROM images WHERE patient_auto_id = '$patient_auto_id' ORDER BY time_stamp DESC";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($sql,'select',$server="",'off');
		if(isset($array[0]['image_id'])&&$array[0]['image_id']!='')
		{
			return $array;
		}
	}
	
	function fetchAllImageOfVisitor()
	{
		$visit_id = $this->getVisitID();
		$sql = " SELECT * FROM images WHERE visit_id = '$visit_id' ORDER BY time_stamp DESC";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($sql,'select',$server="",'off');
		if(isset($array[0]['image_id'])&&$array[0]['image_id']!='')
		{
			return $array;
		}
	}
	
	function fetchImageOfDiagnosis()
	{
		$diagitems_id = $this->getDiagItemID();
		$sql = " SELECT * FROM images WHERE diagitems_id = '$diagitems_id'";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($sql,'select',$server="",'off');
		if(isset($array[0]['image_id'])&&$array[0]['image_id']!='')
		{
			return $array[0]['image_id'];
		}
	}
	
	function fetchImageByimageID()
	{
		$image_id = $this->getImageID();
		$sql = " SELECT * FROM images WHERE image_id = '$image_id'";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($sql,'select',$server="",'off');
		if(isset($array[0]['image_id'])&&$array[0]['image_id']!='')
		{
			return $array;
		}
	}
	
	
}

?>