<?php

function mp_filter_compare($a, $b) {
	return strcmp($a->_name, $b->_name);
}

function mp_already_in_menu(&$menu, $name) {
	foreach ($menu as $field) {
		if ($field->_name == $name)
			return $field;
	}
	return null;
}

/*
	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 (! isset($menu_field->$key_field) )
				$menu_field->$key_field = [];
			if (strlen($value) != 0) {
				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 = [];
			if (strlen($value) != 0)
				array_push($filter->$key_field, $value);
		}
		array_push($menu, $filter);
	}
}

function mp_get_filters($events) {
	$filters = (object)[];

	foreach ($events as $event) {
		$fields = array(
			"pays"			=> $event->location->country,
			"villes"		=> $event->location->city,
			"categories"	=> $event->categorie,
			"mode"			=> ($event->irl)? "En présentiel" : "En ligne",
		);
		foreach ($fields as $key => $value) {
			if (! isset($filters->$key))
				$filters->$key = [];
			mp_fill_name($fields, $key, $filters->$key);
		}
	}

	foreach ($filters as $key => $value) {
		usort($filters->$key, mp_filter_compare);
	}

	return $filters;
}

/*
         {                                                 }
         {                [                             ]  }
         {                [ {  - _name     : ""  }      ]  }
         {  - countries : [ {  - cities    : []  }, ... ]  }
         {                [ {  - categories: []  }      ]  }
         {                [                             ]  }
         {                                                 }
         {                [                             ]  }
         {                [ {  - _name     : ""  }      ]  }
filters: {  - cities    : [ {  - countries : []  }, ... ]  }
         {                [ {  - categories: []  }      ]  }
         {                [                             ]  }
         {                                                 }
         {                [                             ]  }
         {                [ {  - _name     : ""  }      ]  }
         {  - categories: [ {  - countries : []  }, ... ]  }
         {                [ {  - 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;
*/

?>
