<?php
if (isset ( $_POST ['submit'] ) && $_POST ['check'] == 'employee' && $_POST ['action'] == 'simple' && $_POST ['action1'] == 'multiple') {
	
	$check = $_POST ['employee'];
	
	$empmail = $check ['empmail'];
	$password1 = $check ['password'];
	$retype = $check ['retype'];
	$emailcheck = $db->exists ( "users", array ("email" => $empmail ) );
	if ($emailcheck) {
		?>
<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! Please Change Your Email..</h5>

				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
	} else {
		if ($password1 != $retype) {
			?>
<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! Please Passwords And Re-type Password Is Not Equal..</h5>

				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
		
		} else {
			
			$e_code = $check ['e_code'];
			$department = $check ['department'];
			$e_name = $check ['e_name'];
			$f_name = $check ['f_name'];
			$dob = $check ['dob'];
			$address = $check ['address'];
			$pass = $password->stringbreak ( $password1 );
			
			$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'];
			$idproof1 = $check ['idproof1'];
			$idproof2 = $check ['idproof2'];
			if ($check ['j_date'] != '') {
				$get = explode ( "-", $check ['j_date'] );
				
				$j_date = $get [2] . '-' . $get [1] . '-' . $get [0];
			}
			
			$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'];
			
			$degree = $_FILES ['my_field'] ['name'];
			
			$idfield = $_FILES ['id_field'] ['name'];
			
			$declaration = $_FILES ['my_field1'] ['name'];
			
			/*
			 * $paymentdetails = $check ['paymentdetails']; $newpayments =
			 * $check ['newpayments'];
			 */
			$attendence = $check ['attendence'];
			$employeeattendence = $check ['employeeattendence'];
			$mostawarded = $check ['employee_awards'];
			$earlierawards = $check ['earlier_awards'];
			$giveanawards = $check ['give_away'];
			$employeesdetails = $check ['employee_details'];
			$newemployees = $check ['new_employee'];
			/*
			 * $clientdetails = $check ['clientdetails']; $newclient = $check
			 * ['newclient'];
			 */
			$departments = $check ['departments'];
			$newdepartments = $check ['new_departments'];
			/*
			 * $attendence = $check ['attendence'];
			 */
			$singaporetable = $check ['singaporetable'];
			$payslip = $check ['payslip'];
			$incometax_slab = $check ['incometax_slab'];
			/*
			 * $changepasswords = $check ['changepasswords'];
			 */
			
			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 {
				
				$userid = $db->get_col ( 'users', array ('email' => $uemail ), 'id' );
				$insert = $db->insert ( 'employees', array ('e_code' => $e_code, 'department' => $department, 'e_name' => $e_name, 'f_name' => $f_name, 'user_id' => $userid [0] ) );
				
				$lstid = $db->insert_id;
				
				// 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
				
				if ($_FILES ['photo'] ['name'] != '') {
					// 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
					$var = 0;
					for($var = 0; $var < 2; $var ++) {
						if ($var == 0) {
							$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/employees/' . $lstid);
							$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
							
							$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/');
								
								// we now process the image a second time, with
								// some other
								// settings
								
								$handle->image_resize = true;
								$handle->image_ratio_crop = true;
								$handle->image_y = 200;
								$handle->image_x = 250;
								
								$handle->Process ( $dir_dest );
								
								// we check if everything went OK
								if ($handle->processed) {
									// everything was fine !
									$photovalue = $_FILES ['photo'] ['name'];
								}
							}
						} else {
							$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/users');
							$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
							
							$name = 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/');
								
								// we now process the image a second time, with
								// some other
								// settings
								
								$handle->image_resize = true;
								$handle->image_ratio_crop = true;
								$handle->image_y = 200;
								$handle->image_x = 250;
								
								$handle->Process ( $dir_dest );
								
								// we check if everything went OK
								if ($handle->processed) {
									// everything was fine !
									// $photovalue = $_FILES ['photo'] ['name'];
								}
							}
						}
					}
				}
				$savedname = '';
				// set variables
				$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/employees/' . $lstid);
				$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
				if ($degree [0] != '' || $degree [1] != '' || $degree [2] != '' || $degree [3] != '' || $degree [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
					foreach ( $files as $file ) {
						// we instanciate the class for each element of $file
						$name1 = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
						$file ['name'] = str_replace ( array ('(', ')' ), '', $name1 );
						$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 );
						}
					
					}
				
				}
				if ($declaration [0] || $declaration [1]) {
					// set variables
					$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/employees/' . $lstid);
					$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
					
					$files = array ();
					
					foreach ( $_FILES ['my_field1'] 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
					$declaration = '';
					foreach ( $files as $file ) {
						// we instanciate the class for each element of $file
						$name10 = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
						$file ['name'] = str_replace ( array ('(', ')' ), '', $name10 );
						$declaration .= $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 );
						}
					
					}
				}
				
				if ($_FILES ['resume_field'] ['name'] != '') {
					$name2 = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $_FILES ['resume_field'] ['name'] ) );
					$_FILES ['resume_field'] ['name'] = str_replace ( array ('(', ')' ), '', $name2 );
					$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 !
							$resumename = $_FILES ['resume_field'] ['name'];
						}
					}
				}
				if ($idfield [0] != '' || $idfield [1] != '' || $idfield [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;
						}
					}
					
					// now we can loop through $files, and feed each element to
					// the
					// class
					foreach ( $files as $file ) {
						
						// we instanciate the class for each element of $file
						$name3 = mt_rand ( 15, 250 ) . '___' . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
						$file ['name'] = str_replace ( array ('(', ')' ), '', $name3 );
						if ($file ['name'] != '') {
							$idfilesname .= $file ['name'] . ',';
						} else {
							$idfilesname .= ",";
						}
						
						$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 );
						
						}
					
					}
				
				}
				
				if ($idfilesname == '') {
					$idfilesname = ",,,";
				}
				if ($savedname == '') {
					$savedname = ",,,,,";
				}
				
				$query = $db->update ( 'employees', 
						array ( 'e_code' => $e_code, 
								'department' => $department, 
								'e_name' => $e_name, 
								'f_name' => $f_name, 
								'p_photograph' => $photovalue, 
								'email' => $empmail, 
								'password' => $pass, 
								'dob' => $dob, 
								'address' => $address, 
								'highschool' => $highschool, 
								'intermediate' => $intermediate, 
								'graduation' => $graduation,
						 		'p_graduation' => $p_graduation, 
								'extraas' => $extraas, 
								'qualification_files' => $savedname,
						 		'resume' => $resumename, 
								'mobile' => $mobile, 
								'telephone' => $telephone, 
								'idproof' => $idproof, 
								'idproof1' => $idproof1, 
								'idproof2' => $idproof2, 
								'idproof_files' => $idfilesname, 
								'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, 
								'declaration' => $declaration, 
								'user_id' => $userid [0] 
								), array ('e_id' => $lstid ) );
				
				if($photovalue==NULL)
				$userinsert = $db->insert ( "users", array ("name" => $e_name, 
						"contact" => $mobile, 
						"address" => $address, 
						'email' => $empmail, 
						'password' => $pass, 
						"suspen_id" => "0", 
						'type' => '1', 
						'privilege' => $mostawarded . ',' 
						. $earlierawards . ',' 
						. $giveanawards . ',' 
						. $employeesdetails . ',' 
						. $newemployees . ',' 
						. $departments . ',' 
						. $newdepartments . ',' 
						. $singaporetable . ',' 
						. $payslip . ',' 
						. incometax_slab . ',' 
						. $attendence . ',' 
						. $employeeattendence ) );
else
				$userinsert = $db->insert ( "users", array ("name" => $e_name,
						"contact" => $mobile,
						"address" => $address,
						"photo" => $photovalue,
						'email' => $empmail,
						'password' => $pass,
						"suspen_id" => "0",
						'type' => '1',
						'privilege' => $mostawarded . ','
						. $earlierawards . ','
						. $giveanawards . ','
						. $employeesdetails . ','
						. $newemployees . ','
						. $departments . ','
						. $newdepartments . ','
						. $singaporetable . ','
						. $payslip . ','
						. incometax_slab . ','
						. $attendence . ','
						. $employeeattendence ) );
				if ($query) {
					
					/*
					 * $settingcheck = $db->get_col ( "setting", array ('id' =>
					 * 1 ), 'c_email' ); if ($settingcheck [0] != '') { $to =
					 * $uemail; $message = "Your new Employee is:" . $e_name;
					 * $name = $e_name; $return = $mail->send_email ( $to,
					 * $message, $name, C_NAME, C_EMAIL ); }
					 */
					?>
<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 Inserted ...</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 Inserted..</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
				
				}
				
			}
		}
		// -----------------
	}
}
?>