Services/DocumentService.php
changeset 57 186c4121c7b3
child 63 774ba82dca59
equal deleted inserted replaced
49:e854d8cb376c 57:186c4121c7b3
       
     1 <?php
       
     2 /*
       
     3  * This file is part of the WikiTagBundle package.
       
     4  *
       
     5  * (c) IRI <http://www.iri.centrepompidou.fr/>
       
     6  *
       
     7  * For the full copyright and license information, please view the LICENSE
       
     8  * file that was distributed with this source code.
       
     9  */
       
    10 namespace IRI\Bundle\WikiTagBundle\Services;
       
    11  
       
    12 use Symfony\Component\DependencyInjection\ContainerAware;
       
    13 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    14 
       
    15 class DocumentService extends ContainerAware
       
    16 {
       
    17     /**
       
    18      * Get the container associated with this service.
       
    19      * @return ContainerInterface
       
    20      */
       
    21     public function getContainer()
       
    22     {
       
    23         return $this->container;
       
    24     }
       
    25     
       
    26     /**
       
    27      * Public constructor with container as parameter for contruct injection.
       
    28      * @param ContainerInterface $container
       
    29      */
       
    30     public function __construct(ContainerInterface $container)
       
    31     {
       
    32         $this->setContainer($container);
       
    33     }
       
    34     
       
    35     private $doctrine;
       
    36     
       
    37     public function getDoctrine()
       
    38     {
       
    39         if(is_null($this->doctrine))
       
    40         {
       
    41             $this->doctrine = $this->getContainer()->get('doctrine');
       
    42         }
       
    43         return $this->doctrine;
       
    44     }
       
    45     
       
    46 
       
    47     /**
       
    48      * Copy the list of tags of one document to another.
       
    49 	 *
       
    50      * @param mixed $id_doc_src the source document id
       
    51      * @param mixed $id_doc_tgt the target document id
       
    52      */
       
    53     public function copyTags($id_doc_src, $id_doc_tgt)
       
    54     {
       
    55         
       
    56         $doctrine = $this->getDoctrine();
       
    57         $em = $doctrine->getEntityManager();
       
    58         
       
    59         $doc_rep = $em->getRepository("WikiTagBundle:Document");
       
    60         
       
    61         $src_doc = $doc_rep->findOneByExternalId($id_doc_src);
       
    62         if(is_null($src_doc))
       
    63         {
       
    64             throw new Exception("cloneTags: no source doc");
       
    65         }
       
    66         
       
    67         $tgt_doc = $doc_rep->findOneByExternalId($id_doc_tgt);
       
    68         if(is_null($tgt_doc))
       
    69         {
       
    70             throw new Exception("cloneTags: no target doc");
       
    71         }
       
    72         
       
    73         
       
    74         $doc_rep->copyTags($src_doc, $tgt_doc);
       
    75         
       
    76         $em->flush();
       
    77         
       
    78     }
       
    79     
       
    80         
       
    81 }