@@ -13,70 +13,74 @@ class CertificateExcellence
1313 private $ templateName ;
1414
1515 private $ name_of_certificate_holder ;
16+
1617 private $ email_of_certificate_holder ;
18+
1719 private $ resource_path ;
20+
1821 private $ pdflatex ;
22+
1923 private $ personalized_template_name ;
24+
25+ private $ event ;
26+
2027 private $ id ;
28+
2129 private $ edition ;
30+
2231 private $ number_of_activities ;
23- private $ type ;
2432
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 )
2634 {
35+
2736 $ this ->edition = $ edition ;
2837 $ this ->name_of_certificate_holder = $ name_for_certificate ;
2938 $ 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 ' ;
3241 $ this ->pdflatex = config ('codeweek.pdflatex_path ' );
33- $ this ->id = auth ()->id () . '- ' . str_random (10 );
42+ $ this ->id = auth ()->id (). '- ' . str_random (10 );
3443 $ this ->number_of_activities = $ number_of_activities ;
35- $ this ->type = $ type ?: 'excellence ' ;
44+ $ this ->type = $ type ?? 'excellence ' ;
3645
37- // e.g. "excellence-2025.tex" or "super-organiser-2025.tex"
3846 $ this ->templateName = "{$ this ->type }- {$ this ->edition }.tex " ;
3947
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 );
4149 }
4250
43- /**
44- * Generates the certificate PDF, saves to S3, cleans up temp files.
45- * Returns the S3 path of the generated PDF.
46- */
4751 public function generate ()
4852 {
53+
4954 $ this ->customize_and_save_latex ();
5055 $ this ->run_pdf_creation ();
5156 $ s3path = $ this ->copy_to_s3 ();
5257 $ this ->clean_temp_files ();
5358
5459 return $ s3path ;
60+
5561 }
5662
57- /**
58- * Clean up LaTeX artifacts for the generated file.
59- */
6063 private function clean_temp_files ()
6164 {
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 ' );
6669 }
6770
68- /**
69- * Check for Greek characters in the name.
70- */
7171 public function is_greek ()
7272 {
73+
7374 $ 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+
7582 }
7683
77- /**
78- * Escape LaTeX special characters in user data.
79- */
8084 private function tex_escape ($ string )
8185 {
8286 $ map = [
@@ -92,85 +96,82 @@ private function tex_escape($string)
9296 '} ' => '\\} ' ,
9397 ];
9498
95- return preg_replace_callback (
96- "/([\^\%~ \\\\# \$%&_\{\}])/ " ,
99+ return preg_replace_callback ("/([\^\%~ \\\\# \$%&_\{\}])/ " ,
97100 function ($ matches ) use ($ map ) {
98101 foreach ($ matches as $ match ) {
99102 return $ map [$ match ];
100103 }
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+ ]);
104113 }
105114
106115 /**
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
108131 */
109132 protected function customize_and_save_latex ()
110133 {
111- // If the name is Greek, switch to Greek template if it exists:
112134 if ($ this ->is_greek ()) {
113135 $ this ->templateName = "{$ this ->type }_greek- {$ this ->edition }.tex " ;
114136 }
115-
116- Log:: info ( " Using template: { $ this -> templateName }" );
137+ Log:: info ( $ this -> templateName );
138+ //open the latex template
117139 $ base_template = Storage::disk ('latex ' )->get ($ this ->templateName );
118140
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 );
125143
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 ' ) {
132145 $ 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 );
134148 }
135149
136- // For excellence type, you can do other placeholder replacements here if needed.
137-
138- // Save updated .tex
139150 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 );
141154 }
142155
143- /**
144- * Compile the .tex file with pdflatex.
145- */
146156 protected function run_pdf_creation (): void
147157 {
148- $ command = $ this ->pdflatex . ' -interaction=nonstopmode -output-directory ' .
149- $ this ->resource_path . ' ' .
150- $ this ->resource_path . '/ ' . $ this ->personalized_template_name . '.tex ' ;
151158
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+
153164 $ cwd = $ this ->resource_path ;
154165
166+ Log::info ($ cwd );
167+
168+ // $process = new Process($command, $cwd);
155169 $ process = Process::fromShellCommandline ($ command , $ cwd );
156170 $ process ->run ();
157171
158- if (!$ process ->isSuccessful ()) {
172+ // executes after the command finishes
173+ if (! $ process ->isSuccessful ()) {
159174 throw new ProcessFailedException ($ process );
160175 }
161176 }
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- }
176177}
0 commit comments