Controller/WikiTagController.php
author cavaliet
Fri, 21 Oct 2011 17:10:54 +0200
changeset 9 cc32af725176
parent 8 7d2fb5d7c9ff
child 10 a1234ceba912
permissions -rwxr-xr-x
first step for tag list and add Pagerfanta for paginator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
*
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
* (c) IRI <http://www.iri.centrepompidou.fr/>
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
*
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
* For the full copyright and license information, please view the LICENSE
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
* file that was distributed with this source code.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
*/
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Controller;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use IRI\Bundle\WikiTagBundle\Entity\Tag;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
    16
use Pagerfanta\Pagerfanta;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
    17
use Pagerfanta\Adapter\ArrayAdapter;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
    18
use Pagerfanta\Adapter\DoctrineORMAdapter;
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
use Symfony\Component\HttpFoundation\Response;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
class WikiTagController extends Controller
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
     * Fake index action
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    public function indexAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        return new Response('<html><body>Nothing to see here.</body></html>');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
     * Renders the little html to add the css
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    public function addCssAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        return $this->render('WikiTagBundle:WikiTag:css.html.twig');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
     * Renders the little html to add the javascript
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
     * TODO: review why this injection in javascript, t10n?
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    public function addJavascriptAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        // $cats is {"Label":"Créateur"},{"Label":"Datation"},...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        $nbCats = count($cats);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        $ar = array('' => '');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        for($i=0;$i<$nbCats;$i++){
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
    52
            $temp = array($cats[$i]["label"] => $cats[$i]["label"]);
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            $ar = array_merge($ar, $temp);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        // ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        $categories = json_encode($ar);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        return $this->render('WikiTagBundle:WikiTag:javascript.html.twig', array('categories' => $categories));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
     * Display a list of ordered tag for a document
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
     * @param integer $id_doc
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    public function documentTagsAction($id_doc)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        return $this->render('WikiTagBundle:WikiTag:documentTags.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
     * TODO : Enter description here ...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    public function tagUpDownAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        $req = $this->getRequest()->request;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        $id_doc = $req->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        // post vars new_order and old_order indicate the position (from 1) of the tag in the list.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        // NB : it is different from the DocumentTag.order in the database.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        $new_order = intval($req->get('new_order')) - 1;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        $old_order = intval($req->get('old_order')) - 1;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        // First we get the DocumentTags
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        $em = $this->getDoctrine()->getEntityManager();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        // We change the moved DocumentTag's order
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        $new_dt_order = $ordered_tags[$new_order]->getTagOrder();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        $moved_dt = $ordered_tags[$old_order];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        $moved_dt->setTagOrder($new_dt_order);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        // We move the TaggedSheets's order
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        if($new_order > $old_order){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            // And we decrease the other ones
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
            for ($i=($old_order+1); $i <= ($new_order); $i++){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                $dt = $ordered_tags[$i];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                $dt->setTagOrder($dt->getTagOrder() - 1);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        else{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
            // And we increase the other ones
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            for ($i=$new_order; $i <= ($old_order-1); $i++){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
                $dt = $ordered_tags[$i];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                $dt->setTagOrder($dt->getTagOrder() + 1);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        // Save datas.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        $em->flush();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
     * TODO: Enter description here ...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
    public function removeTagFromListAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        $id_tag = $this->getRequest()->request->get('tag_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        // We get the DocumentTag meant to be deleted, and remove it.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        $em = $this->getDoctrine()->getEntityManager();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('tag' => $id_tag, 'document' => $id_doc));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        $em->remove($dt);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
        $em->flush();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
     * TODO: Enter description here ...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    public function modifyDocumentTagAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        $tag_label = $this->getRequest()->request->get('value');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        $id_moved_tag = $this->getRequest()->request->get('id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        if($tagLabel!=$movedTag->getLabel()){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
            // We get the DocumentTags
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
            $em = $this->getDoctrine()->getEntityManager();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
            $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
            $nb_tags = count($tags);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
            $found = false;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
            $i = 0;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
            while($i<$nb_tags && $found==false){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
                $dt = $tags[$i];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
                if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
                    $found = true;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
                }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
                $i++;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
            // If the label was found, we sent a bad request
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
            if($found==true){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
                return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            // We create the new tag or get the already existing tag. $tag, $revision_id, $created
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
            $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
            $tag = $ar[0];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
            $revision_id = $ar[1];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
            $created = $ar[2];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
            // We get the DocumentTag and change its tag
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
            $dt = $this->getDoctrine()->getRepository('WikiTagBundle:WikiTagDocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
            $dt->setTag($tag);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
            $dt->setWikipediaRevisionId($revision_id);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
            //
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            // HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code :
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
            //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
            //results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
            //if len(results) > 0:
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
            //    ts.index_note = results[0].score
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
            //
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            // We save the datas
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
            $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneBy(array('id' => $id_doc));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
            $doc->setManualOrder(true);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
            $em->flush();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
     *
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   185
     * @Route("/wtrtd")
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
     * TODO : Enter description here ...
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   187
     * TODO : implement
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
    public function reorderTagDocumentAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
        $id_Doc = $this->getRequest()->request->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
     * TODO: Enter description here ...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    public function addTagAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
        $tag_label = $this->getRequest()->request->get('value');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
        // We get the DocumentTags
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        $em = $this->getDoctrine()->getEntityManager();
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   205
        $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc);
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
        $nb_tags = count($tags);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
        $found = false;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
        $i = 0;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
        while($i<$nb_tags && $found==false){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
            $dt = $tags[$i];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
            if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
                $found = true;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
            $i++;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
        // If the label was found, we sent a bad request
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
        if($found==true){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
            //TODO : translation
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
            return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
        }
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 7
diff changeset
   221
        // returns array($tag, $revision_id, $created)
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        $tag = $ar[0];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        $revision_id = $ar[1];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        $created = $ar[2];
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 7
diff changeset
   226
        
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   227
        $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
        $nb_tags = count($tags);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        if($created==true || $nb_tags==0){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
            $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
            // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
            $a1 = reset($new_order_ar);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
            $new_order = intval(reset($a1)) + 1;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
            // TODO: use a factory that returns an DocumentTagInterface
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
            $new_DT = new DocumentTag();
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   237
            $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
            $new_DT->setTag($tag);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
            $new_DT->setOriginalOrder($new_order);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
            $new_DT->setTagOrder($new_order);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
            $new_DT->setWikipediaRevisionId($revision_id);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
            $em->persist($new_DT);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
            $em->flush();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
     * TODO: Enter description here ...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    public function removeWpLinkAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        $id_tag = $this->getRequest()->request->get('tag_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        $tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        //return new Response(var_dump(array($tag)));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        // We search if the unsemantized version of the tag already exist.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
        $un_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('label'=>$tag->getLabel(), 'urlStatus'=>Tag::$TAG_URL_STATUS_DICT['null_result']));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
        $em = $this->getDoctrine()->getEntityManager();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        if(!$un_tag){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
            // Create another tag almost identical, without the W info
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
            // TODO: use a factory that return a TagInterface
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
            $un_tag = new Tag();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
            $un_tag->setLabel($tag->getLabel());
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
            $un_tag->setOriginalLabel($tag->getOriginalLabel());
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
            $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['null_result']);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
            $un_tag->setWikipediaUrl(null);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
            $un_tag->setWikipediaPageId(null);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
            $un_tag->setDbpediaUri(null);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
            $un_tag->setCategory($tag->getCategory());
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
            $un_tag->setAlias($tag->getAlias());
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
            $un_tag->setPopularity($tag->getPopularity());
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
            $em->persist($un_tag);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        // We associate the unsemantized tag to the DocumentTag and save datas
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   280
        // TODO: do the request on external id of document
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 7
diff changeset
   281
        $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_tag));
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        $dt->setTag($un_tag);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        $em->flush();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
        
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
     * TODO: Enter description here ...
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
    public function updateTagCategoryAction()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
        $id_tag = $this->getRequest()->request->get('id');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        $cat_label = $this->getRequest()->request->get('value');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        // We get the Tag and update its category.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        $em = $this->getDoctrine()->getEntityManager();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
        $tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
        if($cat_label==''){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
            $cat = null;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
            $tag->nullCategory();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
        else{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
            $cat = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOneBy(array('label' => $cat_label));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
            $tag->setCategory($cat);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
        $em->flush();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
        return $this->renderDocTags($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
     *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
     * Generic render partial template
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
     * @param unknown_type $id_doc
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
    public function renderDocTags($id_doc)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
        $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
        return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
    }
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   325
    
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   326
    /**
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   327
     * 
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   328
     * @Route("/wtrwi")
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   329
     * TODO: Enter description here ...
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   330
     * TODO : implement
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   331
     */
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   332
    public function resetWpInfoAction()
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   333
    {
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   334
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   335
        return $this->renderDocTags($id_doc);
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   336
    }
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   337
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   338
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   339
    /**
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   340
     *
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   341
     * TODO : Enter description here ...
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   342
     * TODO : implement
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   343
     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   344
     */
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   345
    public function updateTagAliasAction()
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   346
    {
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   347
        $id_doc = $this->getRequest()->request->get('wikitag_document_id');
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   348
        return $this->renderDocTags($id_doc);
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   349
    }
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   350
    
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   351
    /**
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   352
     * List of all tags
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   353
     * TODO: Enter description here ...
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   354
     */
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   355
    public function allTagsAction()
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   356
    {
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   357
        //$tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('urlStatus' => 2));
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   358
        //$tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('id' => 10));
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   359
        // We get/set all the parameters for the search and pagination.
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   360
        // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   361
        // Searched string 
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   362
        $searched = "";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   363
        if(array_key_exists('searched', $_GET)){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   364
            $searched = $_GET['searched'];
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   365
        }
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   366
        // Number of tags per page
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 7
diff changeset
   367
        $nb_by_page = 50;
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   368
        if(array_key_exists('nb_by_page', $_GET)){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   369
            $nb_by_page = intval($_GET['nb_by_page']);
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   370
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   371
        // Current page number
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   372
        $num_page = 1;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   373
        if(array_key_exists('num_page', $_GET)){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   374
            $num_page = intval($_GET['num_page']);
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   375
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   376
        
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   377
        
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   378
        
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   379
        // We build the query.
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   380
        $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   381
        $qb->select('t')
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   382
           ->from('WikiTagBundle:Tag','t');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   383
        // We add the search string if necessary
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   384
        if($searched!=""){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   385
            // We replace "*" by "%".
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   386
            $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   387
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   388
        // W add the sorting criteria
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   389
        $sort = "popd"; // sort by descendent popularity by default.
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   390
        $reverse_sort = "popa";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   391
        if(array_key_exists('sort', $_GET)){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   392
            $sort = $_GET['sort'];
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   393
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   394
        $sort_query = "t.popularity DESC t.normalizedLabel ASC t.label ASC";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   395
        switch($sort){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   396
            case "popd":
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   397
                $qb->addOrderBy('t.popularity','DESC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   398
                $qb->addOrderBy('t.normalizedLabel','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   399
                $qb->addOrderBy('t.label','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   400
                $reverse_sort = "popa";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   401
                break;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   402
            case "popa":
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   403
                $qb->addOrderBy('t.popularity','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   404
                $qb->addOrderBy('t.normalizedLabel','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   405
                $qb->addOrderBy('t.label','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   406
                $reverse_sort = "popd";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   407
                break;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   408
            case "labd":
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   409
                $qb->addOrderBy('t.normalizedLabel','DESC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   410
                $qb->addOrderBy('t.label','DESC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   411
                $reverse_sort = "laba";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   412
                break;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   413
            case "laba":
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   414
                $qb->addOrderBy('t.normalizedLabel','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   415
                $qb->addOrderBy('t.label','ASC');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   416
                $reverse_sort = "labd";
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   417
                break;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   418
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   419
        
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   420
        // We paginate
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   421
        $adapter = new DoctrineORMAdapter($qb);
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   422
        $pagerfanta = new Pagerfanta($adapter);
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   423
        $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   424
        $last_page = $pagerfanta->getNbPages();
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   425
        $pagerfanta->setCurrentPage($num_page); // 1 by default
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   426
        $nb_total = $pagerfanta->getNbResults();
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   427
        $tags = $pagerfanta->getCurrentPageResults();
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   428
        $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   429
        
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   430
        // We get the previous and next page number
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 7
diff changeset
   431
        $prev_page = 1;
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   432
        if($pagerfanta->hasPreviousPage()){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   433
            $prev_page = $pagerfanta->getPreviousPage();
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   434
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   435
        $next_page = $last_page;
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   436
        if($pagerfanta->hasNextPage()){
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   437
            $next_page = $pagerfanta->getNextPage();
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   438
        }
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   439
        // We calculate start_index and end_index (tag number in the whole list)
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   440
        $start_index = 1 + (($num_page - 1) * $nb_by_page);
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   441
        $end_index = min($nb_total, $start_index + $nb_by_page - 1);
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   442
        
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   443
        // We build the list of tags's first letters to make quick search.
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   444
        $search_def = array('A'=>'a*', 'B'=>'b*', 'C'=>'c*', 'D'=>'d*', 'E'=>'e*', 'F'=>'f*', 'G'=>'g*', 'H'=>'h*', 'I'=>'i*', 'J'=>'j*', 'K'=>'k*', 'L'=>'l*',
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   445
        	'M'=>'m*', 'N'=>'n*', 'O'=>'o*', 'P'=>'p*', 'Q'=>'q*', 'R'=>'r*', 'S'=>'s*', 'T'=>'t*', 'U'=>'u*', 'V'=>'v*', 'W'=>'w*', 'X'=>'x*', 'Y'=>'y*', 'Z'=>'z*');
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   446
        
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 7
diff changeset
   447
        return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
9
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   448
            array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   449
            'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
cc32af725176 first step for tag list and add Pagerfanta for paginator
cavaliet
parents: 8
diff changeset
   450
        	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort));
7
7a877de630fd add files to all tags list.
tcavalie
parents: 5
diff changeset
   451
    }
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
}