Search/Search.php
author cavaliet
Tue, 15 Nov 2011 12:27:02 +0100
changeset 33 6c87166b819c
parent 30 d2fba1e3b94b
child 34 21fab44f46fe
permissions -rw-r--r--
First step to add context search to a page. Works fine but needs to be improved with several list of selectors.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
namespace IRI\Bundle\WikiTagBundle\Search;
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use Symfony\Component\DependencyInjection\ContainerAware;
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerInterface;
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class Search extends ContainerAware
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
{
30
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    17
    /**
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    18
     * Get the container associated with this service.
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    19
     * @return ContainerInterface
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    20
     */
25
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    public function getContainer()
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    {
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        return $this->container;
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    }
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    
30
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    26
    /**
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    27
     * Public constructor with container as parameter for contruct injection.
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    28
     * @param ContainerInterface $container
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    29
     */
25
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    public function __construct(ContainerInterface $container)
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    {
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        $this->setContainer($container);
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    }
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    
30
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    35
    private $doctrine;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    36
    
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    37
    public function getDoctrine()
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    38
    {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    39
        if(is_null($this->doctrine))
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    40
        {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    41
            $this->doctrine = $this->getContainer()->get('doctrine');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    42
        }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    43
        return $this->doctrine;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    44
    }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    45
    
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    46
    /**
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    47
     * Service to reorder the tags using their notes in the index search
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    48
     * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    49
     */
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    50
    public function reorderTagsForDocument($document)
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    51
    {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    52
        $doctrine = $this->getContainer()->get('doctrine');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    53
        $res = $doctrine->getRepository('WikiTagBundle:Document');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    54
        $tags_score = array();
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    55
        foreach($document->getTags() as $tag)
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    56
        {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    57
            $label = $tag->getTag()->getLabel();
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    58
            
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    59
            //
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    60
            $fields = $this->getContainer()->getParameter("wiki_tag.fields");
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    61
            
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    62
            $fieldquery = array();
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    63
            foreach ($fields as $fieldname => $fielddef) {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    64
                $columns = "$fieldname";
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    65
                $value = $label;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    66
                if(isset($fielddef['weight']))
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    67
                {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    68
                    $weight = $fielddef['weight'];
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    69
                }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    70
                else
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    71
                {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    72
                    $weight = 1.0;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    73
                }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    74
                $fieldquery[] = array("columns"=>$columns, "value"=>$value, "weight"=>$weight);
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    75
            }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    76
            
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    77
            $score_res = $res->search($fieldquery, array("id"=>$document->getId()));
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    78
            
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    79
            if(count($score_res)>0)
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    80
            {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    81
                $score = floatval($score_res[0]['score']);
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    82
            }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    83
            else
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    84
            {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    85
                $score = 0.0;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    86
            }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    87
            $tags_score[] = array($score,$tag);
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    88
        }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    89
        // sort tags based on score
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    90
        $i=1;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    91
        usort($tags_score, function($a, $b) {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    92
            return $a[0]<$b[0]?1:-1;
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    93
        });
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    94
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    95
        foreach($tags_score as $item)
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    96
        {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    97
            $tag = $item[1];
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    98
            $tag->setTagOrder($i++);
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    99
            $tag->setIndexNote($item[0]);
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   100
            $doctrine->getEntityManager()->persist($tag);
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   101
        }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   102
        
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   103
    }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   104
    
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   105
    public function getTagCloud($max_tags)
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   106
    {
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   107
        $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   108
        $qb->select('t', 'COUNT( dt.id ) AS nb_docs');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   109
        $qb->from('WikiTagBundle:Tag','t');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   110
        $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   111
        $qb->addGroupBy('t.id');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   112
        $qb->addOrderBy('nb_docs','DESC');
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   113
        
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   114
    }
d2fba1e3b94b correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   115
    
25
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        
11fd79666374 add missing files
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
}