/**
  * @Route("/protocol/{protocol_id}/monitoring/{monitoring_action_id}", name="protocol_new_monitoring_that_not_amendment")
  * @Template()
  */
 public function monitoringCreateThatNotAmendmentAction($protocol_id, $monitoring_action_id)
 {
     $output = array();
     $request = $this->getRequest();
     $session = $request->getSession();
     $translator = $this->get('translator');
     $em = $this->getDoctrine()->getManager();
     $user = $this->get('security.token_storage')->getToken()->getUser();
     $util = new Util($this->container, $this->getDoctrine());
     $protocol_repository = $em->getRepository('Proethos2ModelBundle:Protocol');
     $monitoring_action_repository = $em->getRepository('Proethos2ModelBundle:MonitoringAction');
     $upload_type_repository = $em->getRepository('Proethos2ModelBundle:UploadType');
     $submission_upload_repository = $em->getRepository('Proethos2ModelBundle:SubmissionUpload');
     $user_repository = $em->getRepository('Proethos2ModelBundle:User');
     // getting the current submission
     $protocol = $protocol_repository->find($protocol_id);
     $output['protocol'] = $protocol;
     if (!$protocol) {
         throw $this->createNotFoundException($translator->trans('No protocol found'));
     }
     if (!$protocol->getMainSubmission()->isOwner($user)) {
         throw $this->createNotFoundException($translator->trans('You don\'t have access to do this'));
     }
     $monitoring_action = $monitoring_action_repository->find($monitoring_action_id);
     $output['monitoring_action'] = $monitoring_action;
     $submission = $protocol->getMainSubmission();
     $output['submission'] = $submission;
     $upload_types = $upload_type_repository->findByStatus(true);
     $output['upload_types'] = $upload_types;
     if (!$monitoring_action) {
         throw $this->createNotFoundException($translator->trans('Monitoring action does not exist'));
     }
     // checking if was a post request
     if ($this->getRequest()->isMethod('POST')) {
         // getting post data
         $post_data = $request->request->all();
         $file = $request->files->get('new-atachment-file');
         if (!empty($file)) {
             if (!isset($post_data['new-atachment-type']) or empty($post_data['new-atachment-type'])) {
                 $session->getFlashBag()->add('error', $translator->trans("Field 'new-atachment-type' is required."));
                 return $output;
             }
             $upload_type = $upload_type_repository->find($post_data['new-atachment-type']);
             if (!$upload_type) {
                 throw $this->createNotFoundException($translator->trans('No upload type found'));
                 return $output;
             }
             $submission_upload = new SubmissionUpload();
             $submission_upload->setSubmission($submission);
             $submission_upload->setUploadType($upload_type);
             $submission_upload->setUser($user);
             $submission_upload->setFile($file);
             $submission_upload->setSubmissionNumber($submission->getNumber());
             $submission_upload->setIsMonitoringAction(true);
             $em = $this->getDoctrine()->getManager();
             $em->persist($submission_upload);
             $em->flush();
             $submission->addAttachment($submission_upload);
             $em = $this->getDoctrine()->getManager();
             $em->persist($submission);
             $em->flush();
             $session->getFlashBag()->add('success', $translator->trans("File uploaded with sucess."));
             return $this->redirectToRoute('protocol_new_monitoring_that_not_amendment', array('protocol_id' => $protocol_id, 'monitoring_action_id' => $monitoring_action_id), 301);
         }
         if (isset($post_data['delete-attachment-id']) and !empty($post_data['delete-attachment-id'])) {
             $submission_upload = $submission_upload_repository->find($post_data['delete-attachment-id']);
             if ($submission_upload) {
                 $em->remove($submission_upload);
                 $em->flush();
                 $session->getFlashBag()->add('success', $translator->trans("File removed with sucess."));
                 return $output;
             }
         }
         // checking required files
         foreach (array('monitoring-action', 'justification') as $field) {
             if (!isset($post_data[$field]) or empty($post_data[$field])) {
                 $session->getFlashBag()->add('error', $translator->trans("Field '%field%' is required.", array("%field%" => $field)));
                 return $output;
             }
         }
         foreach ($protocol->getMainSubmission()->getAttachments() as $submission_upload) {
             $submission_upload->setIsMonitoringAction(false);
             $em->persist($submission_upload);
             $em->flush();
         }
         $protocol->setMonitoringAction($monitoring_action);
         $em->persist($protocol);
         $em->flush();
         // setting the Rejected status
         $protocol->setStatus("S");
         $protocol->setUpdatedIn(new \DateTime());
         // setting protocool history
         $message = $translator->trans("New amendment submited by");
         $message .= ' "' . $user . '" ';
         $message .= $translator->trans("with this justification:");
         $message .= ' "' . $post_data['justification'] . '"';
         $protocol_history = new ProtocolHistory();
         $protocol_history->setProtocol($protocol);
         $protocol_history->setMessage($message);
         $em->persist($protocol_history);
         $em->flush();
         // sending email
         $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
         $url = $baseurl . $this->generateUrl('protocol_show_protocol', array("protocol_id" => $protocol->getId()));
         $recipients = array();
         foreach ($user_repository->findAll() as $secretary) {
             if (in_array("secretary", $secretary->getRolesSlug())) {
                 $recipients[] = $secretary;
             }
         }
         foreach ($recipients as $recipient) {
             $message = \Swift_Message::newInstance()->setSubject("[proethos2] " . $translator->trans("A new monitoring action has been submitted."))->setFrom($util->getConfiguration('committee.email'))->setTo($recipient->getEmail())->setBody($translator->trans("Hello!") . "<br>" . "<br>" . $translator->trans("A new monitoring action has been submitted. Access the link below for more details") . ":" . "<br>" . "<br>{$url}" . "<br>" . "<br>" . $translator->trans("Regards") . "," . "<br>" . $translator->trans("Proethos2 Team"), 'text/html');
             $send = $this->get('mailer')->send($message);
         }
         $session->getFlashBag()->add('success', $translator->trans("Amendment submitted with success!"));
         return $this->redirectToRoute('crud_investigator_protocol_list', array(), 301);
     }
     return $output;
 }
 /**
  * @Route("/submission/new/{submission_id}/seventh", name="submission_new_seventh_step")
  * @Template()
  */
 public function SeventhStepAction($submission_id)
 {
     $output = array();
     $request = $this->getRequest();
     $session = $request->getSession();
     $translator = $this->get('translator');
     $em = $this->getDoctrine()->getManager();
     $user = $this->get('security.token_storage')->getToken()->getUser();
     $util = new Util($this->container, $this->getDoctrine());
     $submission_repository = $em->getRepository('Proethos2ModelBundle:Submission');
     $upload_type_repository = $em->getRepository('Proethos2ModelBundle:UploadType');
     $user_repository = $em->getRepository('Proethos2ModelBundle:User');
     // getting the current submission
     $submission = $submission_repository->find($submission_id);
     $output['submission'] = $submission;
     if (!$submission or $submission->getIsSended()) {
         throw $this->createNotFoundException($translator->trans('No submission found'));
     }
     $allow_to_edit_submission = true;
     $user = $this->get('security.token_storage')->getToken()->getUser();
     // if current user is not owner, check the team
     if ($user != $submission->getOwner()) {
         $allow_to_edit_submission = false;
         foreach ($submission->getTeam() as $team_member) {
             // if current user = some team member, than it allows to edit
             if ($user == $team_member) {
                 $allow_to_edit_submission = true;
             }
         }
     }
     if (!$allow_to_edit_submission) {
         throw $this->createNotFoundException($translator->trans('No submission found'));
     }
     // Revisions
     $revisions = array();
     $final_status = true;
     $text = $translator->trans('Team') . " (" . count($submission->getTeam()) + 1 . " " . $translator->trans('member(s)') . ")";
     $item = array('text' => $text, 'status' => true);
     $revisions[] = $item;
     // $text = $translator->trans('Files Submited') . " (" . count($submission->getAttachments()) . " " . $translator->trans('files(s)') . ")";
     // $item = array('text' => $text, 'status' => true);
     // if(count($submission->getAttachments()) == 0) {
     //     $item = array('text' => $text, 'status' => false);
     //     $final_status = false;
     // }
     // $revisions[] = $item;
     $text = $translator->trans('Abstract');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getAbstract())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Keywords');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getKeywords())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Introduction');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getIntroduction())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Justification');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getJustification())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Goals');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getGoals())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Study Design');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getStudyDesign())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Gender');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getGender())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Minimum Age');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getMinimumAge())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Maximum Age');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getMaximumAge())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Inclusion Criteria');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getInclusionCriteria())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Exclusion Criteria');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getExclusionCriteria())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Inicial recruitment estimated date');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getRecruitmentInitDate())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Interventions');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getInterventions())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Primary Outcome');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getPrimaryOutcome())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Funding Source');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getFundingSource())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Primary Sponsor');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getPrimarySponsor())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Bibliography');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getBibliography())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $text = $translator->trans('Scientific Contact');
     $item = array('text' => $text, 'status' => true);
     if (empty($submission->getSscientificContact())) {
         $item = array('text' => $text, 'status' => false);
         $final_status = false;
     }
     $revisions[] = $item;
     $output['revisions'] = $revisions;
     $output['final_status'] = $final_status;
     // checking if was a post request
     if ($this->getRequest()->isMethod('POST')) {
         // getting post data
         $post_data = $request->request->all();
         if ($final_status) {
             if ($post_data['accept-terms'] == 'on') {
                 // gerando um novo pdf
                 try {
                     $html = $this->renderView('Proethos2CoreBundle:NewSubmission:showPdf.html.twig', $output);
                     $pdf = $this->get('knp_snappy.pdf');
                     // setting margins
                     $pdf->getInternalGenerator()->setOption('margin-top', '50px');
                     $pdf->getInternalGenerator()->setOption('margin-bottom', '50px');
                     $pdf->getInternalGenerator()->setOption('margin-left', '20px');
                     $pdf->getInternalGenerator()->setOption('margin-right', '20px');
                     // adding pdf to tmp file
                     $filepath = "/tmp/" . date("Y-m-d") . "-submission.pdf";
                     file_put_contents($filepath, $pdf->getOutputFromHtml($html));
                     $submission_number = count($submission->getProtocol()->getSubmission());
                     $upload_type = $upload_type_repository->findOneBy(array("slug" => "protocol"));
                     // send tmp file to upload class and save
                     $pdfFile = new SubmissionUpload();
                     $pdfFile->setSubmission($submission);
                     $pdfFile->setSimpleFile($filepath);
                     $pdfFile->setUploadType($upload_type);
                     $pdfFile->setUser($user);
                     $pdfFile->setSubmissionNumber($submission->getNumber());
                     $em->persist($pdfFile);
                     $em->flush();
                 } catch (\RuntimeException $e) {
                     $session->getFlashBag()->add('error', $translator->trans('Problems generating PDF. Please contact the administrator.'));
                     return $output;
                 }
                 // updating protocol and setting status
                 $protocol = $submission->getProtocol();
                 $protocol->setStatus("S");
                 $protocol->setDateInformed(new \DateTime());
                 $protocol->setUpdatedIn(new \DateTime());
                 $em->persist($protocol);
                 $em->flush();
                 $submission->setIsSended(true);
                 $em->persist($submission);
                 $em->flush();
                 $protocol_history = new ProtocolHistory();
                 $protocol_history->setProtocol($protocol);
                 $protocol_history->setMessage($translator->trans("Submission of protocol."));
                 $em->persist($protocol_history);
                 $em->flush();
                 if ($protocol->getMonitoringAction()) {
                     // sending email
                     $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
                     $url = $baseurl . $this->generateUrl('protocol_show_protocol', array("protocol_id" => $protocol->getId()));
                     $recipients = array();
                     foreach ($user_repository->findAll() as $secretary) {
                         if (in_array("secretary", $secretary->getRolesSlug())) {
                             $recipients[] = $secretary;
                         }
                     }
                     foreach ($recipients as $recipient) {
                         $message = \Swift_Message::newInstance()->setSubject("[proethos2] " . $translator->trans("A new monitoring action has been submitted."))->setFrom($util->getConfiguration('committee.email'))->setTo($recipient->getEmail())->setBody($translator->trans("Hello!") . "<br>" . "<br>" . $translator->trans("A new monitoring action has been submitted. Access the link below for more details") . ":" . "<br>" . "<br>{$url}" . "<br>" . "<br>" . $translator->trans("Regards") . "," . "<br>" . $translator->trans("Proethos2 Team"), 'text/html');
                         $send = $this->get('mailer')->send($message);
                     }
                     $session->getFlashBag()->add('success', $translator->trans("Amendment submitted with success!"));
                 } else {
                     $session->getFlashBag()->add('success', $translator->trans("Protocol submitted with sucess!"));
                 }
                 return $this->redirectToRoute('protocol_show_protocol', array('protocol_id' => $protocol->getId()), 301);
             } else {
                 $session->getFlashBag()->add('error', $translator->trans("You must accept the terms and conditions."));
             }
         } else {
             $session->getFlashBag()->add('error', $translator->trans('You have pending reviews.'));
         }
     }
     return $output;
 }