Search/Search.php
changeset 30 d2fba1e3b94b
parent 25 11fd79666374
child 34 21fab44f46fe
equal deleted inserted replaced
29:7496254cfead 30:d2fba1e3b94b
    12 use Symfony\Component\DependencyInjection\ContainerAware;
    12 use Symfony\Component\DependencyInjection\ContainerAware;
    13 use Symfony\Component\DependencyInjection\ContainerInterface;
    13 use Symfony\Component\DependencyInjection\ContainerInterface;
    14 
    14 
    15 class Search extends ContainerAware
    15 class Search extends ContainerAware
    16 {
    16 {
    17     
    17     /**
       
    18      * Get the container associated with this service.
       
    19      * @return ContainerInterface
       
    20      */
    18     public function getContainer()
    21     public function getContainer()
    19     {
    22     {
    20         return $this->container;
    23         return $this->container;
    21     }
    24     }
    22     
    25     
       
    26     /**
       
    27      * Public constructor with container as parameter for contruct injection.
       
    28      * @param ContainerInterface $container
       
    29      */
    23     public function __construct(ContainerInterface $container)
    30     public function __construct(ContainerInterface $container)
    24     {
    31     {
    25         $this->setContainer($container);
    32         $this->setContainer($container);
    26     }
    33     }
    27     
    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      * Service to reorder the tags using their notes in the index search
       
    48      * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
       
    49      */
       
    50     public function reorderTagsForDocument($document)
       
    51     {
       
    52         $doctrine = $this->getContainer()->get('doctrine');
       
    53         $res = $doctrine->getRepository('WikiTagBundle:Document');
       
    54         $tags_score = array();
       
    55         foreach($document->getTags() as $tag)
       
    56         {
       
    57             $label = $tag->getTag()->getLabel();
       
    58             
       
    59             //
       
    60             $fields = $this->getContainer()->getParameter("wiki_tag.fields");
       
    61             
       
    62             $fieldquery = array();
       
    63             foreach ($fields as $fieldname => $fielddef) {
       
    64                 $columns = "$fieldname";
       
    65                 $value = $label;
       
    66                 if(isset($fielddef['weight']))
       
    67                 {
       
    68                     $weight = $fielddef['weight'];
       
    69                 }
       
    70                 else
       
    71                 {
       
    72                     $weight = 1.0;
       
    73                 }
       
    74                 $fieldquery[] = array("columns"=>$columns, "value"=>$value, "weight"=>$weight);
       
    75             }
       
    76             
       
    77             $score_res = $res->search($fieldquery, array("id"=>$document->getId()));
       
    78             
       
    79             if(count($score_res)>0)
       
    80             {
       
    81                 $score = floatval($score_res[0]['score']);
       
    82             }
       
    83             else
       
    84             {
       
    85                 $score = 0.0;
       
    86             }
       
    87             $tags_score[] = array($score,$tag);
       
    88         }
       
    89         // sort tags based on score
       
    90         $i=1;
       
    91         usort($tags_score, function($a, $b) {
       
    92             return $a[0]<$b[0]?1:-1;
       
    93         });
       
    94 
       
    95         foreach($tags_score as $item)
       
    96         {
       
    97             $tag = $item[1];
       
    98             $tag->setTagOrder($i++);
       
    99             $tag->setIndexNote($item[0]);
       
   100             $doctrine->getEntityManager()->persist($tag);
       
   101         }
       
   102         
       
   103     }
       
   104     
       
   105     public function getTagCloud($max_tags)
       
   106     {
       
   107         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
       
   108         $qb->select('t', 'COUNT( dt.id ) AS nb_docs');
       
   109         $qb->from('WikiTagBundle:Tag','t');
       
   110         $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
       
   111         $qb->addGroupBy('t.id');
       
   112         $qb->addOrderBy('nb_docs','DESC');
       
   113         
       
   114     }
       
   115     
    28         
   116         
    29 }
   117 }