src/Controller/Frontend/PublikasiController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Controller\Base\BaseController;
  4. use App\Entity\TInfografik;
  5. use App\Repository\MKategoriDetailRepository;
  6. use App\Repository\MUrusanDetailRepository;
  7. use App\Repository\TDokumenRepository;
  8. use App\Repository\TInfografikRepository;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use ZipArchive;
  16. use Dompdf\Dompdf;
  17. use Dompdf\Options as DomOptions;
  18. use Sonata\SeoBundle\Seo\SeoPageInterface;
  19. use Symfony\Component\HttpFoundation\JsonResponse// Tambahkan ini
  20. class PublikasiController extends BaseController
  21. {
  22.     /**
  23.      * @Route("/publikasii", name="publikasii")
  24.      */
  25.     public function index(SeoPageInterface $seoPageTDokumenRepository $tDokumenRepoMKategoriDetailRepository $mKategoriDetailRepoMUrusanDetailRepository $mUrusanDetailRepository,
  26.     PaginatorInterface $paginatorInterfaceRequest $request): Response
  27.     {
  28.         $seoPage->addTitlePrefix("Publikasi");
  29.         
  30.         $queryBuilder $this->getQueryBuilder(TInfografik::class)
  31.                 ->andWhere("this.is_arsip = false")
  32.                 ->andWhere("lower(this.judul) LIKE lower('%" $request->get('judul''') . "%')")
  33.                 ->addOrderBy('this.release_date''desc');
  34.         $pagination $paginatorInterface->paginate($queryBuilder$request->query->getInt('page'1), 10);
  35.         $icons = array();
  36.         $kategori = array();
  37.         foreach($pagination as $ig){
  38.             $dok $tDokumenRepo->createQueryBuilder('d')
  39.             ->where('d.reff_id = :id')
  40.             ->andWhere('d.reff_name = :name')
  41.             ->andWhere('d.file_type != :file_type')
  42.             ->setParameter('id'$ig->getId())
  43.             ->setParameter('name''INFOGRAFIK')
  44.             ->setParameter('file_type''pdf')
  45.             ->getQuery()
  46.             ->getOneOrNullResult();
  47.             if ($dok) {
  48.                 $icons[$ig->getId()] = $dok->getPath();
  49.             }
  50.             else {
  51.                 $icons[$ig->getId()] = null;
  52.             }
  53.             $kategori_detail $mUrusanDetailRepository->findBy(['reff_id' => $ig->getId(), 'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
  54.             if ($kategori_detail) {
  55.                 $temp = array();
  56.                 foreach($kategori_detail as $kat){
  57.                     $temp[] = $kat->getUrusan()->getNama();
  58.                 }
  59.                 $kategori[$ig->getId()] = implode(', '$temp);
  60.             } else {
  61.                 $kategori[$ig->getId()] = '-';
  62.             }
  63.         }
  64.         return $this->render('frontend/publikasii/index.html.twig', [
  65.             'controller_name' => 'InfografikController',
  66.             'header_kategori' => $this->getHeaderKategori(),
  67.             'pagination' => $pagination,
  68.             'icons' => $icons,
  69.             'kategori' => $kategori,
  70.             'search' => array('judul'=>$request->get('judul''')),
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/publikasii/detail/{id}.html", name="detail_publikasii", methods={"GET"})
  75.      */
  76.     public function detail(?string $idTInfografikRepository $tInfografikRepoTDokumenRepository $tDokumenRepoMKategoriDetailRepository $mKategoriDetailRepoMUrusanDetailRepository $mUrusanDetailRepository): Response
  77.     {
  78.         $infografik $tInfografikRepo->find($id);
  79.         $infografik->setView($infografik->getView() + 1);
  80.         $entityManager $this->getDoctrine()->getManager();
  81.         $entityManager->persist($infografik);
  82.         $entityManager->flush();
  83.         $dokumen $tDokumenRepo->createQueryBuilder('d')
  84.         ->where('d.reff_id = :id')
  85.         ->andWhere('d.reff_name = :name')
  86.         ->andWhere('d.file_type != :file_type')
  87.         ->setParameter('id'$id)
  88.         ->setParameter('name''INFOGRAFIK')
  89.         ->setParameter('file_type''pdf')
  90.         ->getQuery()
  91.         ->getResult();
  92.         $dokumenPDF $tDokumenRepo->createQueryBuilder('d')
  93.         ->where('d.reff_id = :id')
  94.         ->andWhere('d.reff_name = :name')
  95.         ->andWhere('d.file_type = :file_type')
  96.         ->setParameter('id'$id)
  97.         ->setParameter('name''INFOGRAFIK')
  98.         ->setParameter('file_type''pdf')
  99.         ->getQuery()
  100.         ->getOneOrNullResult();
  101.         $kategori $mUrusanDetailRepository->findBy(['reff_id' => $id'reff_name' => 'INFOGRAFIK']);
  102.         dump($dokumenPDF);
  103.         return $this->render('frontend/publikasii/detail.html.twig', [
  104.             'header_kategori' => $this->getHeaderKategori(),
  105.             'infografik' => $infografik,
  106.             'dokumen' => $dokumen,
  107.             'kategori' => $kategori,
  108.             'dokumen_pdf' => $dokumenPDF
  109.         ]);
  110.     }
  111.     /**
  112.      * @Route("/publikasii/{id}/img.html", name="publikasii_download", methods={"GET"})
  113.      */
  114.     public function download(?string $idTInfografikRepository $tInfografikRepoTDokumenRepository $tDokumenRepo)
  115.     {
  116.         $infografik $tInfografikRepo->find($id);
  117.         $uploads_directory $this->getParameter('uploads_directory');
  118.         $dokumen $tDokumenRepo->findBy(['reff_id' => $id'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
  119.         
  120.         $zip = new ZipArchive();
  121.         $zipName $uploads_directory .'//'substr($infografik->getJudul(), 025).'.zip';
  122.         // print_r($zipName); exit;
  123.         if($zip->open($zipNameZipArchive::CREATE)!==TRUE)
  124.         {
  125.             //dump('a'); exit;
  126.             return $this->redirectToRoute('detail_publikasii', ['id' => $id]);
  127.         }
  128.         if ($dokumen) {
  129.             foreach($dokumen as $dok){
  130.                 $path $uploads_directory.'//'.$dok->getPath();
  131.                 // dump($path);exit;
  132.                 $zip->addFile($path$dok->getPath());
  133.             }
  134.         }
  135.         $zip->close();
  136.         if(file_exists($zipName)){
  137.             $pName substr($infografik->getJudul(), 025).'.zip';
  138.             header("Content-Length: ".filesize($zipName));
  139.             header("Cache-Control: no-cache, must-revalidate");
  140.             header('Content-type: application/zip');
  141.             header("Content-Transfer-Encoding: Binary");
  142.             header('Content-Disposition: attachment; filename="'.$pName.'"');
  143.             readfile($zipName);
  144.            // unlink($zipName);
  145.           // dump('b'); exit;
  146.         } else {
  147.             //dump('c'); exit;
  148.             return $this->redirectToRoute('detail_publikasii', ['id' => $id]);
  149.         }
  150.     }
  151.     /**
  152.      * @Route("/publikasii/{id}.pdf", name="publikasii_pdf", methods={"GET"})
  153.      */
  154.     public function pdf(?string $idTInfografikRepository $tInfografikRepoTDokumenRepository $tDokumenRepo)
  155.     {
  156.         $uploads_directory $this->getParameter('uploads_directory');
  157.         $dokumen $tDokumenRepo->findBy(['reff_id' => $id'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
  158.         
  159.         // Assuming $dokumen[0]->getPath() contains the path to the PDF file
  160.         $pdfPath $uploads_directory '/'.'publikasi'.'/' substr($dokumen[0]->getPath(), 025);
  161.         // Check if the file exists
  162.         if (!file_exists($pdfPath)) {
  163.             throw $this->createNotFoundException('The PDF file does not exist');
  164.         }
  165.         exit;
  166.     }
  167. }