|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\CertificateExcellence; |
| 6 | +use App\Excellence; |
| 7 | +use Illuminate\Console\Command; |
| 8 | + |
| 9 | +class CertificateRegenerateGreekSuperOrganiser extends Command |
| 10 | +{ |
| 11 | + protected $signature = 'certificate:regenerate-greek-super-organiser |
| 12 | + {--edition=2025 : Edition year} |
| 13 | + {--dry-run : Only count and list, do not regenerate}'; |
| 14 | + |
| 15 | + protected $description = 'Regenerate PDFs only for Greek Super Organisers (fix garbled “coding activities” text). Does not resend email.'; |
| 16 | + |
| 17 | + public function handle(): int |
| 18 | + { |
| 19 | + $edition = (int) $this->option('edition'); |
| 20 | + $dryRun = (bool) $this->option('dry-run'); |
| 21 | + |
| 22 | + $query = Excellence::query() |
| 23 | + ->where('edition', $edition) |
| 24 | + ->where('type', 'SuperOrganiser') |
| 25 | + ->whereNotNull('certificate_url') |
| 26 | + ->with('user') |
| 27 | + ->orderBy('id'); |
| 28 | + |
| 29 | + $rows = $query->get(); |
| 30 | + $greekRows = $rows->filter(function (Excellence $e) { |
| 31 | + $name = $e->name_for_certificate ?? ($e->user ? trim(($e->user->firstname ?? '') . ' ' . ($e->user->lastname ?? '')) : ''); |
| 32 | + return $this->nameContainsGreek($name); |
| 33 | + }); |
| 34 | + |
| 35 | + $total = $greekRows->count(); |
| 36 | + if ($total === 0) { |
| 37 | + $this->info("No Greek Super Organiser certificates found for edition {$edition} (with existing certificate_url)."); |
| 38 | + return self::SUCCESS; |
| 39 | + } |
| 40 | + |
| 41 | + $this->info("Found {$total} Greek Super Organiser certificate(s) for edition {$edition}."); |
| 42 | + if ($dryRun) { |
| 43 | + $this->line('Dry run: no regeneration. Sample:'); |
| 44 | + $greekRows->take(10)->each(fn (Excellence $e) => $this->line(' ' . ($e->user?->email ?? '?') . ' — ' . ($e->name_for_certificate ?? $e->user?->firstname . ' ' . $e->user?->lastname))); |
| 45 | + if ($total > 10) { |
| 46 | + $this->line(' ... and ' . ($total - 10) . ' more.'); |
| 47 | + } |
| 48 | + return self::SUCCESS; |
| 49 | + } |
| 50 | + |
| 51 | + $this->info('Regenerating PDFs only (certificate_url will be updated; no email sent).'); |
| 52 | + $bar = $this->output->createProgressBar($total); |
| 53 | + $bar->start(); |
| 54 | + |
| 55 | + $ok = 0; |
| 56 | + $failed = 0; |
| 57 | + $failures = []; |
| 58 | + |
| 59 | + foreach ($greekRows as $excellence) { |
| 60 | + $bar->advance(); |
| 61 | + $user = $excellence->user; |
| 62 | + if (! $user) { |
| 63 | + $failed++; |
| 64 | + $failures[] = ['id' => $excellence->id, 'email' => '-', 'error' => 'User missing']; |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $name = $excellence->name_for_certificate ?? trim(($user->firstname ?? '') . ' ' . ($user->lastname ?? '')); |
| 69 | + $name = $name !== '' ? $name : 'Unknown'; |
| 70 | + $numberOfActivities = (int) $user->activities($edition); |
| 71 | + |
| 72 | + try { |
| 73 | + $cert = new CertificateExcellence( |
| 74 | + $edition, |
| 75 | + $name, |
| 76 | + 'super-organiser', |
| 77 | + $numberOfActivities, |
| 78 | + (int) $user->id, |
| 79 | + (string) ($user->email ?? '') |
| 80 | + ); |
| 81 | + // Overwrite existing S3 file so the same URL serves the new PDF (no need to resend email) |
| 82 | + $url = $cert->generateReplacing($excellence->certificate_url); |
| 83 | + $excellence->update([ |
| 84 | + 'certificate_url' => $url, |
| 85 | + 'certificate_generation_error' => null, |
| 86 | + ]); |
| 87 | + $ok++; |
| 88 | + } catch (\Throwable $e) { |
| 89 | + $failed++; |
| 90 | + $excellence->update(['certificate_generation_error' => $e->getMessage()]); |
| 91 | + $failures[] = ['id' => $excellence->id, 'email' => $user->email ?? '-', 'error' => $e->getMessage()]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $bar->finish(); |
| 96 | + $this->newLine(2); |
| 97 | + $this->info("Done. Regenerated: {$ok}, Failed: {$failed}. No emails sent; existing certificate links now point to the new PDFs."); |
| 98 | + if ($failed > 0 && count($failures) > 0) { |
| 99 | + $this->table(['id', 'email', 'error'], array_slice($failures, 0, 20)); |
| 100 | + if (count($failures) > 20) { |
| 101 | + $this->line('(First 20 failures shown.)'); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return $failed > 0 ? self::FAILURE : self::SUCCESS; |
| 106 | + } |
| 107 | + |
| 108 | + private function nameContainsGreek(string $name): bool |
| 109 | + { |
| 110 | + $split = preg_split('/[\p{Greek}]/u', $name); |
| 111 | + return $split !== false && count($split) > 1; |
| 112 | + } |
| 113 | +} |
0 commit comments