<?php
session_start(); include('session.php');
ini_set('max_execution_time',0);

include 'db.php';
if(isset($_POST["Upload_Data"])){
		
    $gid = $_POST['grup'];
		//set_time_limit (0);
		
		echo $filename=$_FILES["file"]["tmp_name"];
		
		$i=0;
		 if($_FILES["file"]["size"] > 0) {

		  	$file = fopen($filename, "r");
	         while (($emapData = fgetcsv($file, 90000, ",")) !== FALSE){
				
				 if($i>0) {
				     
				  //   $numb = $emapData[1];
				     $numb = preg_replace("/[^+0-9]/", "", $emapData[1]);   // remove spaces and other characters in-between numbers
				    $no_ckeck = substr($numb, 0, 1);
				    if($no_ckeck == "0"){
            			$pno = substr($numb, 1);  // remove first character
            			$pno1 = $pno;  // 727992036
            			$pno2 = "+254".$pno;  //+254727992036
            			$pno3 = "254".$pno; 		//254727992036
            		}
            		elseif($no_ckeck == "2"){
            			$pno = substr($numb, 3);  // remove first 3 characters
            			$pno3 = "+".$numb; // +254727992036
            			$pno1 = $pno;   // 727992036
            			$pno2 = "0".$pno;   // 0727992036
            			
            		}
            		elseif($no_ckeck == "7"){
            			$pno1 = "0".$numb;  // 0727992036
            			$pno2 = "+254".$numb;  //+254727992036
            			$pno3 = "254".$numb; 		//254727992036
            		}
            			elseif($no_ckeck == "1"){
            			$pno1 = "0".$numb;  // 0727992036
            			$pno2 = "0".$numb;  //+254727992036
            			$pno3 = "0".$numb; 		//254727992036
            		}
            		elseif($no_ckeck == "+")
            		{
            			$pno = substr($numb, 4);  // remove first 4 characters
            			$pno1 = $pno;  // 727992036
            			$pno2 = "0".$pno;  // 0727992036
            			$pno3 = "254".$pno;  //254727992036
            		} 
            		else{}
            		
					//check duplicate
					
				    $qry = "select * from airtime_contacts where customer_id = '$session_id' and (contact = '$numb' or contact = '$pno1' or contact = '$pno2' or contact = '$pno3') and group_id = '$gid' ";
				     $qresult = mysql_query($qry, $conn);
    				if(mysql_num_rows($qresult) > 0)
    				{
    				    // don't save duplicate
    				}
    				else
    				{ 
    				    // save contact
    				        //It wiil insert a row to our subject table from our csv file`
            	           $sql = "INSERT into airtime_contacts (`customer_id`, `fname`, `contact`, `group_id`) values('$session_id','$emapData[0]','$pno2','$gid')";
            	            $result = mysql_query( $sql, $conn );
            				if(!$result ) {
            					echo "<script type=\"text/javascript\">
            							alert(\"Invalid File:Please Upload CSV File.\");
            							window.location = \"uploadcontacts.php\"
            						</script>";
            				
            				    }
    				}
	      
			    }$i++;

	         }
	         	// get group name
				$qry1 = mysql_query("select * from airtime_groups where group_id = '$gid' ");
				$gdt=mysql_fetch_array($qry1);
				$gname = $gdt['name'];
				
	         	$qry = mysql_query("select * from airtime_logs where id = '$session_lid' ");
				$sdata=mysql_fetch_array($qry);
				$actions=$sdata['user_actions'];
				mysql_query("update airtime_logs set user_actions = '$actions Uploaded new contacts to group - $gname.' where id='$session_lid'  ");
	         fclose($file);
			 
			
	         //throws a message if data successfully imported to mysql database from excel file
	         //echo "<script type=\"text/javascript\">
						//alert(\"CSV File has been successfully Imported. Click OK to Exit\");
						//window.location = \"index.php\"
					//</script>";
					 ?>
	         <script>
//alert('Data Already Exists');
window.location="addnewcontact.php";
</script>
			 <?php

			 //close of connection
			mysql_close($conn); 
				
		 	
			
		 }
	}	 
?>		 