src/Controller/OverviewController.php line 17

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\WorkRecord;
  4. use App\Repository\WorkRecordRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class OverviewController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/", name="overview")
  13.      */
  14.     public function index(Request $requestWorkRecordRepository $workRecordRepository): Response
  15.     {
  16.         $filter $request->query->get('filter');
  17.         $startDate null;
  18.         $endDate null;
  19.     
  20.         if ($filter === 'last_week') {
  21.             $startDate = new \DateTime('-7 days');
  22.         } elseif ($filter === 'last_month') {
  23.             $startDate = new \DateTime('first day of last month');
  24.             $endDate = new \DateTime('last day of last month');
  25.         } elseif ($filter === 'last_year') {
  26.             $startDate = new \DateTime('-1 year');
  27.             $endDate = new \DateTime('now');
  28.         } elseif ($filter !== null && is_numeric($filter)) {
  29.             // Handle filter by month
  30.             $year date('Y');
  31.             $month = (int)$filter;
  32.             $daysInMonth = (int)date('t'mktime(000$month1$year));
  33.             $startDate = new \DateTime("$year-$month-01");
  34.             $endDate = new \DateTime("$year-$month-$daysInMonth");
  35.         }
  36.     
  37.         // Ensure that startDate is always set
  38.         if ($startDate === null) {
  39.             $startDate = new \DateTime('first day of this month');
  40.         }
  41.     
  42.         $user $this->getUser();
  43.         $workRecords $workRecordRepository->findRecordsInDateRange($user$startDate$endDate);
  44.     
  45.         return $this->render('overview.html.twig', [
  46.             'user' => $user,
  47.             'workRecords' => $workRecords,
  48.             'filter' => $filter,
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/export-csv", name="export_csv")
  53.      */
  54.     public function exportCsv(Request $requestWorkRecordRepository $workRecordRepository): Response
  55.     {
  56.         $filter $request->query->get('filter');
  57.         $startDate null;
  58.         $endDate null;
  59.         // Set the date range based on the filter
  60.         if ($filter === 'last_week') {
  61.             $startDate = new \DateTime('-7 days');
  62.         } elseif ($filter === 'last_month') {
  63.             $startDate = new \DateTime('first day of last month');
  64.             $endDate = new \DateTime('last day of last month');
  65.         } elseif ($filter === 'last_year') {
  66.             $startDate = new \DateTime('-1 year');
  67.             $endDate = new \DateTime('now');
  68.         } elseif ($filter !== null && is_numeric($filter)) {
  69.             // Handle filter by month
  70.             $year date('Y');
  71.             $month = (int)$filter;
  72.             $daysInMonth = (int)date('t'mktime(000$month1$year));
  73.             $startDate = new \DateTime("$year-$month-01");
  74.             $endDate = new \DateTime("$year-$month-$daysInMonth");
  75.         }
  76.         // Set a default start date if it's not defined
  77.         if ($startDate === null) {
  78.             $startDate = new \DateTime('first day of this month');
  79.         }
  80.         // Fetch records based on the filter
  81.         $user $this->getUser();
  82.         $workRecords $workRecordRepository->findRecordsInDateRange($user$startDate$endDate);
  83.         // Prepare CSV data
  84.         $csvData = [];
  85.         $csvData[] = ['Start Time''Duration''Department''Type of Work''Description'];
  86.         foreach ($workRecords as $workRecord) {
  87.             $csvData[] = [
  88.                 $workRecord->getStartTime()->format('d-m-Y H:i:s'),
  89.                 $workRecord->getDuration(),
  90.                 $workRecord->getDepartment(),
  91.                 $workRecord->getTypeOfWork(),
  92.                 $workRecord->getDescription(),
  93.             ];
  94.         }
  95.         // Prepare CSV response
  96.         $response = new Response($this->arrayToCsv($csvData));
  97.         $response->headers->set('Content-Type''text/csv');
  98.         $response->headers->set('Content-Disposition''attachment; filename="vykaz-prace.csv"');
  99.         return $response;
  100.     }
  101.     /**
  102.      * Convert a 2D array to a CSV string
  103.      *
  104.      * @param array $data
  105.      * @param string $delimiter
  106.      * @param string $enclosure
  107.      * @param string $escapeChar
  108.      * @return string
  109.      */
  110.     private function arrayToCsv(
  111.         array $data,
  112.         string $delimiter ',',
  113.         string $enclosure '"',
  114.         string $escapeChar '\\'
  115.     ): string {
  116.         $output '';
  117.         $stream fopen('php://temp''r+');
  118.         foreach ($data as $row) {
  119.             fputcsv($stream$row$delimiter$enclosure$escapeChar);
  120.         }
  121.         rewind($stream);
  122.         while (($csvRow fgets($stream)) !== false) {
  123.             $output .= $csvRow;
  124.         }
  125.         fclose($stream);
  126.         return $output;
  127.     }
  128. }