@@ -13,70 +13,74 @@ class CertificateExcellence
13
13
private $ templateName ;
14
14
15
15
private $ name_of_certificate_holder ;
16
+
16
17
private $ email_of_certificate_holder ;
18
+
17
19
private $ resource_path ;
20
+
18
21
private $ pdflatex ;
22
+
19
23
private $ personalized_template_name ;
24
+
25
+ private $ event ;
26
+
20
27
private $ id ;
28
+
21
29
private $ edition ;
30
+
22
31
private $ number_of_activities ;
23
- private $ type ;
24
32
25
- public function __construct ($ edition , $ name_for_certificate , $ type = ' excellence ' , $ number_of_activities = 0 )
33
+ public function __construct ($ edition , $ name_for_certificate , $ type , $ number_of_activities )
26
34
{
35
+
27
36
$ this ->edition = $ edition ;
28
37
$ this ->name_of_certificate_holder = $ name_for_certificate ;
29
38
$ this ->email_of_certificate_holder = auth ()->user ()->email ?? '' ;
30
- $ this ->personalized_template_name = $ edition . '- ' . auth ()->id ();
31
- $ this ->resource_path = resource_path () . '/latex ' ;
39
+ $ this ->personalized_template_name = $ edition. '- ' . auth ()->id ();
40
+ $ this ->resource_path = resource_path (). '/latex ' ;
32
41
$ this ->pdflatex = config ('codeweek.pdflatex_path ' );
33
- $ this ->id = auth ()->id () . '- ' . str_random (10 );
42
+ $ this ->id = auth ()->id (). '- ' . str_random (10 );
34
43
$ this ->number_of_activities = $ number_of_activities ;
35
- $ this ->type = $ type ?: 'excellence ' ;
44
+ $ this ->type = $ type ?? 'excellence ' ;
36
45
37
- // e.g. "excellence-2025.tex" or "super-organiser-2025.tex"
38
46
$ this ->templateName = "{$ this ->type }- {$ this ->edition }.tex " ;
39
47
40
- Log::info ('User ID ' . auth ()->id () . " generating {$ this ->type } certificate with name: " . $ name_for_certificate );
48
+ Log::info ('User ID ' . auth ()->id (). " generating {$ this ->type } certificate with name: " . $ name_for_certificate );
41
49
}
42
50
43
- /**
44
- * Generates the certificate PDF, saves to S3, cleans up temp files.
45
- * Returns the S3 path of the generated PDF.
46
- */
47
51
public function generate ()
48
52
{
53
+
49
54
$ this ->customize_and_save_latex ();
50
55
$ this ->run_pdf_creation ();
51
56
$ s3path = $ this ->copy_to_s3 ();
52
57
$ this ->clean_temp_files ();
53
58
54
59
return $ s3path ;
60
+
55
61
}
56
62
57
- /**
58
- * Clean up LaTeX artifacts for the generated file.
59
- */
60
63
private function clean_temp_files ()
61
64
{
62
- Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.aux ' );
63
- Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.tex ' );
64
- Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.pdf ' );
65
- Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.log ' );
65
+ Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.aux ' );
66
+ Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.tex ' );
67
+ Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.pdf ' );
68
+ Storage::disk ('latex ' )->delete ($ this ->personalized_template_name . '.log ' );
66
69
}
67
70
68
- /**
69
- * Check for Greek characters in the name.
70
- */
71
71
public function is_greek ()
72
72
{
73
+
73
74
$ split = preg_split ('/[\p{Greek}]/u ' , $ this ->name_of_certificate_holder );
74
- return (count ($ split ) > 1 );
75
+ if (count ($ split ) > 1 ) {
76
+ // Log::info("Detected as Greek: " . $this->name_of_certificate_holder);
77
+ return true ;
78
+ }
79
+
80
+ return false ;
81
+
75
82
}
76
83
77
- /**
78
- * Escape LaTeX special characters in user data.
79
- */
80
84
private function tex_escape ($ string )
81
85
{
82
86
$ map = [
@@ -92,85 +96,82 @@ private function tex_escape($string)
92
96
'} ' => '\\} ' ,
93
97
];
94
98
95
- return preg_replace_callback (
96
- "/([\^\%~ \\\\# \$%&_\{\}])/ " ,
99
+ return preg_replace_callback ("/([\^\%~ \\\\# \$%&_\{\}])/ " ,
97
100
function ($ matches ) use ($ map ) {
98
101
foreach ($ matches as $ match ) {
99
102
return $ map [$ match ];
100
103
}
101
- },
102
- $ string
103
- );
104
+ }, $ string );
105
+ }
106
+
107
+ protected function update_event ($ s3path )
108
+ {
109
+ $ this ->event ->update ([
110
+ 'certificate_url ' => $ s3path ,
111
+ 'certificate_generated_at ' => Carbon::now (),
112
+ ]);
104
113
}
105
114
106
115
/**
107
- * Read the base template from disk, replace placeholders, and save the .tex file.
116
+ * @throws \League\Flysystem\FileNotFoundException
117
+ */
118
+ protected function copy_to_s3 (): string
119
+ {
120
+ $ inputStream = Storage::disk ('latex ' )->getDriver ()->readStream ($ this ->personalized_template_name .'.pdf ' );
121
+ $ destination = Storage::disk ('s3 ' )->path ('/certificates/ ' .$ this ->id .'.pdf ' );
122
+ Storage::disk ('s3 ' )->put ($ destination , $ inputStream );
123
+
124
+ return Storage::disk ('s3 ' )->url ('certificates/ ' .$ this ->id .'.pdf ' );
125
+ }
126
+
127
+ /**
128
+ * @return mixed
129
+ *
130
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
108
131
*/
109
132
protected function customize_and_save_latex ()
110
133
{
111
- // If the name is Greek, switch to Greek template if it exists:
112
134
if ($ this ->is_greek ()) {
113
135
$ this ->templateName = "{$ this ->type }_greek- {$ this ->edition }.tex " ;
114
136
}
115
-
116
- Log:: info ( " Using template: { $ this -> templateName }" );
137
+ Log:: info ( $ this -> templateName );
138
+ //open the latex template
117
139
$ base_template = Storage::disk ('latex ' )->get ($ this ->templateName );
118
140
119
- // Always replace <CERTIFICATE_HOLDER_NAME> for both excellence & super-organiser
120
- $ template = str_replace (
121
- '<CERTIFICATE_HOLDER_NAME> ' ,
122
- $ this ->tex_escape ($ this ->name_of_certificate_holder ),
123
- $ base_template
124
- );
141
+ //replace the text in template
142
+ $ template = str_replace ('<CERTIFICATE_HOLDER_NAME> ' , $ this ->tex_escape ($ this ->name_of_certificate_holder ), $ base_template );
125
143
126
- // If super-organiser, we also replace these:
127
- if ($ this ->type === 'super-organiser ' ) {
128
- // Possibly also <EDITION> if you want it dynamic
129
- $ template = str_replace ('<NUMBER_OF_ACTIVITIES> ' , $ this ->tex_escape ($ this ->number_of_activities ), $ template );
130
- $ template = str_replace ('<CERTIFICATE_DATE> ' , $ this ->tex_escape (Carbon::now ()->format ('d/m/Y ' )), $ template );
131
- // If you added <CERTIFICATE_EMAIL> or <EDITION> placeholders, handle them as well:
144
+ if ($ this ->type == 'super-organiser ' ) {
132
145
$ template = str_replace ('<CERTIFICATE_EMAIL> ' , $ this ->tex_escape ($ this ->email_of_certificate_holder ), $ template );
133
- $ template = str_replace ('<EDITION> ' , $ this ->edition , $ template );
146
+ $ template = str_replace ('<CERTIFICATE_DATE> ' , $ this ->tex_escape (Carbon::now ()->format ('d/m/Y ' )), $ template );
147
+ $ template = str_replace ('<NUMBER_OF_ACTIVITIES> ' , $ this ->tex_escape ($ this ->number_of_activities ), $ template );
134
148
}
135
149
136
- // For excellence type, you can do other placeholder replacements here if needed.
137
-
138
- // Save updated .tex
139
150
Log::info ($ template );
140
- Storage::disk ('latex ' )->put ($ this ->personalized_template_name . '.tex ' , $ template );
151
+
152
+ //save it locally
153
+ Storage::disk ('latex ' )->put ($ this ->personalized_template_name .'.tex ' , $ template );
141
154
}
142
155
143
- /**
144
- * Compile the .tex file with pdflatex.
145
- */
146
156
protected function run_pdf_creation (): void
147
157
{
148
- $ command = $ this ->pdflatex . ' -interaction=nonstopmode -output-directory ' .
149
- $ this ->resource_path . ' ' .
150
- $ this ->resource_path . '/ ' . $ this ->personalized_template_name . '.tex ' ;
151
158
152
- Log::info ("pdflatex command: $ command " );
159
+ //call the pdflatex command
160
+ $ command = $ this ->pdflatex .' -interaction=nonstopmode -output-directory ' .$ this ->resource_path .' ' .$ this ->resource_path .'/ ' .$ this ->personalized_template_name .'.tex ' ;
161
+
162
+ Log::info ($ command );
163
+
153
164
$ cwd = $ this ->resource_path ;
154
165
166
+ Log::info ($ cwd );
167
+
168
+ // $process = new Process($command, $cwd);
155
169
$ process = Process::fromShellCommandline ($ command , $ cwd );
156
170
$ process ->run ();
157
171
158
- if (!$ process ->isSuccessful ()) {
172
+ // executes after the command finishes
173
+ if (! $ process ->isSuccessful ()) {
159
174
throw new ProcessFailedException ($ process );
160
175
}
161
176
}
162
-
163
- /**
164
- * Copy the resulting PDF to S3, return its S3 URL.
165
- */
166
- protected function copy_to_s3 (): string
167
- {
168
- $ pdfFile = $ this ->personalized_template_name . '.pdf ' ;
169
- $ inputStream = Storage::disk ('latex ' )->getDriver ()->readStream ($ pdfFile );
170
- $ destination = Storage::disk ('s3 ' )->path ('/certificates/ ' . $ this ->id . '.pdf ' );
171
-
172
- Storage::disk ('s3 ' )->put ($ destination , $ inputStream );
173
-
174
- return Storage::disk ('s3 ' )->url ('certificates/ ' . $ this ->id . '.pdf ' );
175
- }
176
177
}
0 commit comments