diff --git a/api/v1/get/application.php b/api/v1/get/application.php index 6d108c5..063589b 100644 --- a/api/v1/get/application.php +++ b/api/v1/get/application.php @@ -141,8 +141,13 @@ switch ($action) { break; case 'geolocation': - - $sql = 'SELECT e.* FROM equipment e '.$whereclause.''; + + if ($whereclause == ''){ + $whereclause = 'WHERE geolocation is not null'; + } else { + $whereclause .= ' AND geolocation is not null'; + } + $sql = 'SELECT distinct(geolocation) FROM equipment e '.$whereclause.''; break; diff --git a/assets/functions.php b/assets/functions.php index e5f2d6a..8dc7da4 100644 --- a/assets/functions.php +++ b/assets/functions.php @@ -1858,47 +1858,49 @@ function createPartner($user_salesid,$user_soldto,$p_name,$p_type, $token){ // +++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Update GEOlOCATION // +++++++++++++++++++++++++++++++++++++++++++++++++++++++ - function geolocationUpdate($token){ - include dirname(__FILE__,2).'/settings/settings.php'; - //GET ALL WARRANTY REGISTRATIONS WHERE NO GEOLOCATION SET - $sql = 'SELECT e.*, h.* FROM equipment e join history h on e.rowID = h.equipmentid where h.type = "Warranty" AND e.geolocation is Null'; - $pdo = dbConnect($dbname); - $stmt = $pdo->prepare($sql); - $stmt->execute(); - $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); +function geolocationUpdate($token){ + include dirname(__FILE__,2).'/settings/settings.php'; - //FOR ALL RECORDS RETURN FIND GEOLOCATION AT OSM - foreach ($messages as $message){ - $warranty = json_decode($message['description']); - - //API URL OSM - $api_url = 'https://nominatim.openstreetmap.org/search?format=json&city='.$warranty->city.'&country='.$warranty->country; + //GET ALL WARRANTY REGISTRATIONS WHERE NO GEOLOCATION SET + $sql = 'SELECT h.equipmentid, h.description FROM equipment e join history h on e.rowID = h.equipmentid where h.type = "Warranty" AND e.geolocation is Null'; + $pdo = dbConnect($dbname); + $stmt = $pdo->prepare($sql); + $stmt->execute(); + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); - //BUILD UP FILESTREAM - $httpOptions = [ - "http" => [ - "method" => "GET", - "header" => "User-Agent: Nominatim-Test" - ] - ]; + //FOR ALL RECORDS RETURN FIND GEOLOCATION AT OSM + foreach ($messages as $message){ + $warranty = json_decode($message['description']); + + //API URL OSM + $api_url = 'https://nominatim.openstreetmap.org/search?format=json&city='.$warranty->city.'&country='.$warranty->country; + $api_url = str_replace(' ','%20',$api_url); + + //BUILD UP FILESTREAM + $httpOptions = [ + "http" => [ + "method" => "GET", + "header" => "User-Agent: Nominatim-Test" + ] + ]; - $streamContext = stream_context_create($httpOptions); - $geo_feedback = file_get_contents($api_url, false, $streamContext); - $geo_feedback = json_decode($geo_feedback, true) ?? ''; + $streamContext = stream_context_create($httpOptions); + $geo_feedback = file_get_contents($api_url, false, $streamContext); + $geo_feedback = json_decode($geo_feedback, true); + $lat = (isset($geo_feedback[0]["lat"]) && ($geo_feedback[0]["lat"] !='' || $geo_feedback[0]["lat"] != null))?$geo_feedback[0]["lat"]:0; + $long = (isset($geo_feedback[0]["lon"]) && ($geo_feedback[0]["lon"] !='' || $geo_feedback[0]["lon"] != null))?$geo_feedback[0]["lon"]:0; - //CHECK IF RESULTS ARE FOUND - if (!empty($geo_feedback) || $geo_feedback !=''){ - $lat = $geo_feedback[0]["lat"] ?? ''; - $long = $geo_feedback[0]["lon"] ?? ''; - $geo_feedback = array($lat,$long); - //JSON ENCODE GEOLOCATION RECEIVED AND SENT TO EQUIPMENT API - $geo_feedback = json_encode($geo_feedback); - //BUILD POST DATA - $data = json_encode(array("rowID" => $message['equipmentid'] , "geolocation" => $geo_feedback), JSON_UNESCAPED_UNICODE); - //Secure data - $payload = generate_payload($data); - //API call - ioAPI('/v1/equipments', $payload, $token); - } + if ($lat !=0){ + $geo_feedback = array($lat,$long); + //JSON ENCODE GEOLOCATION RECEIVED AND SENT TO EQUIPMENT API + $geo_feedback = json_encode($geo_feedback); + //BUILD POST DATA + $data = json_encode(array("rowID" => $message['equipmentid'] , "geolocation" => $geo_feedback), JSON_UNESCAPED_UNICODE); + //Secure data + $payload = generate_payload($data); + //API call + $responses = ioAPI('/v1/equipments', $payload, $token); + } - } \ No newline at end of file + } +} \ No newline at end of file