| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /**
- * Snippet : AuditExpress_Results
- * Vue "Résultats du diagnostic"
- *
- * Propriétés :
- * &select_resource_id = `4` Pour "Diagnostiquer un autre process"
- * &booking_url = `https://cal.com/augustin.podvin/decouvrir-les-opportunites-suite-a-mon-audit`
- * &devis_url = `mailto:contact@livinweb.fr`
- *
- * L'audit_uid passe en GET (?audit=xxxx) ou est lu depuis la session.
- *
- * POST action : ae_capture_email
- * → sauvegarde l'email dans AuditRequest
- * → génère + envoie le PDF
- * → redirige sur la même page avec ?unlocked=1
- */
- declare(strict_types=1);
- $corePath = $modx->getOption('ae.core_path', null,
- $modx->getOption('core_path') . 'components/audit_express/');
- $modx->addPackage('audit_express', $corePath . 'model/');
- require_once $corePath . 'src/PdfRenderer.php';
- require_once $corePath . 'src/LogMailer.php';
- $selectResourceId = (int) ($scriptProperties['select_resource_id'] ?? $modx->getOption('ae.resource.select', null, 0));
- $bookingUrl = $scriptProperties['booking_url'] ?? $modx->getOption('ae.booking_url', null, '');
- $devisUrl = $scriptProperties['devis_url'] ?? $modx->getOption('ae.devis_url', null, '');
- // ── Charger l'audit ────────────────────────────────────────────────────────
- $auditUid = trim($_GET['audit'] ?? $_SESSION['ae_audit_uid'] ?? '');
- if (!$auditUid) {
- $modx->sendRedirect($selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/');
- return '';
- }
- /** @var AuditRequest|null $auditReq */
- $auditReq = $modx->getObject('AuditRequest', ['audit_uid' => $auditUid]);
- if (!$auditReq || $auditReq->get('status') === 'draft') {
- $modx->sendRedirect($selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/');
- return '';
- }
- // ── POST : capture email + PDF ─────────────────────────────────────────────
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_capture_email') {
- $email = trim(filter_var($_POST['email'] ?? '', FILTER_SANITIZE_EMAIL));
- if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $auditReq->set('email', $email);
- $auditReq->set('status', 'emailed');
- $auditReq->save();
- // Stocker en session pour pré-remplir le formulaire PDF
- $_SESSION['ae_email'] = $email;
- $_SESSION['ae_unlocked_' . $auditUid] = true;
- // Générer et envoyer le PDF (LogMailer V1 = stockage fichier)
- try {
- $packId = $auditReq->get('pack_id');
- $repo = new \AuditExpress\PackRepository($corePath . 'config/');
- $pack = $repo->getPack($packId);
- $result = $auditReq->getResultArray();
- $answers = $auditReq->getAnswersArray();
- $renderer = new \AuditExpress\PdfRenderer(
- $corePath . 'templates/pdf_a4.php',
- $corePath . '../vendor/autoload.php'
- );
- $pdfBytes = $renderer->render($pack, $result, $bookingUrl);
- $mailer = new \AuditExpress\LogMailer($corePath . '../storage/outbox/');
- $mailer->send(
- $email,
- 'Votre diagnostic - Contrôle technique process',
- "Bonjour,\n\nVeuillez trouver ci-joint votre synthèse PDF.\n\nBonne continuation,\nL'équipe Livinweb",
- $pdfBytes,
- 'controle-technique-' . $packId . '.pdf'
- );
- } catch (\Throwable $e) {
- // Ne pas bloquer l'utilisateur si le PDF échoue
- $modx->log(xPDO::LOG_LEVEL_ERROR, '[AuditExpress] PDF error: ' . $e->getMessage());
- }
- }
- $selfUrl = $modx->makeUrl($modx->resource->get('id'), '', 'audit=' . $auditUid . '&unlocked=1', 'full');
- $modx->sendRedirect($selfUrl);
- return '';
- }
- // ── Rendu GET ──────────────────────────────────────────────────────────────
- $r = $auditReq->getResultArray();
- $unlocked = isset($_GET['unlocked']) || ($_SESSION['ae_unlocked_' . $auditUid] ?? false);
- $email = $auditReq->get('email') ?? ($_SESSION['ae_email'] ?? '');
- $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', 'audit=' . $auditUid, 'full');
- $selectUrl = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
- // Niveaux
- $levelColors = ['A' => '#1e8a5e', 'B' => '#1e8a5e', 'C' => '#c07c10', 'D' => '#b83040'];
- $dotColor = $levelColors[$r['level'] ?? ''] ?? '#2F506D';
- // Piliers
- $pillarsHtml = '';
- foreach ($r['pillars'] ?? [] as $p) {
- $pct = $p['max'] ? (int) round(($p['score'] / $p['max']) * 100) : 0;
- $pillarsHtml .= $modx->getChunk('ae_pillar_row', [
- 'label' => htmlspecialchars($p['label'], ENT_QUOTES),
- 'score' => $p['score'],
- 'max' => $p['max'],
- 'pct' => $pct,
- ]);
- }
- // Risques
- $risksHtml = '';
- foreach ($r['risks_top3'] ?? [] as $risk) {
- $risksHtml .= $modx->getChunk('ae_risk_item', [
- 'title' => htmlspecialchars($risk['title'], ENT_QUOTES),
- 'what_happens'=> htmlspecialchars($risk['what_happens'], ENT_QUOTES),
- ]);
- }
- // Quick wins (uniquement si débloqué)
- $winsHtml = '';
- if ($unlocked) {
- foreach ($r['quick_wins_preview'] ?? [] as $win) {
- $winsHtml .= $modx->getChunk('ae_win_item', [
- 'title' => htmlspecialchars($win['title'], ENT_QUOTES),
- 'effect' => htmlspecialchars($win['effect'], ENT_QUOTES),
- 'effort' => htmlspecialchars($win['effort'], ENT_QUOTES),
- ]);
- }
- }
- // Coût caché
- $costHtml = '';
- $cost = $r['cost_hidden'] ?? null;
- if ($cost) {
- $costHtml = $modx->getChunk('ae_cost_hidden', [
- 'hours_per_month' => $cost['hours_per_month'],
- 'eur_per_month' => $cost['eur_per_month'],
- ]);
- }
- // Gate email ou CTAs débloqués
- $gateOrCtaHtml = $unlocked
- ? $modx->getChunk('ae_cta_unlocked', [
- 'booking_url' => htmlspecialchars($bookingUrl, ENT_QUOTES),
- 'devis_url' => htmlspecialchars($devisUrl, ENT_QUOTES),
- 'select_url' => $selectUrl,
- 'action_url' => $actionUrl,
- 'email' => htmlspecialchars($email, ENT_QUOTES),
- 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES),
- ])
- : $modx->getChunk('ae_unlock_gate', [
- 'action_url' => $actionUrl,
- 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES),
- 'risks_html' => $risksHtml,
- 'email' => htmlspecialchars($email, ENT_QUOTES),
- ]);
- return $modx->getChunk('ae_results', [
- 'pack_title' => htmlspecialchars($r['pack_title'] ?? $auditReq->get('pack_id'), ENT_QUOTES),
- 'score_total' => $r['score_total'] ?? 0,
- 'level' => $r['level'] ?? '-',
- 'level_label' => htmlspecialchars($r['level_label'] ?? '', ENT_QUOTES),
- 'dot_color' => $dotColor,
- 'confidence' => htmlspecialchars($r['confidence']['label'] ?? '', ENT_QUOTES),
- 'pillars_html' => $pillarsHtml,
- 'risks_html' => $risksHtml,
- 'wins_html' => $winsHtml,
- 'cost_html' => $costHtml,
- 'gate_html' => $gateOrCtaHtml,
- 'unlocked' => $unlocked ? '1' : '',
- ]);
|