107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Libraries\Radar\RadarSyncService;
|
|
use App\Models\RadarModel;
|
|
use Config\Radar as RadarConfig;
|
|
|
|
/**
|
|
* IDEE-5 / MSOP-2 — proto écran Radar (CI4 / Raven).
|
|
* MSOP-4 — sync Gmail allowlist.
|
|
*/
|
|
class Radar extends BaseController
|
|
{
|
|
public function index()
|
|
{
|
|
$model = new RadarModel();
|
|
$ready = $model->tablesExist();
|
|
$cfg = config(RadarConfig::class);
|
|
|
|
// Flash session souvent perdu sous /v4/ — fallback query string.
|
|
$syncMsg = session()->getFlashdata('radar_sync_msg');
|
|
if ($syncMsg === null || $syncMsg === '') {
|
|
$syncMsg = $this->request->getGet('sync_msg');
|
|
}
|
|
|
|
return view('radar/index', [
|
|
'ready' => $ready,
|
|
'exceptions' => $ready ? $model->listExceptions() : [],
|
|
'threads' => $ready ? $model->listThreads() : [],
|
|
'feedUrl' => site_url('v4/radar/feed'),
|
|
'syncUrl' => site_url('v4/radar/sync'),
|
|
'allowlist' => $cfg->mailboxAllowlist,
|
|
'syncMsg' => is_string($syncMsg) ? $syncMsg : null,
|
|
]);
|
|
}
|
|
|
|
public function feed()
|
|
{
|
|
$model = new RadarModel();
|
|
if (! $model->tablesExist()) {
|
|
return $this->response->setJSON([
|
|
'ok' => false,
|
|
'threads' => [],
|
|
]);
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'ok' => true,
|
|
'at' => date('c'),
|
|
'threads' => $model->listThreads(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Sync Gmail → radar_* (mailbox allowlist uniquement).
|
|
*/
|
|
public function sync()
|
|
{
|
|
$result = (new RadarSyncService())->sync();
|
|
|
|
if ($this->request->isAJAX() || $this->request->getGet('json') !== null) {
|
|
return $this->response->setJSON($result);
|
|
}
|
|
|
|
$msg = $result['ok'] ? 'Sync OK' : 'Sync en erreur';
|
|
if (! empty($result['error'])) {
|
|
$msg .= ' — ' . $result['error'];
|
|
}
|
|
foreach ($result['mailboxes'] as $row) {
|
|
if (! empty($row['ok'])) {
|
|
$msg .= sprintf(
|
|
' | %s: +%d / ~%d (lus %d)',
|
|
$row['mailbox'],
|
|
$row['inserted'] ?? 0,
|
|
$row['updated'] ?? 0,
|
|
$row['fetched'] ?? 0
|
|
);
|
|
} else {
|
|
$msg .= ' | ' . ($row['mailbox'] ?? '?') . ': ' . ($row['error'] ?? 'fail');
|
|
}
|
|
}
|
|
if (! empty($result['ai'])) {
|
|
$ai = $result['ai'];
|
|
if (! empty($ai['enabled'])) {
|
|
$msg .= sprintf(
|
|
' | IA dossards: %d analysé(s)',
|
|
(int) ($ai['ran'] ?? 0)
|
|
);
|
|
if (! empty($ai['errors'])) {
|
|
$msg .= ', ' . (int) $ai['errors'] . ' erreur(s)';
|
|
}
|
|
if (! empty($ai['note']) && (int) ($ai['errors'] ?? 0) > 0) {
|
|
$msg .= ' (' . \mb_substr((string) $ai['note'], 0, 120) . ')';
|
|
}
|
|
} else {
|
|
$msg .= ' | IA: ' . ($ai['note'] ?: 'off');
|
|
}
|
|
}
|
|
|
|
// Message aussi en query : visible même si la flash session CI4 ne tient pas.
|
|
$q = rawurlencode(mb_substr($msg, 0, 800));
|
|
|
|
return redirect()->to(site_url('v4/radar') . '?sync_msg=' . $q)->with('radar_sync_msg', $msg);
|
|
}
|
|
}
|