<?php

class validation {
	
	private $validate = array();

	// public $alpha_space_pattern = "/^[a-zA-Z\s]+$/";
	// public $alpha_pattern = "/^[a-zA-Z]+$/";
	// public $alphanum_space_pattern = "/^[a-zA-Z0-9]+$/";
	// public $alphanum_pattern = "/^[a-zA-Z0-9\s]+$/";
	// public $num_pattern = "/^[0-9]+$/";
	
	function user_validate($role_id, $dealer_id, $batch_id, $user_first_name, $user_last_name, $user_mobile, $user_email, $gender, $employee_code, $room_no){

		$employee_code_pattern = "/^[a-zA-Z-\s]+$/";
		if($role_id == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please select role";
		}
		else if($dealer_id == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please select dealer";
		}
		else if($batch_id == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please select batch";
		}
		else if($user_first_name == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please enter first name";
		}
		else if($user_last_name == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please enter last name";
		}
		else if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){
			$validate['n'] = 0;
			$validate['msg'] = "Please enter a valid Email Id";
		}
		else if(!is_numeric($user_mobile) || strlen($user_mobile) != 10){
			$validate['n'] = 0;
			$validate['msg'] = "Please enter a valid 10 digit mobile number";
		}
		else if($gender == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please select gender";
		}
		else if($employee_code == ""){
			$validate['n'] = 0;
			$validate['msg'] = "Please enter employee code";
		}
		else if($employee_code != "" && preg_match($employee_code_pattern, $employee_code)){
			$validate['n'] = 0;
			$validate['msg'] = "Invalid employee code";
		}
		else if($room_no != "" && preg_match($employee_code_pattern, $room_no)){
			$validate['n'] = 0;
			$validate['msg'] = "Alphabets, Numbers, spaces and hypen is only allowed";
		}
		else{
			$validate['n'] = 1;
			$validate['msg'] = "user validated";
		}
		return $validate;
	}
	
	function validate_image2($image, $type){
		if($type == "FILES"){
			$allowed_file_types = array("image/jpeg","image/jpg","image/png");
			$file_name = $image['name'];
			$file_type = $image['type'];
			$file_error = $image['error'];
			$file_size = $image['size'];
			$file_image = $image['tmp_name'];
			list($width, $height, $image_type) = getimagesize($file_image);
			if( in_array($file_type,$allowed_file_types) && $file_error == 0 && $file_name != "" && is_numeric($width) && is_numeric($height) && ($image_type === 2 || $image_type === 3 ) ){
				if($file_size < 2000000){
					$validate['n'] = 1;
					$validate['msg'] = "Valid File";
					// if($height >= 360 && $width/$height == 2){
					// }
					// else{
					// 	$validate["n"] = 0;
					// 	$validate["msg"] = "Image height should be at least 360px and the height to width ratio should be 1:2";
					// }
				}
				else{
					$validate["n"] = 0;
					$validate["msg"] = "File size is greater than 2 MB";
				}
			}
			else{
				$validate["n"] = 0;
				$validate["msg"] = "Invalid File";
			}
		}
		else if($type == "STRING"){

			// $validate['info'] = $image;
			// list($type, $image) = explode(';', $image);
			// list(, $file_enoceded) = explode(',', $image);

			$file_decoded = base64_decode($image);
			// $file_decoded = base64_decode($file_enoceded);
			$info = getimagesizefromstring($file_decoded);
			if($info == false || !is_array($info)){
				$validate["n"] = 0;
				$validate["msg"] = "Invalid File1";
			}
			else{
				if(is_numeric($info[0]) && is_numeric($info[1]) && ($info[2] == 2 || $info[2] == 3 )){
				// 		if($info[1] >= 360 && $info[0]/$info[1] == 2){
					$validate['n'] = 1;
					$validate['msg'] = "Valid File";
					
					if($info[2] == 2){
						$validate['file_type'] = ".jpeg";
					}
					else{
						$validate['file_type'] = ".png";
					}
				// 		}
				// 		else{
				// 			$validate["n"] = 0;
				// 			$validate["msg"] = "Image height should be at least 360px and the height to width ratio should be 1:2";
				// 		}
				}
				else{
					$validate["n"] = 0;
					$validate["msg"] = "Invalid File";
				}
			}
		}
		else{
			$validate["n"] = 0;
			$validate["msg"] = "Invalid Type";
		}
		return $validate;
	}

	function validate_image($image, $type){
		if($type == "FILES"){
			$allowed_file_types = array("image/jpeg","image/jpg","image/png");
			$file_name = $image['name'][0];
			$file_type = $image['type'][0];
			$file_error = $image['error'][0];
			$file_size = $image['size'][0];
			$file_image = $image['tmp_name'][0];
			list($width, $height, $image_type) = getimagesize($file_image);
			if( in_array($file_type,$allowed_file_types) && $file_error == 0 && $file_name != "" && is_numeric($width) && is_numeric($height) && ($image_type === 2 || $image_type === 3 ) ){
				if($file_size < 2000000){
					if($height >= 450 && $width >= 880){
						$validate['n'] = 1;
						$validate['msg'] = "Valid File";
					}
					else{
						$validate["n"] = 0;
						$validate["msg"] = "Image width should be at least 880px and height should be at least 450px.";
					}
				}
				else{
					$validate["n"] = 0;
					$validate["msg"] = "File size is greater than 2 MB";
				}
			}
			else{
				$validate["n"] = 0;
				$validate["msg"] = "Invalid File";
			}
		}
		else if($type == "MEDIA"){
			$allowed_file_types = array("image/jpeg","image/jpg","image/png");
			$file_name = $image['name'][0];
			$file_type = $image['type'][0];
			$file_error = $image['error'][0];
			$file_size = $image['size'][0];
			$file_image = $image['tmp_name'][0];
			list($width, $height, $image_type) = getimagesize($file_image);
			if( in_array($file_type,$allowed_file_types) && $file_error == 0 && $file_name != "" && is_numeric($width) && is_numeric($height) && ($image_type === 2 || $image_type === 3 ) ){
				//if($file_size < 2000000){

					//if($height == 333 && $width == 448){
						$validate['n'] = 1;
						$validate['msg'] = "Valid File";
				// 	}
				// 	else{
				// 		$validate["n"] = 0;
				// 		$validate["msg"] = "Image width should be 448px and height should be 333px.";
				// 	}
				// }
				// else{
				// 	$validate["n"] = 0;
				// 	$validate["msg"] = "File size is greater than 2 MB";
				// }
			}
			else{
				$validate["n"] = 0;
				$validate["msg"] = "Invalid File";
			}
		}
		
		else if($type == "EVENT"){
			$allowed_file_types = array("image/jpeg","image/jpg","image/png");
			$file_name = $image['name'][0];
			$file_type = $image['type'][0];
			$file_error = $image['error'][0];
			$file_size = $image['size'][0];
			$file_image = $image['tmp_name'][0];
			list($width, $height, $image_type) = getimagesize($file_image);
			if( in_array($file_type,$allowed_file_types) && $file_error == 0 && $file_name != "" && is_numeric($width) && is_numeric($height) && ($image_type === 2 || $image_type === 3 ) ){
				if($file_size < 2000000){

					//if($height == 333 && $width == 448){
						$validate['n'] = 1;
						$validate['msg'] = "Valid File";
					// }
					// else{
					// 	$validate["n"] = 0;
					// 	$validate["msg"] = "Image width should be 448px and height should be 333px.";
					// }
				}
				else{
					$validate["n"] = 0;
					$validate["msg"] = "File size is greater than 2 MB";
				}
			}
			else{
				$validate["n"] = 0;
				$validate["msg"] = "Invalid File";
			}
		}
		else if($type == "STRING"){

				// $validate['info'] = $image;
				// list($type, $image) = explode(';', $image);
				// list(, $file_enoceded)      = explode(',', $image);

			$file_decoded = base64_decode($image);
			$info = getimagesizefromstring($file_decoded);
			if($info == false || !is_array($info)){
				$validate["n"] = 0;
				$validate["msg"] = "Invalid File";
			}
			else{
				if(is_numeric($info[0]) && is_numeric($info[1]) && ($info[2] == 2 || $info[2] == 3 )){
						// if($info[1] >= 360 && $info[0]/$info[1] == 2){
					$validate['n'] = 1;
					$validate['msg'] = "Valid File";
					if($info[2] == 2){
						$validate['file_type'] = ".jpeg";
					}
					else{
						$validate['file_type'] = ".png";
					}
						// }
						// else{
						// 	$validate["n"] = 0;
						// 	$validate["msg"] = "Image height should be at least 360px and the height to width ratio should be 1:2";
						// }
				}
				else{
					$validate["n"] = 0;
					$validate["msg"] = "Invalid File";
				}
			}
		}
		else{
			$validate["n"] = 0;
			$validate["msg"] = "Invalid Type";
		}
		return $validate;
	}
	
	
	function validate_category($category_name){
		$alpha_space_pattern = "/^[a-zA-Z\s]+$/";
		// $numOnly_pattern = "/^[0-9]+$/";
		// $alpha_num_pattern = "/^[A-Za-z0-9]+$/";
		if ($category_name == '') {
			$validate['n'] = 0;
			$validate['type'] = "category_name";
			$validate['msg'] = "Category name cannot be blank";
		}
		else if($category_name != "" && !preg_match($alpha_space_pattern, $category_name)){
			$validate['n'] = 0;
			$validate['type'] = "category_name";
			$validate['msg'] = "Please enter valid name";
		}
		else{
			$validate['n'] = 1;
			$validate['msg'] = "category_name validated";
		}
		return $validate;
	}

	function validate_album($category_id, $album_name)
	{
		$alpha_num_spaceHypen_pattern = "/^[a-zA-Z0-9-\s]+$/";
		if($category_id == "" || $category_id == "0"){
			$validate['n'] = 0;
			$validate['type'] = "categoryName";
			$validate['msg'] = "Please select Category";
		}
		else if($album_name == ""){
			$validate['n'] = 0;
			$validate['type'] = "album_name";
			$validate['msg'] = "Please enter album name";
		}
		else if($album_name != "" && !preg_match($alpha_num_spaceHypen_pattern, $album_name)){
			$validate['n'] = 0;
			$validate['type'] = "album_name";
			$validate['msg'] = "Please enter valid album name";
		}
		else{
			$validate['n'] = 1;
			$validate['msg'] = "user validated";
		}
		return $validate;
	}
}

$obj_validate = new validation();

?>