/**
  * @Route("/wiki/edit/{slug}", name="wikiBundle_edit")
  */
 public function editAction(Request $request, $slug)
 {
     if ($this->container->get('security.authorization_checker')->isGranted('ROLE_WIKI_EDITOR')) {
         $em = $this->getDoctrine()->getManager();
         //get all articles
         $entities = $em->getRepository('WikiBundle:Article')->findAll();
         //only active version
         $articles = array();
         foreach ($entities as $entity) {
             if ($entity->getParent() == null) {
                 $activeEntity = $entity->getActiveChild();
                 $articleAirlines = $activeEntity->getAirlines()->toArray();
                 $articleMarkets = $activeEntity->getMarkets()->toArray();
                 $user = $this->getUser();
                 $userAirlines = $user->getAirlines()->toArray();
                 $userMarkets = $user->getMarkets()->toArray();
                 $airlineFlag = 0;
                 $marketFlag = 0;
                 //check if article's airlines match user's airlines
                 $count = count($articleAirlines);
                 for ($i = 0; $i < $count; $i++) {
                     if (in_array($articleAirlines[$i], $userAirlines)) {
                         $airlineFlag = 1;
                         break;
                     }
                 }
                 //check if article's markets match user's markets
                 $countM = count($articleMarkets);
                 for ($i = 0; $i < $countM; $i++) {
                     if (in_array($articleMarkets[$i], $userMarkets)) {
                         $marketFlag = 1;
                         break;
                     }
                 }
                 if ($airlineFlag == 1 && $marketFlag == 1) {
                     array_unshift($articles, $activeEntity);
                     //latest article on the top
                 }
             }
         }
         //-----
         $entity = $em->getRepository('WikiBundle:Article')->findOneBySlug($slug);
         if (!$entity) {
             throw $this->createNotFoundException('Unable to find WikiArticle entity.');
         }
         $newEntity = new Article();
         $user = $this->getUser();
         $newEntity->setAuthor($user);
         $newEntity->setBody($entity->getBody());
         $newEntity->setTitle($entity->getTitle());
         $newEntity->setMarkets($entity->getMarkets());
         $newEntity->setAirlines($entity->getAirlines());
         $newEntity->setCurrent(1);
         //Active
         $entity->setCurrent(0);
         //Disable
         if ($entity->getParent() == null) {
             $entity->addChild($newEntity);
         } else {
             $entity->getParent()->addChild($newEntity);
         }
         $form = $this->createForm(WikiArticleType::class, $newEntity);
         $form->handleRequest($request);
         if ($form->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $em->persist($entity);
             $em->persist($newEntity);
             $em->flush();
             return $this->redirect($this->generateUrl('wikiBundle_show', array('slug' => $newEntity->getSlug())));
         }
         return $this->render('WikiBundle:Article:edit.html.twig', array('newEntity' => $newEntity, 'form' => $form->createView(), 'articles' => $articles));
     } else {
         /*
          * DISPLAY ERROR MESSAGE
          */
         return $this->render('DocumentBundle:Action/Document:archived.html.twig', array('success' => false, 'updated_document' => $updated_document, 'menu' => array('top' => 'Document')));
     }
 }