src/Controller/Frontend/InfografikController.php line 38

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 InfografikController extends BaseController
  21. {
  22.     /**
  23.      * @Route("/infografik", name="infografik")
  24.      */
  25.     public function index(SeoPageInterface $seoPageTDokumenRepository $tDokumenRepoMKategoriDetailRepository $mKategoriDetailRepoMUrusanDetailRepository $mUrusanDetailRepository,
  26.     PaginatorInterface $paginatorInterfaceRequest $request): Response
  27.     {
  28.         $seoPage->addTitlePrefix("Infografik");
  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.id''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/infografik/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("/infografik/detail/{id}.html", name="detail_infografik", 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/infografik/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("/infografik/{id}/img.html", name="infografik_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_infografik', ['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_infografik', ['id' => $id]);
  149.         }
  150.     }
  151.     /**
  152.      * @Route("/infografik/{id}.pdf", name="infografik_pdf", methods={"GET"})
  153.      */
  154.     public function pdf(?string $idTInfografikRepository $tInfografikRepoTDokumenRepository $tDokumenRepo)
  155.     {
  156.         $infografik $tInfografikRepo->find($id);
  157.         $uploads_directory $this->getParameter('uploads_directory');
  158.         $dokumen $tDokumenRepo->findBy(['reff_id' => $id'reff_name' => 'INFOGRAFIK'], ['id' => 'ASC']);
  159.         
  160.         // Assuming $dokumen[0]->getPath() contains the path to the PDF file
  161.         $pdfPath $uploads_directory '/'.'publikasi'.'/' substr($dokumen[0]->getPath(), 025);
  162.         echo 'check :'$pdfPath;
  163.         // Check if the file exists
  164.         if (!file_exists($pdfPath)) {
  165.             throw $this->createNotFoundException('The PDF file does not exist');
  166.         }
  167.         dump($pdfPath);
  168.         exit;
  169.         // Create a BinaryFileResponse to download the PDF file
  170.         // $response = new BinaryFileResponse($pdfPath);
  171.         // $response->setContentDisposition(
  172.         //     ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  173.         //     basename($pdfPath)
  174.         // );
  175.         // return $response;
  176.         // $pdfOptions = new DomOptions();
  177.         // $pdfOptions->set('defaultFont', 'Arial');
  178.         // $pdfOptions->setIsHtml5ParserEnabled(true);
  179.         // $dompdf = new Dompdf($pdfOptions);
  180.         // $html = $this->renderView('frontend/infografik/pdfimg.html.twig', [
  181.         //     'infografik' => $infografik,
  182.         //     'dokumen' => $dokumen[0],
  183.         // ]);
  184.         // $dompdf->loadHtml($html);
  185.         // $dompdf->setPaper('A4', 'portrait');
  186.         // $dompdf->render();
  187.         // // Save PDF to a file
  188.         // $pdfPath = $uploads_directory . '/' .substr($dokumen[0]->getPath(), 0, 25) . ".pdf";
  189.         // file_put_contents($pdfPath, $dompdf->output());
  190.         // $response = new Response();
  191.         // $response->headers->set('Content-Type', 'application/pdf');
  192.         // $response->headers->set('Content-Disposition', 'attachment; filename="' . basename($pdfPath) . '"');
  193.         // $response->setContent(file_get_contents($pdfPath));
  194.         // return $response;
  195.     }
  196. }