Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e464738
Exports student applications to CSV
betsyecastro Apr 8, 2025
5a0bb3d
Changes `toCsv()` macro from Builder facade to Collection
betsyecastro Apr 11, 2025
b78eaf2
Corrects value returned by the exportStudentApps() method
betsyecastro Apr 14, 2025
c81a9c3
Styles 'Export' button, renames the 'export' method to 'exportToCsv' …
betsyecastro Apr 14, 2025
85caada
Adds student applications PDF export feature
betsyecastro May 6, 2025
74df4ec
Adds export menu Livewire component
betsyecastro May 6, 2025
fd70896
Merge branch 'allow-bulk-export-andor-view-of-student-applications' i…
betsyecastro May 6, 2025
a56c355
Adds download menu and PDF export option for student applications
betsyecastro May 19, 2025
8b5e69d
Removes unnecessary files
betsyecastro May 19, 2025
d9e4782
Generates student apps pdf file in the background
betsyecastro Sep 3, 2025
4d9dcbe
Adds an interface to support multiple PDF generation service implemen…
betsyecastro Sep 16, 2025
7f8385a
Removes unnecessary files and clean up
betsyecastro Sep 16, 2025
da2ffa8
Refactoring download request and download routes for generic authoriz…
betsyecastro Sep 22, 2025
7dfaad4
Refactoring auth and moving file download endpoint to download via Li…
betsyecastro Sep 25, 2025
c781a47
Adds single student app download
betsyecastro Sep 25, 2025
3e2984b
Fixes filter summary
betsyecastro Sep 25, 2025
4aa0c6a
downloadAsPdf refactoring and add HasPdfDownloads trait
betsyecastro Sep 25, 2025
9b3f3de
Removes unnecessary event listener
betsyecastro Sep 25, 2025
3a421e7
Merge branch 'develop' into bulk-export-student-apps-pdf
betsyecastro Nov 24, 2025
82534b5
Resolve conflicts related to Nov 2025 NPM and composer updates
betsyecastro Nov 24, 2025
a13215e
Update .env.example to add pdf and queue options
betsyecastro Dec 1, 2025
ff242da
Remove unnecessary listener and fix filing_status initialization
betsyecastro Dec 5, 2025
f15bc65
Move logic to get selected filters to HasFilters trait
betsyecastro Dec 5, 2025
6b0f7e0
Fix logic to refresh students property
betsyecastro Dec 5, 2025
c8de2bd
Fix returned value for empty $students collection
betsyecastro Dec 5, 2025
f4d5da4
Add filing_status property to ProfileStudentsDownloadMenu
betsyecastro Dec 8, 2025
75bf162
Fix psalm errors
betsyecastro Dec 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync
IMAGE_DRIVER=gd
PDF_DRIVER=

IMAGE_MAX_MB=10

Expand Down Expand Up @@ -92,6 +93,7 @@ ENABLE_STUDENTS=false

# Export profiles to PDF (requires exporter installation)
ENABLE_PDF=false
ENABLE_AUTO_CLEAN_PDFS_DIR=false
# CHROME_PATH="/path/to/my/chrome"
# CHROME_ARGS="{\"font-render-hinting\": \"none\"}"
# NODE_PATH="/path/to/my/node"
Expand Down
85 changes: 85 additions & 0 deletions app/Console/Commands/CleanUpPdfFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class CleanUpPdfFiles extends Command
{
protected $signature = 'files:clean-up-pdfs
{--root=tmp/reports : Root directory where timestamped folders live}
{--hours=2 : Delete folders older than this many hours}
{--disk= : Filesystem disk to use (defaults to FILESYSTEM_DISK)}
{--dry : Show what would be deleted, but don\'t delete}';

protected $description = 'Delete temporary PDF folders (Ymd_Hi format) older than the specified number of hours. Default: 2 hours.';

public function handle(): int
{
$disk_name = $this->option('disk') ?: config('filesystems.default');
$disk = Storage::disk($disk_name);

$root = rtrim($this->option('root') ?? 'tmp/reports', '/');
$hours_opt = (string) ($this->option('hours') ?? '2');


if (!ctype_digit($hours_opt)) { // validate hours
$this->error("Invalid --hours value: {$hours_opt}. Use a non-negative integer.");
return self::FAILURE;
}
$hours = (int) $hours_opt;

$now = now()->second(0);
$cutoff = $now->copy()->subHours($hours);

if (!$disk->exists($root)) {
$this->info("Root '{$root}' does not exist on disk '{$disk_name}'. Nothing to do.");
return self::SUCCESS;
}

// Only consider folders named Ymd_Hi (e.g., 20250826_1430)
$dirs = collect($disk->directories($root))
->filter(fn ($path) => (bool) preg_match('~^' . preg_quote($root, '~') . '/\d{8}_\d{4}$~', $path))
->sort()
->values();

if ($dirs->isEmpty()) {
$this->info("No timestamped folders found under '{$root}'.");
return self::SUCCESS;
}

$dry = (bool) $this->option('dry');
$deleted = 0; $skipped = 0;

foreach ($dirs as $dir) {
$stamp = basename($dir);

// Parse folder timestamp; skip if malformed
$bucket_time = Carbon::createFromFormat('Ymd_Hi', $stamp, $now->timezone)->second(0);
if ($bucket_time === false) {
$this->warn("Skipping malformed folder name: {$dir}");
$skipped++;
continue;
}

if ($bucket_time->lt($cutoff)) {
if ($dry) {
$this->line("[DRY] Would delete: {$dir} ({$bucket_time->toDateTimeString()})");
} else {
$disk->deleteDirectory($dir);
$this->line("Deleted: {$dir} ({$bucket_time->toDateTimeString()})");
}
$deleted++;
} else {
$skipped++;
}
}

$summary = $dry ? 'would delete' : 'deleted';
$this->info("Cleanup complete: {$summary} {$deleted}, kept {$skipped}. Disk='{$disk_name}', Root='{$root}', Cutoff='{$cutoff->toDateTimeString()}'.");

return self::SUCCESS;
}
}
7 changes: 7 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ protected function schedule(Schedule $schedule)
$schedule->command('profiles:update-orcid')->weekly()->sundays()->at('05:00')->when(function() {
return config('app.enable_orcid_update');
});
$schedule
->command('files:clean-up-pdfs --root=tmp/reports --hours=2')
->everyTwoHours()
->withoutOverlapping()
->when(function() {
return config('app.enable_auto_clean_pdfs_dir');
});
}

/**
Expand Down
81 changes: 81 additions & 0 deletions app/Helpers/BrowsershotPdfHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Helpers;

use App\Helpers\Contracts\PdfGenerationHelperContract;
use App\PdfGenerationResult;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
use Illuminate\Support\Str;

class BrowsershotPdfHelper implements PdfGenerationHelperContract
{
public function generate(array $payload): PdfGenerationResult
{
$filename = $payload['filename'] ?? '';
$data = $payload['data'] ?? '';
$view = $payload['view'] ?? '';

$folder_timestamp = $this->currentReportBucket();
$dir = "tmp/reports/{$folder_timestamp}";

$file_timestamp = Carbon::now()->addMinutes(30)->format('YmdHis');
$filename = "{$filename}_{$file_timestamp}.pdf";

$storage_path = "$dir/$filename";
$absolute_path = Storage::path($storage_path);

Storage::makeDirectory($dir);

$html = '';
$html .= view($view, $data)->render();

$content = Browsershot::html($html)
->waitUntilNetworkIdle()
->ignoreHttpsErrors()
->margins(30, 15, 30, 15);

if (config('pdf.node')) {
$content = $content->setNodeBinary(config('pdf.node'));
}

if (config('pdf.npm')) {
$content = $content->setNpmBinary(config('pdf.npm'));
}

if (config('pdf.modules')) {
$content = $content->setIncludePath(config('pdf.modules'));
}

if (config('pdf.chrome')) {
$content = $content->setChromePath(config('pdf.chrome'));
}

if (config('pdf.chrome_arguments')) {
$content = $content->addChromiumArguments(config('pdf.chrome_arguments'));
}

$content->timeout(60)
->save($absolute_path);

return new PdfGenerationResult(
success: true,
filename: $filename,
path: $storage_path,
job_id: (string) Str::ulid(),
);
}


public function currentReportBucket()
{
$t = Carbon::now()->copy()->second(0);

$bucket_minute = (int) (floor($t->minute / 30) * 30);
$t->minute($bucket_minute);

return $t->format('Ymd_Hi');
}

}
11 changes: 11 additions & 0 deletions app/Helpers/Contracts/PdfGenerationHelperContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Helpers\Contracts;

use App\PdfGenerationResult;

interface PdfGenerationHelperContract {

public function generate(array $payload): PdfGenerationResult;

}
29 changes: 29 additions & 0 deletions app/Helpers/LambdaPdfHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Helpers;

use App\Helpers\Contracts\PdfGenerationHelperContract;
use App\PdfGenerationResult;
use Illuminate\Support\Str;

class LambdaPdfHelper implements PdfGenerationHelperContract
{
public function generate(array $payload): PdfGenerationResult
{

// Pdf::view('pdf.invoice', $data)
// ->onLambda()
// ->save('invoice.pdf');

$filename = $payload['filename'] ?? '';
$storage_path = $payload['storage_path'] ?? '';
$job_id = $payload['job_id'] ?? (string) Str::ulid();

return new PdfGenerationResult(
success: true,
filename: $filename,
path: $storage_path,
job_id: $job_id,
);
}
}
11 changes: 11 additions & 0 deletions app/Http/Controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace App\Http\Controllers;

use App\Profile;
use App\Student;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View as ViewContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;

class AppController extends Controller
Expand All @@ -23,4 +28,10 @@ public function faq(): View|ViewContract
{
return view('faq');
}

public function requestDownload(User $user, string $token)
{
return view('initiate-download', compact('token'));
}

}
1 change: 1 addition & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

}
1 change: 0 additions & 1 deletion app/Http/Controllers/ProfileStudentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Profile;
use Illuminate\Contracts\View\View as ViewContract;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ProfileStudentsController extends Controller
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/StudentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function show(Request $request, Student $student): View|ViewContract
'custom_questions' => StudentData::customQuestions(),
'languages' => StudentData::$languages,
'majors' => StudentData::majors(),
'user' => $request->user(),
]);
}

Expand All @@ -122,6 +123,7 @@ public function edit(Student $student): View|ViewContract
'custom_questions' => StudentData::customQuestions(),
'languages' => StudentData::$languages,
'majors' => StudentData::majors(),
'user' => auth()->user(),
]);
}

Expand Down
5 changes: 5 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Middleware\ValidateSignature;

class Kernel extends HttpKernel
{
Expand Down Expand Up @@ -62,4 +63,8 @@ class Kernel extends HttpKernel
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
// 'cors' => \App\Http\Middleware\Cors::class,
];

protected $routeMiddleware = [
'signed' => ValidateSignature::class,
];
}
11 changes: 11 additions & 0 deletions app/Http/Livewire/Concerns/HasFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Http\Livewire\Concerns;

use App\StudentData;
use Illuminate\Support\Collection;

trait HasFilters
{
public function resetFilters()
Expand All @@ -23,6 +26,13 @@ protected function emitFilterUpdatedEvent($name, $value)
}
}

public function getSelectedFilters()
{
return collect($this->only($this->availableFilters()))
->filter(fn($value) => filled(trim($value)))
->all();
}

protected function availableFilters(): array
{
return array_filter(array_keys(get_class_vars(self::class)), function ($property_name) {
Expand All @@ -35,4 +45,5 @@ protected function isAFilter($name)
// Any property with a name including "_filter"
return strpos($name, '_filter') !== false;
}

}
35 changes: 35 additions & 0 deletions app/Http/Livewire/Concerns/HasPdfDownloads.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Livewire\Concerns;

use App\Jobs\ProcessPdfJob;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;

trait HasPdfDownloads
{
public function initiatePdfDownload($view, $data, $filename, $file_description = '')
{
if (!$data) { return false; }

$user = auth()->user();
$token = (string) Str::ulid();

$this->cachePdfToken($user, $token);

$download_request_url = URL::temporarySignedRoute('pdf.requestDownload', now()->addMinutes(10), ['user' => $user, 'token' => $token]);

ProcessPdfJob::dispatch($user, $view, $filename, $file_description, $token, $data);

$this->dispatchBrowserEvent('initiatePdfDownload', ['download_request_url' => $download_request_url]);
}

protected function cachePdfToken($user, $token) {
$user_tokens = Cache::get("pdf:tokens:{$user->pea}", collect());
$user_tokens->push($token);

Cache::put("pdf:tokens:{$user->pea}", $user_tokens, now()->addMinutes(30));
}

}
Loading
Loading