sparqapi/api/application/helpers/satellite_image_helper.php

188 lines
7.8 KiB
PHP

<?php
class satellite_image
{
public static function getSatelliteImage($pd_id,$pd_type = 1,$api = null)//1 - credit PD,2 - sales pd, 3- smc credit pd
{
// echo 'satellite'.$pd_type;
log_message('ERROR','####Satellite IMG HELPER '.$pd_id);
$pdid = $pd_id;
$api_return = null;
$CI =&get_instance();
$path_to_check = ($pd_type == 1 || $pd_type == 3) ? $CI->external_path.'/pd_reports/'.$pdid : $CI->external_path.'/sales_pd/'.$pdid;
$geo_location = null;
$pdmas = array();
$logger = $pd_type == 1 ? MYLOG::logFunction(null,$pdid) : MYLOG::logSalesPD($pdid);
$model_to_load = ($pd_type == 1 || $pd_type == 3) ? 'PD_Model' : 'Sales_PD_Model';
//print_r($logger);die();
if(!file_exists($path_to_check))
{
mkdir($path_to_check,0777);
}
$google_map_static_api_key = GOOGLE_STATIC_MAP_API_KEY;
if($pd_type == 1 || $pd_type == 3)
{
$pdmas = $CI->PD_Model->getPDMasterDetails($pdid);
$geo_location = $pdmas[0]['start_location'];
// $satellite_script_from_db = $CI->$model_to_load->selectCustomRecords(array('*'),array('script_name' => SATELLITE_IMAGE_LOCATION),SCRIPTS);
// $satellite_script = $satellite_script_from_db[0]['script_content'];
//Replacing Place holders
// $satellite_script = str_replace('{{APPPATH}}',$path_to_check,$satellite_script);
// $satellite_script = str_replace('{{ADDRESS}}',$geo_location,$satellite_script);
// $satellite_script = str_replace('{{GEOLOCATION}}',$geo_location,$satellite_script);
// $satellite_script = str_replace('{{API_KEY}}',$google_map_static_api_key,$satellite_script);
// //echo $satellite_script;die();
// file_put_contents(APPPATH.'/views/js/phantomsatellite.js',$satellite_script);
// //call& execute phantom script
// $str = 'phantomjs '.APPPATH.'/views/js/phantomsatellite.js';
// exec($str,$ou);
// exec('phantom.exit();');
}
else if($pd_type == 2)
{
$pdmas = $CI->Sales_PD_Model->selectCustomRecords(array('geo_location'),array('sales_pd_id' => $pdid),SALES_PD_DATA);
$geo_location = $pdmas[0]['geo_location'];
}
$geo_location = preg_replace('/\s+/', '', $geo_location);
$url = "https://maps.googleapis.com/maps/api/staticmap?center=".$geo_location."&zoom=14&markers=color:red|".$geo_location."&size=800x300&maptype=roadmap&key=".$google_map_static_api_key;
//echo $url;
$ou = file_put_contents($path_to_check.'/satellite.jpeg', file_get_contents($url));
$satellite_script = $url;
//echo $url;
// //convert as base 64 image.
$satellite_image_png_path = $path_to_check.'/satellite.jpeg';
//echo 'all done';
//die();
if($pd_type == 1 )
{
$bucketname = LENDER_BUCKET_NAME_PREFIX.$pdmas[0]['fk_lender_id'];
$key = 'pd'.$pdid.'/'.'satellite.jpeg';
$bucket_data = array('bucket_name'=>$bucketname,'key'=>$key,'sourcefile'=>$CI->external_path.'/pd_reports/'.$pdid.'/satellite.jpeg');
$s3result= $CI->aws_s3->uploadFileToS3Bucket($bucket_data);
if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200)
{
$CI->PD_Model->updateRecords(array('isactive' => 0),PDDOCUMENTS,array('pd_document_title' => 'satellite','fk_pd_id' => $pd_id));
$record_data = array('fk_pd_id' => $pdid,'pd_document_title'=>'satellite','pd_document_name'=>'satellite.jpeg','fk_form_id'=>null,'rental_count_id' => null,'location' => null);
$id = $CI->PD_Model->saveRecords($record_data,PDDOCUMENTS);
$api_return = $id;
$custom_log_info = '###CUSTOM DEBUG SATELLITE IMAGE STORED IN S3 DONE - '. $id;
log_message('ERROR',$custom_log_info);
}
}
else if($pd_type == 2) //save satellite img as base64 in sale pd doc table
{
// Images As blob
// $satellite_base64 = base64_encode(file_get_contents($satellite_image_png_path));
// $CI->Sales_PD_Model->updateRecords(array('isactive' => 0),SALEPD_DOCS,array('img_name' => 'satellite','fk_sales_pd_id' => $pd_id));
// $CI->Sales_PD_Model->saveRecords(array('img' => $satellite_base64,'img_name' => 'satellite','fk_sales_pd_id' => $pdid),SALEPD_DOCS);
$image_inserted = 0;
$source=$satellite_image_png_path;
// Google Library - GCP
$CI->load->library('Googlecloud');
$gcpBucketName = GCP_BUCKET_NAME;
$objectName = "pd".$pdid."/images/satellite.png";
$result = $CI->googlecloud->uploadFileToGCPBucket($gcpBucketName,$objectName,$source);
if($result == true){
$image_inserted++;
log_message('ERROR', '### Sales-PD ('.$pdid.') Satellite Image Stored In GCP ');
}else{
log_message('ERROR', '### Sales-PD ('.$pdid.') Satellite Image Not Stored In GCP');
}
// Amazon Library - S3
$columns = array('SALES_PD_DATA.lender_id');
$table = SALES_PD_DATA.' as SALES_PD_DATA';
$where_condition_array = array('SALES_PD_DATA.sales_pd_id' => $pdid);
$sales_pd_data = $CI->Sales_PD_Model->selectCustomRecords($columns,$where_condition_array,$table);
$bucket_name = LENDER_BUCKET_NAME_PREFIX.$sales_pd_data[0]['lender_id'];
$bucket_key = "sales_pd/".$pdid."/images/satellite.png";
$bucket_data = array('bucket_name'=>$bucket_name,'key'=>$bucket_key,'sourcefile'=>$source);
$s3result= $CI->aws_s3->uploadFileToS3Bucket($bucket_data);
if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200)
{
$image_inserted++;
log_message('ERROR', '### Sales-PD ('.$pdid.') Satellite Image Stored In S3 ');
}else{
log_message('ERROR', '### Sales-PD ('.$pdid.') Satellite Image Not Stored In S3 ');
}
if($image_inserted>=1){
$CI->Sales_PD_Model->updateRecords(array('isactive' => 0),SALEPD_DOCS,array('img_name' => 'satellite','fk_sales_pd_id' => $pdid));
$CI->Sales_PD_Model->saveRecords(array('img' => null,'img_name' => 'satellite','fk_sales_pd_id' => $pdid),SALEPD_DOCS);
log_message('ERROR', '### Sales-PD ('.$pdid.') Satellite Images Saved in DB');
}
}
else if($pd_type == 3)
{
//echo 'updae';
$satellite_base64 = base64_encode(file_get_contents($satellite_image_png_path));
$CI->PD_Model->updateRecords(array('isactive' => 0),SMC_CREDIT_PD_DOCS,array('img_name' => 'satellite','fk_smc_credit_pd_id' => $pd_id));
$CI->PD_Model->saveRecords(array('img' => $satellite_base64,'img_name' => 'satellite','fk_smc_credit_pd_id' => $pd_id),SMC_CREDIT_PD_DOCS);
}
//generate log
$logger->info("Generate Satellite Called in (".ENVIRONMENT.")",array('script' => $satellite_script,'script_output' => $ou));
log_message('ERROR',"Generate Satellite Called in (".ENVIRONMENT.")",array('script' => $satellite_script,'script_output' => $ou));
//return $base64_pdscore_chart_url;
if($api)
{
return $api_return;
}
}
public static function getdistancematrix($from_addr,$to_addr){
if((int)$from_addr != 0){
// $apikey = GOOGLE_DISTANCE_MATRIX_API_KEY;
$apikey = "AIzaSyA1QV-A3t_3RwlozdlwgxlXnLNb_3WqxfY"; // now hardcoded
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$from_addr."&destinations=".$to_addr."&mode=driving&key=".$apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
if($response_a['rows'][0]['elements'][0]['status'] != "ZERO_RESULTS"){
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
return array('status'=>true,'distance' => $dist, 'time' => $time,'url'=>$url);
}else{
return array('status'=>false,'distance' => '0 km', 'time' => '0 days','url'=>$url);
}
}else{
return array('status'=>false,'distance' => '0 km', 'time' => '0 days','url'=>"");
}
}
}
?>