<?php
//die('test');
/**
 * xml2array() will convert the given XML text to an array in the XML structure.
 * Link: http://www.bin-co.com/php/scripts/xml2array/
 * Arguments : $contents - The XML text
 *                $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
 *                $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
 * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
 * Examples: $array =  xml2array(file_get_contents('feed.xml'));
 *              $array =  xml2array(file_get_contents('feed.xml', 1, 'attribute'));
 */
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {
        //print "'xml_parser_create()' function not found!";
        return array();
    }

    //Get the XML parser of PHP - PHP must have this module for the parser to work
    $parser = xml_parser_create('');
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);

    if(!$xml_values) return;//Hmm...

    //Initializations
    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array; //Refference

    //Go through the tags.
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
    foreach($xml_values as $data) {
        unset($attributes,$value);//Remove existing values, or there will be trouble

        //This command will extract these variables into the foreach scope
        // tag(string), type(string), level(int), attributes(array).
        extract($data);//We could use the array by itself, but this cooler.

        $result = array();
        $attributes_data = array();
        
        if(isset($value)) {
            if($priority == 'tag') $result = $value;
            else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
        }

        //Set the attributes too.
        if(isset($attributes) and $get_attributes) {
            foreach($attributes as $attr => $val) {
                if($priority == 'tag') $attributes_data[$attr] = $val;
                else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
            }
        }

        //See tag status and do the needed.
        if($type == "open") {//The starting of the tag '<tag>'
            $parent[$level-1] = &$current;
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
                $repeated_tag_index[$tag.'_'.$level] = 1;

                $current = &$current[$tag];

            } else { //There was another element with the same tag name

					//if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
					if (is_array($current[$tag]) && isset($current[$tag][0])) {//If there is a 0th element it is already an array
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    $repeated_tag_index[$tag.'_'.$level]++;
                } else {//This section will make the value an array if multiple tags with the same name appear together
                    $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
                    $repeated_tag_index[$tag.'_'.$level] = 2;
                    
                    if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                        unset($current[$tag.'_attr']);
                    }

                }
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
                $current = &$current[$tag][$last_item_index];
            }

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
            //See if the key is already taken.
            if(!isset($current[$tag])) { //New Key
                $current[$tag] = $result;
                $repeated_tag_index[$tag.'_'.$level] = 1;
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

            } else { //If taken, put all things inside a list(array)
                if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

                    // ...push the new element into that array.
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    
                    if($priority == 'tag' and $get_attributes and $attributes_data) {
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;

                } else { //If it is not an array...
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                    $repeated_tag_index[$tag.'_'.$level] = 1;
                    if($priority == 'tag' and $get_attributes) {
                        if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
                            
                            $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                            unset($current[$tag.'_attr']);
                        }
                        
                        if($attributes_data) {
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                        }
                    }
                    $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
                }
            }

        } elseif($type == 'close') { //End of tag '</tag>'
            $current = &$parent[$level-1];
        }
    }
    
    return($xml_array);
}
include(dirname(__FILE__).'/config-cogep.php');
$conn = mysql_connect($host,$user,$password,$db);
if($conn){
	echo 'Connection Established'.'<br>';
}

$output =  xml2array(file_get_contents($importfile));
$result_array = $output['biens'];
$inserted_ids = array();
foreach($result_array as $result){
	foreach($result as $property){
		if(isset($property['id_agence'])){
//	echo '<pre>';
	//print_r($property);
	//echo '</pre>';
	$mls_id = mysql_real_escape_string($property['reference']);
	$type = '0';
	$stypes = mysql_real_escape_string($property['type_transaction']);
	if($stypes =='vente'){ 
	$stype = '1'; 
	}else
	if($stypes =='programme neuf'){ $stype = '7'; }else
	if($stypes =='location'){ $stype = '4'; }else
	if($stypes =='location saisonnière'){ $stype = '8'; }else{ $stype = '0'; }
	$stype_freq = '';
	$listing_office =  '423';
	$street_num = '';
	$street = '';
	$street2= '';
	$apt ='';
	$title = mysql_real_escape_string(remove_accent($property['titre']));
	$alias = mysql_real_escape_string($title);
	$hide_address = '0';
	$show_map = '1';
	$description = mysql_real_escape_string(remove_accent($property['description_internet']));
	$short_description = mysql_real_escape_string(remove_accent(substr($description, 0, 100)));
	$terms = '';
	$agent_notes = '';
	$city = mysql_real_escape_string($property['ville']);
	$locstate = '0';
	$province = '';
	$postcode = mysql_real_escape_string($property['code_postal']);
	$region = '';
	$county = '';
	$country = '205';
	if(isset($property['latitude'])){
		$latitude = mysql_real_escape_string($property['latitude']);
	}else{
		
		$latitude = '';
	}
	$latitude;
	if(isset($property['longitude'])){
		$longitude = mysql_real_escape_string($property['longitude']);
	}else{
		
		$longitude = '';
	}
	$longitude;
	//$gbase_address = $property[''];
	$gbase_address = '';
	$concat_address = '';
	if($property['prix']){
	$price = mysql_real_escape_string($property['prix']);	
	}else{
	$price = '0';
	}
	$price2 = '0';
	$call_for_price = '0';
	$show_address = '0';
	if(!empty($property['nb_piece'])){
		 $beds = mysql_real_escape_string($property['nb_piece']);
	}else{
		
		 $beds = '0';
	}
	
	$beds;
	
	if(!empty($property['nb_sdb'])){
		 $baths = mysql_real_escape_string($property['nb_sdb']);
	}else{
		
		 $baths = '0';
	}
	
	$baths;
	$reception = '0';
	$tax = '';
	$income = '';
	if(!empty($property['surface_terrain'])){
		$sqft = mysql_real_escape_string($property['surface_terrain']);
	}else{ $sqft = '0';}
	$sqft;
	$lotsize = $property['surface_habitable'];
	$lot_acres = '';
	if(!empty($property['annee'])){
		 $yearbuilt = mysql_real_escape_string($property['annee']);
	}else{
		
		 $yearbuilt = '';
	}
	$yearbuilt;
	if(!empty($property['chauffage'])){
		 $heat = mysql_real_escape_string($property['chauffage']);
	}else{
		
		 $heat = '';
	}
	$heat;
	if(!empty($property['nb_chambre'])){
		 $cool = mysql_real_escape_string($property['nb_chambre']);
	}else{
		
		 $cool = '0';
	}
	$cool;
	if(!empty($property['etage'])){
		 $fuel = mysql_real_escape_string($property['etage']);
	}else{
		
		 $fuel = '';
	}
	$fuel;
	$garage_type = '0';
	$garage_size = '0';
	$zoning = '0';
	$frontage = '1';
	if(!empty($property['nb_wc'])){
		 $siding = mysql_real_escape_string($property['nb_wc']);
	}else{
		
		 $siding = '0';
	}
	
	$siding;
	if(!empty($property['terrasse'])){
		$roof = mysql_real_escape_string($property['terrasse']);
	}else{ $roof = 0; }
	$roof;
	if($roof == 'oui'){ $roof = '1';}else{ $roof = '0';}
	$roof;
	$propview = '0';
	$school_district = '0';
	$lot_type = '';
	$style = '';
	if(!empty($property['ameublement'])){
		 $hoa = mysql_real_escape_string($property['ameublement']);
	}else{
		
		 $hoa = '0';
	}
	$hoa;
	if($hoa == 'Partiellement meublé'){ $hoa = '1';}else if($hoa == 'Entièrement meublé'){ $hoa = '1';}else{ $hoa = '0';}
	$hoa;
	$reo = $property['vue'];
	if($reo == 'Mer'){ $reo = '1';}else{ $reo = '0';}
	$reo;
	$vtour = '';
	$video = '';
	$gbase_url = '0';
	$gbase_timestamp = $property['date_creation'];
    $d = DateTime::createFromFormat('d/m/Y', $gbase_timestamp);
	$ts =  $d->getTimestamp();
	$timestamp = $d->getTimestamp();
	$datetimeFormat = 'Y-m-d H:i:s';
	$date = new \DateTime();
	$date->setTimestamp($timestamp);
	$gbase_timestamp = $date->format($datetimeFormat);
	if(!empty($property['visite_virtuelle'][0])){
		$hits = mysql_real_escape_string($property['visite_virtuelle']);
	}else{
		$hits = '0';
	}
	$hits;
	$featured = '0';
	if(!empty($property['disponibilite'])){
		$available = mysql_real_escape_string($property['disponibilite']);
	}else{
		$available = '0000-00-00';
	}
	$available;
	$metadesc = '';
	$metakey = '';
	$created = $property['date_creation'];
	 $d = DateTime::createFromFormat('d/m/Y', $created);
	$ts =  $d->getTimestamp();
	$timestamp = $d->getTimestamp();
	$datetimeFormat = 'Y-m-d H:i:s';
	$date = new \DateTime();
	$date->setTimestamp($timestamp);
	$created = $date->format($datetimeFormat);
	$created_by = '584';
	$modified = $property['date_mise_a_jour'];
	$d = DateTime::createFromFormat('d/m/Y', $modified);
	$ts =  $d->getTimestamp();
	$timestamp = $d->getTimestamp();
	$datetimeFormat = 'Y-m-d H:i:s';
	$date = new \DateTime();
	$date->setTimestamp($timestamp);
	$modified = $date->format($datetimeFormat);
	$modified_by = '584';
	$access = '1';
	$publish_up = mysql_real_escape_string($created);
	$publish_down = '0000-00-00 00:00:00';
	$state = '1';
	$approved = '1';
	$checked_out = '0';
	$checked_out_time = '0000-00-00 00:00:00';
	$language = '';
	$ip_source = 'NULL';
	$listing_info = '';
	$cron = '0';
	$check_query  = "SELECT COUNT( * ) FROM  `immoclic_tahiti`.`sam1b_iproperty`  WHERE  `title` =  '$title' AND  `mls_id` =  '$mls_id'";
	$check_res = mysql_query($check_query) or die(mysql_error());
	while( $check_loop = mysql_fetch_array($check_res)){
		$prop_count = $check_loop['0'];
		echo 'Count for '.$title.'->'.$prop_count.'<br>';
	}
	if($prop_count == 0){
	echo 'Importing Data....'.'<br>';
    $query = "INSERT INTO `immoclic_tahiti`.`sam1b_iproperty` 
(`id`, `mls_id`, `type`, `stype`, `stype_freq`, `listing_office`, `street_num`, `street`, `street2`, `apt`, `title`, `alias`, `hide_address`, `show_map`, `short_description`, `description`, `terms`, `agent_notes`, `city`, `locstate`, `province`, `postcode`, `region`, `county`, `country`, `latitude`, `longitude`, `gbase_address`, `concat_address`, `price`, `price2`, `call_for_price`, `show_address`, `beds`, `baths`, `reception`, `tax`, `income`, `sqft`, `lotsize`, `lot_acres`, `yearbuilt`, `heat`, `cool`, `fuel`, `garage_type`, `garage_size`, `zoning`, `frontage`, `siding`, `roof`, `propview`, `school_district`, `lot_type`, `style`, `hoa`, `reo`, `vtour`, `video`, `gbase_url`, `gbase_timestamp`, `hits`, `featured`, `available`, `metadesc`, `metakey`, `created`, `created_by`, `modified`, `modified_by`, `access`, `publish_up`, `publish_down`, `state`, `approved`, `checked_out`, `checked_out_time`, `language`, `ip_source`, `listing_info`, `cron`) 
VALUES 
(NULL, '$mls_id', '$type', '$stype', '$stype_freq', '$listing_office', '$street_num', '$street', '$street2', '$apt', '$title', '$alias', '$hide_address', '$show_map', '$short_description', '$description', '$terms', '$agent_notes', '$city', '$locstate', '$province', '$postcode', '$region', '$county', '$country', '$latitude', '$longitude', '$gbase_address', '$concat_address', '$price', '$price2', '$call_for_price', '$show_address', '$beds', '$baths', '$reception', '$tax', '$income', '$sqft', '$lotsize','$lot_acres', '$yearbuilt', '$heat', '$cool', '$fuel', '$garage_type', '$garage_size', '$zoning', '$frontage', '$siding', '$roof', '$propview', '$school_district', '$lot_type', '$style', '$hoa','$reo', '$vtour', '$video', '$gbase_url', '$gbase_timestamp', '$hits', '$featured', '$available', '$metadesc', '$metakey', '$created', '$created_by', '$modified', '$modified_by', '$access', '$publish_up', '$publish_down', '$state', '$approved', '$checked_out', '$checked_out_time', '$language', NULL, '$listing_info', '$cron')";
	echo '<br>';
   $res = mysql_query($query) or die(mysql_error());
	if($res){
	$prop_id = mysql_insert_id();
	array_push($inserted_ids,$prop_id);
	echo 'Property id: '.$prop_id.' Inserted'.'<br>';
	$cat_ids = $property['type_bien'];
	if($cat_ids == 'parking'){ $cat_id = '1';}else
	if($cat_ids == 'terrain'){ $cat_id = '2';}else
	if($cat_ids == 'immeuble'){ $cat_id = '5';}else
	if($cat_ids == 'appartement'){ $cat_id = '6';}else
	if($cat_ids == 'maison'){ $cat_id = '7';}else
	if($cat_ids == 'fond de commerce'){ $cat_id = '3';}else
	if($cat_ids == 'local commercial'){ $cat_id = '3';}else
	if($cat_ids == "bureau"){ $cat_id = '3';}else
	if($cat_ids == "local professionnel"){ $cat_id = '3';}else
	if($cat_ids == 'local industriel'){ $cat_id = '3';}else
	if($cat_ids == "entrepot"){ $cat_id = '3';}else
	if($cat_ids == "studio"){ $cat_id = '6';}else{  $cat_id = '0'; }
	if(isset($property['garage'])){
		if($property['garage'] == 'OUI'){
		$amen_id = '4';	
		}
	}
	if(isset($property['garage'])){
		if($property['garage'] == 'OUI'){
		$amen_id = '4';	
		}
	}
	if(isset($property['piscine'])){
		if($property['piscine'] == 'OUI'){
		$amen_id = '9';	
		}
	}
	if(isset($property['park_int'])){
		if($property['park_int'] == 'OUI'){
		$amen_id = '56';	
		}
	}
	
	if(isset($property['park_ext'])){
		if($property['park_ext'] == 'OUI'){
		$amen_id = '56';	
		}
	}
	if(isset($property['nb_cave'])){
		if($property['nb_cave'] > '1'){
		$amen_id = '73';	
		}
	}
	if(isset($property['ascenseur'])){
		if($property['ascenseur'] == 'OUI'){
		$amen_id = '97';	
		}
	}
	if(isset($property['interphone'])){
		if($property['interphone'] == 'OUI'){
		$amen_id = '98';	
		}
	}
	$ip_source = 'NULL';
		
		 $query = "INSERT INTO `immoclic_tahiti`.`sam1b_iproperty_propmid` (`id`, `prop_id`, `cat_id`, `amen_id`, `ip_source`) VALUES (NULL, '$prop_id', '$cat_id', '$amen_id', NULL);";
		$res = mysql_query($query) or die(mysql_error());
		echo '<br>';
		echo 'Category id: '.$cat_id.'<br>';
		echo 'Agent Assigned'.'<br>';
			if(!empty($property['images'])){
	if(is_array($property['images']['image'])){
	$image_array = $property['images']['image'];
	foreach ($image_array as $key => $value) {
    if (!is_int($key)) {
        unset($image_array[$key]);
    }
}
	//print_r($image_array);
	$order = '0';
	foreach($image_array as $image){
		$img_ext = '.'.pathinfo(parse_url($image)['path'], PATHINFO_EXTENSION);
		$img_array = explode($img_ext,$image);
		$image_name = basename($image);
		$image_name_db = basename($image,$img_ext);
		echo '<br>';
		$thumbname = basename($image,$img_ext);
		$thumb_image = $thumbname.'_thumb'.$img_ext;
		echo '<br>';
		$url = $image;
		$dir = dirname(dirname(__FILE__))."/media/com_iproperty/pictures/";
		$content = file_get_contents($image);
		$fp = fopen($dir.$image_name, "w");
		$img_test = fwrite($fp, $content);
		$fp2 = fopen($dir.$thumb_image, "w");
		fwrite($fp2, $content);
		fclose($fp2);
		if($img_test){
			file_put_contents(dirname(__FILE__).'/images-added.txt',print_r($dir.$image_name.PHP_EOL,true),FILE_APPEND);
			
		}else{
			file_put_contents(dirname(__FILE__).'/images-missed.txt',print_r($dir.$image_name.PHP_EOL,true),FILE_APPEND);
		}
		//file_put_contents(dirname(dirname(__FILE__))."/media/com_iproperty/pictures/test/", $image);
		$counter++;
		$query_images = "INSERT INTO `immoclic_tahiti`.`sam1b_iproperty_images` 
		(`id`, `propid`, `title`, `description`, `fname`, `type`, `path`, `remote`, `owner`, `ordering`, `language`, `state`, `ip_source`) VALUES 
		(NULL, '$prop_id', '$title', 'image', '$image_name_db', '$img_ext', '/media/com_iproperty/pictures/', '1', '42', '$order', '', '1', NULL);";
		mysql_query($query_images) or die(mysql_error());
		echo $counter.': Saving: '.$image.' to '.dirname(dirname(__FILE__))."/media/com_iproperty/pictures/".'<br>';
		echo '<br>';
		echo 'Images Imported....'.'<br>';
		$order++;
	}
}
	}
	//print_r($image_array);
	
	//$agent_name = $property['nom_negociateur'];
	 $agent_id = '484';
	$query = "INSERT INTO `immoclic_tahiti`.`sam1b_iproperty_agentmid` (`id`, `prop_id`, `agent_id`, `agent_type`, `ip_source`) VALUES (NULL, '$prop_id', '$agent_id', '0', NULL);";
	mysql_query($query) or die(mysql_error());
	echo '<br>';

	}
	}else{
			echo $title.' Entry Skipped'.'<br>';
	}
}	
}
}
function remove_accent($str)
{
  $a = array("'",'Â°','À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'Ĳ', 'ĳ', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ŉ', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
  $b = array("",'A','A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
  return str_replace($a, $b, $str);
}
$new_query = "Select id from `immoclic_tahiti`.`sam1b_iproperty` ";
$res = mysqli_query($conn,$new_query) or die(mysql_error());
while($result = mysqli_fetch_assoc($res)){
array_push($all_ids,$result['id']);
}
$disable =array_diff($all_ids,$inserted_ids);
foreach($result as $id){
	$agent_query = "Select agent_id from `immoclic_tahiti`.`sam1b_iproperty_agentmid` where prop_id=".$id;
	$agent_res = mysqli_query($conn,$agent_query) or die(mysql_error());
	while($agent_result = mysqli_fetch_assoc($agent_res)){
		if($agent_result['agent_id'] == $agent_id){
         $upquery  = "UPDATE `immoclic_tahiti`.`sam1b_iproperty` SET `approved` = '0' WHERE `sam1b_iproperty`.`id` ='$id'";
          mysql_query($upquery) or die(mysql_error());
	      echo 'Property ID: '.$id.' Disapproved'.'<br />';
		}
	}
}