Controller/WikiTagController.php
changeset 63 774ba82dca59
parent 61 1bc0ec8ed05b
child 65 ba6b8e38d90e
equal deleted inserted replaced
62:10be6b9e55e7 63:774ba82dca59
    14 use Doctrine\DBAL\DriverManager;
    14 use Doctrine\DBAL\DriverManager;
    15 
    15 
    16 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
    16 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
    17 use IRI\Bundle\WikiTagBundle\Entity\Tag;
    17 use IRI\Bundle\WikiTagBundle\Entity\Tag;
    18 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
    18 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
       
    19 use IRI\Bundle\WikiTagBundle\Services\WikiTagServiceException;
    19 use Pagerfanta\Pagerfanta;
    20 use Pagerfanta\Pagerfanta;
    20 use Pagerfanta\Adapter\ArrayAdapter;
    21 use Pagerfanta\Adapter\ArrayAdapter;
    21 use Pagerfanta\Adapter\DoctrineORMAdapter;
    22 use Pagerfanta\Adapter\DoctrineORMAdapter;
    22 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    23 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    23 use Symfony\Component\HttpFoundation\Response;
    24 use Symfony\Component\HttpFoundation\Response;
   265      */
   266      */
   266     public function addTagAction()
   267     public function addTagAction()
   267     {
   268     {
   268         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   269         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   269         $tag_label = $this->getRequest()->request->get('value');
   270         $tag_label = $this->getRequest()->request->get('value');
   270         // We get the DocumentTags
   271         
   271         $em = $this->getDoctrine()->getEntityManager();
   272         
   272         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc);
   273         try
   273         $nb_tags = count($tags);
   274         {
   274         $found = false;
   275             $this->get('wiki_tag.document')->addTag($id_doc, $tag_label);
   275         $i = 0;
   276         }
   276         while($i<$nb_tags && $found==false){
   277         catch (WikiTagServiceException $e)
   277             $dt = $tags[$i];
   278         {
   278             if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){
   279             return new Response(json_encode(array('error' => $e->getErrorCode(), 'message' => $e->getMessage())),$e->getCode());
   279                 $found = true;
       
   280             }
       
   281             $i++;
       
   282         }
       
   283         // If the label was found, we sent a bad request
       
   284         if($found==true){
       
   285             //TODO : translation
       
   286             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
       
   287         }
       
   288         // returns array($tag, $revision_id, $created)
       
   289         try {
       
   290             $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
       
   291         }
       
   292         catch (\Exception $e){
       
   293             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
       
   294         }
       
   295         
       
   296         $tag = $ar[0];
       
   297         $revision_id = $ar[1];
       
   298         $created = $ar[2];
       
   299         
       
   300         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
       
   301         $nb_tags = count($tags);
       
   302 
       
   303         if($created==true || $nb_tags==0){
       
   304             $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
       
   305             // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
       
   306             $a1 = reset($new_order_ar);
       
   307             $new_order = intval(reset($a1)) + 1;
       
   308             $new_DT = new DocumentTag();
       
   309             $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
       
   310             $new_DT->setTag($tag);
       
   311             $new_DT->setOriginalOrder($new_order);
       
   312             $new_DT->setTagOrder($new_order);
       
   313             $new_DT->setWikipediaRevisionId($revision_id);
       
   314             $em->persist($new_DT);
       
   315             $em->flush();
       
   316         }
   280         }
   317 
   281 
   318         return $this->renderDocTags($id_doc, $this->getRequest()->request->get('wikitag_document_profile'));
   282         return $this->renderDocTags($id_doc, $this->getRequest()->request->get('wikitag_document_profile'));
   319     }
   283     }
   320 
   284 
   538         // We update the tag label and its wikipedia info with the new label.
   502         // We update the tag label and its wikipedia info with the new label.
   539         try {
   503         try {
   540             $this->updateTagWithNewLabel($moved_tag, $tag_label);
   504             $this->updateTagWithNewLabel($moved_tag, $tag_label);
   541         }
   505         }
   542         catch (\Exception $e){
   506         catch (\Exception $e){
   543             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
   507             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),500);
   544         }
   508         }
   545         // We render the tag list.
   509         // We render the tag list.
   546         return $this->renderAllTags();
   510         return $this->renderAllTags();
   547     }
   511     }
   548         
   512         
   558         // We update the tag label and its wikipedia info with the original label.
   522         // We update the tag label and its wikipedia info with the original label.
   559         try {
   523         try {
   560             $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
   524             $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
   561         }
   525         }
   562         catch (\Exception $e){
   526         catch (\Exception $e){
   563             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
   527             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),500);
   564         }
   528         }
   565         
   529         
   566         // We render the tag list.
   530         // We render the tag list.
   567         return $this->renderAllTags();
   531         return $this->renderAllTags();
   568     }
   532     }
   578         // We update the tag label and its wikipedia info with the original label.
   542         // We update the tag label and its wikipedia info with the original label.
   579         try {
   543         try {
   580             $this->updateTagWithNewLabel($tag, $tag->getLabel());
   544             $this->updateTagWithNewLabel($tag, $tag->getLabel());
   581         }
   545         }
   582         catch (\Exception $e){
   546         catch (\Exception $e){
   583             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
   547             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),500);
   584         }
   548         }
   585     
   549     
   586         // We render the tag list.
   550         // We render the tag list.
   587         return $this->renderAllTags();
   551         return $this->renderAllTags();
   588     }
   552     }