<?php



$errors = array();
$info = array();
$info_success = array();



//check the id passed
if(isset($_POST['videos']))
{
	$id =  $_POST['videos'];
}


//check if action is save pictures




//validate the title
if(!empty($_POST['title']))
{
   $title = mysqli_real_escape_string($dbc, $_POST['title']);
   
   $_SESSION['title'] = $title;
 
}
else
{
   $errors[] = '<p> * </p>';
}



//validate the link
if(!empty($_POST['link']))
{
   $link = mysqli_real_escape_string($dbc, $_POST['link']);
   
   $_SESSION['link'] = $title;
 
}
else
{
   $errors[] = '<p> * </p>';
}



//check image
//check for image using my own custom script
if (array_key_exists('upload', $_POST))
{
	//define the path
	define('UPLOAD_DIRECTORY', 'uploads/pictures/video_pictures/');
	
	//getting my caption now 
	//replace any space with an underscore 
	$file = $_FILES['image']['name'];
	$image_r = str_replace(' ', '_',$file);
	
	
	//convert the maximum size to kilobyte
	$maximum_size = number_format(MAX_FILE_SIZE/1024 ,1 ).'KB';
	
	//create an array of permitted files
	$permitted_images_type = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
	
	//begin by assume type is not ok
	$type_ok = false;
	
	//begin by assuming the filesize aint aceptable
	$size_ok = false;
	
	//check if he filoe is of permitted type
	foreach($permitted_images_type as $type)
	{
		if ($type == $_FILES['image']['type'] )
		{
			$type_ok = true;
			
			break;
		}
		
	}
	
	//check file if is within the permitted size
	if (($_FILES['image']['size']) > 0  && ($_FILES['image']['size'] <= MAX_FILE_SIZE))
	{
		$size_ok = true;
	}
	
	
	if($size_ok and $type_ok)
	{
		switch($_FILES['image']['error'])
		{
			case 0: 
			{
			
				//move the file to the upload folder and rename it
				$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIRECTORY.$image_r);
				
				if($success)
				{
					$image = $image_r;
					$info['image_result_message'] = "$file uploaded successfully";
				}
				else
				{
					$errors['image_result_message'] = "Error uploading $file";
				}
			break;	
			}
			
			case 3:
			{
				$errors['image_result_message'] = "Error Uploading $file. Please try again";
			}
			default:
			{
				$errors['image_result_message'] = "System error uploading $file. Contact Webmaster.";
			}
			
			
		}//end of switch
	}//end of size ok
	else if($_FILES['image']['error'] == 4)
	{
		//collect the former name of the image
			$query = "SELECT image FROM videos WHERE '$id' = id";
			$result = mysqli_query($dbc,$query);
			
			if(mysqli_num_rows($result) > 0)
			{
				while($row_i = mysqli_fetch_array($result, MYSQLI_ASSOC))
				{
					$image = $row_i['image']; 
				}
				
			}
			//end of checking if file exist 
	}
	
	else
	{
		$errors['image_result_message'] = "$file cannot be uploaded. Maximum size: $maximum_size. Types: 
		gif, jpg, 	png.";
	}
	
									

}//end of if upload



//collect the description
$description = (!empty($_POST['description']))? (mysqli_real_escape_string($dbc,$_POST['description'])): 
 NULL;
$_SESSION['description'] = $description;


//collect the status
$status = (!empty($_POST['status']))? (mysqli_real_escape_string($dbc,$_POST['status'])): 
 NULL;
$_SESSION['status'] = $status;





//finaly get security id
$user_id = get_user_id();


$security_id = get_security_id($user_id);



 
 
 
if(empty($errors))
{
	//destroy session if there is no error
	unset($_SESSION['title']) ;
	unset($_SESSION['link']);
	unset($_SESSION['description']);
	unset($_SESSION['status']);
	
	
	 //check if the query is for adding den add
	
	
		$q = "UPDATE `videos` SET `title`='$title', `youtube_link`='$link', `image`='$image', `description`='$description',`date_modified`=NOW(),
		     `status`='$status', `modified_by` = '$security_id'  WHERE id = '$id'";
										
							
		$r = mysqli_query($dbc,$q);
	
	if($r)
	{
		$info_success['success'] = 'Updated Successfully!';
		header("Location: gateway.php?act=view_videos&is=".urlencode($info_success['success']).""); //is - info success
		
	}
	else
	{
		$errors['error'] =  'Video not updated due to a system error. We apologize for any inconvenience.';
										
		header("Location: gateway.php?act=view_videos&error=".urlencode($errors['error'])."");
	}
	
}
else
{//stay back in video


	$errors['error'] =  'Video not updated, Please re-select and Fill the form appropriately, take note of <span class="ast">*</span>.';
										
	header("Location: gateway.php?act=view_videos&error=".urlencode($errors['error'])."");

}



?>