<?php
namespace App\Controller\Frontend;
use App\Controller\Base\BaseController;
use App\Entity\TInfografik;
use App\Repository\MKategoriDetailRepository;
use App\Repository\MUrusanDetailRepository;
use App\Repository\TDokumenRepository;
use App\Repository\TInfografikRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use ZipArchive;
use Dompdf\Dompdf;
use Dompdf\Options as DomOptions;
use Sonata\SeoBundle\Seo\SeoPageInterface;
use Symfony\Component\HttpFoundation\JsonResponse; // Tambahkan ini
class InfografikController extends BaseController
{
/**
* @Route("/infografik", name="infografik")
*/
public function index(SeoPageInterface $seoPage, TDokumenRepository $tDokumenRepo, MKategoriDetailRepository $mKategoriDetailRepo, MUrusanDetailRepository $mUrusanDetailRepository,
PaginatorInterface $paginatorInterface, Request $request): Response
{
$seoPage->addTitlePrefix("Infografik");
$queryBuilder = $this->getQueryBuilder(TInfografik::class)
->andWhere("this.is_arsip = false")
->andWhere("lower(this.judul) LIKE lower('%" . $request->get('judul', '') . "%')")
->addOrderBy('this.id', 'desc');
$pagination = $paginatorInterface->paginate($queryBuilder, $request->query->getInt('page', 1), 10);
$icons = array();
$kategori = array();
foreach($pagination as $ig){
$dok = $tDokumenRepo->createQueryBuilder('d')
->where('d.reff_id = :id')
->andWhere('d.reff_name = :name')
->andWhere('d.file_type != :file_type')
->setParameter('id', $ig->getId())
->setParameter('name', 'INFOGRAFIK')
->setParameter('file_type', 'pdf')
->getQuery()
->getOneOrNullResult();
if ($dok) {
$icons[$ig->getId()] = $dok->getPath();
}
else {
$icons[$ig->getId()] = null;
}
$kategori_detail = $mUrusanDetailRepository->findBy(['reff_id' => $ig->getId(), 'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
if ($kategori_detail) {
$temp = array();
foreach($kategori_detail as $kat){
$temp[] = $kat->getUrusan()->getNama();
}
$kategori[$ig->getId()] = implode(', ', $temp);
} else {
$kategori[$ig->getId()] = '-';
}
}
return $this->render('frontend/infografik/index.html.twig', [
'controller_name' => 'InfografikController',
'header_kategori' => $this->getHeaderKategori(),
'pagination' => $pagination,
'icons' => $icons,
'kategori' => $kategori,
'search' => array('judul'=>$request->get('judul', '')),
]);
}
/**
* @Route("/infografik/detail/{id}.html", name="detail_infografik", methods={"GET"})
*/
public function detail(?string $id, TInfografikRepository $tInfografikRepo, TDokumenRepository $tDokumenRepo, MKategoriDetailRepository $mKategoriDetailRepo, MUrusanDetailRepository $mUrusanDetailRepository): Response
{
$infografik = $tInfografikRepo->find($id);
$infografik->setView($infografik->getView() + 1);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($infografik);
$entityManager->flush();
$dokumen = $tDokumenRepo->createQueryBuilder('d')
->where('d.reff_id = :id')
->andWhere('d.reff_name = :name')
->andWhere('d.file_type != :file_type')
->setParameter('id', $id)
->setParameter('name', 'INFOGRAFIK')
->setParameter('file_type', 'pdf')
->getQuery()
->getResult();
$dokumenPDF = $tDokumenRepo->createQueryBuilder('d')
->where('d.reff_id = :id')
->andWhere('d.reff_name = :name')
->andWhere('d.file_type = :file_type')
->setParameter('id', $id)
->setParameter('name', 'INFOGRAFIK')
->setParameter('file_type', 'pdf')
->getQuery()
->getOneOrNullResult();
$kategori = $mUrusanDetailRepository->findBy(['reff_id' => $id, 'reff_name' => 'INFOGRAFIK']);
dump($dokumenPDF);
return $this->render('frontend/infografik/detail.html.twig', [
'header_kategori' => $this->getHeaderKategori(),
'infografik' => $infografik,
'dokumen' => $dokumen,
'kategori' => $kategori,
'dokumen_pdf' => $dokumenPDF
]);
}
/**
* @Route("/infografik/{id}/img.html", name="infografik_download", methods={"GET"})
*/
public function download(?string $id, TInfografikRepository $tInfografikRepo, TDokumenRepository $tDokumenRepo)
{
$infografik = $tInfografikRepo->find($id);
$uploads_directory = $this->getParameter('uploads_directory');
$dokumen = $tDokumenRepo->findBy(['reff_id' => $id, 'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
$zip = new ZipArchive();
$zipName = $uploads_directory .'//'. substr($infografik->getJudul(), 0, 25).'.zip';
// print_r($zipName); exit;
if($zip->open($zipName, ZipArchive::CREATE)!==TRUE)
{
//dump('a'); exit;
return $this->redirectToRoute('detail_infografik', ['id' => $id]);
}
if ($dokumen) {
foreach($dokumen as $dok){
$path = $uploads_directory.'//'.$dok->getPath();
// dump($path);exit;
$zip->addFile($path, $dok->getPath());
}
}
$zip->close();
if(file_exists($zipName)){
$pName = substr($infografik->getJudul(), 0, 25).'.zip';
header("Content-Length: ".filesize($zipName));
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$pName.'"');
readfile($zipName);
// unlink($zipName);
// dump('b'); exit;
} else {
//dump('c'); exit;
return $this->redirectToRoute('detail_infografik', ['id' => $id]);
}
}
/**
* @Route("/infografik/{id}.pdf", name="infografik_pdf", methods={"GET"})
*/
public function pdf(?string $id, TInfografikRepository $tInfografikRepo, TDokumenRepository $tDokumenRepo)
{
$infografik = $tInfografikRepo->find($id);
$uploads_directory = $this->getParameter('uploads_directory');
$dokumen = $tDokumenRepo->findBy(['reff_id' => $id, 'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
// Assuming $dokumen[0]->getPath() contains the path to the PDF file
$pdfPath = $uploads_directory . '/'.'publikasi'.'/' . substr($dokumen[0]->getPath(), 0, 25);
echo 'check :', $pdfPath;
// Check if the file exists
if (!file_exists($pdfPath)) {
throw $this->createNotFoundException('The PDF file does not exist');
}
dump($pdfPath);
exit;
// Create a BinaryFileResponse to download the PDF file
// $response = new BinaryFileResponse($pdfPath);
// $response->setContentDisposition(
// ResponseHeaderBag::DISPOSITION_ATTACHMENT,
// basename($pdfPath)
// );
// return $response;
// $pdfOptions = new DomOptions();
// $pdfOptions->set('defaultFont', 'Arial');
// $pdfOptions->setIsHtml5ParserEnabled(true);
// $dompdf = new Dompdf($pdfOptions);
// $html = $this->renderView('frontend/infografik/pdfimg.html.twig', [
// 'infografik' => $infografik,
// 'dokumen' => $dokumen[0],
// ]);
// $dompdf->loadHtml($html);
// $dompdf->setPaper('A4', 'portrait');
// $dompdf->render();
// // Save PDF to a file
// $pdfPath = $uploads_directory . '/' .substr($dokumen[0]->getPath(), 0, 25) . ".pdf";
// file_put_contents($pdfPath, $dompdf->output());
// $response = new Response();
// $response->headers->set('Content-Type', 'application/pdf');
// $response->headers->set('Content-Disposition', 'attachment; filename="' . basename($pdfPath) . '"');
// $response->setContent(file_get_contents($pdfPath));
// return $response;
}
}