<?php

if (isset ( $_POST ['submit'] ) && $_POST ['check'] == 'admin') {
	$getvalue = $_POST ['admin'];
	
	$name = $getvalue ['name'];
	$email = $_POST ['email'];
	$contact = $getvalue ['contact'];
	$address = $getvalue ['address'];
	if ($_FILES ['photo'] ['name'] != '') {
		$empcheck = $db->exists ( "employees", array ("email" => $uemail ) );
		$empid = $db->get_col ( "employees", array ('email' => $uemail ), 'e_id' );
		
		$var = 0;
		$randnum = mt_rand ( 15, 250 );
		
		$_FILES ['photo'] ['name'] = $randnum . '___' . preg_replace ( '/\s+/', '', $_FILES ['photo'] ['name'] );
		$_FILES ['photo'] ['name'] = str_replace ( array ('(', ')' ), '', $_FILES ['photo'] ['name'] );
		
		if ($empcheck) {
			echo 'if';
			$handle = new upload ( $_FILES ['photo'] );
			
			$pic = $db->get_col ( 'employees', array ('email' => $email ), 'p_photograph' );
			
			$path = SERVER_ROOT . '/uploads/employees/' . $empid [0] . '/' . $pic [0];
			if ($pic [0] != '' && file_exists ( $path )) {
				unlink ( $path );
			}
			if ($handle->uploaded) {
				
				$handle->image_resize = true;
				$handle->image_ratio_crop = true;
				$handle->image_y = 100;
				$handle->image_x = 100;
				
				$handle->Process ( 'uploads/employees/' . $empid [0] );
				
				// we check if everything went OK
				if ($handle->processed) {
					// everything was fine !
					$source = SERVER_ROOT . '/uploads/employees/' . $empid [0] . '/' . $_FILES ['photo'] ['name'];
					$dest = SERVER_ROOT . '/uploads/users/' . $_FILES ['photo'] ['name'];
					
					$copy = copy ( $source, $dest );
					$photovalue1 = $_FILES ['photo'] ['name'];
					if ($copy) {
						$query = $db->update ( 'users', array ('name' => $name, 'email' => $email, 'contact' => $contact, 'address' => $address, 'photo' => $photovalue1 ), array ('email' => $uemail ) );
						
						$query1 = $db->update ( "employees", array ('e_name' => $name, 'email' => $email, 'mobile' => $contact, 'address' => $address, 'p_photograph' => $photovalue1 ), array ('email' => $uemail ) );
					}
				}
			}
		} else {
			$handle = new upload ( $_FILES ['photo'] );
			
			$pic = $db->get_col ( 'users', array ('email' => $email ), 'photo' );
			
			$path = SERVER_ROOT . '/uploads/users/' . $pic [0];
			if ($pic [0] != '' && file_exists ( $path )) {
				unlink ( $path );
			}
			if ($handle->uploaded) {
				
				$handle->image_resize = true;
				$handle->image_ratio_crop = true;
				$handle->image_y = 100;
				$handle->image_x = 100;
				
				$handle->Process ( 'uploads/users' );
				
				// we check if everything went OK
				if ($handle->processed) {
					// everything was fine !
					$photovalue1 = $_FILES ['photo'] ['name'];
					
					$query2 = $db->update ( 'users', array ('name' => $name, 'contact' => $contact, 'address' => $address, 'photo' => $photovalue1 ), array ('email' => $uemail ) );
					// $db->debug();
				
				}
			}
		}
	} else {
		$query3 = $db->update ( 'users', array ('name' => $name, 'email' => $email, 'contact' => $contact, 'address' => $address ), array ('email' => $uemail ) );
		
		$query4 = $db->update ( "employees", array ('e_name' => $name, 'email' => $email, 'mobile' => $contact, 'address' => $address ), array ('email' => $uemail ) );
	}
	if ($query || $query1 || $query2 || $query3 || $query4) {
		?>
<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Updated...</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
		// $session->redirect('profile', Admin);
	} else {
		?>
<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Are Some Problem In Updated...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
	}
}
?>