type (ex : group, repeater, taxonomy, text...) */ public static $acf_schema = [ 'nom' => 'text', 'type_de_local' => 'select', 'local_bloque' => 'checkbox', 'commentaire' => 'textarea', ]; public function __construct($data = []) { foreach ($data as $key => $value) { if (property_exists($this, $key)) { $this->$key = $value; } } } public static function load($id, $fields = []) { // Charger depuis CPT/meta $local = get_post($id); if (!$local) { return null; } // Si des champs spécifiques sont demandés, ne charger que ceux-ci if (!empty($fields)) { $data = []; foreach ($fields as $field) { if ($field === 'id') { $data['id'] = $local->ID; } elseif (property_exists(self::class, $field)) { $data[$field] = get_field($field, $local->ID); } } return new self($data); } // Sinon, charger tous les champs par défaut return new self([ 'id' => $local->ID, 'nom' => get_field('nom', $local->ID), 'type_de_local' => get_field('type_de_local', $local->ID), 'capacite' => get_field('capacite', $local->ID), 'local_bloque' => get_field('local_bloque', $local->ID), 'commentaire' => get_field('commentaire', $local->ID), ]); } public function save() { // À implémenter : sauvegarder dans CPT/meta return true; } public function is_disponible($date) { // À implémenter : logique de disponibilité return true; } public function get_relations() { // À implémenter : récupérer les rendez-vous associés, etc. return []; } public static function get_locals($filters = [],$simple_list = false) { $locals = get_posts([ 'post_type' => 'local', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => $filters, ]); if ($simple_list) { $locals = array_map(function($local) { return [ 'id' => $local->ID, 'nom' => $local->post_title, ]; }, $locals); } return $locals; } /** * Retourne les locaux filtrés par type de rendez-vous * @param string $type_rdv 'individuel' ou 'groupe' * @return array Liste des locaux filtrés */ public static function get_locaux_par_type($type_rdv) { $meta_query = []; // Filtrer selon le type de rendez-vous if ($type_rdv === 'individuel') { // Pour les RDV individuels : locaux individuels ou mixtes $meta_query[] = [ 'relation' => 'OR', [ 'key' => 'type_de_local', 'value' => 'individuel', 'compare' => '=' ], [ 'key' => 'type_de_local', 'value' => 'mixte', 'compare' => '=' ] ]; } elseif ($type_rdv === 'groupe') { // Pour les RDV de groupe : locaux de groupe ou mixtes $meta_query[] = [ 'relation' => 'OR', [ 'key' => 'type_de_local', 'value' => 'groupe', 'compare' => '=' ], [ 'key' => 'type_de_local', 'value' => 'mixte', 'compare' => '=' ] ]; } // Exclure les locaux bloqués $meta_query[] = [ 'key' => 'local_bloque', 'value' => '1', 'compare' => '!=' ]; $args = [ 'post_type' => 'local', 'numberposts' => -1, 'post_status' => 'publish', 'meta_query' => $meta_query ]; $locals_posts = get_posts($args); $locaux_filtres = []; foreach ($locals_posts as $local_post) { $type_local = get_post_meta($local_post->ID, 'type_de_local', true); $capacite = get_post_meta($local_post->ID, 'capacite', true); $locaux_filtres[] = [ 'id' => $local_post->ID, 'nom' => $local_post->post_title, 'type_de_local' => $type_local, 'capacite' => $capacite, ]; } return $locaux_filtres; } /** * Créer un local. * @param array $data * @return int|WP_Error */ public function create(array $data) { // Vérifier les permissions (désactivé temporairement pour les tests) // if (!is_user_logged_in() || !get_current_user_id()) { // return Api_Helper::json_error('Authentification requise', 401); // } // if (!current_user_can('edit_posts')) { // return Api_Helper::json_error('Non autorisé', 403); // } // Vérifier que le nom est fourni if (empty($data['nom'])) { return Api_Helper::json_error('Le nom du local est obligatoire', 400); } // Vérifier l'unicité du nom $existing = get_posts([ 'post_type' => 'local', 'meta_key' => 'nom', 'meta_value' => $data['nom'], 'post_status'=> 'any', 'numberposts'=> 1, ]); if ($existing) { return Api_Helper::json_error('Un local avec ce nom existe déjà', 409); } $post_id = wp_insert_post([ 'post_type' => 'local', 'post_title' => $data['nom'], 'post_status' => 'publish', ]); if (is_wp_error($post_id)) { return $post_id; } update_post_meta($post_id, 'nom', $data['nom']); if (!empty($data['type_de_local'])) { update_post_meta($post_id, 'type_de_local', $data['type_de_local']); } if (!empty($data['capacite'])) { update_post_meta($post_id, 'capacite', $data['capacite']); } if (isset($data['local_bloque'])) { update_post_meta($post_id, 'local_bloque', $data['local_bloque']); } if (!empty($data['commentaire'])) { update_post_meta($post_id, 'commentaire', $data['commentaire']); } // ... autres champs return $post_id; } /** * Mettre à jour un local. * @param int $id * @param array $data * @return bool|WP_Error */ public function update(int $id, array $data) { // Vérifier les permissions (désactivé temporairement pour les tests) // if (!is_user_logged_in() || !get_current_user_id()) { // return Api_Helper::json_error('Authentification requise', 401); // } // if (!current_user_can('edit_posts')) { // return Api_Helper::json_error('Non autorisé', 403); // } $post = get_post($id); if (!$post || $post->post_type !== 'local') { return Api_Helper::json_error('Local introuvable', 404); } // Vérifier unicité du nom si modifié if (!empty($data['nom'])) { $existing = get_posts([ 'post_type' => 'local', 'meta_key' => 'nom', 'meta_value' => $data['nom'], 'post_status'=> 'any', 'exclude' => [$id], 'numberposts'=> 1, ]); if ($existing) { return Api_Helper::json_error('Un local avec ce nom existe déjà', 409); } } $result = wp_update_post([ 'ID' => $id, 'post_title' => $data['nom'] ?? get_post_meta($id, 'nom', true), ], true); if (is_wp_error($result)) { return $result; } foreach ($data as $key => $value) { update_post_meta($id, $key, $value); } return true; } /** * Supprimer un local (corbeille WordPress) * @param int $id * @return bool|WP_Error */ public function delete(int $id) { if (!is_user_logged_in() || !get_current_user_id()) { return Api_Helper::json_error('Authentification requise', 401); } if (!current_user_can('delete_posts')) { return Api_Helper::json_error('Non autorisé', 403); } $post = get_post($id); if (!$post || $post->post_type !== 'local') { return Api_Helper::json_error('Local introuvable', 404); } $result = wp_trash_post($id); if (!$result) { return Api_Helper::json_error('Erreur lors de la suppression', 500); } return true; } public static function all() { return get_posts([ 'post_type' => 'local', 'posts_per_page' => -1, 'post_status' => 'publish', ]); } }