<?php

/*
	FORMS FIELDS

	 0: "engagement"
	 1: "fichier"
	 2: "liste_des_depenses"
	 3: "montant_demande"
	 4: "recettes_prevues"
	 5: "depenses_prevues"
	 6: "participation"
	 7: "plan_de_communication"
	 8: "composition_autres"
	 9: "composition_4"
	10: "composition_3"
	11: "composition_2"
	12: "composition_1"
	13: "composition"
	14: "resultats_attendus"
	15: "public_vise"
	16: "activite_concerne"
	17: "description_longue"
	18: "description_courte"
---	19: "categorie"
	20: "se_connecter"
	21: "lien_internet"
	22: "adresse_courriel_de_contact"
---	23: "heure_de_fin"
---	24: "heure_de_debut"
---	25: "date"
	26: "map"
---	27: "pays"
---	28: "ville"
---	29: "adresse"
	30: "mode"
	31: "fonction"
	32: "institution"
	33: "telephone"
	34: "adresse_courriel"
---	35: "prenom"
---	36: "nom"

	ADDED BY THIS PLUGIN

---	37: "coordinates"
---	38: "coordinates_valid"
*/

/*
	GET POST EXEMPLE :

	ID: 29693
	comment_count: "0"
	comment_status: "closed"
	filter: "raw"
	guid: "https://local_lejourduprof.com/?p=29693"
	menu_order: 0
	ping_status: "closed"
	pinged: ""
	post_author: "1"
	post_content: ""
	post_content_filtered: ""
	post_date: "2022-11-04 18:05:49"
	post_date_gmt: "2022-11-04 17:05:49"
	post_excerpt: "Les enseignants vont présenter les projets développés dans leurs classes et partager leurs pratiques et expériences."
	post_mime_type: ""
	post_modified: "2022-11-05 09:39:46"
	post_modified_gmt: "2022-11-05 08:39:46"
	post_name: "construisons-ensemble-lavenir"
	post_parent: 0
	post_password: ""
	post_status: "draft"
	post_title: "Construisons ensemble l'avenir"
	post_type: "post"
	to_ping: ""
*/

function mp_get_published_posts() {

	$get_posts_args = array(
		'numberposts' => -1,
		'post_status' => 'publish',
		'post_type'   => 'post',
	);
	$posts_published = get_posts($get_posts_args);

	return $posts_published;
}

function mp_fill_fields_value($id) {

	/*
	 * get_field is an ACF function
	 * in "pure" worpdress use :
	 * get_post_meta or get_post_custom_values
	 * - https://developer.wordpress.org/reference/functions/get_post_meta/
	 * - https://developer.wordpress.org/reference/functions/get_post_custom_values/
	 */

	// add fields
	$fields = array(
		"heure_de_debut",
		"heure_de_fin",
		"categorie",
		"date",
		"pays",
		"ville",
		"adresse",
		"prenom",
		"nom",
		"test",
		"location",
	);
	$event = (object)[];
	foreach($fields as $field) {
		$value = get_field($field, $id);
		if ($value === "↓")
			$value = "Autre";
		if (gettype($value) === "string")
			$value = trim($value, " ");
		$event->$field = $value;
	}

	// add mode irl or online (irl: true | false)
	$presentiel = get_field("mode", $id);
	$event->irl = false;
	if ($presentiel[0] === "En présentiel")
		$event->irl = true;

	// add post url
	$event->url = get_post_permalink($id);

	return $event;
}

function mp_get_published_events() {
	$posts_list = mp_get_published_posts();
	$events = [];
	foreach ($posts_list as $post) {
		$event = mp_fill_fields_value($post->ID);
		$event->id = $post->ID;
		$event->title = trim($post->post_title, " ");
		$event->index = null;
		array_push($events, $event);
	}
	return $events;
}

/*
	event					: {}
		- heure_de_debut	: "";
		- heure_de_fin		: "";
		- categorie			: "";
		- date				: "";
		- pays				: "";
		- ville				: "";
		- adresse			: "";
		- prenom			: "";
		- nom				: "";
		- irl				: bool;
		- id				: x;
		- index				: x (default null);
		- title				: "";
		- url				: "";
		- location			: {}
			- street		: "";
			- city			: "";
			- country		: "";
			- address		: "";
			- approximate	: bool;
			- coordinates	: {}
				- lat		: x;
				- lng		: x;
*/


?>
