/**
  * @param \Symfony\Component\Form\FormBuilderInterface $builder
  * @param array $options
  *
  * @return void
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->add(self::FIELD_IGNORE_DATE, HiddenType::class)->add(self::FIELD_FILE, FileType::class, ['label' => 'field_notes.upload.file']);
     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
         $data = $event->getData();
         $form = $event->getForm();
         // check if we have a last log date
         // if so, show the checkbox to ignore logs before this date
         if ($data[self::FIELD_IGNORE_DATE]) {
             $date = DateUtil::dateTimeFromMySqlFormat($data[self::FIELD_IGNORE_DATE])->format($this->translator->trans('field_notes.date_format'));
             $form->add(self::FIELD_IGNORE, CheckboxType::class, ['required' => false, 'attr' => ['checked' => 'checked'], 'label' => $this->translator->trans('field_notes.upload.label.ignore', ['%date%' => $date])]);
         }
     });
 }
 /**
  * @Route("/field-notes/", name="field-notes")
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function indexAction(Request $request)
 {
     $this->denyAccessUnlessGranted('ROLE_USER');
     $user = $this->getUser();
     $fieldNoteService = $this->get('app.service.field_note');
     $dataProvider = $this->get('app.dataprovider.upload_field_note');
     $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
     $fieldNotes = $repository->findBy(['user' => $user->getId()], ['date' => 'ASC', 'id' => 'ASC']);
     $form = $this->createForm(UploadFieldNotesType::class, $dataProvider->getData($user->getId()));
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
         $file = $form->getData()[UploadFieldNotesType::FIELD_FILE];
         try {
             $ignoreDate = null;
             if ($form->getData()[UploadFieldNotesType::FIELD_IGNORE] && $form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE]) {
                 $ignoreDate = DateUtil::dateTimeFromMySqlFormat($form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE]);
             }
             $fieldNoteService->importFromFile($file->getRealPath(), $user->getId(), $ignoreDate);
         } catch (\Exception $e) {
             $this->addErrorMessage($e->getMessage());
             return $this->redirectToRoute('field-notes');
         }
         if ($fieldNoteService->hasErrors()) {
             foreach ($fieldNoteService->getErrors() as $error) {
                 $this->addErrorMessage($error);
             }
             return $this->redirectToRoute('field-notes');
         }
         $this->addSuccessMessage($this->get('translator')->trans('field_notes.upload.success'));
         return $this->redirectToRoute('field-notes');
     }
     $this->setMenu(MNU_MYPROFILE_FIELD_NOTES);
     $this->setTitle($this->get('translator')->trans('field_notes.field_notes'));
     return $this->render('field-notes/index.html.twig', ['user' => $user, 'form' => $form->createView(), 'fieldNotes' => $fieldNotes]);
 }
 /**
  * @param string $entityName
  * @param int $userId
  *
  * @return \DateTime|null
  */
 protected function getMaxDateFromEntityByUserId($entityName, $userId)
 {
     $max = null;
     $query = $this->entityManager->createQueryBuilder();
     $query->select('MAX(e.date) AS max_date')->from($entityName, 'e')->where('e.user = :user_id')->setParameter('user_id', $userId)->setMaxResults(1);
     $result = $query->getQuery()->getResult();
     if ($result && isset($result[0]['max_date'])) {
         $max = DateUtil::dateTimeFromMySqlFormat($result[0]['max_date']);
     }
     return $max;
 }