1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * solr actions. |
---|
5 | * |
---|
6 | * @package cpc |
---|
7 | * @subpackage solr |
---|
8 | * @author Your name here |
---|
9 | * @version SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $ |
---|
10 | */ |
---|
11 | class solrActions extends sfActions |
---|
12 | { |
---|
13 | |
---|
14 | private function getPhoto($obj) { |
---|
15 | sfProjectConfiguration::getActive()->loadHelpers(array('Url')); |
---|
16 | switch(get_class($obj)) { |
---|
17 | case 'Intervention': |
---|
18 | if ($obj->getParlementaire()->__toString()) { |
---|
19 | return $this->getPartial('parlementaire/photoParlementaire', array('parlementaire'=>$obj->getParlementaire(), 'height'=>70)); |
---|
20 | } |
---|
21 | case 'QuestionEcrite': |
---|
22 | return $this->getPartial('parlementaire/photoParlementaire', array('parlementaire'=>$obj->getParlementaire(), 'height'=>70)); |
---|
23 | case 'Amendement': |
---|
24 | return ''; |
---|
25 | case 'Parlementaire': |
---|
26 | return $this->getPartial('parlementaire/photoParlementaire', array('parlementaire'=>$obj, 'height'=>70)); |
---|
27 | case 'Commentaire': |
---|
28 | return '<img width="53" class="jstitle" title="'.$obj->getCitoyen()->getLogin().'" alt="'.$obj->getCitoyen()->getLogin().'" src="'.url_for('@photo_citoyen?slug='.$obj->getCitoyen()->getSlug()).'"/>'; |
---|
29 | case 'Citoyen': |
---|
30 | return '<img width="53" class="jstitle" title="'.$obj->getLogin().'" alt="'.$obj->getLogin().'" src="'.url_for('@photo_citoyen?slug='.$obj->getSlug()).'"/>'; |
---|
31 | case 'NonObjectPage': |
---|
32 | return $obj->getImage(); |
---|
33 | } |
---|
34 | } |
---|
35 | /** |
---|
36 | * Executes index action |
---|
37 | * |
---|
38 | * @param sfRequest $request A request object |
---|
39 | */ |
---|
40 | public function executeSearch(sfWebRequest $request) |
---|
41 | { |
---|
42 | if ($search = $request->getParameter('search')) { |
---|
43 | if ($ob = $request->getParameter('object_name')) |
---|
44 | $ob = '&object_name='.$ob; |
---|
45 | return $this->redirect('solr/search?query='.$search.$ob); |
---|
46 | } |
---|
47 | $this->query = $request->getParameter('query'); |
---|
48 | |
---|
49 | $query = preg_replace('/\*/', '', $this->query); |
---|
50 | |
---|
51 | if (!strlen($query)) { |
---|
52 | $query = '*'; |
---|
53 | } |
---|
54 | |
---|
55 | $nb = 20; |
---|
56 | $deb = ($request->getParameter('page', 1) - 1) * $nb ; |
---|
57 | $fq = ''; |
---|
58 | $this->facet = array(); |
---|
59 | |
---|
60 | $this->selected = array(); |
---|
61 | if ($on = $request->getParameter('object_name')) { |
---|
62 | $this->selected['object_name'][$on] = 1; |
---|
63 | $fq .= " object_name:$on"; |
---|
64 | } |
---|
65 | if ($tags = $request->getParameter('tag')) { |
---|
66 | foreach(explode(',', $tags) as $tag) { |
---|
67 | $this->selected['tag'][$tag] = 1; |
---|
68 | $fq .= ' tag:"'.$tag.'"'; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | //Récupère les résultats auprès de SolR |
---|
73 | $params = array('hl'=>'true', 'fl' => 'id,object_id,object_name,date,description', 'hl.fragsize'=>500, "facet"=>"true", "facet.field"=>array("object_name","tag"), "facet.date" => "date", "facet.date.start"=>"2007-05-01T00:00:00Z", "facet.date.end"=>"NOW", "facet.date.gap"=>"+1MONTH", 'fq' => $fq, "facet.date.include" => "edge"); |
---|
74 | $this->sort_type = 'pertinence'; |
---|
75 | |
---|
76 | $this->sort = $request->getParameter('sort'); |
---|
77 | $this->ajax = $request->getParameter('ajax'); |
---|
78 | $date = $request->getParameter('date'); |
---|
79 | $format = $request->getParameter('format'); |
---|
80 | |
---|
81 | $this->tags = 0; |
---|
82 | if ($format) { |
---|
83 | sfConfig::set('sf_web_debug', false); |
---|
84 | $this->tags = $request->getParameter('tags'); |
---|
85 | $this->format = $format; |
---|
86 | } |
---|
87 | |
---|
88 | $this->title = $request->getParameter('title'); |
---|
89 | |
---|
90 | if ($format == 'rss') { |
---|
91 | $this->setTemplate('rss'); |
---|
92 | $this->feed = new sfRssFeed(); |
---|
93 | $this->feed->setLanguage('fr'); |
---|
94 | $this->sort = 1; |
---|
95 | $date = null; |
---|
96 | $from = null; |
---|
97 | } |
---|
98 | |
---|
99 | if ($format == 'json') { |
---|
100 | $this->getResponse()->setContentType('text/plain; charset=utf-8'); |
---|
101 | $this->setTemplate('json'); |
---|
102 | $this->setLayout(false); |
---|
103 | } |
---|
104 | |
---|
105 | if ($format == 'xml') { |
---|
106 | $this->getResponse()->setContentType('text/xml; charset=utf-8'); |
---|
107 | $this->setTemplate('xml'); |
---|
108 | $this->setLayout(false); |
---|
109 | } |
---|
110 | |
---|
111 | if ($format == 'csv') { |
---|
112 | // $this->getResponse()->setContentType('application/csv; charset=utf-8'); |
---|
113 | $this->getResponse()->setContentType('text/plain; charset=utf-8'); |
---|
114 | $this->setTemplate('csv'); |
---|
115 | $this->setLayout(false); |
---|
116 | } |
---|
117 | |
---|
118 | if ($this->sort) { |
---|
119 | $this->selected['sort'] = 1; |
---|
120 | $params['sort'] = "date desc"; |
---|
121 | $this->sort_type = 'date'; |
---|
122 | } |
---|
123 | |
---|
124 | $this->vue = 'par_mois'; |
---|
125 | |
---|
126 | $period = ''; |
---|
127 | |
---|
128 | if ($date) { |
---|
129 | $this->selected['date'][$date] = $date; |
---|
130 | if (preg_match('/\d{8}/',$date)) { |
---|
131 | $date = preg_replace('/(\d{4})(\d{2})(\d{2})/', '\1-\2-\3T00:00:00Z', $date); |
---|
132 | } |
---|
133 | $dates = explode(',', $date); |
---|
134 | list($from, $to) = $dates; |
---|
135 | |
---|
136 | $nbjours = round((strtotime($to) - strtotime($from))/(60*60*24)-1); |
---|
137 | $jours_max = 90; // Seuil en nb de jours qui détermine l'affichage par jour ou par mois d'une période |
---|
138 | |
---|
139 | $comp_date_from = explode("T", $from); |
---|
140 | $comp_date_from = explode("-", $comp_date_from[0]); |
---|
141 | $comp_date_from = mktime(0, 0, 0, $comp_date_from[1] + 1, $comp_date_from[2], $comp_date_from[0]); |
---|
142 | $comp_date_from = date("Y-m-d", $comp_date_from).'T00:00:00Z'; |
---|
143 | |
---|
144 | // Affichage d'une période |
---|
145 | if(($nbjours < $jours_max) and ($from != $to) and ($comp_date_from != $to)) { |
---|
146 | $period = 'DAY'; |
---|
147 | $this->vue = 'par_jour'; |
---|
148 | } |
---|
149 | if($nbjours > $jours_max) { |
---|
150 | $period = 'MONTH'; |
---|
151 | $to = $to.'+1MONTH'; |
---|
152 | $this->vue = 'par_mois'; |
---|
153 | } |
---|
154 | // Affichage d'un jour |
---|
155 | if($from == $to) { |
---|
156 | $period = 'DAY'; |
---|
157 | $this->vue = 'jour'; |
---|
158 | } |
---|
159 | // Affichage d'un mois |
---|
160 | if($comp_date_from == $to) { |
---|
161 | $period = 'DAY'; |
---|
162 | $this->vue = 'mois'; |
---|
163 | } |
---|
164 | |
---|
165 | if ($period == 'DAY') { |
---|
166 | $from = date ('Y-m-d', strtotime($from)-(3600*2+1)).'T23:59:59Z'; |
---|
167 | } |
---|
168 | |
---|
169 | $query .= ' date:['.$from.' TO '.$to.']'; |
---|
170 | $params['facet.date.start'] = $from; |
---|
171 | $params['facet.date.end'] = $to; |
---|
172 | $params['facet.date.gap'] = '+1'.$period; |
---|
173 | } |
---|
174 | |
---|
175 | $this->start = $params['facet.date.start']; |
---|
176 | if ($period == 'DAY') { |
---|
177 | $this->start = date ('Ymd', strtotime($this->start)+1); |
---|
178 | } |
---|
179 | $this->end = $params['facet.date.end']; |
---|
180 | if($this->end == 'NOW') { |
---|
181 | $this->end = date("Ymd"); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | try { |
---|
186 | $s = new SolrConnector(); |
---|
187 | $results = $s->search($query, $params, $deb, $nb); |
---|
188 | } |
---|
189 | catch(Exception $e) { |
---|
190 | $results = array('response' => array('docs' => array(), 'numFound' => 0)); |
---|
191 | $this->getUser()->setFlash('error', 'Désolé, le moteur de recherche est indisponible pour le moment'); |
---|
192 | } |
---|
193 | |
---|
194 | if (!$format && count($results['response']['docs']) == 1 && $results['response']['docs'][0]['object_name'] == 'Parlementaire') { |
---|
195 | return $this->redirect($results['response']['docs'][0]['object']->getLink()); |
---|
196 | } |
---|
197 | |
---|
198 | //Reconstitut les résultats |
---|
199 | $this->results = $results['response']; |
---|
200 | for($i = 0 ; $i < count($this->results['docs']) ; $i++) { |
---|
201 | $res = $this->results['docs'][$i]; |
---|
202 | $obj = $res['object']; |
---|
203 | $this->results['docs'][$i]['link'] = $obj->getLink(); |
---|
204 | $this->results['docs'][$i]['photo'] = $this->getPhoto($obj); |
---|
205 | $this->results['docs'][$i]['titre'] = $obj->getTitre(); |
---|
206 | $this->results['docs'][$i]['personne'] = $obj->getPersonne(); |
---|
207 | if (isset($results['highlighting'][$res['id']]['text'])) { |
---|
208 | $high_res = array(); |
---|
209 | foreach($results['highlighting'][$res['id']]['text'] as $h) { |
---|
210 | $h = preg_replace('/.*=/', '', $h); |
---|
211 | array_push($high_res, $h); |
---|
212 | } |
---|
213 | $this->results['docs'][$i]['highlighting'] = preg_replace('/^'."$this->results['docs'][$i]['personne']".'/', '', implode('...', $high_res)); |
---|
214 | } |
---|
215 | else $this->results['docs'][$i]['highlighting'] = $this->results['docs'][$i]['description']; |
---|
216 | } |
---|
217 | |
---|
218 | $this->results['end'] = $deb + $nb; |
---|
219 | $this->results['page'] = $deb/$nb + 1; |
---|
220 | if ($this->results['end'] > $this->results['numFound'] && $this->results['numFound']) { |
---|
221 | $this->results['end'] = $this->results['numFound'] + 1; |
---|
222 | } |
---|
223 | |
---|
224 | if (isset($results['facet_counts'])) { |
---|
225 | $this->facet['type']['prefix'] = ''; |
---|
226 | $this->facet['type']['facet_field'] = 'object_name'; |
---|
227 | $this->facet['type']['name'] = 'Types'; |
---|
228 | $this->facet['type']['values'] = $results['facet_counts']['facet_fields']['object_name']; |
---|
229 | |
---|
230 | //Prépare les facets des parlementaires |
---|
231 | $this->facet['parlementaires']['prefix'] = 'parlementaire='; |
---|
232 | $this->facet['parlementaires']['facet_field'] = 'tag'; |
---|
233 | $this->facet['parlementaires']['name'] = 'Parlementaires'; |
---|
234 | |
---|
235 | $tags = $results['facet_counts']['facet_fields']['tag']; |
---|
236 | $this->facet['tag']['prefix'] = ''; |
---|
237 | $this->facet['tag']['facet_field'] = 'tag'; |
---|
238 | $this->facet['tag']['name'] = 'Tags'; |
---|
239 | foreach($tags as $tag => $nb ) { |
---|
240 | if (!$nb) |
---|
241 | continue; |
---|
242 | if (!preg_match('/=/', $tag)) |
---|
243 | $this->facet['tag']['values'][$tag] = $nb; |
---|
244 | if (preg_match('/^parlementaire=(.*)/', $tag, $matches)) { |
---|
245 | $this->facet['parlementaires']['values'][$matches[1]] = $nb; |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | if (!$results['response']['numFound']) { |
---|
251 | if ($format) |
---|
252 | return ; |
---|
253 | return $this->setTemplate('noresults'); |
---|
254 | } |
---|
255 | $this->fdates = array(); |
---|
256 | $this->fdates['max'] = 1; |
---|
257 | foreach($results['facet_counts']['facet_dates']['date'] as $date => $nb) { |
---|
258 | if ($period == 'DAY') { |
---|
259 | $date = date ('Ymd', strtotime($date)+1); |
---|
260 | }else{ |
---|
261 | $date = date ('Ymd', strtotime($date)); |
---|
262 | } |
---|
263 | if (preg_match('/^20/', $date)) { |
---|
264 | $pc = $nb/$results['response']['numFound']; |
---|
265 | $this->fdates['values'][$date] = array('nb' => $nb, 'pc' => $pc); |
---|
266 | if ($this->fdates['max'] < $pc) { |
---|
267 | $this->fdates['max'] = $pc; |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | public function executeRedirect(sfWebRequest $request) |
---|
274 | { |
---|
275 | if ($p = $request->getParameter('slug')) { |
---|
276 | $parlementaire = Doctrine::getTable('Parlementaire')->findOneBySlug($p); |
---|
277 | $request->setParameter('tag', 'parlementaire='.$parlementaire); |
---|
278 | } |
---|
279 | |
---|
280 | return $this->forward('solr', 'search'); |
---|
281 | } |
---|
282 | } |
---|