AuditExpress_Results.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Snippet : AuditExpress_Results
  4. * Vue "Résultats du diagnostic"
  5. *
  6. * Propriétés :
  7. * &select_resource_id = `4` Pour "Diagnostiquer un autre process"
  8. * &booking_url = `https://calendly.com/livinweb/diagnostic`
  9. * &devis_url = `mailto:contact@livinweb.fr`
  10. *
  11. * L'audit_uid passe en GET (?audit=xxxx) ou est lu depuis la session.
  12. *
  13. * POST action : ae_capture_email
  14. * → sauvegarde l'email dans AuditRequest
  15. * → génère + envoie le PDF
  16. * → redirige sur la même page avec ?unlocked=1
  17. */
  18. declare(strict_types=1);
  19. $corePath = $modx->getOption('ae.core_path', null,
  20. $modx->getOption('core_path') . 'components/audit_express/');
  21. $modx->addPackage('audit_express', $corePath . 'model/');
  22. require_once $corePath . 'src/PdfRenderer.php';
  23. require_once $corePath . 'src/LogMailer.php';
  24. $selectResourceId = (int) ($scriptProperties['select_resource_id'] ?? $modx->getOption('ae.resource.select', null, 0));
  25. $bookingUrl = $scriptProperties['booking_url'] ?? $modx->getOption('ae.booking_url', null, '');
  26. $devisUrl = $scriptProperties['devis_url'] ?? $modx->getOption('ae.devis_url', null, '');
  27. // ── Charger l'audit ────────────────────────────────────────────────────────
  28. $auditUid = trim($_GET['audit'] ?? $_SESSION['ae_audit_uid'] ?? '');
  29. if (!$auditUid) {
  30. $modx->sendRedirect($selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/');
  31. return '';
  32. }
  33. /** @var AuditRequest|null $auditReq */
  34. $auditReq = $modx->getObject('AuditRequest', ['audit_uid' => $auditUid]);
  35. if (!$auditReq || $auditReq->get('status') === 'draft') {
  36. $modx->sendRedirect($selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/');
  37. return '';
  38. }
  39. // ── POST : capture email + PDF ─────────────────────────────────────────────
  40. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_capture_email') {
  41. $email = trim(filter_var($_POST['email'] ?? '', FILTER_SANITIZE_EMAIL));
  42. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  43. $auditReq->set('email', $email);
  44. $auditReq->set('status', 'emailed');
  45. $auditReq->save();
  46. // Stocker en session pour pré-remplir le formulaire PDF
  47. $_SESSION['ae_email'] = $email;
  48. $_SESSION['ae_unlocked_' . $auditUid] = true;
  49. // Générer et envoyer le PDF (LogMailer V1 = stockage fichier)
  50. try {
  51. $packId = $auditReq->get('pack_id');
  52. $repo = new \AuditExpress\PackRepository($corePath . 'config/');
  53. $pack = $repo->getPack($packId);
  54. $result = $auditReq->getResultArray();
  55. $answers = $auditReq->getAnswersArray();
  56. $renderer = new \AuditExpress\PdfRenderer(
  57. $corePath . 'templates/pdf_a4.php',
  58. $corePath . '../vendor/autoload.php'
  59. );
  60. $pdfBytes = $renderer->render($pack, $result, $bookingUrl);
  61. $mailer = new \AuditExpress\LogMailer($corePath . '../storage/outbox/');
  62. $mailer->send(
  63. $email,
  64. 'Votre diagnostic — Contrôle technique process',
  65. "Bonjour,\n\nVeuillez trouver ci-joint votre synthèse PDF.\n\nBonne continuation,\nL'équipe Livinweb",
  66. $pdfBytes,
  67. 'controle-technique-' . $packId . '.pdf'
  68. );
  69. } catch (\Throwable $e) {
  70. // Ne pas bloquer l'utilisateur si le PDF échoue
  71. $modx->log(xPDO::LOG_LEVEL_ERROR, '[AuditExpress] PDF error: ' . $e->getMessage());
  72. }
  73. }
  74. $selfUrl = $modx->makeUrl($modx->resource->get('id'), '', 'audit=' . $auditUid . '&unlocked=1', 'full');
  75. $modx->sendRedirect($selfUrl);
  76. return '';
  77. }
  78. // ── Rendu GET ──────────────────────────────────────────────────────────────
  79. $r = $auditReq->getResultArray();
  80. $unlocked = isset($_GET['unlocked']) || ($_SESSION['ae_unlocked_' . $auditUid] ?? false);
  81. $email = $auditReq->get('email') ?? ($_SESSION['ae_email'] ?? '');
  82. $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', 'audit=' . $auditUid, 'full');
  83. $selectUrl = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
  84. // Niveaux
  85. $levelColors = ['A' => '#1e8a5e', 'B' => '#1e8a5e', 'C' => '#c07c10', 'D' => '#b83040'];
  86. $dotColor = $levelColors[$r['level'] ?? ''] ?? '#2F506D';
  87. // Piliers
  88. $pillarsHtml = '';
  89. foreach ($r['pillars'] ?? [] as $p) {
  90. $pct = $p['max'] ? (int) round(($p['score'] / $p['max']) * 100) : 0;
  91. $pillarsHtml .= $modx->getChunk('ae_pillar_row', [
  92. 'label' => htmlspecialchars($p['label'], ENT_QUOTES),
  93. 'score' => $p['score'],
  94. 'max' => $p['max'],
  95. 'pct' => $pct,
  96. ]);
  97. }
  98. // Risques
  99. $risksHtml = '';
  100. foreach ($r['risks_top3'] ?? [] as $risk) {
  101. $risksHtml .= $modx->getChunk('ae_risk_item', [
  102. 'title' => htmlspecialchars($risk['title'], ENT_QUOTES),
  103. 'what_happens'=> htmlspecialchars($risk['what_happens'], ENT_QUOTES),
  104. ]);
  105. }
  106. // Quick wins (uniquement si débloqué)
  107. $winsHtml = '';
  108. if ($unlocked) {
  109. foreach ($r['quick_wins_preview'] ?? [] as $win) {
  110. $winsHtml .= $modx->getChunk('ae_win_item', [
  111. 'title' => htmlspecialchars($win['title'], ENT_QUOTES),
  112. 'effect' => htmlspecialchars($win['effect'], ENT_QUOTES),
  113. 'effort' => htmlspecialchars($win['effort'], ENT_QUOTES),
  114. ]);
  115. }
  116. }
  117. // Coût caché
  118. $costHtml = '';
  119. $cost = $r['cost_hidden'] ?? null;
  120. if ($cost) {
  121. $costHtml = $modx->getChunk('ae_cost_hidden', [
  122. 'hours_per_month' => $cost['hours_per_month'],
  123. 'eur_per_month' => $cost['eur_per_month'],
  124. ]);
  125. }
  126. // Gate email ou CTAs débloqués
  127. $gateOrCtaHtml = $unlocked
  128. ? $modx->getChunk('ae_cta_unlocked', [
  129. 'booking_url' => htmlspecialchars($bookingUrl, ENT_QUOTES),
  130. 'devis_url' => htmlspecialchars($devisUrl, ENT_QUOTES),
  131. 'select_url' => $selectUrl,
  132. 'action_url' => $actionUrl,
  133. 'email' => htmlspecialchars($email, ENT_QUOTES),
  134. 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES),
  135. ])
  136. : $modx->getChunk('ae_unlock_gate', [
  137. 'action_url' => $actionUrl,
  138. 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES),
  139. 'risks_html' => $risksHtml,
  140. 'email' => htmlspecialchars($email, ENT_QUOTES),
  141. ]);
  142. return $modx->getChunk('ae_results', [
  143. 'pack_title' => htmlspecialchars($r['pack_title'] ?? $auditReq->get('pack_id'), ENT_QUOTES),
  144. 'score_total' => $r['score_total'] ?? 0,
  145. 'level' => $r['level'] ?? '—',
  146. 'level_label' => htmlspecialchars($r['level_label'] ?? '', ENT_QUOTES),
  147. 'dot_color' => $dotColor,
  148. 'confidence' => htmlspecialchars($r['confidence']['label'] ?? '', ENT_QUOTES),
  149. 'pillars_html' => $pillarsHtml,
  150. 'risks_html' => $risksHtml,
  151. 'wins_html' => $winsHtml,
  152. 'cost_html' => $costHtml,
  153. 'gate_html' => $gateOrCtaHtml,
  154. 'unlocked' => $unlocked ? '1' : '',
  155. ]);