REPORT de la branche 1.0
[euphorik.git] / js / pageAdmin.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // La page d'administation, ne peut être accédée que par les ekMaster (admins)
20
21 function PageAdmin(client, formateur, util)
22 {
23 this.nom = "admin"
24
25 this.client = client
26 this.formateur = formateur
27 this.util = util
28
29 this.pageEvent = new PageEvent("admin", this.util)
30
31 // le timer qui rappelle periodiquement le rafraichissement des IP bannies
32 this.timeoutIDmajIPs = null
33 }
34
35 /**
36 * Interface des pages.
37 */
38 PageAdmin.prototype.contenu = function()
39 {
40 return '\
41 <h1>Trolls</h1>\
42 <p>Un troll est un sujet à débat, en général une question, affiché sur la page principale.</p>\
43 <p>Chaque semaine un troll est choisi au hasard parmis les trolls proposés et devient le troll de la semaine.</p>\
44 <form action="" id="nouveauTroll">\
45 <p>\
46 <input class="troll" name="troll" type="text" maxlength="500" value=""></input>\
47 <button class="return" value="return">poster</button>\
48 </p>\
49 </form>\
50 <div id="trolls"></div>\
51 <h1>IPs bannies</h1>\
52 <div id="ips"></div>'
53 }
54
55 /**
56 * Interface des pages.
57 */
58 PageAdmin.prototype.charger = function()
59 {
60 $("#page form#nouveauTroll").submit(function(){return false})
61
62 var thisPage = this
63
64 // la liste des trolls proposés par les ekMasters
65 this.trolls = new Trolls(this.client, this.util, this.formateur)
66
67 this.waitEvent()
68
69 this.majIPs()
70
71 $("#page form#nouveauTroll input.troll").focus()
72
73 $("#page form#nouveauTroll button.return").click(
74 function()
75 {
76 thisPage.posterTroll()
77 }
78 )
79 }
80
81 /**
82 * Interface des pages.
83 */
84 PageAdmin.prototype.decharger = function()
85 {
86 this.pageEvent.stopAttenteCourante()
87
88 // supprime le rafraichissement période des ips
89 if (this.timeoutIDmajIPs)
90 clearTimeout(this.timeoutIDmajIPs)
91 }
92
93 /**
94 * Post un troll, le contenu est lu à partir de "input.troll".
95 */
96 PageAdmin.prototype.posterTroll = function()
97 {
98 var thisPageAdmin = this
99
100 var content = $("#page form#nouveauTroll input.troll").val()
101
102 content = content.trim()
103 if (content == "")
104 {
105 this.util.messageDialogue("Le troll est vide")
106 return
107 }
108
109 var dataToSend =
110 {
111 "action" : "put_troll",
112 "cookie" : this.client.cookie,
113 "content" : content
114 }
115
116 ;; dumpObj(dataToSend)
117 jQuery.ajax(
118 {
119 type: "POST",
120 url: "request",
121 dataType: "json",
122 data: this.util.jsonVersAction(dataToSend),
123 success:
124 function(data)
125 {
126 ;; dumpObj(data)
127
128 if (data["reply"] == "ok")
129 {
130 $("#page form#nouveauTroll input.troll").val("")
131 }
132 else if (data["reply"] == "error")
133 {
134 thisPageAdmin.util.messageDialogue(data["error_message"])
135 }
136 }
137 }
138 )
139 }
140
141 /**
142 * Met à jour la liste des IP bannies.
143 */
144 PageAdmin.prototype.majIPs = function()
145 {
146 if (this.timeoutIDmajIPs)
147 clearTimeout(this.timeoutIDmajIPs)
148
149 var thisPageAdmin = this
150
151 var dataToSend =
152 {
153 "action" : "list_banned_ips",
154 "cookie" : this.client.cookie
155 }
156
157 ;; dumpObj(dataToSend)
158 jQuery.ajax(
159 {
160 type: "POST",
161 url: "request",
162 dataType: "json",
163 data: this.util.jsonVersAction(dataToSend),
164 success:
165 function(data)
166 {
167 ;; dumpObj(data)
168
169 if (data["reply"] == "list_banned_ips")
170 {
171 var XHTML = ""
172 for(var i = 0; i < data["list"].length; i++)
173 {
174 var ip = data["list"][i]
175 XHTML += '<div class="ban"><span class="ip">' + ip["ip"] + '</span>|' +
176 '<span class="temps">' +
177 ip["remaining_time"] +
178 '</span>|'
179 for(var j = 0; j < ip["users"].length; j++)
180 {
181 var user = ip["users"][j]
182 XHTML += (j > 0 ? ", " : "") +
183 '<span class="pseudo">' + thisPageAdmin.formateur.traitementComplet(user["nick"]) + '</span>' +
184 (user["login"] == "" ? "" : '<span class="login">(' + thisPageAdmin.formateur.traitementComplet(user["login"]) + ')</span>')
185 }
186 XHTML += '<span class="deban">débannir</span></div>'
187 }
188
189 if (data["list"].length == 0)
190 XHTML += '<p>Aucune IP bannie</p>'
191
192 $("#ips").html(XHTML)
193
194 $(".ban").each(
195 function()
196 {
197 var ip = $(".ip", this).html()
198 $(".deban", this).click(
199 function()
200 {
201 thisPageAdmin.util.messageDialogue("Êtes-vous sur de vouloir débannir l'IP ''" + ip + "'' ?", messageType.question,
202 {"Oui" : function()
203 {
204 thisPageAdmin.deban(ip)
205 },
206 "Non" : function(){}
207 }
208 )
209 }
210 )
211 }
212 )
213 }
214 else if (data["reply"] == "error")
215 {
216 thisPageAdmin.util.messageDialogue(data["error_message"])
217 }
218
219 // rafraichissement toutes les minutes (je sais c'est mal)
220 // le problème est le rafraichissement des temps restant de bannissement qui doit être fait du coté client
221 thisPageAdmin.timeoutIDmajIPs = setTimeout(function(){ thisPageAdmin.majIPs() }, 60 * 1000)
222 }
223 }
224 )
225 }
226
227 /**
228 * Débannie une ip donnée.
229 */
230 PageAdmin.prototype.deban = function(ip)
231 {
232 var thisPageAdmin = this
233
234 var dataToSend =
235 {
236 "action" : "unban",
237 "cookie" : this.client.cookie,
238 "ip" : ip
239 }
240
241 ;; dumpObj(dataToSend)
242 jQuery.ajax(
243 {
244 type: "POST",
245 url: "request",
246 dataType: "json",
247 data: this.util.jsonVersAction(dataToSend),
248 success:
249 function(data)
250 {
251 ;; dumpObj(data)
252 if(data["reply"] == "error")
253 {
254 thisPageAdmin.util.messageDialogue(data["error_message"])
255 }
256 }
257 }
258 )
259 }
260
261 /**
262 * Attente d'événement de la part du serveur.
263 */
264 PageAdmin.prototype.waitEvent = function()
265 {
266 var thisPageAdmin = this
267
268 this.pageEvent.waitEvent(
269 function() { return { "last_troll" : thisPageAdmin.trolls.dernierTroll }},
270 {
271 "troll_added" : function(data){ thisPageAdmin.trolls.ajouterTrollEvent(data) },
272 "troll_modified" : function(data){ thisPageAdmin.trolls.modifierTrollEvent(data) },
273 "troll_deleted" : function(data){ thisPageAdmin.trolls.supprimerTrollEvent(data) },
274 "banned_ips_refresh" : function(data){ thisPageAdmin.majIPs() },
275 "error" :
276 function(data)
277 {
278 thisTrolls.util.messageDialogue(data["error_message"])
279 }
280 }
281 )
282 }
283
284 ///////////////////////////////////////////////////////////////////////////////////////////////////
285
286 /**
287 * Représente un troll, pas grand chose finalement.
288 */
289 function Troll(content, author)
290 {
291 this.content = content
292 this.author = author
293 }
294
295
296 ///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
299 function Trolls(client, util, formateur)
300 {
301 this.client = client
302 this.util = util
303 this.formateur = formateur
304 this.dernierTroll = 0
305
306 this.trolls = {}
307 }
308
309 Trolls.prototype.ajouterTrollEvent = function(data)
310 {
311 var thisTrolls = this
312
313 var XHTML = ""
314 for (var i = 0; i < data["trolls"].length; i++)
315 {
316 var troll = new Troll(data["trolls"][i]["content"], data["trolls"][i]["author"])
317 var trollId = data["trolls"][i]["troll_id"]
318 thisTrolls.trolls[trollId] = troll
319
320 XHTML +=
321 '<div id="troll' + trollId + '" class="troll">' +
322 '<span class="content">' + thisTrolls.formateur.traitementComplet(troll.content, troll.author) + '</span>' +
323 '<span class="author"> - ' + thisTrolls.formateur.traitementComplet(troll.author) + '</span>' +
324 (data["trolls"][i]["author_id"] == thisTrolls.client.id ? '<span class="editTroll">éditer</span><span class="delTroll">Supprimer</span>' : '') +
325 '</div>'
326 }
327 $("#trolls").append(XHTML)
328 $("#trolls .troll").filter(function(){return parseInt($(this).attr("id").substr(5)) > thisTrolls.dernierTroll}).each(
329 function()
330 {
331 var troll = this
332 var id = parseInt($(this).attr("id").substr(5))
333
334 $("a[@rel*=lightbox]", this).lightBox()
335
336 $(this).keypress(
337 function(e)
338 {
339 if (e.which == 13) // return
340 $(".modifier", this).click()
341 }
342 )
343 $(".delTroll", this).click(
344 function()
345 {
346 thisTrolls.util.messageDialogue(
347 "Êtes-vous sur de vouloir supprimer le troll \"" + thisTrolls.trolls[id].content + "\" ?",
348 messageType.question,
349 {
350 "oui" : function()
351 {
352 thisTrolls.supprimer(id)
353 },
354 "non" : function(){}
355 }
356 )
357 }
358 )
359 $(".editTroll", this).click(
360 function()
361 {
362 $("span", troll).css("display", "none")
363 $(troll).append(
364 '<form><p><input class="content" type="text" size="50" maxlength="500" value="' +
365 thisTrolls.trolls[id].content +
366 '"></input><span class="modifier">modifier</span><span class="annuler">annuler</span></p></form>'
367 )
368 $("form input.content").focus()
369
370 var virerLeFormulaire = function()
371 {
372 $('form', troll).remove()
373 $('span', troll).css("display", "inline")
374 }
375 $("span.modifier", troll).click(
376 function()
377 {
378 var content = $("form input.content", troll).val()
379 virerLeFormulaire()
380 thisTrolls.modifier(id, content)
381 }
382 )
383 $("span.annuler", troll).click( virerLeFormulaire )
384 $("form", troll).submit(function(){ return false})
385 }
386 )
387 }
388 )
389
390 if (data["trolls"].length > 0)
391 thisTrolls.dernierTroll = data["trolls"][data["trolls"].length - 1]["troll_id"]
392 }
393
394 Trolls.prototype.modifierTrollEvent = function(data)
395 {
396 var thisTrolls = this
397 $("#trolls #troll" + data["troll_id"] + " .content").html(thisTrolls.formateur.traitementComplet(data["content"], thisTrolls.trolls[data["troll_id"]].author))
398 $("#trolls #troll" + data["troll_id"] + " a[@rel*=lightbox]").lightBox()
399 thisTrolls.trolls[data["troll_id"]].content = data["content"]
400 }
401
402 Trolls.prototype.supprimerTrollEvent = function(data)
403 {
404 $("#trolls #troll"+data["troll_id"]).remove()
405 }
406
407 Trolls.prototype.modifier = function(id, content)
408 {
409 var thisTrolls = this
410
411 var dataToSend =
412 {
413 "action" : "mod_troll",
414 "cookie" : this.client.cookie,
415 "troll_id" : id,
416 "content" : content
417 }
418
419 ;; dumpObj(dataToSend)
420 jQuery.ajax(
421 {
422 type: "POST",
423 url: "request",
424 dataType: "json",
425 data: this.util.jsonVersAction(dataToSend),
426 success:
427 function(data)
428 {
429 ;; dumpObj(data)
430 if (data["reply"] == "error")
431 {
432 thisTrolls.util.messageDialogue(data["error_message"])
433 }
434 }
435 }
436 )
437 }
438
439 /**
440 * Supprime un troll en fonction de son id.
441 */
442 Trolls.prototype.supprimer = function(id)
443 {
444 var thisTrolls = this
445
446 var dataToSend =
447 {
448 "action" : "del_troll",
449 "cookie" : this.client.cookie,
450 "troll_id" : id
451 }
452
453 ;; dumpObj(dataToSend)
454 jQuery.ajax(
455 {
456 type: "POST",
457 url: "request",
458 dataType: "json",
459 data: this.util.jsonVersAction(dataToSend),
460 success:
461 function(data)
462 {
463 ;; dumpObj(data)
464 if (data["reply"] == "error")
465 {
466 thisTrolls.util.messageDialogue(data["error_message"])
467 }
468 }
469 }
470 )
471 }