/**
  * @Route("/documents/document/", name="documentBundle_show_document_2")
  * @Route("/documents/document/{id}", name="documentBundle_show_document", requirements={"id" = "\d+"}, defaults={"id" = 0})
  * @Template()
  */
 public function DocumentProfileAction(Request $request, $id)
 {
     //Get all UploadProfiles
     $allUploadProfiles = $this->getDoctrine()->getRepository('DocumentBundle:UploadProfiles')->findBy(array(), array('tag' => 'DESC', 'name' => 'DESC'));
     //Get all Agencies
     $allAgencies = $this->getDoctrine()->getRepository('AppBundle:Agency')->findAll();
     /*
      * CHECK FOR PERMISSIONS
      * TODO: Check if the acting user has all needed permissions to perform this action.
      */
     if ($this->get('security.authorization_checker')->isGranted('ROLE_APP_ADMIN')) {
         /*
          * GET DOCUMENT FROM DB
          */
         $em = $this->getDoctrine()->getManager();
         $document = $em->getRepository('DocumentBundle:Document')->find($id);
         $uploadprofile = $em->getRepository('DocumentBundle:UploadProfiles')->find($id);
         /*
          * CHECK Document
          * Does it exist? Is the current user allowed to see this document?
          */
         if ($document == null) {
             $error_msg = 'error.404';
             // Error message
             return $this->render('UserBundle:Error:error.html.twig', array('error_msg' => $error_msg));
         }
         $new_comment = new \CommentBundle\Entity\Comment();
         $new_comment->setDocument($document);
         $new_comment->setAuthor($this->get('security.token_storage')->getToken()->getUser());
         //            /*
         //             * Get the documents which are to display on the current site from the database
         //             */
         //            $dql = "SELECT d FROM DocumentBundle:Document d
         //                        WHERE d.signed = true and d.active = false
         //                        ORDER BY d.name ASC";
         //
         //            $query = $em->createQuery($dql)
         //                ->setMaxResults(10);
         //            $pagination = new Paginator($query, $fetchJoinCollection = true);
         //
         //            // Get the total amount of documents in the database
         //            $itemCount = count($pagination);
         $form = $this->createFormBuilder($new_comment)->add('comment', 'textarea', array('label' => false, 'required' => true, 'attr' => array('rows' => '5', 'placeholder' => 'Add new comment')))->add('save', 'submit', array('label' => 'form.buttons.add_comment', 'translation_domain' => 'Documents'))->getForm();
         /*
          * IF REQUEST IS VALID
          */
         $form->handleRequest($request);
         if ($form->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $em->persist($new_comment);
             $em->flush();
             return $this->render('DocumentBundle:Document:DocumentProfile.html.twig', array('document' => $document, 'menu' => array('top' => 'Document'), 'form' => $form->createView(), 'uploadprofile' => $uploadprofile, 'success' => true));
         }
         /*
          * DISPLAY Document
          */
         return $this->render('DocumentBundle:Document:DocumentProfile.html.twig', array('document' => $document, 'menu' => array('top' => 'Document'), 'uploadprofile' => $uploadprofile, 'allUploadProfiles' => $allUploadProfiles, 'allAgencies' => $allAgencies, 'form' => $form->createView()));
     } else {
         /*
          * THROW ERROR
          * If the acting user is not allowed to perform this action
          */
         $error_msg = 'error.access.action';
         // Error message
         return $this->render('UserBundle:Error:error.html.twig', array('error_msg' => $error_msg));
     }
 }
 /**
  * @Route("/documents/uploadprofiles/{id}/add", name="documentBundle_addAgency", requirements={"id" = "\d+"})
  * @Method("POST")
  * @Template()
  */
 public function addAgencyAction(Request $request, $id)
 {
     /*
      * CHECK FOR PERMISSIONS
      * TODO: Check if the acting user has all needed permissions to perform this action.
      */
     // if($this->ROLE_IS(Array('ROLE_SPC')))
     // {
     /*
      * GET Upload Profile FROM DB
      */
     $em = $this->getDoctrine()->getManager();
     $uploadprofile = $em->getRepository('DocumentBundle:UploadProfiles')->find($id);
     /*
      * GET CURRENT USER
      */
     $user = $this->get('security.token_storage')->getToken()->getUser();
     if (isset($_POST['selectDistriList'])) {
         //                \Doctrine\Common\Util\Debug::dump($_POST['selectDistriList']);
         //                echo 'test';
         $agencies = $_POST['selectDistriList'];
         // Add agencies to upload profiles
         $zl = 0;
         foreach ($agencies as $ag) {
             $ag = $em->getRepository('UserBundle:Agency')->find($ag);
             $uploadprofile->addAgency($ag);
             ++$zl;
         }
         // Add comments to Log
         $zl1 = 0;
         foreach ($agencies as $ag) {
             /*
              * ADD AN COMMENT
              */
             $comment = new \CommentBundle\Entity\Comment();
             $ag = $em->getRepository('UserBundle:Agency')->find($ag);
             // $comment->setComment('<strong>"'.$ag->getAcccount().'" was added to "'.$uploadprofile->getTagName().'" </strong>');
             // $comment->setUploadProfile($uploadprofile);
             // $comment->setAuthor($user);
             // $em->persist($comment);
             ++$zl1;
         }
         $comment->setUploadProfile($uploadprofile);
         $comment->setAuthor($user);
         $uploadprofile->setModifiedBy($this->get('security.token_storage')->getToken()->getUser());
         $uploadprofile->setModified(new \DateTime("now"));
         $em->persist($uploadprofile);
         $em->flush();
     }
     /*
      * REDERICT TO THE UPDATED PROFILE PROFILE
      */
     $response = new RedirectResponse($this->get('router')->generate('documentBundle_show_uploadprofiles', array('id' => $uploadprofile->getId())));
     $response->send();
     //         } else {
     //                 /*
     //                  * REDERICT TO THE UPDATED PROFILE PROFILE
     //                  */
     // $response = new RedirectResponse($this->get('router')->generate('documentBundle_show_uploadprofiles', Array('id' => $uploadprofile->getId())));
     // $response->send();
     //         }
 }
 /**
  * @Route("/documents/uploadprofiles/", name="documentBundle_show_uploadprofiles_2")
  * @Route("/documents/uploadprofiles/{id}", name="documentBundle_show_uploadprofiles", requirements={"id" = "\d+", "page" = "\d+"}, defaults={"id" = 1,"page" = 1, "filter" = null})
  * @Template()
  */
 public function UploadProfilesShowAction(Request $request, $id, $page, $filter)
 {
     $currentFiler = ' ';
     //        /*
     //         * CHECK FOR PERMISSIONS
     //         * TODO: Check if the acting user has all needed permissions to perform this action.
     //         */
     //        if($this->ROLE_IS(Array('ROLE_SPC')))
     //        {
     // Create a new uploadprofile instance
     $em = $this->getDoctrine()->getManager();
     $uploadprofile = $em->getRepository('DocumentBundle:UploadProfiles')->find($id);
     // Create a new userinstance
     $user = new \UserBundle\Entity\User();
     //Get all UploadProfiles
     $allAgencies = $this->getDoctrine()->getRepository('UserBundle:Agency')->findAll();
     /*
      * CHECK Upload Profile
      * Does it exist? Is the current user allowed to see this upload profile?
      */
     if ($uploadprofile == null) {
         $error_msg = 'error.404';
         // Error message
         return $this->render('UserBundle:Error:error.html.twig', array('error_msg' => $error_msg));
     }
     $new_comment = new \CommentBundle\Entity\Comment();
     $new_comment->setUploadprofile($uploadprofile);
     $new_comment->setAuthor($this->get('security.token_storage')->getToken()->getUser());
     $form = $this->createFormBuilder($new_comment)->add('comment', 'textarea', array('label' => false, 'required' => true, 'attr' => array('rows' => '5', 'placeholder' => 'Add new comment')))->add('save', 'submit', array('label' => 'form.buttons.add_comment', 'translation_domain' => 'Documents'))->getForm();
     //         if(isset($_POST['selectDistriList']))
     //         {
     //
     // //                \Doctrine\Common\Util\Debug::dump($_POST['selectDistriList']);
     // //                echo 'test';
     //             $agencies = $_POST['selectDistriList'];
     //
     //             $zl = 0;
     //             foreach ($agencies as $ag)
     //             {
     //                 $ag = $em->getRepository('UserBundle:Agency')->find($ag);
     //                 $uploadprofile->addAgency($ag);
     //                 ++$zl;
     //             }
     //         }
     /*
      * IF REQUEST IS VALID
      */
     $form->handleRequest($request);
     if ($form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $uploadprofile->setModifiedBy($this->get('security.token_storage')->getToken()->getUser());
         $em->persist($uploadprofile);
         $em->persist($new_comment);
         $em->flush();
         return $this->render('DocumentBundle:UploadProfiles:UploadProfilesProfile.html.twig', array('uploadprofile' => $uploadprofile, 'menu' => array('top' => 'Document'), 'form' => $form->createView(), 'success' => true));
     }
     /*
      * DISPLAY Upload Profile
      */
     return $this->render('DocumentBundle:UploadProfiles:UploadProfilesProfile.html.twig', array('uploadprofile' => $uploadprofile, 'menu' => array('top' => 'Document'), 'form' => $form->createView(), 'allAgencies' => $allAgencies));
     //        } else
     //        {
     //            /*
     //             * THROW ERROR
     //             * If the acting user is not allowed to perform this action
     //             */
     //            $error_msg = 'error.access.action'; // Error message
     //
     //            return $this->render('UserBundle:Error:error.html.twig', Array(
     //                'error_msg' => $error_msg,
     //            ));
     //        }
 }
 /**
  * @Route("/documents/document/{id}/addAgency", name="documentBundle_document_addAgency", requirements={"id" = "\d+"})
  * @Method("POST")
  * @Template()
  */
 public function addAgencyAction(Request $request, $id)
 {
     /*
      * CHECK FOR PERMISSIONS
      * TODO: Check if the acting user has all needed permissions to perform this action.
      */
     if ($this->get('security.authorization_checker')->isGranted('ROLE_CONTRACT_ADMIN')) {
         /*
          * GET DOCUMENT FROM DB
          */
         $em = $this->getDoctrine()->getManager();
         $document = $em->getRepository('DocumentBundle:Document')->find($id);
         //Get all UploadProfiles
         $allAgencies = $this->getDoctrine()->getRepository('AppBundle:Agency')->findAll();
         /*
          * GET CURRENT USER
          */
         $user = $this->get('security.token_storage')->getToken()->getUser();
         if (isset($_POST['agSelectDistriList'])) {
             //                \Doctrine\Common\Util\Debug::dump($_POST['agSelectDistriList']);
             //                echo 'test';
             $agencies = $_POST['agSelectDistriList'];
             // Add agencies to upload profiles
             $zl = 0;
             foreach ($agencies as $ag) {
                 $ag = $em->getRepository('AppBundle:Agency')->find($ag);
                 $document->addAgency($ag);
                 ++$zl;
             }
             // Add comments to Log
             $zl1 = 0;
             foreach ($agencies as $ag) {
                 /*
                  * ADD AN COMMENT
                  */
                 $comment = new \CommentBundle\Entity\Comment();
                 $ag = $em->getRepository('AppBundle:Agency')->find($ag);
                 $comment->setComment('<strong>"' . $ag->getIataName() . '" was added to "' . $document->getName() . '" </strong>');
                 $comment->setDocument($document);
                 $comment->setAuthor($user);
                 $em->persist($comment);
                 ++$zl1;
             }
             $document->setModifiedBy($this->get('security.token_storage')->getToken()->getUser());
             $document->setModified(new \DateTime("now"));
             $em->persist($document);
             $em->flush();
         }
         /*
          * REDERICT TO THE UPDATED PROFILE PROFILE
          */
         $response = new RedirectResponse($this->get('router')->generate('documentBundle_show_document', array('id' => $document->getId())));
         $response->send();
     } else {
         /*
          * REDERICT TO THE UPDATED PROFILE PROFILE
          */
         $response = new RedirectResponse($this->get('router')->generate('documentBundle_show_document', array('id' => $document->getId())));
         $response->send();
     }
 }