| author | cavaliet |
| Mon, 28 Nov 2011 15:58:34 +0100 | |
| changeset 49 | e854d8cb376c |
| parent 34 | 21fab44f46fe |
| permissions | -rwxr-xr-x |
| 25 | 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\Search; |
|
11 |
||
12 |
use Symfony\Component\DependencyInjection\ContainerAware; |
|
13 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14 |
||
15 |
class Search extends ContainerAware |
|
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 | 21 |
public function getContainer() |
22 |
{ |
|
23 |
return $this->container; |
|
24 |
} |
|
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 | 30 |
public function __construct(ContainerInterface $container) |
31 |
{ |
|
32 |
$this->setContainer($container); |
|
33 |
} |
|
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 |
} |
| 34 | 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 |
} |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
77 |
|
|
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 |
* 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
|
80 |
* @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
81 |
*/ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
82 |
public function reorderTagsForDocument($document) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
83 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
84 |
$doctrine = $this->getContainer()->get('doctrine'); |
| 34 | 85 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
86 |
$tags_score = array(); |
| 34 | 87 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
88 |
foreach($document->getTags() as $tag) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
89 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
90 |
$label = $tag->getTag()->getLabel(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
91 |
|
| 34 | 92 |
$score_res = $this->search($label, array("id"=>$document->getId())); |
|
30
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 |
if(count($score_res)>0) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
95 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
96 |
$score = floatval($score_res[0]['score']); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
97 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
98 |
else |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
99 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
100 |
$score = 0.0; |
|
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 |
$tags_score[] = array($score,$tag); |
|
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 |
// sort tags based on score |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
105 |
$i=1; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
106 |
usort($tags_score, function($a, $b) { |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
107 |
return $a[0]<$b[0]?1:-1; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
108 |
}); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
109 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
110 |
foreach($tags_score as $item) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
111 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
112 |
$tag = $item[1]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
113 |
$tag->setTagOrder($i++); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
114 |
$tag->setIndexNote($item[0]); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
115 |
$doctrine->getEntityManager()->persist($tag); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
116 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
117 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
118 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
119 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
120 |
public function getTagCloud($max_tags) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
121 |
{ |
| 34 | 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; |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
136 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
137 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
138 |
|
| 25 | 139 |
|
140 |
} |