Services/DocumentService.php
author ymh <ymh.work@gmail.com>
Tue, 06 Dec 2011 14:53:12 +0100
changeset 57 186c4121c7b3
child 63 774ba82dca59
permissions -rw-r--r--
add service tp copy tag from one document to another
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
 
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use Symfony\Component\DependencyInjection\ContainerAware;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerInterface;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class DocumentService extends ContainerAware
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
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     * 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
    19
     * @return ContainerInterface
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    public function getContainer()
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
        return $this->container;
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
    
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
     * 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
    28
     * @param ContainerInterface $container
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    public function __construct(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
        $this->setContainer($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
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    private $doctrine;
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
    public function getDoctrine()
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
        if(is_null($this->doctrine))
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
            $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
    42
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        return $this->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
    
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
     * Copy the list of tags of one document to another.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
     * @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
    51
     * @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
    52
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    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
    54
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        $doctrine = $this->getDoctrine();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        $em = $doctrine->getEntityManager();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        $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
    60
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        $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
    62
        if(is_null($src_doc))
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
            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
    65
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        $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
    68
        if(is_null($tgt_doc))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            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
    71
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        $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
    75
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        $em->flush();
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
    
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
}