<?php
namespace App\Http\Controllers\Admin;
use App\Facades\DataTable;
use App\Http\Controllers\Controller;
use App\Models\PelatihanPeserta;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\File;
class LaporanController extends Controller
{
public function sertifikatView()
{
$sort = str_replace(
['created_at'],
['created_at'],
request()->query('col')
);
$query = PelatihanPeserta::has('peserta')->has('pelatihan')->with('peserta', 'pelatihan');
$query = $query->whereHas('pelatihan', function ($w) {
if (auth()->user()->role == "instruktur") {
$w->where('user_id', auth()->user()->id);
}
if(request()->provinsi_id){
$w->where('provinsi_id',request()->provinsi_id);
}
if(request()->kabupaten_id){
$w->where('kabupaten_id',request()->kabupaten_id);
}
if(request()->kecamatan_id){
$w->where('kecamatan_id',request()->kecamatan_id);
}
if(request()->kelurahan_id){
$w->where('kelurahan_id',request()->kelurahan_id);
}
});
$result = DataTable::query($query)
->with(['peserta', 'pelatihan'])
->searchable(['peserta.nama_lengkap', 'pelatihan.title'])
->applySort($sort)
->allowedSorts(['created_at'])
->make()->toArray();
return Inertia::render('Admin/Laporan/Sertifikat/Index', [
'datatable' => $result,
'provinsi_id' => request()->provinsi_id,
'kabupaten_id' => request()->kabupaten_id,
'kecamatan_id' => request()->kecamatan_id,
'kelurahan_id' => request()->kelurahan_id,
]);
}
public function sertifikatAction(Request $request)
{
$request->validate([
'provinsi_id'=>'required',
'kabupaten_id'=>'required',
'kecamatan_id'=>'required',
]);
$query = PelatihanPeserta::has('peserta')->has('pelatihan')->with('peserta', 'pelatihan');
$query = $query->whereHas('pelatihan', function ($w) {
if (auth()->user()->role == "instruktur") {
$w->where('user_id', auth()->user()->id);
}
if(request()->provinsi_id){
$w->where('provinsi_id',request()->provinsi_id);
}
if(request()->kabupaten_id){
$w->where('kabupaten_id',request()->kabupaten_id);
}
if(request()->kecamatan_id){
$w->where('kecamatan_id',request()->kecamatan_id);
}
if(request()->kelurahan_id){
$w->where('kelurahan_id',request()->kelurahan_id);
}
});
$search = request()->search;
if ($search) {
$query = $query->where(function ($q) use ($search) {
$lower = 'LOWER';
$like = 'LIKE';
$searchTerm = '%' . strtolower($search) . '%';
foreach (['peserta.nama_lengkap', 'pelatihan.title'] as $column) {
if (str()->contains($column, '.')) {
$parts = explode('.', $column);
$relationPath = implode('.', array_slice($parts, 0, -1));
$columnName = end($parts);
$q->orWhereHas($relationPath, function ($nestedQuery) use ($searchTerm, $lower, $like, $columnName) {
$nestedQuery->whereRaw("$lower($columnName) $like ?", [$searchTerm]);
});
} else {
$q->orWhereRaw("$lower($column) $like ?", [$searchTerm]);
}
}
});
}
$pendaaftarans = $query->get();
$files = [];
foreach ($pendaaftarans as $pelatihanPeserta) {
$noSertifikat = ($pelatihanPeserta->id+170).'GN-'.date("m/Y",strtotime($pelatihanPeserta->pelatihan->end_date));
// return view('sertifikat',compact('pelatihanPeserta','noSertifikat'));
$pdf = Pdf::loadView('sertifikat', compact('pelatihanPeserta','noSertifikat'));
$pdf->setPaper('A4', 'landscape');
$path = public_path("file-sertifikat/".\Str::slug($pelatihanPeserta->peserta->nama_lengkap." ".$noSertifikat).'.pdf');
$pdf->save($path);
$files[] = $path;
}
$zip = new \ZipArchive();
$fileName = 'zip-sertifikat/download-sertifikat-'.date("YmdHis").'.zip';
if ($zip->open(public_path($fileName), \ZipArchive::CREATE)== TRUE)
{
foreach ($files as $key => $value){
$relativeName = basename($value);
$zip->addFile($value, $relativeName);
}
$zip->close();
}
// return response()->download(public_path($fileName));
return redirect()->route('admin.laporan.sertifikat.index',request()->all())->with('link',route('admin.laporan.sertifikat.download', ['path'=>$fileName]))->withInput(request()->all());
}
public function download(){
return response()->download(public_path(request()->path));
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]