Services/DocumentService.php
author cavaliet
Fri, 24 Feb 2012 11:23:10 +0100
changeset 78 130e95ca5049
parent 68 e7384fb35f7a
child 80 cf6a88559482
permissions -rw-r--r--
correct call getMaxOrder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
namespace IRI\Bundle\WikiTagBundle\Services;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    12
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerAware;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Symfony\Component\DependencyInjection\ContainerInterface;
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    15
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
class DocumentService extends ContainerAware
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
{
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
     * Get the container associated with this service.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
     * @return ContainerInterface
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    public function getContainer()
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        return $this->container;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     * Public constructor with container as parameter for contruct injection.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
     * @param ContainerInterface $container
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    public function __construct(ContainerInterface $container)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        $this->setContainer($container);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    private $doctrine;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    public function getDoctrine()
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        if(is_null($this->doctrine))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            $this->doctrine = $this->getContainer()->get('doctrine');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        return $this->doctrine;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    /**
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    50
     * 	Copy the list of tags of one document to another.
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    51
     * 	The ids are the ids of the "host" document.
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    52
	 * 	Beware, both Documents must exists in the database. therefore this method can be called only after a EntityManager::push()
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    53
	 * 	If one or the other doc is not found, an execption is raised.
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
     * @param mixed $id_doc_src the source document id
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
     * @param mixed $id_doc_tgt the target document id
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    public function copyTags($id_doc_src, $id_doc_tgt)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        $doctrine = $this->getDoctrine();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        $em = $doctrine->getEntityManager();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        $doc_rep = $em->getRepository("WikiTagBundle:Document");
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        $src_doc = $doc_rep->findOneByExternalId($id_doc_src);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        if(is_null($src_doc))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            throw new Exception("cloneTags: no source doc");
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        $tgt_doc = $doc_rep->findOneByExternalId($id_doc_tgt);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        if(is_null($tgt_doc))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            throw new Exception("cloneTags: no target doc");
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        $doc_rep->copyTags($src_doc, $tgt_doc);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        $em->flush();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    85
    /**
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    86
     * Add a new tag (or tags) to a "host" document.
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    87
     * If the label already exists, an exception is raised.
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    88
     * Also, the document must exists in the database, i.e. Entitymanager::flush() must have been called on this objects before.
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    89
     *
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    90
     * @param mixed $doc the document to add the tags to
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    91
     * @param string|array $tag_label : the label of the new tag
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    92
     */
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    93
    public function addTags($doc, $tag_labels)
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    94
    {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    95
        // We get the DocumentTags
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    96
        $em = $this->getDoctrine()->getEntityManager();
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    97
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    98
        $class = $this->getContainer()->getParameter("wiki_tag.document_class");
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    99
        
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   100
        if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) {
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   101
            $doc_rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   102
            if(is_a($doc, $class)) {
78
130e95ca5049 correct call getMaxOrder
cavaliet
parents: 68
diff changeset
   103
                // Get the document column id, set in the config file.
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   104
                $doc_id = $doc_rep->reflectionGetField($doc, Container()->getParameter("wiki_tag.document_id_column"));
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   105
            }
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   106
            else {
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   107
                $doc_id = $doc;
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   108
            }
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   109
            $doc = $doc_rep->findOneByExternalId($doc_id);
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   110
        }
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   111
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   112
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   113
        if(!is_array($tag_labels)) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   114
            $tag_labels = array($tag_labels);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   115
        }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   116
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   117
        foreach ($tag_labels as $tag_label) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   118
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   119
            $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   120
            $created = false;
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   121
            
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   122
            $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label");
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   123
            $query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label));
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   124
            
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   125
            $nb_tags = $query->getSingleScalarResult();
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   126
            
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   127
            if($nb_tags == 0) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   128
                # look in unit of work
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   129
                $uow = $em->getUnitOfWork();
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   130
                foreach($uow->getScheduledEntityInsertions() as $entity) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   131
                    if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface")) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   132
                        $tag = $entity->getTag();
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   133
                        if(!is_null($tag)) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   134
                            if($tag->getNormalizedLabel() === $normalized_tag_label)
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   135
                            {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   136
                                $nb_tags++;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   137
                                break;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   138
                            }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   139
                        }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   140
                    }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   141
                }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   142
            }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   143
            
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   144
            // If the label was found, we sent a bad request
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   145
            if($nb_tags > 0) {
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   146
                throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   147
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   148
            // returns array($tag, $revision_id, $created)
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   149
            try {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   150
                $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'), $this->getContainer()->get('logger'));
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   151
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   152
            catch (\Exception $e){
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   153
                throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   154
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   155
            
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   156
            $tag = $ar[0];
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   157
            $revision_id = $ar[1];
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   158
            $created = $ar[2];
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   159
            
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   160
            if(!$created) {
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   161
                $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt WHERE dt.document = :id_doc AND dt.tag = :tag");
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   162
                $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag));
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   163
                $nb_tags = $query->getSingleScalarResult();
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   164
            }
78
130e95ca5049 correct call getMaxOrder
cavaliet
parents: 68
diff changeset
   165
            
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   166
            if($created || $nb_tags==0){
78
130e95ca5049 correct call getMaxOrder
cavaliet
parents: 68
diff changeset
   167
                $max_order = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc_id);
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   168
                $new_order = $max_order + 1;
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   169
                $new_DT = new DocumentTag();
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   170
                $new_DT->setDocument($doc);
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   171
                $new_DT->setTag($tag);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   172
                $new_DT->setOriginalOrder($new_order);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   173
                $new_DT->setTagOrder($new_order);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   174
                $new_DT->setWikipediaRevisionId($revision_id);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   175
                $em->persist($new_DT);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   176
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   177
        }
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   178
                
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   179
    }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   180
    
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   181
    /**
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   182
     * Returns the list of tags labels for @author ymh
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   183
     *
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   184
     * @param mixed $id_doc the document id.
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   185
     * @throws WikiTagServiceException if the document is not found
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   186
     */
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   187
    public function getTagLabels($id_doc)
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   188
    {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   189
        $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   190
        $doc = $rep->findOneByExternalId($id_doc);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   191
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   192
        if(is_null($doc)) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   193
            throw new WikiTagServiceException("Unknown document id");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   194
        }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   195
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   196
        return $rep->getTagsStr($doc);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   197
    }
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   198
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   199
   	/**
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   200
     * Service to reorder the tags using their notes in the index search.
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   201
     * This service is configured in the configuration file by affecting the weight in each field definition.
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   202
     *
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   203
     * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   204
     */
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   205
    public function reorderTags($document)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   206
    {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   207
        $doctrine = $this->getContainer()->get('doctrine');
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   208
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   209
        $tags_score = array();
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   210
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   211
        foreach($document->getTags() as $tag)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   212
        {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   213
            $label = $tag->getTag()->getLabel();
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   214
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   215
            $score_res = $this->search($label, array("id"=>$document->getId()));
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   216
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   217
            if(count($score_res)>0)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   218
            {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   219
                $score = floatval($score_res[0]['score']);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   220
            }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   221
            else
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   222
            {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   223
                $score = 0.0;
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   224
            }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   225
            $tags_score[] = array($score,$tag);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   226
        }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   227
        // sort tags based on score
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   228
        $i=1;
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   229
        usort($tags_score, function($a, $b) {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   230
            return $a[0]<$b[0]?1:-1;
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   231
        });
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   232
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   233
        foreach($tags_score as $item)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   234
        {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   235
            $tag = $item[1];
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   236
            $tag->setTagOrder($i++);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   237
            $tag->setIndexNote($item[0]);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   238
            $doctrine->getEntityManager()->persist($tag);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   239
        }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   240
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   241
    }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   242
    
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
}