From d8053ac82e95721542f025cb5833655875aaafb7 Mon Sep 17 00:00:00 2001 From: Valentin Le Moign Date: Thu, 28 May 2026 17:04:20 +0200 Subject: [PATCH] =?UTF-8?q?Import=20unitaire,=20statut=20publi=C3=A9/pendi?= =?UTF-8?q?ng=20et=20validation=20des=20idHAL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-admin-page.php | 212 ++++++++++++++++++++++++++++++---- includes/class-hal-api.php | 43 +++++++ includes/class-importer.php | 16 +++ 3 files changed, 246 insertions(+), 25 deletions(-) diff --git a/includes/class-admin-page.php b/includes/class-admin-page.php index ae1492c..b2a3ec7 100644 --- a/includes/class-admin-page.php +++ b/includes/class-admin-page.php @@ -81,6 +81,10 @@ class Thalim_HAL_Admin_Page { $this->handle_import(); } + if ($action === 'import_single') { + $this->handle_import_single(); + } + if (self::CSV_IMPORT_ENABLED) { if ($action === 'csv_upload') $this->handle_csv_upload(); if ($action === 'csv_batch') $this->handle_csv_batch(); @@ -116,6 +120,7 @@ class Thalim_HAL_Admin_Page { $imported = 0; $skipped = 0; $errors = []; + $cache_updates = []; // hal_id => ['id', 'status'] for cache mutation foreach ($raw_docs as $doc) { $hal_id = $doc['halId_s'] ?? ''; @@ -132,9 +137,12 @@ class Thalim_HAL_Admin_Page { $errors[] = $hal_id . ': ' . $post_id->get_error_message(); } else { $imported++; + $cache_updates[$hal_id] = ['id' => $post_id, 'status' => 'pending']; } } + $this->update_preview_cache_after_import($date_from, $date_to, $author_hal_id, $cache_updates); + $msg = sprintf('%d publication(s) importée(s) en statut "En attente".', $imported); if ($skipped) $msg .= sprintf(' %d ignorée(s) (déjà importées ou sans membre THALIM correspondant).', $skipped); if (!empty($errors)) $msg .= ' Erreurs : ' . implode('; ', $errors); @@ -142,6 +150,61 @@ class Thalim_HAL_Admin_Page { $this->message = [empty($errors) ? 'success' : 'warning', $msg]; } + /** + * Import a single ready publication. hal_id + filter values are POSTed + * from the per-row form so the cache lookup hits the right preview entry. + */ + private function handle_import_single() { + $hal_id = sanitize_text_field($_POST['hal_id'] ?? ''); + $date_from = sanitize_text_field($_POST['hal_date_from'] ?? ''); + $date_to = sanitize_text_field($_POST['hal_date_to'] ?? ''); + $author_hal_id = sanitize_text_field($_POST['hal_author_id'] ?? ''); + + if (!$hal_id) { + $this->message = ['error', 'hal_id manquant.']; + return; + } + + $preview = $this->get_preview_data($date_from, $date_to, $author_hal_id); + if (is_wp_error($preview)) { + $this->message = ['error', 'API Error: ' . $preview->get_error_message()]; + return; + } + + $doc = null; + foreach ($preview['raw_docs'] ?? [] as $d) { + if (($d['halId_s'] ?? '') === $hal_id) { $doc = $d; break; } + } + if (!$doc) { + $this->message = ['warning', "Publication $hal_id introuvable dans le tableau (cache expiré ?). Rafraîchir et réessayer."]; + return; + } + + $importer = new Thalim_HAL_Importer_Logic(); + if ($importer->is_imported($hal_id)) { + $this->message = ['warning', "Publication $hal_id déjà importée."]; + return; + } + + $this->load_wp_users_hal_ids(); + if (empty($this->match_authors_to_users($doc['authIdHal_s'] ?? []))) { + $this->message = ['error', "Aucun membre THALIM identifié pour $hal_id."]; + return; + } + + $post_id = $importer->import($doc, $this->wp_users_by_hal_id); + if (is_wp_error($post_id)) { + $this->message = ['error', "Erreur import : " . $post_id->get_error_message()]; + return; + } + + $this->update_preview_cache_after_import($date_from, $date_to, $author_hal_id, [ + $hal_id => ['id' => $post_id, 'status' => 'pending'], + ]); + + $this->message = ['success', sprintf('Publication %s importée (post #%d, en attente).', $hal_id, $post_id)]; + } + private function render_styles() { ?>