<?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 PublikasiController extends BaseController
{
/**
* @Route("/publikasii", name="publikasii")
*/
public function index(SeoPageInterface $seoPage, TDokumenRepository $tDokumenRepo, MKategoriDetailRepository $mKategoriDetailRepo, MUrusanDetailRepository $mUrusanDetailRepository,
PaginatorInterface $paginatorInterface, Request $request): Response
{
$seoPage->addTitlePrefix("Publikasi");
$queryBuilder = $this->getQueryBuilder(TInfografik::class)
->andWhere("this.is_arsip = false")
->andWhere("lower(this.judul) LIKE lower('%" . $request->get('judul', '') . "%')")
->addOrderBy('this.release_date', '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/publikasii/index.html.twig', [
'controller_name' => 'InfografikController',
'header_kategori' => $this->getHeaderKategori(),
'pagination' => $pagination,
'icons' => $icons,
'kategori' => $kategori,
'search' => array('judul'=>$request->get('judul', '')),
]);
}
/**
* @Route("/publikasii/detail/{id}.html", name="detail_publikasii", 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/publikasii/detail.html.twig', [
'header_kategori' => $this->getHeaderKategori(),
'infografik' => $infografik,
'dokumen' => $dokumen,
'kategori' => $kategori,
'dokumen_pdf' => $dokumenPDF
]);
}
/**
* @Route("/publikasii/{id}/img.html", name="publikasii_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_publikasii', ['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_publikasii', ['id' => $id]);
}
}
/**
* @Route("/publikasii/{id}.pdf", name="publikasii_pdf", methods={"GET"})
*/
public function pdf(?string $id, TInfografikRepository $tInfografikRepo, TDokumenRepository $tDokumenRepo)
{
$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);
// Check if the file exists
if (!file_exists($pdfPath)) {
throw $this->createNotFoundException('The PDF file does not exist');
}
exit;
}
}