Search/Search.php
changeset 34 21fab44f46fe
parent 30 d2fba1e3b94b
equal deleted inserted replaced
30:d2fba1e3b94b 34:21fab44f46fe
    40         {
    40         {
    41             $this->doctrine = $this->getContainer()->get('doctrine');
    41             $this->doctrine = $this->getContainer()->get('doctrine');
    42         }
    42         }
    43         return $this->doctrine;
    43         return $this->doctrine;
    44     }
    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     }
    45     
    77     
    46     /**
    78     /**
    47      * Service to reorder the tags using their notes in the index search
    79      * Service to reorder the tags using their notes in the index search
    48      * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
    80      * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
    49      */
    81      */
    50     public function reorderTagsForDocument($document)
    82     public function reorderTagsForDocument($document)
    51     {
    83     {
    52         $doctrine = $this->getContainer()->get('doctrine');
    84         $doctrine = $this->getContainer()->get('doctrine');
    53         $res = $doctrine->getRepository('WikiTagBundle:Document');
    85         
    54         $tags_score = array();
    86         $tags_score = array();
       
    87         
    55         foreach($document->getTags() as $tag)
    88         foreach($document->getTags() as $tag)
    56         {
    89         {
    57             $label = $tag->getTag()->getLabel();
    90             $label = $tag->getTag()->getLabel();
    58             
    91             
    59             //
    92             $score_res = $this->search($label, array("id"=>$document->getId()));
    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             
    93             
    79             if(count($score_res)>0)
    94             if(count($score_res)>0)
    80             {
    95             {
    81                 $score = floatval($score_res[0]['score']);
    96                 $score = floatval($score_res[0]['score']);
    82             }
    97             }
   102         
   117         
   103     }
   118     }
   104     
   119     
   105     public function getTagCloud($max_tags)
   120     public function getTagCloud($max_tags)
   106     {
   121     {
   107         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
   122         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
   108         $qb->select('t', 'COUNT( dt.id ) AS nb_docs');
   123         return $rep->getTagCloud($max_tags);
   109         $qb->from('WikiTagBundle:Tag','t');
   124     }
   110         $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
   125     
   111         $qb->addGroupBy('t.id');
   126     public function completion($seed)
   112         $qb->addOrderBy('nb_docs','DESC');
   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;
   113         
   136         
   114     }
   137     }
   115     
   138     
   116         
   139         
   117 }
   140 }