| author | tcavalie |
| Wed, 19 Oct 2011 15:44:23 +0200 | |
| changeset 7 | 7a877de630fd |
| parent 5 | 45378793512a |
| child 8 | 7d2fb5d7c9ff |
| permissions | -rw-r--r-- |
| 2 | 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 |
||
11 |
namespace IRI\Bundle\WikiTagBundle\Controller; |
|
12 |
||
13 |
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag; |
|
14 |
use IRI\Bundle\WikiTagBundle\Entity\Tag; |
|
15 |
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils; |
|
16 |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
17 |
use Symfony\Component\HttpFoundation\Response; |
|
18 |
||
19 |
||
20 |
class WikiTagController extends Controller |
|
21 |
{ |
|
22 |
/** |
|
23 |
* Fake index action |
|
24 |
*/ |
|
25 |
public function indexAction() |
|
26 |
{ |
|
27 |
return new Response('<html><body>Nothing to see here.</body></html>'); |
|
28 |
} |
|
29 |
||
30 |
/** |
|
31 |
* Renders the little html to add the css |
|
32 |
*/ |
|
33 |
public function addCssAction() |
|
34 |
{ |
|
35 |
return $this->render('WikiTagBundle:WikiTag:css.html.twig'); |
|
36 |
} |
|
37 |
||
38 |
/** |
|
39 |
* Renders the little html to add the javascript |
|
40 |
* TODO: review why this injection in javascript, t10n? |
|
41 |
*/ |
|
42 |
public function addJavascriptAction() |
|
43 |
{ |
|
44 |
$cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories(); |
|
45 |
// $cats is {"Label":"Créateur"},{"Label":"Datation"},... |
|
46 |
$nbCats = count($cats); |
|
47 |
$ar = array('' => ''); |
|
48 |
for($i=0;$i<$nbCats;$i++){ |
|
| 7 | 49 |
$temp = array($cats[$i]["label"] => $cats[$i]["label"]); |
| 2 | 50 |
$ar = array_merge($ar, $temp); |
51 |
} |
|
52 |
// ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},... |
|
53 |
$categories = json_encode($ar); |
|
54 |
return $this->render('WikiTagBundle:WikiTag:javascript.html.twig', array('categories' => $categories)); |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* Display a list of ordered tag for a document |
|
59 |
* @param integer $id_doc |
|
60 |
*/ |
|
61 |
public function documentTagsAction($id_doc) |
|
62 |
{ |
|
63 |
$ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc); |
|
64 |
return $this->render('WikiTagBundle:WikiTag:documentTags.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc)); |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* |
|
69 |
* TODO : Enter description here ... |
|
70 |
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response |
|
71 |
*/ |
|
72 |
public function tagUpDownAction() |
|
73 |
{ |
|
74 |
||
75 |
$req = $this->getRequest()->request; |
|
76 |
$id_doc = $req->get('wikitag_document_id'); |
|
77 |
// post vars new_order and old_order indicate the position (from 1) of the tag in the list. |
|
78 |
// NB : it is different from the DocumentTag.order in the database. |
|
79 |
$new_order = intval($req->get('new_order')) - 1; |
|
80 |
$old_order = intval($req->get('old_order')) - 1; |
|
81 |
// First we get the DocumentTags |
|
82 |
$em = $this->getDoctrine()->getEntityManager(); |
|
83 |
$ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc); |
|
84 |
// We change the moved DocumentTag's order |
|
85 |
$new_dt_order = $ordered_tags[$new_order]->getTagOrder(); |
|
86 |
$moved_dt = $ordered_tags[$old_order]; |
|
87 |
$moved_dt->setTagOrder($new_dt_order); |
|
88 |
// We move the TaggedSheets's order |
|
89 |
if($new_order > $old_order){ |
|
90 |
// And we decrease the other ones |
|
91 |
for ($i=($old_order+1); $i <= ($new_order); $i++){ |
|
92 |
$dt = $ordered_tags[$i]; |
|
93 |
$dt->setTagOrder($dt->getTagOrder() - 1); |
|
94 |
} |
|
95 |
} |
|
96 |
else{ |
|
97 |
// And we increase the other ones |
|
98 |
for ($i=$new_order; $i <= ($old_order-1); $i++){ |
|
99 |
$dt = $ordered_tags[$i]; |
|
100 |
$dt->setTagOrder($dt->getTagOrder() + 1); |
|
101 |
} |
|
102 |
} |
|
103 |
// Save datas. |
|
104 |
$em->flush(); |
|
105 |
||
106 |
return $this->renderDocTags($id_doc); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* |
|
111 |
* TODO: Enter description here ... |
|
112 |
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response |
|
113 |
*/ |
|
114 |
public function removeTagFromListAction() |
|
115 |
{ |
|
116 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
117 |
$id_tag = $this->getRequest()->request->get('tag_id'); |
|
118 |
// We get the DocumentTag meant to be deleted, and remove it. |
|
119 |
$em = $this->getDoctrine()->getEntityManager(); |
|
120 |
$dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('tag' => $id_tag, 'document' => $id_doc)); |
|
121 |
$em->remove($dt); |
|
122 |
$em->flush(); |
|
123 |
||
124 |
return $this->renderDocTags($id_doc); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* |
|
129 |
* TODO: Enter description here ... |
|
130 |
*/ |
|
131 |
public function modifyDocumentTagAction() |
|
132 |
{ |
|
133 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
134 |
$tag_label = $this->getRequest()->request->get('value'); |
|
135 |
$id_moved_tag = $this->getRequest()->request->get('id'); |
|
136 |
$moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag)); |
|
137 |
if($tagLabel!=$movedTag->getLabel()){ |
|
138 |
// We get the DocumentTags |
|
139 |
$em = $this->getDoctrine()->getEntityManager(); |
|
140 |
$tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc)); |
|
141 |
$nb_tags = count($tags); |
|
142 |
$found = false; |
|
143 |
$i = 0; |
|
144 |
while($i<$nb_tags && $found==false){ |
|
145 |
$dt = $tags[$i]; |
|
146 |
if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){ |
|
147 |
$found = true; |
|
148 |
} |
|
149 |
$i++; |
|
150 |
} |
|
151 |
// If the label was found, we sent a bad request |
|
152 |
if($found==true){ |
|
153 |
return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400); |
|
154 |
} |
|
155 |
// We create the new tag or get the already existing tag. $tag, $revision_id, $created |
|
156 |
$ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label) |
|
157 |
$tag = $ar[0]; |
|
158 |
$revision_id = $ar[1]; |
|
159 |
$created = $ar[2]; |
|
160 |
// We get the DocumentTag and change its tag |
|
161 |
$dt = $this->getDoctrine()->getRepository('WikiTagBundle:WikiTagDocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag)); |
|
162 |
$dt->setTag($tag); |
|
163 |
$dt->setWikipediaRevisionId($revision_id); |
|
164 |
// |
|
165 |
// HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code : |
|
166 |
//kwargs = {DJANGO_ID + "__exact": unicode(ds_id)} |
|
167 |
//results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs) |
|
168 |
//if len(results) > 0: |
|
169 |
// ts.index_note = results[0].score |
|
170 |
// |
|
171 |
// We save the datas |
|
172 |
$doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneBy(array('id' => $id_doc)); |
|
173 |
$doc->setManualOrder(true); |
|
174 |
$em->flush(); |
|
175 |
} |
|
176 |
||
177 |
return $this->renderDocTags($id_doc); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* |
|
| 7 | 182 |
* @Route("/wtrtd") |
| 2 | 183 |
* TODO : Enter description here ... |
| 7 | 184 |
* TODO : implement |
| 2 | 185 |
*/ |
186 |
public function reorderTagDocumentAction() |
|
187 |
{ |
|
188 |
$id_Doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
189 |
return $this->renderDocTags($id_doc); |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* |
|
194 |
* TODO: Enter description here ... |
|
195 |
*/ |
|
196 |
public function addTagAction() |
|
197 |
{ |
|
198 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
199 |
$tag_label = $this->getRequest()->request->get('value'); |
|
200 |
// We get the DocumentTags |
|
201 |
$em = $this->getDoctrine()->getEntityManager(); |
|
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
202 |
$tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc); |
| 2 | 203 |
$nb_tags = count($tags); |
204 |
$found = false; |
|
205 |
$i = 0; |
|
206 |
while($i<$nb_tags && $found==false){ |
|
207 |
$dt = $tags[$i]; |
|
208 |
if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){ |
|
209 |
$found = true; |
|
210 |
} |
|
211 |
$i++; |
|
212 |
} |
|
213 |
// If the label was found, we sent a bad request |
|
214 |
if($found==true){ |
|
215 |
//TODO : translation |
|
216 |
return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400); |
|
217 |
} |
|
218 |
// $tag, $revision_id, $created |
|
219 |
$ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label) |
|
220 |
||
221 |
$tag = $ar[0]; |
|
222 |
$revision_id = $ar[1]; |
|
223 |
$created = $ar[2]; |
|
224 |
||
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
225 |
$tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId())); |
| 2 | 226 |
$nb_tags = count($tags); |
227 |
||
228 |
if($created==true || $nb_tags==0){ |
|
229 |
$new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc); |
|
230 |
// The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned. |
|
231 |
$a1 = reset($new_order_ar); |
|
232 |
$new_order = intval(reset($a1)) + 1; |
|
233 |
// TODO: use a factory that returns an DocumentTagInterface |
|
234 |
$new_DT = new DocumentTag(); |
|
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
235 |
$new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc)); |
| 2 | 236 |
$new_DT->setTag($tag); |
237 |
$new_DT->setOriginalOrder($new_order); |
|
238 |
$new_DT->setTagOrder($new_order); |
|
239 |
$new_DT->setWikipediaRevisionId($revision_id); |
|
240 |
$em->persist($new_DT); |
|
241 |
$em->flush(); |
|
242 |
} |
|
243 |
||
244 |
return $this->renderDocTags($id_doc); |
|
245 |
} |
|
246 |
||
247 |
||
248 |
/** |
|
249 |
* |
|
250 |
* TODO: Enter description here ... |
|
251 |
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response |
|
252 |
*/ |
|
253 |
public function removeWpLinkAction() |
|
254 |
{ |
|
255 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
256 |
$id_tag = $this->getRequest()->request->get('tag_id'); |
|
257 |
$tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag); |
|
258 |
//return new Response(var_dump(array($tag))); |
|
259 |
// We search if the unsemantized version of the tag already exist. |
|
260 |
$un_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('label'=>$tag->getLabel(), 'urlStatus'=>Tag::$TAG_URL_STATUS_DICT['null_result'])); |
|
261 |
$em = $this->getDoctrine()->getEntityManager(); |
|
262 |
if(!$un_tag){ |
|
263 |
// Create another tag almost identical, without the W info |
|
264 |
// TODO: use a factory that return a TagInterface |
|
265 |
$un_tag = new Tag(); |
|
266 |
$un_tag->setLabel($tag->getLabel()); |
|
267 |
$un_tag->setOriginalLabel($tag->getOriginalLabel()); |
|
268 |
$un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['null_result']); |
|
269 |
$un_tag->setWikipediaUrl(null); |
|
270 |
$un_tag->setWikipediaPageId(null); |
|
271 |
$un_tag->setDbpediaUri(null); |
|
272 |
$un_tag->setCategory($tag->getCategory()); |
|
273 |
$un_tag->setAlias($tag->getAlias()); |
|
274 |
$un_tag->setPopularity($tag->getPopularity()); |
|
275 |
$em->persist($un_tag); |
|
276 |
} |
|
277 |
// We associate the unsemantized tag to the DocumentTag and save datas |
|
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
278 |
// TODO: do the request on external id of document |
|
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
279 |
$dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $idTag)); |
| 2 | 280 |
$dt->setTag($un_tag); |
281 |
$em->flush(); |
|
282 |
||
283 |
return $this->renderDocTags($id_doc); |
|
284 |
} |
|
285 |
||
286 |
||
287 |
/** |
|
288 |
* |
|
289 |
* TODO: Enter description here ... |
|
290 |
*/ |
|
291 |
public function updateTagCategoryAction() |
|
292 |
{ |
|
293 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
294 |
$id_tag = $this->getRequest()->request->get('id'); |
|
295 |
$cat_label = $this->getRequest()->request->get('value'); |
|
296 |
// We get the Tag and update its category. |
|
297 |
$em = $this->getDoctrine()->getEntityManager(); |
|
298 |
$tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag); |
|
299 |
if($cat_label==''){ |
|
300 |
$cat = null; |
|
301 |
$tag->nullCategory(); |
|
302 |
} |
|
303 |
else{ |
|
304 |
$cat = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOneBy(array('label' => $cat_label)); |
|
305 |
$tag->setCategory($cat); |
|
306 |
} |
|
307 |
$em->flush(); |
|
308 |
||
309 |
return $this->renderDocTags($id_doc); |
|
310 |
} |
|
311 |
||
312 |
||
313 |
/** |
|
314 |
* |
|
315 |
* Generic render partial template |
|
316 |
* @param unknown_type $id_doc |
|
317 |
*/ |
|
318 |
public function renderDocTags($id_doc) |
|
319 |
{ |
|
320 |
$ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc); |
|
321 |
return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc)); |
|
322 |
} |
|
| 7 | 323 |
|
324 |
/** |
|
325 |
* |
|
326 |
* @Route("/wtrwi") |
|
327 |
* TODO: Enter description here ... |
|
328 |
* TODO : implement |
|
329 |
*/ |
|
330 |
public function resetWpInfoAction() |
|
331 |
{ |
|
332 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
333 |
return $this->renderDocTags($id_doc); |
|
334 |
} |
|
335 |
||
336 |
||
337 |
/** |
|
338 |
* |
|
339 |
* TODO : Enter description here ... |
|
340 |
* TODO : implement |
|
341 |
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response |
|
342 |
*/ |
|
343 |
public function updateTagAliasAction() |
|
344 |
{ |
|
345 |
$id_doc = $this->getRequest()->request->get('wikitag_document_id'); |
|
346 |
return $this->renderDocTags($id_doc); |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* List of all tags |
|
351 |
* TODO: Enter description here ... |
|
352 |
*/ |
|
353 |
public function allTagsAction() |
|
354 |
{ |
|
355 |
$tags = $this->getDoctrine()->getRepository('WikiTagBundle:WikiTagTag')->findAll(); |
|
356 |
// $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET. |
|
357 |
$toto = null; |
|
358 |
if(array_key_exists('toto', $_GET)){ |
|
359 |
$toto = $_GET['toto']; |
|
360 |
} |
|
361 |
return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', array('tags' => $tags, 'toto' => $toto)); |
|
362 |
} |
|
| 2 | 363 |
|
364 |
||
365 |
} |