118 lines
4.7 KiB
PHP
118 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ESI_CRVI_AGENDA\models;
|
|
|
|
/**
|
|
* Modèle générique pour la gestion des imports ACF
|
|
*/
|
|
class Main_Model {
|
|
/**
|
|
* Applique la logique d'import selon le type de champ défini dans $acf_schema
|
|
* @param int $post_id
|
|
* @param string $field
|
|
* @param mixed $value
|
|
* @param array $acf_schema
|
|
*/
|
|
public static function set_acf_field($post_id, $field, $value, $acf_schema) {
|
|
if (!isset($acf_schema[$field])) {
|
|
// Champ inconnu, fallback update_field
|
|
update_field($field, $value, $post_id);
|
|
return;
|
|
}
|
|
$type = $acf_schema[$field];
|
|
switch ($type) {
|
|
case 'taxonomy':
|
|
// Gérer plusieurs valeurs séparées par |
|
|
$terms = is_array($value) ? $value : explode('|', (string)$value);
|
|
$term_ids = [];
|
|
foreach ($terms as $term) {
|
|
$term = trim($term);
|
|
if (!$term) continue;
|
|
$slug = sanitize_title($term);
|
|
$taxonomy = $field; // On suppose que le nom du champ = nom de la taxonomie
|
|
if (!taxonomy_exists($taxonomy)) {
|
|
register_taxonomy($taxonomy, '', ['label' => ucfirst($taxonomy), 'public' => false]);
|
|
}
|
|
$term_obj = get_term_by('slug', $slug, $taxonomy);
|
|
if (!$term_obj) {
|
|
$result = wp_insert_term($term, $taxonomy, ['slug' => $slug]);
|
|
if (!is_wp_error($result) && isset($result['term_id'])) {
|
|
$term_ids[] = $result['term_id'];
|
|
}
|
|
} else {
|
|
$term_ids[] = $term_obj->term_id;
|
|
}
|
|
}
|
|
if (!empty($term_ids)) {
|
|
update_field($field, $term_ids, $post_id);
|
|
}
|
|
break;
|
|
case 'repeater':
|
|
// Valeur attendue : string format pipe ou array d'items
|
|
$rows = [];
|
|
if (is_string($value)) {
|
|
$items = explode('|', $value);
|
|
foreach ($items as $item) {
|
|
$row = [];
|
|
$pairs = explode(';', $item);
|
|
foreach ($pairs as $pair) {
|
|
[$k, $v] = array_pad(explode(':', $pair, 2), 2, null);
|
|
if ($k && $v !== null) {
|
|
if (self::is_date($v)) {
|
|
$v = \DateTime::createFromFormat('d/m/Y', $v);
|
|
$v = $v->format('Y-m-d');
|
|
}
|
|
$row[trim($k)] = trim($v);
|
|
}
|
|
}
|
|
if ($row) $rows[] = $row;
|
|
}
|
|
} elseif (is_array($value)) {
|
|
$rows = $value;
|
|
}
|
|
|
|
/* echo '<pre>';
|
|
print_r($rows);
|
|
echo '</pre>';
|
|
die(); */
|
|
|
|
update_field($field, $rows, $post_id);
|
|
break;
|
|
case 'group':
|
|
// Valeur attendue : array associatif
|
|
if (is_string($value)) {
|
|
$group = [];
|
|
$pairs = explode(';', $value);
|
|
foreach ($pairs as $pair) {
|
|
[$k, $v] = array_pad(explode(':', $pair, 2), 2, null);
|
|
if ($k && $v !== null) $group[trim($k)] = trim($v);
|
|
}
|
|
update_field($field, $group, $post_id);
|
|
} elseif (is_array($value)) {
|
|
update_field($field, $value, $post_id);
|
|
}
|
|
break;
|
|
case 'checkbox':
|
|
// Valeur attendue : string format pipe ou array d'items
|
|
$value = is_array($value) ? $value : explode('|', (string)$value);
|
|
update_field($field, $value, $post_id);
|
|
break;
|
|
case 'date_picker':
|
|
// Valeur attendue : string format d/m/Y
|
|
$value = \DateTime::createFromFormat('d/m/Y', $value);
|
|
$value = $value->format('Y-m-d');
|
|
update_field($field, $value, $post_id);
|
|
break;
|
|
default:
|
|
// Champ simple (text, select, radio, etc.)
|
|
update_field($field, $value, $post_id);
|
|
}
|
|
}
|
|
|
|
public static function is_date($input, $format = 'd/m/Y') {
|
|
$d = \DateTime::createFromFormat($format, $input);
|
|
return $d && $d->format($format) === $input;
|
|
}
|
|
}
|