getDefaultQuery(); $showPreview = false; $tooManyResults = false; $maxItemsPreview = 500; $entries = []; $form = $this->getToolbarForm($query, 'GET'); if ($this->handleSearch($form, $request)) { return $this->redirectToRoute('export'); } $byCustomer = []; if ($form->isValid() && ($query->hasBookmark() || $request->query->has('performSearch'))) { try { $showPreview = true; $entries = $this->getEntries($query); foreach ($entries as $entry) { $cid = $entry->getProject()->getCustomer()->getId(); if (!isset($byCustomer[$cid])) { $byCustomer[$cid] = [ 'customer' => $entry->getProject()->getCustomer(), 'rate' => 0, 'internalRate' => 0, 'duration' => 0, ]; } $byCustomer[$cid]['rate'] += $entry->getRate(); $byCustomer[$cid]['internalRate'] += $entry->getInternalRate(); $byCustomer[$cid]['duration'] += $entry->getDuration(); } } catch (TooManyItemsExportException $ex) { $tooManyResults = true; $showPreview = false; $entries = []; $this->logException($ex); } } $page = new PageSetup('export'); $page->setHelp('export.html'); return $this->render('export/index.html.twig', [ 'page_setup' => $page, 'too_many' => $tooManyResults, 'by_customer' => $byCustomer, 'query' => $query, 'entries' => $entries, 'form' => $form->createView(), 'renderer' => $this->export->getRenderer(), 'preview_limit' => $maxItemsPreview, 'preview_show' => $showPreview, 'decimal' => $this->getUser()->isExportDecimal(), ]); } #[Route(path: '/data', name: 'export_data', methods: ['POST'])] public function export(Request $request): Response { $query = $this->getDefaultQuery(); $form = $this->getToolbarForm($query, 'POST'); $form->handleRequest($request); $type = $query->getRenderer(); if (null === $type) { throw $this->createNotFoundException('Missing export renderer'); } $renderer = $this->export->getRendererById($type); if (null === $renderer) { throw $this->createNotFoundException('Unknown export renderer'); } // display file inline if supported and `markAsExported` is not set if ($renderer instanceof DispositionInlineInterface && !$query->isMarkAsExported()) { $renderer->setDispositionInline(true); } $entries = $this->getEntries($query); $response = $renderer->render($entries, $query); if ($query->isMarkAsExported()) { $this->export->setExported($entries); } return $response; } private function getDefaultQuery(): ExportQuery { $begin = $this->getDateTimeFactory()->getStartOfMonth(); $end = $this->getDateTimeFactory()->getEndOfMonth(); $query = new ExportQuery(); $query->setBegin($begin); $query->setEnd($end); $query->setCurrentUser($this->getUser()); return $query; } /** * @param ExportQuery $query * @return ExportableItem[] * @throws TooManyItemsExportException */ private function getEntries(ExportQuery $query): array { if (null !== $query->getBegin()) { $query->getBegin()->setTime(0, 0, 0); } if (null !== $query->getEnd()) { $query->getEnd()->setTime(23, 59, 59); } return $this->export->getExportItems($query); } private function getToolbarForm(ExportQuery $query, string $method): FormInterface { return $this->createSearchForm(ExportToolbarForm::class, $query, [ 'action' => $this->generateUrl('export', []), 'include_user' => $this->isGranted('view_other_timesheet'), 'include_export' => $this->isGranted('edit_export_other_timesheet'), 'method' => $method, 'timezone' => $this->getDateTimeFactory()->getTimezone()->getName(), 'attr' => [ 'id' => 'export-form' ] ]); } }