From 0f4eb2bd84ba2d251961bf429a8c9b5d07f620c8 Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Nov 2022 11:47:40 +0100 Subject: [PATCH] wip create filters but values are unique for sub cateegories --- srcs/plugins/map_prof/map_prof_hooks.php | 5 +- srcs/plugins/map_prof/mp_get_coordinates.php | 14 +-- srcs/plugins/map_prof/mp_get_events.php | 3 +- srcs/plugins/map_prof/mp_get_filters.php | 123 +++++++++++++++---- 4 files changed, 114 insertions(+), 31 deletions(-) diff --git a/srcs/plugins/map_prof/map_prof_hooks.php b/srcs/plugins/map_prof/map_prof_hooks.php index c9b5151..5d9f62c 100644 --- a/srcs/plugins/map_prof/map_prof_hooks.php +++ b/srcs/plugins/map_prof/map_prof_hooks.php @@ -67,15 +67,16 @@ function mp_ljdp_map() { */ $events = mp_get_published_events(); + $locations = mp_sort_events($events); + $filters = mp_get_filters($events); $to_add = array( "locations" => $locations, + "filters" => $filters, ); mp_add_to_scripts($to_add); - $filters = mp_get_filters($locations); - /* * * * * * * * * ADD FILTERS diff --git a/srcs/plugins/map_prof/mp_get_coordinates.php b/srcs/plugins/map_prof/mp_get_coordinates.php index 86ac9f0..040749c 100644 --- a/srcs/plugins/map_prof/mp_get_coordinates.php +++ b/srcs/plugins/map_prof/mp_get_coordinates.php @@ -32,25 +32,25 @@ function mp_get_coordinates($id) { // extract coordinates from json // https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Types $content = json_decode($jsoncontent); - $location->coordinates = $content->results[0]->geometry->location; - $location->address = $content->results[0]->formatted_address; + $location->coordinates = trim($content->results[0]->geometry->location, " "); + $location->address = trim($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; + $location->street = trim($component->long_name, " "); else if (in_array("route", $component->types)) { if (strlen($location->street) != 0) $location->street .= " "; - $location->street .= $component->long_name; + $location->street .= trim($component->long_name, " "); } else if (in_array("locality", $component->types)) - $location->city .= $component->long_name; + $location->city = trim($component->long_name, " "); else if (in_array("postal_town", $component->types)) { if (strlen($location->city) != 0) $location->city .= "/"; - $location->city .= $component->long_name; + $location->city .= trim($component->long_name, " "); } else if (in_array("country", $component->types)) - $location->country = $component->long_name; + $location->country = trim($component->long_name, " "); } if ($content->results[0]->geometry->location_type == "APPROXIMATE") $location->approximate = true; diff --git a/srcs/plugins/map_prof/mp_get_events.php b/srcs/plugins/map_prof/mp_get_events.php index 4def814..4e345d5 100644 --- a/srcs/plugins/map_prof/mp_get_events.php +++ b/srcs/plugins/map_prof/mp_get_events.php @@ -172,7 +172,8 @@ function mp_get_published_events() { $event->title = $post->post_title; array_push($events, $event); } - mp_console_log("nombre de posts: " . count($events)); + mp_console_log("events:"); + mp_console_log($events); return $events; } diff --git a/srcs/plugins/map_prof/mp_get_filters.php b/srcs/plugins/map_prof/mp_get_filters.php index 7bb1d25..92a81f7 100644 --- a/srcs/plugins/map_prof/mp_get_filters.php +++ b/srcs/plugins/map_prof/mp_get_filters.php @@ -1,32 +1,113 @@ country = []; - $filters->city = []; - $filters->category = []; - - foreach ($locations as $loc) { - $country = $loc->events[0]->location->country; - mp_add_filters($filters->country, $country); +function mp_already_in_menu(&$menu, $name) { + foreach ($menu as $field) { + if ($field->_name == $name) + return true; } + return false; +} + +/* + menu: [ { _name:"", field_1:[], field_2:[] }, ... ] + fields: [ countries:"", cities:"", categories:"", ... ] +*/ + +function mp_fill_name($fields, $name, &$menu) { + if ($fields[$name] == null) + return; + if (gettype($fields[$name]) != 'string') + return; + if (strlen($fields[$name]) == 0) + return; + $menu_field = mp_already_in_menu($menu, $fields[$name]); + if ($menu_field != null) { + foreach ($fields as $key_field => $value) { + if ($key_field == $name) + continue; + if (! in_array($value, $menu_field->$key_field) ) + array_push($menu_field->$key_field, $value); + } + } + else { + $filter = (object)[]; + $filter->_name = $fields[$name]; + foreach ($fields as $key_field => $value) { + if ($key_field == $name) + continue; + $filter->$key_field = []; + array_push($filter->$key_field, $value); + } + array_push($menu, $filter); + } +} + +function mp_get_filters($events) { + $filters = (object)[]; + $filters->countries = []; + $filters->cities = []; + $filters->categories = []; + + foreach ($events as $event) { + $fields = array( + "countries" => $event->location->country, + "cities" => $event->location->city, + "categories" => $event->categorie, + ); + foreach ($fields as $key => $value) { + mp_fill_name($fields, $key, $filters->$key); + } + } + //usort(, mp_filter_compare); mp_console_log("filters:"); mp_console_log($filters); } +/* + $country = (object)[]; + $country->_name = ""; + $country->cities = []; + $country->categories = []; + + $city = (object)[]; + $city->_name = ""; + $city->countries = []; + $city->categories = []; + + $category = (object)[]; + $category->_name = ""; + $category->countries = []; + $category->cities = []; +*/ + +/* + event : {} + - heure_de_debut : ""; + - heure_de_fin : ""; + - categorie : ""; + - date : ""; + - pays : ""; + - ville : ""; + - adresse : ""; + - prenom : ""; + - nom : ""; + - irl : bool; + - id : x; + - title : ""; + - location : {} + - street : ""; + - city : ""; + - country : ""; + - address : ""; + - approximate : bool; + - coordinates : {} + - lat : x; + - lng : x; +*/ + ?>