src/Controller/Backend/TCleansingRawController.php line 132

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use App\Controller\Base\BaseController;
  4. use App\Entity\TCleansingRaw;
  5. use App\Entity\TFileGrafik;
  6. use App\Entity\TGrafik;
  7. use App\Form\TGrafikType;
  8. use App\Entity\TGrafikDinamis;
  9. use App\Entity\THeader;
  10. use App\Entity\TRawData;
  11. use App\Entity\TVariabelGrafik;
  12. use App\Entity\TWeeklyEploreReport;
  13. use App\Repository\MKategoriDetailRepository;
  14. use App\Repository\MKategoriRepository;
  15. use App\Repository\TDivRepository;
  16. use App\Repository\TGrafikDinamisRepository;
  17. use App\Repository\TGrafikRepository;
  18. use App\Repository\TLogRepository;
  19. use App\Repository\TVariabelGrafikRepository;
  20. use App\Repository\TFileGrafikRepository;
  21. use Doctrine\Persistence\ManagerRegistry;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Kematjaya\Breadcrumb\Lib\Builder as BreacrumbBuilder;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. use Kematjaya\ImportBundle\Manager\ImportManagerInterface;
  29. use Kematjaya\ImportBundle\DataSource\RemoteDataSource;
  30. use App\Transformer\PublikasiTransformer;
  31. use App\Transformer\VariabelTransformer;
  32. use DateTime;
  33. use Sonata\SeoBundle\Seo\SeoPageInterface;
  34. use PhpOffice\PhpSpreadsheet\IOFactory;
  35. use Knp\Component\Pager\PaginatorInterface;
  36. use App\Filter\TGrafikDinamisFilterType;
  37. use App\Kernel;
  38. use App\Repository\TDataGrafikRepository;
  39. use App\Repository\THeaderRepository;
  40. use App\Repository\TProgresRepository;
  41. use App\Repository\TRawDataRepository;
  42. use Symfony\Component\Process\Process;
  43. use Symfony\Component\Process\Exception\ProcessFailedException;
  44. use Kematjaya\Export\Manager\ManagerInterface;
  45. use Kematjaya\Export\Processor\Excel\SpreadsheetFromArrayProcessor;
  46. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  47. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  48. use PhpOffice\PhpSpreadsheet\Writer\Xls;
  49. use Symfony\Component\HttpFoundation\Response;
  50. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  51. class TCleansingRawController extends BaseController
  52. {
  53.     private $pageName 'cleansing_raw';
  54.     private $class TCleansingRaw::class;
  55.     /**
  56.      * @Route("/cleansing_raw", name="cleansing_raw")
  57.      */
  58.     public function index(SeoPageInterface $seoPageBreacrumbBuilder $builderRequest $requestPaginatorInterface $paginatorInterface): Response
  59.     {
  60.         $userRoles $this->getUser()->getRoles();
  61.         $seoPage->addTitlePrefix("Cleansing");
  62.         $builder->add('Dashboard '"dashboard", array(), "&nbsp;<i class='fa fa-home'></i>");
  63.         $builder->add('Cleansing Raw Data');
  64.         $array $this->getDataTableCleansing(nullnullnullnull);
  65.         // print_r($array); exit;
  66.         $filter TGrafikDinamisFilterType::class;
  67.         $form $this->get('form.factory')->create($filter$this->getFilters($filter));
  68.         $queryBuilder $this->getQueryBuilder($this->class);
  69.         // if ($userRoles[0] != 'ROLE_ADMINISTRATOR' && $userRoles[0] != 'ROLE_WALIDATA' && $userRoles[0] != 'ROLE_SUPER_USER'){
  70.         //     $queryBuilder = $queryBuilder->where('this.div = ' . $this->getUser()->getDiv()->getId());
  71.         // }
  72.         $queryBuilder $this->buildFilter($request$form$queryBuilder)->addOrderBy("this.id""DESC");
  73.         $this->setSessionLimit($request);
  74.         $maxPerPage $request->getSession()->get("limit") ? $request->getSession()->get("limit") : $this->getLimit();
  75.         $pagination $paginatorInterface->paginate($queryBuilder$request->query->getInt('page'1), $maxPerPage);
  76.         // $raw_data = $this->getDataTableRawData(null, null, null, null);
  77.         $header $this->getDataHeader();
  78.         if (isset($header[0]['head1'])) {
  79.             $head1 $header[0]['head1'];
  80.             $processedHeaders = [];
  81.             $keys array_keys($head1);
  82.             $total count($keys);
  83.             $i 0;
  84.             while ($i $total) {
  85.                 $key $keys[$i];
  86.                 $label $head1[$key];
  87.                 if ($label !== null) {
  88.                     $colspan 1;
  89.                     $j $i 1;
  90.                     while ($j $total && $head1[$keys[$j]] === null) {
  91.                         $colspan++;
  92.                         $j++;
  93.                     }
  94.                     $processedHeaders[] = [
  95.                         'label' => $label,
  96.                         'colspan' => $colspan,
  97.                     ];
  98.                     $i += $colspan;
  99.                 } else {
  100.                     $i++;
  101.                 }
  102.             }
  103.         } else {
  104.             $processedHeaders null;
  105.         }
  106.         // print_r($raw_data); exit;
  107.         return $this->render('backend/cleansing_raw/index.html.twig', [
  108.             'page_name' => $this->pageName,
  109.             'header' => $header,
  110.             'headers_grouped' => $processedHeaders,
  111.             'button_credential' => $this->buttonCredentials(),
  112.             'filter' => $form->createView(),
  113.             'pagination' => $pagination,
  114.         ]);
  115.     }
  116.     /**
  117.      * @Route("/cleansing_raw/export",name="cleansing_raw_export")
  118.      */
  119.     public function export(Request $requestManagerInterface $managerInterface)
  120.     {
  121.         $header $this->getDataHeader();
  122.         // print_r($header); exit;
  123.         $head1 $header[0]['head1'];
  124.         $head2 $header[1]['head2'];
  125.         $head_tambahan = ['kategori_plant' => 'KATEGORI PLANT''regional' => 'REGIONAL''status_pp_update' => 'STATUS PP UPDATE''kategori_tahap' => 'KATEGORI TAHAP''nilai_saving_terhadap_anggaran' => 'NILAI SAVING TERHADAP ANGGARAN''nilai_saving_terhadap_hps' => 'NILAI SAVING TERHADAP HPS''saving_persen_terhadap_angggaran' => 'NILAI SAVING TERHADAP ANGGARAN (%)''saving_persen_terhadap_hps' => 'NILAI SAVING TERHADAP HPS (%)'];
  126.         // Gabungkan semua field-key
  127.         $all_keys array_merge(array_keys($head2), array_keys($head_tambahan));
  128.         // Gabungkan label head2
  129.         $head2 array_merge($head2$head_tambahan);
  130.         // Head1 untuk tambahan diisi kosong
  131.         foreach ($head_tambahan as $key => $label) {
  132.             $head1[$key] = ''// tidak merge
  133.         }
  134.         // Ambil data
  135.         $array $this->getDataTableCleansing(nullnullnullnull);
  136.         $data $array['isi'] ?? [];
  137.         // Buat spreadsheet
  138.         $spreadsheet = new Spreadsheet();
  139.         $sheet $spreadsheet->getActiveSheet();
  140.         // == HEADER ==
  141.         $colIndex 1;
  142.         foreach ($all_keys as $key) {
  143.             $colLetter Coordinate::stringFromColumnIndex($colIndex);
  144.             // Head 1
  145.             $label1 $head1[$key] ?? '';
  146.             $sheet->setCellValue("{$colLetter}1"$label1);
  147.             // Head 2
  148.             $label2 $head2[$key] ?? '';
  149.             $sheet->setCellValue("{$colLetter}2"$label2);
  150.             $colIndex++;
  151.         }
  152.         // Merge Head1 yang perlu digabung (sama seperti sebelumnya)
  153.         $prevVal null;
  154.         $mergeStart 1;
  155.         $colIndex 1;
  156.         foreach ($all_keys as $i => $key) {
  157.             $label $head1[$key] ?? '';
  158.             if ($label !== '' && $label !== $prevVal) {
  159.                 if ($prevVal !== null && $colIndex $mergeStart) {
  160.                     $startLetter Coordinate::stringFromColumnIndex($mergeStart);
  161.                     $endLetter Coordinate::stringFromColumnIndex($colIndex 1);
  162.                     $sheet->mergeCells("{$startLetter}1:{$endLetter}1");
  163.                 }
  164.                 $mergeStart $colIndex;
  165.                 $prevVal $label;
  166.             }
  167.             $colIndex++;
  168.         }
  169.         // merge terakhir
  170.         if ($prevVal !== null && $colIndex >= $mergeStart) {
  171.             $startLetter Coordinate::stringFromColumnIndex($mergeStart);
  172.             $endLetter Coordinate::stringFromColumnIndex($colIndex 1);
  173.             $sheet->mergeCells("{$startLetter}1:{$endLetter}1");
  174.         }
  175.         // == ISI DATA ==
  176.         $rowIndex 3;
  177.         foreach ($data as $rowData) {
  178.             $colIndex 1;
  179.             foreach ($all_keys as $key) {
  180.                 $colLetter Coordinate::stringFromColumnIndex($colIndex);
  181.                 $sheet->setCellValue("{$colLetter}{$rowIndex}"$rowData[$key] ?? '');
  182.                 $colIndex++;
  183.             }
  184.             $rowIndex++;
  185.         }
  186.         // Styling
  187.         $lastColLetter Coordinate::stringFromColumnIndex(count($all_keys));
  188.         $sheet->getStyle("A1:{$lastColLetter}2")->getFont()->setBold(true);
  189.         foreach (range('A'$lastColLetter) as $col) {
  190.             $sheet->getColumnDimension($col)->setAutoSize(true);
  191.         }
  192.         // Output
  193.         $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  194.         ob_start();
  195.         $writer->save('php://output');
  196.         $content ob_get_clean();
  197.         return new \Symfony\Component\HttpFoundation\Response(
  198.             $content,
  199.             200,
  200.             [
  201.                 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  202.                 'Content-Disposition' => 'attachment; filename="export_cleansing_data.xlsx"',
  203.             ]
  204.         );
  205.     }
  206.     /**
  207.      * @Route("/cleansing_raw_data_table", name="cleansing_raw_data_table", methods={"GET", "POST"})
  208.      */
  209.     public function dataset_table(?string $idRequest $request)
  210.     {
  211.         // Ambil data dari sumber
  212.         $array $this->getDataTableCleansing(nullnullnullnull);
  213.         $items $array['isi'] ?? [];
  214.         // Ambil parameter datatables
  215.         $draw = (int) $request->get('draw'1);
  216.         $start = (int) $request->get('start'0);
  217.         $length = (int) $request->get('length'10);
  218.         // Ambil parameter pencarian per kolom
  219.         $columns $request->get("columns", []);
  220.         $columnSearch = [];
  221.         foreach ($columns as $col) {
  222.             $columnName $col["data"] ?? '';
  223.             $searchValue $col["search"]["value"] ?? '';
  224.             if ($columnName !== '') {
  225.                 $columnSearch[$columnName] = $searchValue;
  226.             }
  227.         }
  228.         // Filter data berdasarkan input pencarian
  229.         $filteredData array_filter($items, function ($row) use ($columnSearch) {
  230.             foreach ($columnSearch as $columnName => $search) {
  231.                 if ($search !== '') {
  232.                     if (!isset($row[$columnName]) || stripos($row[$columnName], $search) === false) {
  233.                         return false;
  234.                     }
  235.                 }
  236.             }
  237.             return true;
  238.         });
  239.         // Total & filtered
  240.         $recordsTotal count($items);
  241.         $recordsFiltered count($filteredData);
  242.         // Pagination
  243.         $pagedData array_slice($filteredData$start$length);
  244.         // Response JSON untuk DataTables
  245.         return new JsonResponse([
  246.             'draw' => $draw,
  247.             'recordsTotal' => $recordsTotal,
  248.             'recordsFiltered' => $recordsFiltered,
  249.             'data' => array_values($pagedData),
  250.         ]);
  251.     }
  252.     /**
  253.      * @Route("/cleansing_raw/progres", name="cleansing_raw_progres", methods={"POST"})
  254.      */
  255.     public function progress(TProgresRepository $tProgresRepo)
  256.     {
  257.         $progress $tProgresRepo->findOneBy(['reff_kode' => 'CLEANSING_RAW']);
  258.         $current null;
  259.         $total null;
  260.         if ($progress) {
  261.             $current $progress->getDataMasuk();
  262.             $total $progress->getJumlahData();
  263.         }
  264.         return new JsonResponse([
  265.             'show' => $progress true false,
  266.             'current' => $current,
  267.             'total' => $total
  268.         ]);
  269.     }
  270. }