Current File : /home/n742ef5/royalanteam.com/mls_bkp/mls_import.php |
<?php
ini_set('memory_limit', '1024M');
set_time_limit(0);
ini_set('max_execution_time', 7200);
ini_set('display_errors', 1);
ini_set("log_errors", 1);
ini_set('default_charset', 'UTF-8');
error_reporting(E_ALL);
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set('America/Toronto');
//ini_set("error_log", "/home3/sitwoweb/public_html/royalanteam/mls/mls_resi_new.txt");
//echo "dfdf";exit;
$plus_v = urlencode("+");
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] . $mtime[0];
$t_start = $mtime;
$to = 'msuman1610@gmail.com';
$subject = 'Property Import';
require_once("config/mls_config.php");
require_once("lib/phrets.php");
require_once("config/properties_config.php");
require_once("/home/n742ef5/royalanteam.com/wp-load.php");
global $wpdb;
$message = "";
$message = '<html><head><title>RoyalTeam Property Import</title></head><body>';
$timestamp_field = 'Timestamp_sql';
$file_name = "mls_import.php";
$curr_resource = "Property";
$mls_key_field = "Ml_num";
$curr_mls_id = 1;
date_default_timezone_set('America/Toronto');
$curr_date = date('Y-m-d H:i:s');
$cron_tablename = 'properties_cron_log';
$tmp_tablename = 'property_queue';
/*$a = $property_mapping[ 'COM' ];
$query = "SHOW COLUMNS FROM `rets_property_data_comm` ";
$alldb_columns = array();
$res = $wpdb->get_results( $query,ARRAY_A );
foreach ($res as $key => $value) {
//echo "<br>'".$value['Field']."'=>'".$value['Field']."'," ;
$alldb_columns[] = $value['Field'];
}
$d = array_diff($a,$alldb_columns);
echo "<pre>";
print_r($d);
exit;
*/
foreach($all_classes as $classkey=> $classdata ) {
$className = $classdata['ClassName'];
$pro_table = $classdata['TableName'];
echo $classNameText = $classdata['classNameText'];
//exit;
//************* Step 1 *************/
$wpdb->insert( $cron_tablename, array(
'cron_file_name' => $file_name,
'cron_start_time' => $curr_date,
'property_class' => $className,
'mls_no' => $curr_mls_id,
'steps_completed' => 1
));
echo "LastId : ".$curr_log_id = $wpdb->insert_id;
$last_success_query = "SELECT * FROM $cron_tablename where cron_file_name='" . $file_name . "' and property_class='" . $className . "' and mls_no ='" . $curr_mls_id . "' and success = 1 and cron_end_time<> '0000-00-00 00:00:00' and properties_download_end_time<>'0000-00-00 00:00:00' ORDER BY id DESC LIMIT 1 ";
$last_cron_data = $wpdb->get_results($last_success_query);
$start_pull = false;
if(!$last_cron_data){
$default_date = "2022-01-01T00:00:01";
$start_pull = true;
$property_query_start_time = $default_date ;
} else {
$last_success_cron_end_time = $last_cron_data[0]->properties_download_end_time;
if ($last_success_cron_end_time == '' || $last_success_cron_end_time == '0000-00-00 00:00:00' || $last_success_cron_end_time == '0000-00-00') {
$property_query_start_time = strtotime($start_pull_time, time());
$property_query_start_time = date("Y-m-d H:i:s", $property_query_start_time);
} else {
$property_query_start_time = $last_success_cron_end_time;
}
}
$date_time = explode(' ', $property_query_start_time);
$property_query_start_time = $date_time[0] . 'T' . $date_time[1];
$date_query = "($timestamp_field=$property_query_start_time+)";
$status_query = "(Status=A)";//U,A
$curr_timestamp = time();
$old_timestamp = strtotime($property_query_start_time);
$diff = ($curr_timestamp - $old_timestamp);
//exit;
$new_starttime = 0;
if ($diff > 3600 * 24 * 30) {
$new_starttime = strtotime("+30 days", $old_timestamp);
}
/*if($diff < 3600 * 1){
$new_starttime = strtotime("-4 hours", $old_timestamp);
}*/
if ($new_starttime > 0 && $new_starttime < time()) {
$time_range1 = $property_query_start_time;
$time_range2 = date('Y-m-d H:i:s', $new_starttime);
$date_time2 = explode(' ', $time_range2);
$datetime_range2 = $date_time2[0] . 'T' . $date_time2[1];
$date_query = "($timestamp_field=$time_range1-$datetime_range2)";
$property_end_time = $time_range2;
/*if ($diff < 3600 * 1 ) {
$new_starttime = strtotime("-4 hours", $old_timestamp);
$date_query = "($timestamp_field=$datetime_range2+)";
$property_end_time = $curr_date;
}*/
} else {
$property_end_time = $curr_date;
$date_query = "($timestamp_field=$property_query_start_time+)";
}
$sale_query = "(S_r=Sale)";
$rets_query = "$date_query,$status_query,$sale_query";
if ($start_pull) {
//$property_end_time = $curr_date ;
//$rets_query = "($mls_key_field=0 ),$status_query";
}
//$date_query = "($timestamp_field=2020-08-17T00:00:01-2020-08-18T00:00:01)";
//$date_query = "($timestamp_field=2021-01-01T00:00:01+)";
$rets_query = "$date_query,$status_query";
//$rets_query = "$date_query,$status_query";
//$rets_query = "$status_query";
//$rets_query = "$status_query,$sale_query";
//$rets_query = "(Timestamp_sql=2018-01-01T00:00:01$plus_v),(Status=|A)";
//echo $plus_v;
//echo "(Timestamp_sql=2018-01-01T00:00:01+),(Status=|A)";
//$rets_query = "(Ml_num=|C4780966)";
//echo "<br>".$rets_query;
//continue;
//exit;
$cron_update_data = array(
'properties_download_start_time' => $property_query_start_time,
'steps_completed' => 2,
'rets_query' => $rets_query
);
$step2 = $wpdb->update($cron_tablename, $cron_update_data, array('id'=>$curr_log_id) );
echo "<br>";
$property_data_mapping = $property_mapping[ $classkey ];
$rets = new phRETS;
$rets->AddHeader("Accept", "*/*");
$rets->AddHeader("RETS-Version", "RETS/$rets_version");
$rets->AddHeader("User-Agent", 'UserAgent/1.0');
$rets->SetParam('debug_mode', false);
$rets->SetParam("offset_support", false);
$rets->SetParam("compression_enabled", true);
//$connect = $rets->Connect($vow_login_url, $vow_username, $vow_password);
$connect = $rets->Connect($login_url, $username, $password);
if (!$connect) {
$error_details = $rets->Error();
$error_text = strip_tags($error_details['text']);
$error_type = strtoupper($error_details['type']);
echo "<center><span style='color:red;font-weight:bold;'>{$error_type} ({$error_details['code']}) {$error_text}</span></center>";
exit;
}
$search = $rets->SearchQuery($curr_resource, $className, $rets_query, array( 'Count' => 2 ));
$total_property_count = $rets->TotalRecordsFound($search);
echo " $className - $rets_query : $total_property_count <br>";
$message .= "<h3>$className - $rets_query : $total_property_count </h3><br>";
$cron_update_data3 = array(
'properties_count_from_mls' => $total_property_count,
'steps_completed' => 3
);
$step3 = $wpdb->update($cron_tablename, $cron_update_data3, array('id'=>$curr_log_id) );
error_log(" $className - $rets_query : $total_property_count <br>");
$inserted_prop = 0;
$updated_prop = 0;
$properties_inserted_in_db = 0;
if ($total_property_count > 0) {
$search_chunks = $rets->SearchQuery("Property", $className, $rets_query, array(//'Limit' => $limit,//'Offset' => $offset,
'Format' => 'COMPACT-DECODED','Count' => 1, "UsePost" => 1 ));
echo "FOUND" . $rets->NumRows($search_chunks);
$all_arr = array();
$message .= "<table border=1><thead><tr><th>Sr. No</th><th>MLS#</th><th>Style</th><th>Action</th></tr></thead> ";
while ($record = $rets->FetchRow($search_chunks)) {
/*echo "<pre>";
print_r($record);
exit;*/
$listing_key = $record[ $mls_key_field ];
$ci_p = date('Y-m-d H:i:s');
$properties_inserted_in_db++;
echo "<br>".$listing_key ;
$property_data = array();
$trim = array('Cd','Cndsold_xd','Dt_sus','Dt_ter','Timestamp_sql','Td','Oh_date','Pix_updt','Unavail_dt','Vtour_updt',
'Xdtd','Oh_date1','Oh_date2','Oh_date3','Oh_dt_stamp','Condo_corp','Input_date','Ld','Unavail_dt');
$date_array = array('Vtour_updt');
foreach( $property_data_mapping as $db_key=>$mls_key ){
if( isset($record[ $mls_key ]) ){
if( in_array($mls_key,$trim)){
if( $record[ $mls_key ]==''){
$property_data[ $db_key ] = NULL;
} else {
$property_data[ $db_key ] = rtrim($record[ $mls_key ],'.0');
}
} else {
$property_data[ $db_key ] = $record[ $mls_key ];
}
}
}
$pro_table_temp = $pro_table;
$property_data['processed'] = 0;
//$property_data['classNameText'] = $classNameText;
$property_data['property_last_updated'] = date('Y-m-d H:i:s');
$message .= "<tr><td>".$properties_inserted_in_db."</td><td>".$listing_key."</td><td>".$property_data['Style']."</td>";
$qn_query = "SELECT id FROM $pro_table_temp where $mls_key_field='$listing_key'";
$pre_post = $wpdb->get_results( $qn_query,ARRAY_A );
if ($pre_post && !empty($pre_post) ) {
$prp_id = $pre_post[0]['id'];
$curr_opr = 'U';
$updated_prop++;
$message .= "<td>Update</td></tr>";
echo "<br>$updated_prop : Updated";
/*$wpdb->update($pro_table_temp, $property_data, array("$mls_key_field"=>$record[$mls_key_field]) );
if($wpdb->last_error !== '') {
$wpdb->print_error();
exit;
}*/
$query = 'UPDATE '.$pro_table_temp.' SET ';
$fields = '';
$values = '';
foreach($property_data as $prp_key=>$prp_value){
$fields .= ' '.$prp_key.' ="'.esc_sql($prp_value).'", ';
}
$fields = rtrim($fields,', ');
$currquery = $query.$fields.' WHERE '.$mls_key_field.' = "'.$record[$mls_key_field].'"';
//echo "<br>".$currquery;
$wpdb->query( $currquery);
if($wpdb->last_error !== '') {
$str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
print "<div id='error'><p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /><code>$query</code></p></div>";
$wpdb->print_error();
exit;
}
} else {
$curr_opr = 'I';
$inserted_prop++;
$message .= "<td>Insert</td></tr>";
echo "<br>$inserted_prop : Inserted";
$property_data['property_insert_time'] = date('Y-m-d H:i:s');
/*$wpdb->insert( $pro_table_temp, $property_data);
if($wpdb->last_error !== '') {
$wpdb->print_error();
exit;
}*/
$query = 'INSERT INTO '.$pro_table_temp.' (';
$values = '';
$fields = '';
foreach($property_data as $prp_key=>$prp_value){
$fields .= " $prp_key ,";
//$values .= ' "'.(stripslashes($prp_value)).'",';
$values .= ' "'.(esc_sql($prp_value)).'",';
}
$fields = rtrim($fields,',');
$values = rtrim($values,',');
$currquery = $query.$fields.') VALUES('.$values.')';
$wpdb->query( $currquery);
if($wpdb->last_error !== '') {
$wpdb->print_error();
exit;
}
}
}
$message .= "</table>";
}
echo "<br>".$message;
$cron_update_data4 = array(
'cron_end_time' => date('Y-m-d H:i:s'),
'steps_completed' => 4,
'success' => 1,
'property_inserted' => $inserted_prop,
'property_updated' => $updated_prop,
'properties_download_end_time' => $property_end_time,
'properties_count_actual_downloaded' => $properties_inserted_in_db
);
$step4 = $wpdb->update($cron_tablename, $cron_update_data4, array('id'=>$curr_log_id) );
}
$message .= "</body></html>";
$end_time = date('Y-m-d H:i:s');
//$headers = 'From: No Reply <noreply@'.$_SERVER['HTTP_HOST'].'>' . "\r\n";
//$headers .= "MIME-Version: 1.0\r\n";
//$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
//$headers = 'From: '. $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers = 'From: Royalanteam <noreply@'.$_SERVER['HTTP_HOST'].'>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
//mail('msuman1610@gmail.com','MLS Download '.$end_time,$message,$headers);