src/Controller/OverviewController.php line 17
<?phpnamespace App\Controller;use App\Entity\WorkRecord;use App\Repository\WorkRecordRepository;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;class OverviewController extends AbstractController{/*** @Route("/", name="overview")*/public function index(Request $request, WorkRecordRepository $workRecordRepository): Response{$filter = $request->query->get('filter');$startDate = null;$endDate = null;if ($filter === 'last_week') {$startDate = new \DateTime('-7 days');} elseif ($filter === 'last_month') {$startDate = new \DateTime('first day of last month');$endDate = new \DateTime('last day of last month');} elseif ($filter === 'last_year') {$startDate = new \DateTime('-1 year');$endDate = new \DateTime('now');} elseif ($filter !== null && is_numeric($filter)) {// Handle filter by month$year = date('Y');$month = (int)$filter;$daysInMonth = (int)date('t', mktime(0, 0, 0, $month, 1, $year));$startDate = new \DateTime("$year-$month-01");$endDate = new \DateTime("$year-$month-$daysInMonth");}// Ensure that startDate is always setif ($startDate === null) {$startDate = new \DateTime('first day of this month');}$user = $this->getUser();$workRecords = $workRecordRepository->findRecordsInDateRange($user, $startDate, $endDate);return $this->render('overview.html.twig', ['user' => $user,'workRecords' => $workRecords,'filter' => $filter,]);}/*** @Route("/export-csv", name="export_csv")*/public function exportCsv(Request $request, WorkRecordRepository $workRecordRepository): Response{$filter = $request->query->get('filter');$startDate = null;$endDate = null;// Set the date range based on the filterif ($filter === 'last_week') {$startDate = new \DateTime('-7 days');} elseif ($filter === 'last_month') {$startDate = new \DateTime('first day of last month');$endDate = new \DateTime('last day of last month');} elseif ($filter === 'last_year') {$startDate = new \DateTime('-1 year');$endDate = new \DateTime('now');} elseif ($filter !== null && is_numeric($filter)) {// Handle filter by month$year = date('Y');$month = (int)$filter;$daysInMonth = (int)date('t', mktime(0, 0, 0, $month, 1, $year));$startDate = new \DateTime("$year-$month-01");$endDate = new \DateTime("$year-$month-$daysInMonth");}// Set a default start date if it's not definedif ($startDate === null) {$startDate = new \DateTime('first day of this month');}// Fetch records based on the filter$user = $this->getUser();$workRecords = $workRecordRepository->findRecordsInDateRange($user, $startDate, $endDate);// Prepare CSV data$csvData = [];$csvData[] = ['Start Time', 'Duration', 'Department', 'Type of Work', 'Description'];foreach ($workRecords as $workRecord) {$csvData[] = [$workRecord->getStartTime()->format('d-m-Y H:i:s'),$workRecord->getDuration(),$workRecord->getDepartment(),$workRecord->getTypeOfWork(),$workRecord->getDescription(),];}// Prepare CSV response$response = new Response($this->arrayToCsv($csvData));$response->headers->set('Content-Type', 'text/csv');$response->headers->set('Content-Disposition', 'attachment; filename="vykaz-prace.csv"');return $response;}/*** Convert a 2D array to a CSV string** @param array $data* @param string $delimiter* @param string $enclosure* @param string $escapeChar* @return string*/private function arrayToCsv(array $data,string $delimiter = ',',string $enclosure = '"',string $escapeChar = '\\'): string {$output = '';$stream = fopen('php://temp', 'r+');foreach ($data as $row) {fputcsv($stream, $row, $delimiter, $enclosure, $escapeChar);}rewind($stream);while (($csvRow = fgets($stream)) !== false) {$output .= $csvRow;}fclose($stream);return $output;}}