src/Controller/Backend/MPlantController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use App\Controller\Base\BaseController;
  4. use App\Entity\MPlant;
  5. use App\Entity\TLogplant;
  6. use App\Entity\Tplant;
  7. use App\Filter\MPlantFilterType;
  8. use App\Filter\TplantFilterType;
  9. use App\Form\MPlantType;
  10. use App\Form\TplantType;
  11. use App\Repository\MKategoriRepository;
  12. use App\Repository\MPlantRepository;
  13. use App\Repository\MSektorDetailRepository;
  14. use App\Repository\MSektorRepository;
  15. use App\Repository\TDivRepository;
  16. use App\Repository\TLogplantRepository;
  17. use App\Repository\TLogRepository;
  18. use App\Repository\TplantRepository;
  19. use App\Service\EmailSender;
  20. use App\Services\FileUploader;
  21. use App\Utils\ObjectManager;
  22. use Doctrine\Persistence\ManagerRegistry;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Kematjaya\Breadcrumb\Lib\Builder as BreacrumbBuilder;
  28. use Kematjaya\Export\Manager\ManagerInterface;
  29. use Kematjaya\Export\Processor\Excel\SpreadsheetFromArrayProcessor;
  30. use Knp\Component\Pager\PaginatorInterface;
  31. use Symfony\Component\HttpFoundation\JsonResponse;
  32. use Symfony\Component\Mailer\MailerInterface;
  33. class MPlantController extends BaseController
  34. {
  35.     private $pageName 'plant';
  36.     private $class MPlant::class;
  37.     /**
  38.      * @Route("/plant", name="plant", methods={"GET", "POST"})
  39.      */
  40.     public function index(BreacrumbBuilder $builderRequest $requestPaginatorInterface $paginatorInterface): Response
  41.     {
  42.         $builder->add('Dashboard '"dashboard", array(), "&nbsp;<i class='fa fa-home'></i>");
  43.         $builder->add('Plant');
  44.         $roles $this->getUser()->getRoles()[0];
  45.         $form $this->createFormFilter(MPlantFilterType::class);
  46.         $queryBuilder $this->getQueryBuilder($this->class);
  47.         // if($roles == 'ROLE_KOTA' || $roles == 'ROLE_OPERATOR') {
  48.         //     $queryBuilder->andWhere('this.div = :div_id');
  49.         //     $queryBuilder->andWhere('this.status IN (:status)');
  50.         //     $queryBuilder->setParameter('status', [2,3,4]);
  51.         //     $queryBuilder->setParameter('div_id', $this->getUser()->getDivAkses()[0]);
  52.         // }
  53.         $queryBuilder $this->buildFilter($request$form$queryBuilder)->addOrderBy("this.id""DESC");
  54.         
  55.         $this->setSessionLimit($request);
  56.         $maxPerPage $request->getSession()->get("limit") ? $request->getSession()->get("limit") : $this->getLimit();
  57.         $pagination $paginatorInterface->paginate($queryBuilder$request->query->getInt('page'1), $maxPerPage);
  58.         
  59.         return $this->render('backend/plant/index.html.twig', [
  60.             'page_name' => $this->pageName,
  61.             'button_credential' => $this->buttonCredentials($this->pageName),
  62.             'filter' => $form->createView(),
  63.             'pagination' => $pagination,
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route("/plant/form/{id}", defaults={"id"= null}, name="plant_form", methods={"POST", "GET"})
  68.      */
  69.     public function form(?string $id nullRequest $requestMPlantRepository $mPlantRepo)
  70.     {
  71.         if($id){
  72.             $m_plant $mPlantRepo->find($id);
  73.         } else {
  74.             $m_plant = new MPlant();
  75.         }
  76.         if(!$m_plant){
  77.             throw new NotFoundHttpException();
  78.         }
  79.         $form $this->createForm(MPlantType::class, $m_plant, ['action' => $this->generateUrl('plant_form', ['id' => $m_plant->getId()])]);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             $user $form->getData();
  83.             $entityManager $this->getDoctrine()->getManager();
  84.             $entityManager->persist($user);
  85.             $entityManager->flush();
  86.             return $this->redirectToRoute('plant');
  87.         }
  88.         return $this->render('/backend/plant/form.html.twig', ['form' => $form->createView()]);
  89.     }
  90.     /**
  91.      * @Route("/plant/{id}/delete_plant", name="plant_delete", methods={"GET"})
  92.      */
  93.     public function delete(?string $id nullMPlantRepository $mPlantRepo)
  94.     {
  95.         $plant $mPlantRepo->find($id);
  96.         $entityManager $this->getDoctrine()->getManager();
  97.         $entityManager->remove($plant);
  98.         $entityManager->flush();
  99.         $this->addFlash('notice''plant "' $plant->getNama() . '" berhasil dihapus');
  100.         return $this->redirectToRoute('plant');
  101.     }
  102.      /**
  103.      * @Route("/add_selected", name="plant_add_selected", methods={"POST"})
  104.      */
  105.     public function add_selected(Request $requestObjectManager $objectManager)
  106.     {
  107.         $sessionName $request->get("name");
  108.         $this->get('session')->set($sessionName$request->get('selected'));
  109.         $selected = (!empty($this->get('session')->get($sessionName))) ? $this->get('session')->get($sessionName) : [];
  110.         return $this->json($selected);
  111.     }
  112.     /**
  113.      * @Route("plant/action_selected", name="plant_action_selected", methods={"POST"})
  114.      */
  115.     public function action_selected(Request $requestObjectManager $objectManager)
  116.     {
  117.         $sessionName $request->get("name");
  118.         if ($this->isCsrfTokenValid($sessionName '_action_selected'$request->request->get('_token')))
  119.         {
  120.             $selected = (!empty($this->get('session')->get($sessionName))) ? $this->get('session')->get($sessionName) : [];
  121.             $deleted $objectManager->deleteByIds($this->class$selected);
  122.             if($deleted) {
  123.                 $this->get('session')->set($sessionName, []);
  124.                 $this->addFlash('success'$this->getTranslator()->trans('messages.deleted.success'));
  125.             } else {
  126.                 $this->addFlash('error'$this->getTranslator()->trans('messages.deleted.error'));
  127.             }
  128.         }
  129.         
  130.         return $this->redirectToRoute('plant');
  131.     }
  132.     /**
  133.      * @Route("/plant/{id}/detail_plant", name="plant_detail", methods={"GET","POST"})
  134.      */
  135.     public function detail(
  136.         BreacrumbBuilder $builder
  137.         TplantRepository $tplantrepo
  138.         Tplant $tplant
  139.         TDivRepository $tDivRepository,
  140.         MSektorDetailRepository $mSektorDetailRepository,
  141.         TLogplantRepository $tLogplantRepository
  142.         )
  143.     {
  144.         $builder->add('Dashboard '"dashboard", array(), "&nbsp;<i class='fa fa-home'></i>");
  145.         $builder->add('plant''plant');
  146.         $builder->add('Detail');
  147.         $detail $tplantrepo->find($tplant);
  148.         $sektor $detail->getSektor();
  149.         $m_opd $tDivRepository->findAll();
  150.         $opd $detail->getDiv() ? $detail->getDiv()->getId() : null;
  151.         if($sektor != null) {
  152.             if($detail->getDiv()) {
  153.                 $m_sektor $sektor $mSektorDetailRepository->findBy(['div' => $detail->getDiv()->getId()]) : null;
  154.             } else {
  155.                 $m_sektor $mSektorDetailRepository->findOneBy(['sektor' => $sektor->getId()]);
  156.                 $opd $m_sektor->getDiv()->getId();
  157.             }
  158.         } else {
  159.             $m_sektor null;
  160.         }
  161.         
  162.         return $this->render('backend/plant/detail.html.twig', [
  163.             'plants' => $detail,
  164.             'm_opd' => $m_opd,
  165.             'selected_sektor' => $sektor $sektor->getId() : null,
  166.             'm_sektor' => $m_sektor,
  167.             'selected_opd' => $opd,
  168.         ]);
  169.     }
  170.     /**
  171.      * @Route("/plant/filter_sektor", name="plant_filter_sektor", methods={"GET","POST"})
  172.      */
  173.     public function getSektorByOpd(Request $requestMSektorDetailRepository $mSektorDetailRepository) {
  174.         $mSektor $mSektorDetailRepository->findBy(['div' => $request->get('div_id')], ['sektor' => 'asc']);
  175.         $results = [];
  176.         foreach($mSektor as $sektor) {
  177.             array_push($results, [
  178.                 'id' => $sektor->getSektor()->getId(),
  179.                 'nama' => $sektor->getSektor()->getNama()
  180.             ]);
  181.         }
  182.         return new JsonResponse($results);
  183.     }
  184.     /**
  185.      * @Route("/plant/update_status", name="plant_update_status", methods={"POST"})
  186.      */
  187.     public function updateStatus(Request $request
  188.     ManagerRegistry $managerRegistry
  189.     TplantRepository $tplantRepository,
  190.     TDivRepository $tDivRepository,
  191.     MSektorRepository $mSektorRepository
  192.     ) {
  193.        $sektor $request->get('sektor');
  194.        $opd $request->get('perangkat_daerah');
  195.        $id $request->get('plant_id');
  196.        $file $request->files->get('input_file');
  197.        $status $request->get('status');
  198.        $deskripsi $request->get('deskripsi');
  199.        $plant $tplantRepository->find(number_format($id));
  200.     
  201.        $roles $this->getUser()->getRoles()[0];
  202.         if (in_array('ROLE_WALIDATA'$this->getUser()->getRoles())) {
  203.             if($opd === "" || $sektor === "") {
  204.                 $this->addFlash('error''Pilih OPD dan Sektor Terlebih Dahulu.');
  205.                 return $this->redirectToRoute('plant_detail', ['id' => $id]);
  206.             }
  207.             
  208.             $plant->setDiv($tDivRepository->find($opd));
  209.             $plant->setSektor($mSektorRepository->find($sektor));
  210.             $plant->setStatus(2);
  211.         }
  212.        $doctrine $managerRegistry->getManager();
  213.        $doctrine->persist($plant);
  214.        $doctrine->flush();
  215.        $this->curlplant($plant);
  216.         $this->addFlash('notice''Update Status Berhasil.');
  217.        return $this->redirectToRoute('plant_detail', ['id' => $id]);
  218.      }
  219.     public function curlplant(Tplant $plant)
  220.     {
  221.         // dump( $this->getParameter('api_plant')); exit;
  222.         $data = [
  223.             "reff_id" => $plant->getId(),
  224.             "nama" => $plant->getNama(),
  225.             "telepon" => $plant->getTelepon(),
  226.             "email" => $plant->getEmail(),
  227.             "pekerjaan_id" => ($plant->getPekerjaan()) ? $plant->getPekerjaan()->getId() : null,
  228.             "desk_pekerjaan" => $plant->getDeskPekerjaan(),
  229.             "judul_data" => $plant->getJudulData(),
  230.             "desk_kebutuhan" => $plant->getDeskKebutuhan(),
  231.             "tujuan_data" => $plant->getTujuanData(),
  232.             "is_done" => $plant->getIsDone(),
  233.             "sektor" => $plant->getSektor() ? $plant->getSektor()->getId() : null,
  234.             "div_id" => ($plant->getDiv()) ? $plant->getDiv()->getId() : null,
  235.             'status' => $plant->getStatus()
  236.         ];
  237.         $curl curl_init();
  238.         curl_setopt_array($curl, array(
  239.             CURLOPT_URL => $this->getParameter('api_plant'),
  240.             CURLOPT_RETURNTRANSFER => true,
  241.             CURLOPT_ENCODING => '',
  242.             CURLOPT_MAXREDIRS => 10,
  243.             CURLOPT_TIMEOUT => 0,
  244.             CURLOPT_SSL_VERIFYHOST => false,
  245.             CURLOPT_SSL_VERIFYPEER => false,
  246.             CURLOPT_FOLLOWLOCATION => true,
  247.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  248.             CURLOPT_CUSTOMREQUEST => "POST",
  249.             CURLOPT_HTTPHEADER => array(
  250.                 "Content-Type: multipart/form-data",
  251.                 "x-token: " $this->getParameter('token_post_plant')
  252.             ),
  253.             CURLOPT_POSTFIELDS => $data
  254.         ));
  255.         $response curl_exec($curl);
  256.         // dump($response); exit;
  257.         curl_close($curl);
  258.     }
  259.     /**
  260.      * @Route("/plant/{id}/update_form", name="form_plant_update", methods={"GET"})
  261.      */
  262.      public function formUpdateplantDataset(string $id) {
  263.         return $this->render('backend/plant/form_plant_dataset.html.twig', [
  264.             'id' => $id
  265.         ]);
  266.      }
  267.     /**
  268.      * @Route("/plant/{path}/download_attachment", name="plant_download_attachment", methods={"GET"})
  269.      */
  270.      public function downloadAttachmentData(string $path) {
  271.         $uploads_directory $this->getParameter('uploads_directory');
  272.         $path $uploads_directory '/' $path;
  273.         header("Cache-Control: no-cache, must-revalidate");
  274.         header('Content-type: application/zip');
  275.         header("Content-Transfer-Encoding: Binary");
  276.         header('Content-Disposition: attachment; filename="' $path '"');
  277.         readfile($path);
  278.      }
  279.     /**
  280.      * @Route("/plant/{id}/done_plant", name="plant_done", methods={"GET", "POST"})
  281.      */
  282.     public function done(Tplant $plant){
  283.         $plant->setIsDone(true);
  284.         $entityManager $this->getDoctrine()->getManager();
  285.         $entityManager->persist($plant);
  286.         $entityManager->flush();
  287.         $this->addFlash('notice''plant "' $plant->getNama() . '" berhasil diselesaikan');
  288.         return $this->redirectToRoute('plant');
  289.     }
  290.     /**
  291.      * @Route("/plant/report", name="plant_report", methods={"GET"})
  292.      */
  293.     public function report(Request $requestMKategoriRepository $mKategoriRepo)
  294.     {
  295.         $kategori $mKategoriRepo->findAll();
  296.         $bulan $this->getBulan();
  297.         return $this->render('backend/plant/report.html.twig', [
  298.             'kategori' => $kategori,
  299.             'bulans' => $bulan
  300.         ]);
  301.     }
  302.     /**
  303.      * @Route("/plant/download_report", name="plant_download_report", methods={"GET"})
  304.      */
  305.     public function download_report(Request $requestTplantRepository $tplantrepoManagerInterface $managerInterface)
  306.     {
  307.         $filterSektor = [];
  308.         if ($request->get('sektor')) {
  309.             $filterSektor['sektor'] = $request->get('sektor') == "" null $request->get('sektor');
  310.         }
  311.         $bulan $request->get('bulan');
  312.         $tahun $request->get('tahun');
  313.         $startDate = new \DateTimeImmutable("01-$bulan-$tahun-01T00:00:00");
  314.         $endDate $startDate->modify('last day of this month')->setTime(235959);
  315.         $tplant $tplantrepo->getPerhonanByMonthSectorAndYear($filterSektor$startDate$endDate);
  316.         $data = [["No""Nama""Telepon""Email""Pekerjaan""Deskripsi Pekerjaan""Tanggal Buat""Judul Data""Perangkat Daerah""Tujuan Data""Sektor""Status"]];
  317.         
  318.         $no 1;
  319.         foreach ($tplant as $plant) {
  320.             $data[] = [
  321.                 $no++,
  322.                 $plant->getNama(),
  323.                 $plant->getTelepon(),
  324.                 $plant->getEmail(),
  325.                 ($plant->getPekerjaan()) ? $plant->getPekerjaan()->getNama() : "",
  326.                 $plant->getDeskPekerjaan(),
  327.                 $plant->getCreatedAt(),
  328.                 $plant->getJudulData(),
  329.                 ($plant->getDiv()) ? $plant->getDiv()->getDivNama() : "",
  330.                 $plant->getTujuanData(),
  331.                 ($plant->getSektor()) ? $plant->getSektor()->getNama() : "",
  332.                 ($plant->getIsDone()) ? "Selesai" "Belum"
  333.             ];
  334.         }
  335.         $arrayToExcel $managerInterface->render($data, new SpreadsheetFromArrayProcessor('plant_data.xlsx'));
  336.         return $arrayToExcel;
  337.     }
  338. }