Services/DocumentService.php
author ymh <ymh.work@gmail.com>
Fri, 26 Sep 2014 10:04:40 +0200
changeset 136 f209fcb0df6c
parent 80 cf6a88559482
permissions -rwxr-xr-x
Added tag V00.18 for changeset ab728cb3632c
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
        {
80
cf6a88559482 Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    69
            throw new \Exception("cloneTags: no source doc");
57
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
        {
80
cf6a88559482 Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    75
            throw new \Exception("cloneTags: no target doc");
57
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
    {
80
cf6a88559482 Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    95
        if(is_null($tag_labels) || (!is_string($tag_labels) && !is_array($tag_labels)) ) {
cf6a88559482 Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    96
            return;
cf6a88559482 Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    97
        }
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    98
        // We get the DocumentTags
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    99
        $em = $this->getDoctrine()->getEntityManager();
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   100
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   101
        $class = $this->getContainer()->getParameter("wiki_tag.document_class");
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   102
        
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   103
        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
   104
            $doc_rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   105
            if(is_a($doc, $class)) {
78
130e95ca5049 correct call getMaxOrder
cavaliet
parents: 68
diff changeset
   106
                // 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
   107
                $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
   108
            }
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   109
            else {
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   110
                $doc_id = $doc;
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   111
            }
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   112
            $doc = $doc_rep->findOneByExternalId($doc_id);
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   113
        }
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   114
        
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
        if(!is_array($tag_labels)) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   117
            $tag_labels = array($tag_labels);
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
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   120
        foreach ($tag_labels as $tag_label) {
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
            $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   123
            $created = false;
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
            $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
   126
            $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
   127
            
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   128
            $nb_tags = $query->getSingleScalarResult();
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   129
            
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   130
            if($nb_tags == 0) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   131
                # look in unit of work
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   132
                $uow = $em->getUnitOfWork();
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   133
                foreach($uow->getScheduledEntityInsertions() as $entity) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   134
                    if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface")) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   135
                        $tag = $entity->getTag();
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   136
                        if(!is_null($tag)) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   137
                            if($tag->getNormalizedLabel() === $normalized_tag_label)
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
                                $nb_tags++;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   140
                                break;
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
                    }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   144
                }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   145
            }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   146
            
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   147
            // 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
   148
            if($nb_tags > 0) {
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   149
                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
   150
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   151
            // returns array($tag, $revision_id, $created)
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   152
            try {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   153
                $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
   154
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   155
            catch (\Exception $e){
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   156
                throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   157
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   158
            
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   159
            $tag = $ar[0];
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   160
            $revision_id = $ar[1];
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   161
            $created = $ar[2];
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   162
            
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   163
            if(!$created) {
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   164
                $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
   165
                $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag));
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   166
                $nb_tags = $query->getSingleScalarResult();
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   167
            }
78
130e95ca5049 correct call getMaxOrder
cavaliet
parents: 68
diff changeset
   168
            
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   169
            if($created || $nb_tags==0){
78
130e95ca5049 correct call getMaxOrder
cavaliet
parents: 68
diff changeset
   170
                $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
   171
                $new_order = $max_order + 1;
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   172
                $new_DT = new DocumentTag();
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   173
                $new_DT->setDocument($doc);
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   174
                $new_DT->setTag($tag);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   175
                $new_DT->setOriginalOrder($new_order);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   176
                $new_DT->setTagOrder($new_order);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   177
                $new_DT->setWikipediaRevisionId($revision_id);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   178
                $em->persist($new_DT);
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
        }
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   181
                
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   182
    }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   183
    
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   184
    /**
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   185
     * Returns the list of tags labels for @author ymh
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   186
     *
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   187
     * @param mixed $id_doc the document id.
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   188
     * @throws WikiTagServiceException if the document is not found
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   189
     */
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   190
    public function getTagLabels($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
        $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   193
        $doc = $rep->findOneByExternalId($id_doc);
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
        if(is_null($doc)) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   196
            throw new WikiTagServiceException("Unknown document id");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   197
        }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   198
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   199
        return $rep->getTagsStr($doc);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
   200
    }
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   201
    
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
     * 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
   204
     * 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
   205
     *
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   206
     * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   207
     */
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   208
    public function reorderTags($document)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   209
    {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   210
        $doctrine = $this->getContainer()->get('doctrine');
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   211
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   212
        $tags_score = array();
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   213
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   214
        foreach($document->getTags() as $tag)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   215
        {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   216
            $label = $tag->getTag()->getLabel();
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   217
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   218
            $score_res = $this->search($label, array("id"=>$document->getId()));
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   219
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   220
            if(count($score_res)>0)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   221
            {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   222
                $score = floatval($score_res[0]['score']);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   223
            }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   224
            else
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   225
            {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   226
                $score = 0.0;
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   227
            }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   228
            $tags_score[] = array($score,$tag);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   229
        }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   230
        // sort tags based on score
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   231
        $i=1;
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   232
        usort($tags_score, function($a, $b) {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   233
            return $a[0]<$b[0]?1:-1;
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
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   236
        foreach($tags_score as $item)
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   237
        {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   238
            $tag = $item[1];
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   239
            $tag->setTagOrder($i++);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   240
            $tag->setIndexNote($item[0]);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   241
            $doctrine->getEntityManager()->persist($tag);
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   242
        }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   243
    
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   244
    }
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   245
    
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
}