--- a/Controller/WikiTagController.php Fri Dec 09 06:43:49 2011 +0100
+++ b/Controller/WikiTagController.php Wed Dec 14 23:28:57 2011 +0100
@@ -16,6 +16,7 @@
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
use IRI\Bundle\WikiTagBundle\Entity\Tag;
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
+use IRI\Bundle\WikiTagBundle\Services\WikiTagServiceException;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Adapter\DoctrineORMAdapter;
@@ -267,52 +268,15 @@
{
$id_doc = $this->getRequest()->request->get('wikitag_document_id');
$tag_label = $this->getRequest()->request->get('value');
- // We get the DocumentTags
- $em = $this->getDoctrine()->getEntityManager();
- $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc);
- $nb_tags = count($tags);
- $found = false;
- $i = 0;
- while($i<$nb_tags && $found==false){
- $dt = $tags[$i];
- if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){
- $found = true;
- }
- $i++;
- }
- // If the label was found, we sent a bad request
- if($found==true){
- //TODO : translation
- return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
- }
- // returns array($tag, $revision_id, $created)
- try {
- $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
+
+
+ try
+ {
+ $this->get('wiki_tag.document')->addTag($id_doc, $tag_label);
}
- catch (\Exception $e){
- return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
- }
-
- $tag = $ar[0];
- $revision_id = $ar[1];
- $created = $ar[2];
-
- $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
- $nb_tags = count($tags);
-
- if($created==true || $nb_tags==0){
- $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
- // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
- $a1 = reset($new_order_ar);
- $new_order = intval(reset($a1)) + 1;
- $new_DT = new DocumentTag();
- $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
- $new_DT->setTag($tag);
- $new_DT->setOriginalOrder($new_order);
- $new_DT->setTagOrder($new_order);
- $new_DT->setWikipediaRevisionId($revision_id);
- $em->persist($new_DT);
- $em->flush();
+ catch (WikiTagServiceException $e)
+ {
+ return new Response(json_encode(array('error' => $e->getErrorCode(), 'message' => $e->getMessage())),$e->getCode());
}
return $this->renderDocTags($id_doc, $this->getRequest()->request->get('wikitag_document_profile'));
@@ -540,7 +504,7 @@
$this->updateTagWithNewLabel($moved_tag, $tag_label);
}
catch (\Exception $e){
- return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+ return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),500);
}
// We render the tag list.
return $this->renderAllTags();
@@ -560,7 +524,7 @@
$this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
}
catch (\Exception $e){
- return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+ return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),500);
}
// We render the tag list.
@@ -580,7 +544,7 @@
$this->updateTagWithNewLabel($tag, $tag->getLabel());
}
catch (\Exception $e){
- return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+ return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),500);
}
// We render the tag list.