Services/SearchService.php
changeset 57 186c4121c7b3
child 68 e7384fb35f7a
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 SearchService 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      * Enter description here ...
       
    48      * @param string $value
       
    49      * @param array $conditions
       
    50      * @param array $fields
       
    51      */
       
    52     public function search($value, array $conditions, array $fields=null)
       
    53     {
       
    54         if(is_null($fields))
       
    55         {
       
    56             $fields = $this->getContainer()->getParameter("wiki_tag.fields");
       
    57         }
       
    58         $doctrine = $this->getContainer()->get('doctrine');
       
    59         $res = $doctrine->getRepository('WikiTagBundle:Document');
       
    60         $fieldquery = array();
       
    61         foreach ($fields as $fieldname => $fielddef) {
       
    62             if(isset($fielddef['weight']))
       
    63             {
       
    64                 $weight = $fielddef['weight'];
       
    65             }
       
    66             else
       
    67             {
       
    68                 $weight = 1.0;
       
    69             }
       
    70             $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
       
    71         }
       
    72         
       
    73         $score_res = $res->search($fieldquery, $conditions);
       
    74         
       
    75         return $score_res;
       
    76     }
       
    77     
       
    78     /**
       
    79      * Service to reorder the tags using their notes in the index search
       
    80      * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
       
    81      */
       
    82     public function reorderTagsForDocument($document)
       
    83     {
       
    84         $doctrine = $this->getContainer()->get('doctrine');
       
    85         
       
    86         $tags_score = array();
       
    87         
       
    88         foreach($document->getTags() as $tag)
       
    89         {
       
    90             $label = $tag->getTag()->getLabel();
       
    91             
       
    92             $score_res = $this->search($label, array("id"=>$document->getId()));
       
    93             
       
    94             if(count($score_res)>0)
       
    95             {
       
    96                 $score = floatval($score_res[0]['score']);
       
    97             }
       
    98             else
       
    99             {
       
   100                 $score = 0.0;
       
   101             }
       
   102             $tags_score[] = array($score,$tag);
       
   103         }
       
   104         // sort tags based on score
       
   105         $i=1;
       
   106         usort($tags_score, function($a, $b) {
       
   107             return $a[0]<$b[0]?1:-1;
       
   108         });
       
   109 
       
   110         foreach($tags_score as $item)
       
   111         {
       
   112             $tag = $item[1];
       
   113             $tag->setTagOrder($i++);
       
   114             $tag->setIndexNote($item[0]);
       
   115             $doctrine->getEntityManager()->persist($tag);
       
   116         }
       
   117         
       
   118     }
       
   119     
       
   120     public function getTagCloud($max_tags)
       
   121     {
       
   122         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
       
   123         return $rep->getTagCloud($max_tags);
       
   124     }
       
   125     
       
   126     public function completion($seed)
       
   127     {
       
   128         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
       
   129         
       
   130         $res = array();
       
   131         foreach ($rep->getCompletion($seed) as $value) {
       
   132             $res[] = $value['label'];
       
   133         }
       
   134         
       
   135         return $res;
       
   136         
       
   137     }
       
   138     
       
   139         
       
   140 }