<?php
$getname = $link->hrefquery ();
if ($getname [0] == 'id') {
	
	$getid = $getname [1];
	
	$query = $db->get_row ( 'employees', array ('e_id' => $getid ) );

}

if ($getname [0] == 'p_photograph') {
	$details = explode ( '___', $getname [1] );
	$e_id = $details ['0'];
	$filename = $details ['1'] . "___" . $details ['2'];
	
	$update = $db->update ( 'employees', array ('p_photograph' => '' ), array ('e_id' => $e_id ) );
	
	if ($update) {
		unlink ( SERVER_ROOT . '/uploads/employees/' . $e_id . "/" . $filename );
		$session->redirect ( 'edit_employeedetails?id=' . $e_id, Admin );
	}
}
if ($getname [0] == 'qualification_files') 

{
	$details = explode ( '___', $getname [1] );
	
	$e_id = $details [0];
	$filename = $details ['1'] . '___' . $details ['2'];
	
	$colname = $db->get_col ( 'employees', array ('e_id' => $e_id ), 'qualification_files' );
	
	$remove = str_replace ( $filename, '', $colname [0] );
	
	$update = $db->update ( 'employees', array ('qualification_files' => $remove ), array ('e_id' => $e_id ) );
	if ($update) {
		unlink ( SERVER_ROOT . '/uploads/employees/' . $e_id . '/' . $filename );
		$session->redirect ( 'edit_employeedetails?id=' . $e_id, Admin );
	}
}
if ($getname [0] == 'resume') {
	$details = explode ( '___', $getname [1] );
	
	$e_id = $details [0];
	$filename = $details [1] . '___' . $details [2];
	$update = $db->update ( 'employees', array ('resume' => '' ), array ('e_id' => $e_id ) );
	if ($update) {
		unlink ( SERVER_ROOT . '/uploads/employees/' . $e_id . '/' . $filename );
		$session->redirect ( 'edit_employeedetails?id=' . $e_id, Admin );
	}
}
if ($getname [0] == 'idproof_files') {
	
	$details = explode ( '___', $getname [1] );
	
	$e_id = $details [0];
	$filename = $details [1] . '___' . $details [2];
	$colname = $db->get_col ( 'employees', array ('e_id' => $e_id ), 'idproof_files' );
	
	$remove = str_replace ( $filename, '', $colname [0] );
	$update = $db->update ( 'employees', array ('idproof_files' => $remove ), array ('e_id' => $e_id ) );
	if ($update) {
		unlink ( SERVER_ROOT . '/uploads/employees/' . $e_id . '/' . $filename );
		$session->redirect ( 'edit_employeedetails?id=' . $e_id, Admin );
	}
}

?>
<?php

if (isset ( $_POST ['submit'] ) && $_POST ['check'] == 'employee' && $_POST ['action'] == 'simple' && $_POST ['action1'] == 'multiple') {
	
	$check = $_POST ['employee'];
	$e_code = $check ['e_code'];
	$department = $check ['department'];
	$e_name = $check ['e_name'];
	$f_name = $check ['f_name'];
	
	$dob = $check ['dob'];
	$address = $check ['address'];
	// $status= $check['status'];
	$highschool = $check ['highschool'];
	$intermediate = $check ['intermediate'];
	$graduation = $check ['graduation'];
	$p_graduation = $check ['p_graduation'];
	$extraas = $check ['extraas'];
	$mobile = $check ['mobile'];
	$telephone = $check ['telephone'];
	$idproof = $check ['idproof'];
	$j_date = $check ['j_date'];
	$b_name = $check ['b_name'];
	$a_number = $check ['a_number'];
	$branch = $check ['branch'];
	$city = $check ['city'];
	$ifsccode = $check ['ifsccode'];
	$salary = $check ['salary'];
	$remarks = $check ['remarks'];
	$appointment_date = $check ['appointment_date'];
	
	$qfiles_name = $_FILES ['my_field'] ['name'];
	$idfiles_name = $_FILES ['id_field'] ['name'];
	
	// retrieve eventual CLI parameters
	$cli = (isset ( $argc ) && $argc > 1);
	if ($cli) {
		if (isset ( $argv [1] ))
			$_GET ['file'] = $argv [1];
		if (isset ( $argv [2] ))
			$_GET ['dir'] = $argv [2];
		if (isset ( $argv [3] ))
			$_GET ['pics'] = $argv [3];
	}
	
	// set variables
	$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/employees/' . $_POST ['e_id']);
	$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
	
	$query = $db->get_row ( 'employees', array ('e_id' => $_POST ['e_id'] ) );
	if ($_FILES ['photo'] ['type'] != 'image/jpeg' && $_FILES ['photo'] ['name'] != '') {
		?>
<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! Your File Is Not JPG Or JPEG...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
	} else {
		
		if ($_FILES ['photo'] ['name'] != '') {
			
			$pfile = SERVER_ROOT . '/uploads/employees/' . $query ['e_id'] . '/' . $query ['p_photograph'];
			
			// we create an instance of the class, giving as argument the PHP
			// object
			// corresponding to the file field from the form
			// All the uploads are accessible from the PHP object $_FILES
			
			$name = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $_FILES ['photo'] ['name'] ) );
			$_FILES ['photo'] ['name'] = str_replace ( array ('(', ')' ), '', $name );
			$handle = new upload ( $_FILES ['photo'] );
			
			// then we check if the file has been uploaded properly
			// in its *temporary* location in the server (often, it is /tmp)
			if ($handle->uploaded) {
				
				// yes, the file is on the server
				// now, we start the upload 'process'. That is, to copy the
				// uploaded file
				// from its temporary location to the wanted location
				// It could be something like
				// $handle->Process('/home/www/my_uploads/');
				
				$handle->image_resize = true;
				$handle->image_ratio_crop = true;
				$handle->image_y = 150;
				$handle->image_x = 140;
				
				$handle->Process ( $dir_dest );
				
				// we check if everything went OK
				if ($handle->processed) {
					// everything was fine !
					$photovalue .= $_FILES ['photo'] ['name'];
					if (file_exists ( $pfile ) && $query ['p_photograph'] != '')
						unlink ( $pfile );
				
				}
			}
		
		}
		if ($_FILES ['resume_field'] ['name'] != '') {
			
			$rfile = SERVER_ROOT . '/uploads/employees/' . $query ['e_id'] . '/' . $query ['resume'];
			
			// we create an instance of the class, giving as argument the PHP
			// object
			// corresponding to the file field from the form
			// All the uploads are accessible from the PHP object $_FILES
			
			$name1 = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $_FILES ['resume_field'] ['name'] ) );
			$_FILES ['resume_field'] ['name'] = str_replace ( array ('(', ')' ), '', $name1 );
			$handle = new upload ( $_FILES ['resume_field'] );
			
			// then we check if the file has been uploaded properly
			// in its *temporary* location in the server (often, it is /tmp)
			if ($handle->uploaded) {
				
				// yes, the file is on the server
				// now, we start the upload 'process'. That is, to copy the
				// uploaded file
				// from its temporary location to the wanted location
				// It could be something like
				// $handle->Process('/home/www/my_uploads/');
				$handle->Process ( $dir_dest );
				
				// we check if everything went OK
				if ($handle->processed) {
					// everything was fine !
					$resume_field .= $_FILES ['resume_field'] ['name'];
					if (file_exists ( $rfile ) && $query ['resume'] != '')
						unlink ( $rfile );
				}
			}
		}
		
		if ($qfiles_name [0] != '' || $qfiles_name [1] != '' || $qfiles_name [2] != '' || $qfiles_name [3] != '' || $qfiles_name [4] != '') {
			
			$files = array ();
			
			foreach ( $_FILES ['my_field'] as $k => $l ) {
				foreach ( $l as $i => $v ) {
					
					if (! array_key_exists ( $i, $files ))
						$files [$i] = array ();
					
					$files [$i] [$k] = $v;
				}
			}
			
			// now we can loop through $files, and feed each element to the
			// class
			$count = 0;
			$oldfilename = explode ( ',', $query ['qualification_files'] );
			foreach ( $files as $file ) {
				// we instanciate the class for each element of $file
				if ($file ['name'] != '') {
					$qfile = SERVER_ROOT . '/uploads/employees/' . $_POST ['e_id'] . '/' . $oldfilename [$count];
					
					$remove = str_replace ( $oldfilename [$count], '', $oldfilename );
					if (file_exists ( $qfile ) && $oldfilename [$count] != '')
						unlink ( $qfile );
					
					$loop = $_POST ['e_id'] . "___" . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
					$file ['name'] = str_replace ( array ('(', ')' ), '', $loop );
					$savedname .= $file ['name'] . ',';
					$handle = new upload ( $file );
					
					// then we check if the file has been uploaded properly
					// in its *temporary* location in the server (often, it is
					// /tmp)
					
					if ($handle->uploaded) {
						
						// now, we start the upload 'process'. That is, to copy
						// the uploaded file
						// from its temporary location to the wanted location
						// It could be something like
						// $handle->Process('/home/www/my_uploads/');
						$handle->Process ( $dir_dest );
					}
				} else {
					$savedname .= $oldfilename [$count] . ',';
				}
				$count ++;
			
			}
		
		}
		
		if ($idfiles_name [0] != '' || $idfiles_name [1] != '' || $idfiles_name [2] != '') {
			$files = array ();
			
			foreach ( $_FILES ['id_field'] as $k => $l ) {
				foreach ( $l as $i => $v ) {
					
					if (! array_key_exists ( $i, $files ))
						$files [$i] = array ();
					
					$files [$i] [$k] = $v;
				}
			}
			
			$count = 0;
			$oldfilename = explode ( ',', $query ['idproof_files'] );
			
			$idfile = "";
			foreach ( $files as $file ) {
				// we instanciate the class for each element of $file
				if ($file ['name'] != '') {
					
					$qfile = SERVER_ROOT . '/uploads/employees/' . $_POST ['e_id'] . '/' . $oldfilename [$count];
					
					if (file_exists ( $qfile ) && $oldfilename [$count] != '') {
						unlink ( $qfile );
					}
					$loop1 = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
					$test1 = str_replace ( array ('(', ')' ), '', $loop1 );
					$idfile .= $test1 . ",";
					
					$handle = new upload ( $file );
					
					// then we check if the file has been uploaded properly
					// in its *temporary* location in the server (often, it is
					// /tmp)
					
					if ($handle->uploaded) {
						
						// now, we start the upload 'process'. That is, to copy
						// the uploaded file
						// from its temporary location to the wanted location
						// It could be something like
						// $handle->Process('/home/www/my_uploads/');
						$handle->Process ( $dir_dest );
					}
				} else {
					$idfile1 .= $oldfilename [$count] . ',';
				}
				$count ++;
			
			}
			
			$ridfile = $idfile . $idfile1;
		
		}
		if ($_FILES ['photo'] ['name'] === '') {
			$photovalue = $query ['p_photograph'];
		}
		if ($_FILES ['resume_field'] ['name'] === '') {
			$resume_field = $query ['resume'];
		}
		if ($qfiles_name [0] === '' && $qfiles_name [1] === '' && $qfiles_name [2] === '' && $qfiles_name [3] === '' && $qfiles_name [4] === '') {
			$savedname = $query ['qualification_files'];
		}
		if ($idfiles_name [0] === '' && $idfiles_name [1] === '' && $idfiles_name [2] === '') {
			$idfile = $query ['idproof_files'];
		}
		if ($check ['status'] == 'on') {
			$status = '0';
		} else {
			$status = '1';
		}
		
		$final = $db->update ( 'employees', array ('e_code' => $e_code, 'department' => $department, 'e_name' => $e_name, 'f_name' => $f_name, 'p_photograph' => $photovalue, 'dob' => $dob, 'address' => $address, 'status' => $status, 'highschool' => $highschool, 'intermediate' => $intermediate, 'graduation' => $graduation, 'p_graduation' => $p_graduation, 'extraas' => $extraas, 'qualification_files' => $savedname, 'resume' => $resume_field, 'mobile' => $mobile, 'telephone' => $telephone, 'idproof' => $idproof, 'idproof_files' => $ridfile, 'j_date' => $j_date, 'b_name' => $b_name, 'a_number' => $a_number, 'branch' => $branch, 'city' => $city, 'ifsccode' => $ifsccode, 'salary' => $salary, 'remarks' => $remarks, 'appointment_date' => $appointment_date ), array ('e_id' => $_POST ['e_id'] ) );
		
		if ($final) {
			?>
<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
		
		} 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 Is Some Problem In Updated..</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
		
		}
	
	}
}

?>