diff --git a/README.md b/README.md index 7b37954..f1c774d 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ # MAP -questions: -- liens vers les pages +#### improvement suggestions: +- add a field "more infos" to address +- localise on map when form is filled -questions traitees: +#### questions traitees: - bound la carte limite pour ne pas voir la zone grise - quelles infos on mets dans les infowindow - adresse en haut (surtout pour les markers avec plusieurs evenements) @@ -20,7 +21,7 @@ questions traitees: - quand c'est un cluster, on zoom, - quand c'est plusieurs evenements au meme endroit, on les affiche -todo: +#### todo: - add info-window - add filter options - deal with bad address diff --git a/srcs/plugins/map_prof/map_prof_hooks.php b/srcs/plugins/map_prof/map_prof_hooks.php index b9017d0..e984563 100644 --- a/srcs/plugins/map_prof/map_prof_hooks.php +++ b/srcs/plugins/map_prof/map_prof_hooks.php @@ -25,6 +25,7 @@ require_once(dirname(__FILE__) . '/settings/mp_optionnals.php'); require_once(dirname(__FILE__) . '/mp_get_events.php'); require_once(dirname(__FILE__) . '/mp_sort_events.php'); require_once(dirname(__FILE__) . '/mp_get_filters.php'); +require_once(dirname(__FILE__) . '/mp_address_errors.php'); @@ -120,6 +121,7 @@ function mp_ljdp_map() { $events = mp_get_published_events(); $locations = mp_sort_events($events); + $to_add = array( "locations" => $locations, ); @@ -130,8 +132,7 @@ function mp_ljdp_map() { "ville", "categorie", ); - - $filters = mp_get_filters($events, $filters_fields); + $filters = mp_get_filters($locations, $filters_fields); /* * * * * * * * @@ -180,31 +181,7 @@ add_shortcode('lejourduprof_map', 'mp_ljdp_map'); */ function mp_errors_map() { - - $errors = ""; - $count = 0; - - $get_posts_args = array( - 'numberposts' => -1, - 'post_status' => 'publish', - 'post_type' => 'post', - ); - $posts = get_posts($get_posts_args); - foreach ($posts as $post) { - $id = $post->ID; - $coordinate = get_field('coordinates', $id); - if ($coordinate == null) { - $count++; - $errors .= '

erreur :

- article : "'; - $errors .= $post->post_title . '"

- adresse : "'; - $errors .= get_field('adresse', $id) . ', '; - $errors .= get_field('ville', $id) . ', '; - $errors .= get_field('pays', $id) . ''; - $errors .= '"

'; - } - } - $errors = "

nombre d'erreurs : " . strval($count) . "

" . $errors; - return $errors; + return mp_find_address_errors(); } add_shortcode('ljdp_errors_map', 'mp_errors_map'); @@ -219,10 +196,11 @@ add_shortcode('ljdp_errors_map', 'mp_errors_map'); function post_published_coordinates($id, $post) { - $coordinates = mp_get_coordinates($id); + $location = mp_get_coordinates($id); + + if ( ! add_post_meta( $id, 'location', $location, true ) ) + update_post_meta( $id, 'location', $location ); - if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) ) - update_post_meta( $id, 'coordinates', $coordinates ); } add_action( 'publish_post', 'post_published_coordinates', 10, 2 ); diff --git a/srcs/plugins/map_prof/mp_address_errors.php b/srcs/plugins/map_prof/mp_address_errors.php new file mode 100644 index 0000000..93ef820 --- /dev/null +++ b/srcs/plugins/map_prof/mp_address_errors.php @@ -0,0 +1,96 @@ +approximate) { + return false; + } + } + + return true; +} + +function mp_is_address_complete($post, $id, $location) { + + // is presentiel but not complete address ? + $presentiel = get_field("mode", $id); + if ($presentiel[0] === "En présentiel") { + if (strlen($location->street) == 0) { + return false; + } + if (strlen($location->city) == 0) { + return false; + } + } + + return true; +} + +function mp_is_valid_address($post, $id, $location) { + + // is coordinates ? + if ($location->coordinates == null) + return false; + + return true; +} + +function mp_fill_address_message($post, $id, $location) { + + $message = '

article : "' + . $post->post_title . '"

- adresse fournie : "' + . get_field('adresse', $id) . ', ' + . get_field('ville', $id) . ', ' + . get_field('pays', $id) + . '"

- adresse trouvée : "' + . $location->address + . '"

'; + + return $message; +} + +function mp_find_address_errors() { + + $errors = ""; + $incompletes = ""; + $approximates = ""; + $count_errors = 0; + $count_incompletes = 0; + $count_approximates = 0; + + $get_posts_args = array( + 'numberposts' => -1, + 'post_status' => 'publish', + 'post_type' => 'post', + ); + $posts = get_posts($get_posts_args); + foreach ($posts as $post) { + $id = $post->ID; + $location = get_field('location', $id); + if (! mp_is_valid_address($post, $id, $location)) { + $count_errors++; + $errors .= mp_fill_address_message($post, $id, $location); + } + // else if (! mp_is_address_complete($post, $id, $location)) { + // $count_incompletes++; + // $incompletes .= mp_fill_address_message($post, $id, $location); + // } + else if (! mp_is_precise($post, $id, $location)) { + $count_approximates++; + $approximates .= mp_fill_address_message($post, $id, $location); + } + } + $message = "

nombre d'erreurs : " . $count_errors . "

"; +// $message .= "

nombre d'adresses incompletes pour des evenements en presentiels : " . $count_incompletes . "

"; + $message .= "

nombre d'adresses approximatives pour des evenements en presentiels : " . $count_approximates . "

"; + $message .= "

erreurs :

" . $errors; +// $message .= "

adresses incompletes:

" . $incompletes; + $message .= "

approximatives :

" . $approximates; + + return $message; +} + +?> diff --git a/srcs/plugins/map_prof/mp_get_coordinates.php b/srcs/plugins/map_prof/mp_get_coordinates.php index 9ee6aeb..3fa4315 100644 --- a/srcs/plugins/map_prof/mp_get_coordinates.php +++ b/srcs/plugins/map_prof/mp_get_coordinates.php @@ -3,7 +3,13 @@ function mp_get_coordinates($id) { global $mp_api_key; $event = (object)[]; - $coordinates = null; + $location = (object)[]; + $location->coordinates = null; + $location->street = ""; + $location->city = ""; + $location->country = ""; + $location->address = ""; + $location->approximate = false; // get address $address = ""; @@ -14,9 +20,6 @@ function mp_get_coordinates($id) { ); foreach($fields as $field) { $address_section = get_field($field, $id); -// if ($field == "pays") -// if ($address_section == null) -// //return null; $address .= $address_section . ","; } @@ -27,10 +30,30 @@ function mp_get_coordinates($id) { $jsoncontent = file_get_contents($geolocation); // extract coordinates from json + // https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Types $content = json_decode($jsoncontent); - $coordinates = $content->results[0]->geometry->location; + $location->coordinates = $content->results[0]->geometry->location; + $location->address = $content->results[0]->formatted_address; + foreach ($content->results[0]->address_components as $component) { + if (in_array("street_number", $component->types)) + $location->street = " " . $component->long_name; + else if (in_array("route", $component->types)) + $location->street .= " " . $component->long_name; + else if (in_array("locality", $component->types)) + $location->city .= " " . $component->long_name; + else if (in_array("postal_town", $component->types)) + $location->city .= " " . $component->long_name; + else if (in_array("country", $component->types)) + $location->country = $component->long_name; + } + if ($content->results[0]->geometry->location_type == "APPROXIMATE") + $location->approximate = true; + foreach ($location as $value) { + $value = trim($value, " "); + mp_console_log($value); + } - return $coordinates; + return $location; } ?> diff --git a/srcs/plugins/map_prof/mp_get_events.php b/srcs/plugins/map_prof/mp_get_events.php index 32a2c9f..08231d2 100644 --- a/srcs/plugins/map_prof/mp_get_events.php +++ b/srcs/plugins/map_prof/mp_get_events.php @@ -99,20 +99,20 @@ function mp_get_published_posts() { // FOR TESTS // script to publish or unpublish posts // -// $post_args = array( -// 'numberposts' => -1, -// 'post_status' => 'draft', -// //'post_status' => 'publish', -// 'post_type' => 'post', -// ); -// $post_list = get_posts($post_args); -// foreach ($post_list as $post) { -// wp_update_post(array( -// 'ID' => $post->ID, -// //'post_status' => 'draft', -// 'post_status' => 'publish', -// )); -// }; + $post_args = array( + 'numberposts' => -1, + 'post_status' => 'draft', + //'post_status' => 'publish', + 'post_type' => 'post', + ); + $post_list = get_posts($post_args); + foreach ($post_list as $post) { + wp_update_post(array( + 'ID' => $post->ID, + //'post_status' => 'draft', + 'post_status' => 'publish', + )); + }; $get_posts_args = array( 'numberposts' => -1, @@ -146,8 +146,7 @@ function mp_fill_fields_value($id) { "adresse", "prenom", "nom", - "coordinates", - "coordinates_valid", + "location", ); $event = (object)[]; foreach($fields as $field) { diff --git a/srcs/plugins/map_prof/mp_get_filters.php b/srcs/plugins/map_prof/mp_get_filters.php index 912bd20..aff5310 100644 --- a/srcs/plugins/map_prof/mp_get_filters.php +++ b/srcs/plugins/map_prof/mp_get_filters.php @@ -1,9 +1,25 @@ $field = []; + foreach ($locations as $loc) { + foreach ($loc->events as $event) { + mp_add_filters($filters->$field, $event->$field); + } + } } mp_console_log("filters:"); mp_console_log($filters); diff --git a/srcs/plugins/map_prof/mp_sort_events.php b/srcs/plugins/map_prof/mp_sort_events.php index a3f0f07..f95c85a 100644 --- a/srcs/plugins/map_prof/mp_sort_events.php +++ b/srcs/plugins/map_prof/mp_sort_events.php @@ -10,7 +10,7 @@ function mp_coord_already_exist(&$coordinates, &$locations) { } function mp_sort_n_insert(&$event, &$locations) { - $coordinates = $event->coordinates; + $coordinates = $event->location->coordinates; $already_exist = mp_coord_already_exist($coordinates, $locations); if ($already_exist) {