/**
  * {@inherited}
  */
 public function save()
 {
     $answers = array_filter($this->getRequest()->get('option_answers', array()));
     $correct = array_filter($this->getRequest()->get('correct_answers', array()));
     if (empty($answers)) {
         $this->getFlashBag()->add('message', array('type' => 'error', 'message' => 'Set the answers!'));
         return false;
     }
     try {
         $this->getManager()->getConnection()->beginTransaction();
         $data = $this->getForm()->getData();
         $questionId = $data->getId();
         if (!empty($questionId)) {
             $results = $this->getManager()->getRepository('CekurteZCPEBundle:QuestionHasAnswer')->findBy(array('question' => $data->getId()));
             foreach ($results as $key => $result) {
                 $this->getManager()->remove($result);
             }
         }
         $id = parent::save();
         foreach ($answers as $index => $answer) {
             $answerEntity = new Answer();
             $answerEntity->setTitle($answer)->setDeleted(false)->setCreatedBy($this->getUser())->setCreatedAt(new \DateTime('NOW'));
             $this->getManager()->persist($answerEntity);
             $this->getManager()->flush();
             $correctAnswer = false;
             foreach ($correct as $key => $indexCorrect) {
                 if ($indexCorrect == $index) {
                     $correctAnswer = true;
                 }
             }
             $questionAnswerEntity = new QuestionHasAnswer();
             $questionAnswerEntity->setAnswer($answerEntity)->setQuestion($data)->setCorrect($correctAnswer);
             $this->getManager()->persist($questionAnswerEntity);
             $this->getManager()->flush();
         }
         $question = $this->getManager()->getRepository('CekurteZCPEBundle:Question')->find($id);
         $question->setApproved($this->getSecurityContext()->isGranted('ROLE_ADMIN'));
         $this->getManager()->persist($question);
         $this->getManager()->flush();
         $this->getManager()->getConnection()->commit();
         return $id;
     } catch (\Exception $e) {
         $this->getFlashBag()->clear();
         $this->getFlashBag()->add('message', array('type' => 'error', 'message' => $e->getMessage()));
         $this->getManager()->getConnection()->rollback();
         return false;
     }
     return false;
 }
 /**
  * @Route("/admin/parser-database-content")
  * @Method("GET")
  * @Template("CekurteZCPEBundle:Default:parser.html.twig")
  */
 public function parserDatabaseContentAction()
 {
     set_time_limit(0);
     $em = $this->get('doctrine')->getManager();
     $itens = $em->getRepository('CekurteZCPEBundle:Parser')->findAll();
     foreach ($itens as $item) {
         $subject = $item->getSubject();
         $content = $item->getContent();
         if (preg_match_all("/[\r\n]+[^:alpha]{1}\\s*[\\:\\)\\/]{1}\\s*(.*)/", $content, $matches)) {
             $answers = end($matches);
             $answersComplete = $matches[0];
             $question = $content;
             foreach ($answersComplete as $answer) {
                 $question = str_replace($answer, '', $question);
             }
         }
         if (preg_match_all("/[\r\n]+[^:alpha]{1}\\s*[\\:\\)\\/]{1}\\s*(.*)/", $content, $matches)) {
             $answers = end($matches);
             $answersComplete = $matches[0];
             $question = $content;
             foreach ($answersComplete as $answer) {
                 $question = str_replace($answer, '', $question);
             }
         }
         $questionType = 'Single Choice';
         if (preg_match_all("/.*(\\_{5}).*/", $content, $matches)) {
             $questionType = 'Text';
         }
         if (preg_match("/\\(\\s*choose\\s*(\\d+)\\)/i", $content, $matches)) {
             $choose = (int) end($matches);
             if ($choose > 1) {
                 $questionType = 'Multiple Choice';
             }
             $question = str_replace($matches[0], '', $question);
         }
         if (preg_match("/\\d{1,4}/", $subject, $matches)) {
             $googleGroupsId = (int) $matches[0];
         }
         $category = array();
         if (preg_match("/(Categoria|Category){1}\\:{1}\\s+(.*)\\./i", $content, $matches)) {
             $category = explode(', ', end($matches));
         }
         $entity = $em->getRepository('CekurteZCPEBundle:Question')->findOneBy(array('googleGroupsId' => $googleGroupsId));
         if (!$entity instanceof Question) {
             $entity = new Question();
             $entity->setDeleted(false)->setCreatedBy($this->getUser())->setCreatedAt($item->getCreatedAt())->setGoogleGroupsId($googleGroupsId)->setGoogleGroupsAuthor($item->getAuthor())->setTitle(trim(nl2br($question)))->setComment($content);
             $questionTypeEntity = $em->getRepository('CekurteZCPEBundle:QuestionType')->findOneBy(array('title' => trim($questionType)));
             if ($questionTypeEntity instanceof QuestionType) {
                 $entity->setQuestionType($questionTypeEntity);
             }
             if (!empty($category)) {
                 foreach ($category as $title) {
                     $categoryEntity = $em->getRepository('CekurteZCPEBundle:Category')->findOneBy(array('title' => trim($title)));
                     if ($categoryEntity instanceof Category) {
                         $entity->addCategory($categoryEntity);
                     }
                 }
             }
             if (!empty($answers)) {
                 $em->persist($entity);
                 $em->flush();
                 $entity->setApproved(false)->setImportedFromGoogleGroups(true)->setDifficulty(0)->setEmailHasSent(true);
                 $em->persist($entity);
                 $em->flush();
                 foreach ($answers as $answer) {
                     $answerEntity = new Answer();
                     $answerEntity->setTitle($answer)->setDeleted(false)->setCreatedBy($this->getUser())->setCreatedAt(new \DateTime('NOW'));
                     $em->persist($answerEntity);
                     $em->flush();
                     $answerEntityRel = new QuestionHasAnswer();
                     $answerEntityRel->setAnswer($answerEntity)->setQuestion($entity)->setCorrect(false);
                     $em->persist($answerEntityRel);
                     $em->flush();
                 }
             }
         }
     }
     return array();
 }