type (ex : group, repeater, taxonomy, text...) */ public static $acf_schema = [ 'nom' => 'text', 'type_dinterventions' => 'repeater', 'actif' => 'true_false', ]; /** * Charge un département par ID (CPT) * @param int $id * @param array $fields Champs spécifiques à charger (optionnel) * @return self|null */ public static function load($id, $fields = []) { $post = get_post($id); if (!$post || $post->post_type !== 'departement') { 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'] = $post->ID; } elseif ($field === 'nom') { $data['nom'] = $post->post_title; } elseif ($field === 'actif') { // Charger le champ ACF 'actif' $data['actif'] = function_exists('get_field') ? get_field('actif', $post->ID) : true; } elseif (isset(self::$acf_schema[$field])) { $data[$field] = function_exists('get_field') ? get_field($field, $post->ID) : null; } } return new self($data); } // Sinon, retourner le post complet (comportement par défaut) return $post; } /** * Retourne la liste de tous les départements (CPT) * @param bool $simple_list Si true, retourne uniquement id et nom * @param bool $only_active Si true (défaut), ne retourne que les départements actifs */ public static function all($simple_list = false, $only_active = true) { $posts = get_posts([ 'post_type' => 'departement', 'posts_per_page' => -1, 'post_status' => 'publish', ]); // Filtrer par statut actif si demandé if ($only_active && function_exists('get_field')) { $posts = array_filter($posts, function($post) { $actif = get_field('actif', $post->ID); // Si le champ n'existe pas encore, considérer le département comme actif par défaut return ($actif === null || $actif === true || $actif === 1 || $actif === '1'); }); } if ($simple_list) { $posts = array_map(function($post) { return [ 'id' => $post->ID, 'nom' => $post->post_title, ]; }, $posts); } return $posts; } //crete or update if exist //check if nom is unique public static function create(array $data, bool $as_rest = false) { $is_update = false; // 1. Vérifier les droits if (!current_user_can('edit_posts')) { if ($as_rest) { return [ 'success' => false, 'code' => 403, 'message' => 'Non autorisé', 'data' => null ]; } return false; } // 2. Valider les données (nom obligatoire) if (empty($data['nom'])) { if ($as_rest) { return [ 'success' => false, 'code' => 400, 'message' => 'Le nom du département est obligatoire', 'data' => null ]; } return false; } // 3. Vérifier si le département existe déjà (unicité nom) $existing = get_posts([ 'post_type' => 'departement', 'meta_key' => 'nom', 'meta_value' => $data['nom'], 'post_status'=> 'any', 'numberposts'=> 1, ]); if ($existing) { $is_update = true; // Mise à jour $post_id = $existing[0]->ID ?? $existing[0]; wp_update_post([ 'ID' => $post_id, 'post_title' => $data['nom'], ]); foreach ($data as $key => $value) { if (isset(self::$acf_schema[$key])) { self::set_acf_field($post_id, $key, $value, self::$acf_schema); } else { update_post_meta($post_id, $key, $value); } } /* if ($as_rest) { return [ 'success' => true, 'code' => 200, 'message' => 'Département mis à jour', 'data' => [ 'id' => $post_id ] ]; } */ /* return 'updated'; */ } else { // Création $post_id = wp_insert_post([ 'post_type' => 'departement', 'post_title' => $data['nom'], 'post_status' => 'publish', ]); if (is_wp_error($post_id)) { if ($as_rest) { return [ 'success' => false, 'code' => 500, 'message' => 'Erreur lors de la création', 'data' => null ]; } return false; } foreach ($data as $key => $value) { update_post_meta($post_id, $key, $value); } /* if ($as_rest) { return [ 'success' => true, 'code' => 201, 'message' => 'Département créé', 'data' => [ 'id' => $post_id ] ]; } */ /* return 'created'; */ } // 4. Créer le type d'intervention associé $type_interventions = $data['types-dintervention'] ?? []; if (!is_array($type_interventions)) { if (strstr($type_interventions, '|')) { $type_interventions = explode('|', $type_interventions); } else { $type_interventions = [$type_interventions]; } } //delete all type_intervention_id meta //acf type_dinterventions -> repeater foreach ($type_interventions as $type_intervention) { $type_intervention_id = CRVI_Type_Intervention_Model::create([ 'post_title' => $type_intervention, ]); $type_interventions_ar[] = [ 'type' => $type_intervention, 'article_intervention' => $type_intervention_id ]; self::set_acf_field($post_id, 'type_dinterventions', $type_interventions_ar, self::$acf_schema); } if ($as_rest) { return [ 'success' => true, 'code' => 201, 'message' => 'Département créé', 'data' => [ 'id' => $post_id ] ]; } return $is_update ? 'updated' : 'created'; } }