From e6857d2849bc85921ff632db74deca0a1e267759 Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 3 Nov 2022 11:21:06 +0100 Subject: [PATCH] create array of objects with all forms fields plus coordinates --- srcs/plugins/map_prof/mp_get_markers.php | 76 +++++++++++++++++++++--- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/srcs/plugins/map_prof/mp_get_markers.php b/srcs/plugins/map_prof/mp_get_markers.php index a2442a6..b9b788d 100644 --- a/srcs/plugins/map_prof/mp_get_markers.php +++ b/srcs/plugins/map_prof/mp_get_markers.php @@ -1,5 +1,20 @@ -1, + 'post_status' => 'publish', + 'post_type' => 'acf-field', + ); + $forms = get_posts($get_form_args); + $fields = []; + foreach ($forms as $form) { + $field = $form->post_excerpt; + array_push($fields, $field); + } + return $fields; +} + function mp_get_published_posts() { // $get_posts_args = array( // 'numberposts' => -1, @@ -20,6 +35,12 @@ function mp_get_published_posts() { // mp_console_log("acf published: "); // mp_console_log($posts_published); // mp_console_log("\n"); +// $list_fields = []; +// foreach ($posts_published as $post) { +// $field = $post->post_excerpt; +// array_push($list_fields, $field); +// } +// mp_console_log($list_fields); // $get_posts_args = array( // 'numberposts' => 1, @@ -47,16 +68,20 @@ function mp_get_published_posts() { 'post_type' => 'post', ); $posts_published = get_posts($get_posts_args); - mp_console_log("posts: "); - mp_console_log($posts_published); - mp_console_log("\n"); -// $post = $posts_published[0]; +// mp_console_log("posts: "); +// mp_console_log($posts_published); +// mp_console_log("\n"); +// $post = $posts_published[1]; // mp_console_log("post :"); // mp_console_log($post); // mp_console_log("\n"); // $adresse = get_field('adresse', $post->ID); // mp_console_log("field adresse :"); // mp_console_log($adresse); +// mp_console_log("\n"); +// $adresse = get_field('fonction', $post->ID); +// mp_console_log("field fonction :"); +// mp_console_log($adresse); // mp_console_log("\n"); return $posts_published; @@ -83,6 +108,23 @@ function mp_convert_coordinates(&$address) { return $coordinates; } +function mp_get_coordinates(&$marker) { + global $mp_api_key; + $address = $marker->adresse . "," + . $marker->ville . "," + . $marker->pays; + mp_console_log("address:"); + mp_console_log($address); + $geolocation = 'https://maps.googleapis.com/maps/api/geocode/json' + . '?address=' . urlencode($address) + . '&key=' . $mp_api_key; + + $jsoncontent = file_get_contents($geolocation); + $content = json_decode($jsoncontent); + $coordinates = $content->results[0]->geometry->location; + return $coordinates; +} + function mp_post_content($post) { //temp @@ -145,11 +187,11 @@ function mp_post_content($post) { return $content; } -function mp_retrieve_address(&$posts_list) { +function mp_retrieve_address($posts_list) { $locs = array(); foreach ($posts_list as $post) { $content = mp_post_content($post); - $address = mp_extract_address($content); + $address = mp_extract_address($post); if (strlen($address) > 0) { $lat_lng = mp_convert_coordinates($address); @@ -160,10 +202,28 @@ function mp_retrieve_address(&$posts_list) { return $locs; } +function mp_fill_fields_value(&$form_fields, $id) { + $marker = (object)[]; + foreach($form_fields as $field) { + $marker->$field = get_field($field, $id); + } + return $marker; +} + function mp_get_published_markers() { $posts_list = mp_get_published_posts(); - $markers = mp_retrieve_address($posts_list); - return $markers; + $form_fields = mp_get_form_fields(); + $markers = []; + foreach ($posts_list as $post) { + $marker = mp_fill_fields_value($form_fields, $post->ID); + $marker->coordinates = mp_get_coordinates($marker); + array_push($markers, $marker); + } + mp_console_log("markers:"); + mp_console_log($markers); + + $tmp_markers = mp_retrieve_address($posts_list); + return $tmp_markers; } ?>