Controller/WikiTagController.php
changeset 50 e967654e90cb
parent 49 e854d8cb376c
child 52 e804ae133f27
equal deleted inserted replaced
49:e854d8cb376c 50:e967654e90cb
   191             // If the label was found, we sent a bad request
   191             // If the label was found, we sent a bad request
   192             if($found==true){
   192             if($found==true){
   193                 return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   193                 return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   194             }
   194             }
   195             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
   195             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
   196             $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
   196             try {
       
   197                 $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
       
   198             }
       
   199             catch (\Exception $e){
       
   200                 return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
       
   201             }
   197             $tag = $ar[0];
   202             $tag = $ar[0];
   198             $revision_id = $ar[1];
   203             $revision_id = $ar[1];
   199             $created = $ar[2];
   204             $created = $ar[2];
   200             
   205             
   201             // We get the DocumentTag and change its tag
   206             // We get the DocumentTag and change its tag
   273         if($found==true){
   278         if($found==true){
   274             //TODO : translation
   279             //TODO : translation
   275             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   280             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   276         }
   281         }
   277         // returns array($tag, $revision_id, $created)
   282         // returns array($tag, $revision_id, $created)
   278         $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);// tag, revision_id, created = get_or_create_tag(tag_label)
   283         try {
       
   284             $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
       
   285         }
       
   286         catch (\Exception $e){
       
   287             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
       
   288         }
       
   289         
   279         $tag = $ar[0];
   290         $tag = $ar[0];
   280         $revision_id = $ar[1];
   291         $revision_id = $ar[1];
   281         $created = $ar[2];
   292         $created = $ar[2];
   282         
   293         
   283         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
   294         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
   522     {
   533     {
   523         $tag_label = $this->getRequest()->request->get('value');
   534         $tag_label = $this->getRequest()->request->get('value');
   524         $id_moved_tag = $this->getRequest()->request->get('id');
   535         $id_moved_tag = $this->getRequest()->request->get('id');
   525         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
   536         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
   526         // We update the tag label and its wikipedia info with the new label.
   537         // We update the tag label and its wikipedia info with the new label.
   527         $this->updateTagWithNewLabel($moved_tag, $tag_label);
   538         try {
   528         
   539             $this->updateTagWithNewLabel($moved_tag, $tag_label);
       
   540         }
       
   541         catch (\Exception $e){
       
   542             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
       
   543         }
   529         // We render the tag list.
   544         // We render the tag list.
   530         $num_page = $this->getRequest()->request->get('num_page');
   545         $num_page = $this->getRequest()->request->get('num_page');
   531         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
   546         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
   532         $sort = $this->getRequest()->request->get('sort');
   547         $sort = $this->getRequest()->request->get('sort');
   533         $searched = $this->getRequest()->request->get('searched');
   548         $searched = $this->getRequest()->request->get('searched');
   542     public function resetWpInfoAction()
   557     public function resetWpInfoAction()
   543     {
   558     {
   544         $id_moved_tag = $this->getRequest()->request->get('tag_id');
   559         $id_moved_tag = $this->getRequest()->request->get('tag_id');
   545         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
   560         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
   546         // We update the tag label and its wikipedia info with the original label.
   561         // We update the tag label and its wikipedia info with the original label.
   547         return $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
   562         try {
       
   563             $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
       
   564         }
       
   565         catch (\Exception $e){
       
   566             return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
       
   567         }
   548         
   568         
   549         // We render the tag list.
   569         // We render the tag list.
   550         $num_page = $this->getRequest()->request->get('num_page');
   570         $num_page = $this->getRequest()->request->get('num_page');
   551         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
   571         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
   552         $sort = $this->getRequest()->request->get('sort');
   572         $sort = $this->getRequest()->request->get('sort');
   562     {
   582     {
   563         if($tag!=null && $label!=null){
   583         if($tag!=null && $label!=null){
   564             if($label!=$tag->getLabel()){
   584             if($label!=$tag->getLabel()){
   565                 // We get the Wikipedia informations for the sent label
   585                 // We get the Wikipedia informations for the sent label
   566                 $tag_label_normalized = WikiTagUtils::normalizeTag($label);
   586                 $tag_label_normalized = WikiTagUtils::normalizeTag($label);
   567                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
   587                 try {
       
   588                     $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
       
   589                 }
       
   590                 catch (\Exception $e){
       
   591                     throw new \Exception($e->getMessage());
       
   592                 }
   568                 $tag->setWikipediaInfo($wp_response);
   593                 $tag->setWikipediaInfo($wp_response);
   569                 // Save datas.
   594                 // Save datas.
   570                 $em = $this->getDoctrine()->getEntityManager();
   595                 $em = $this->getDoctrine()->getEntityManager();
   571                 $em->persist($tag);
   596                 $em->persist($tag);
   572                 $em->flush();
   597                 $em->flush();