MOD maj TODO
[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 function(data)
271 {
272 switch (data["reply"])
273 {
274 case "troll_added" :
275 thisPageAdmin.trolls.ajouterTrollEvent(data)
276 break
277 case "troll_modified" :
278 thisPageAdmin.trolls.modifierTrollEvent(data)
279 break
280 case "troll_deleted" :
281 thisPageAdmin.trolls.supprimerTrollEvent(data)
282 break
283 case "banned_ips_refresh" :
284 thisPageAdmin.majIPs()
285 break
286 case "error" :
287 thisTrolls.util.messageDialogue(data["error_message"])
288 break
289 }
290 }
291 )
292 }
293
294 ///////////////////////////////////////////////////////////////////////////////////////////////////
295
296 /**
297 * Représente un troll, pas grand chose finalement.
298 */
299 function Troll(content, author)
300 {
301 this.content = content
302 this.author = author
303 }
304
305
306 ///////////////////////////////////////////////////////////////////////////////////////////////////
307
308
309 function Trolls(client, util, formateur)
310 {
311 this.client = client
312 this.util = util
313 this.formateur = formateur
314 this.dernierTroll = 0
315
316 this.trolls = {}
317 }
318
319 Trolls.prototype.ajouterTrollEvent = function(data)
320 {
321 var thisTrolls = this
322
323 var XHTML = ""
324 for (var i = 0; i < data["trolls"].length; i++)
325 {
326 var troll = new Troll(data["trolls"][i]["content"], data["trolls"][i]["author"])
327 var trollId = data["trolls"][i]["troll_id"]
328 thisTrolls.trolls[trollId] = troll
329
330 XHTML +=
331 '<div id="troll' + trollId + '" class="troll">' +
332 '<span class="content">' + thisTrolls.formateur.traitementComplet(troll.content, troll.author) + '</span>' +
333 '<span class="author"> - ' + thisTrolls.formateur.traitementComplet(troll.author) + '</span>' +
334 (data["trolls"][i]["author_id"] == thisTrolls.client.id ? '<span class="editTroll">éditer</span><span class="delTroll">Supprimer</span>' : '') +
335 '</div>'
336 }
337 $("#trolls").append(XHTML)
338 $("#trolls .troll").filter(function(){return parseInt($(this).attr("id").substr(5)) > thisTrolls.dernierTroll}).each(
339 function()
340 {
341 var troll = this
342 var id = parseInt($(this).attr("id").substr(5))
343
344 $("a[@rel*=lightbox]", this).lightBox()
345
346 $(this).keypress(
347 function(e)
348 {
349 if (e.which == 13) // return
350 $(".modifier", this).click()
351 }
352 )
353 $(".delTroll", this).click(
354 function()
355 {
356 thisTrolls.util.messageDialogue(
357 "Êtes-vous sur de vouloir supprimer le troll \"" + thisTrolls.trolls[id].content + "\" ?",
358 messageType.question,
359 {
360 "oui" : function()
361 {
362 thisTrolls.supprimer(id)
363 },
364 "non" : function(){}
365 }
366 )
367 }
368 )
369 $(".editTroll", this).click(
370 function()
371 {
372 $("span", troll).css("display", "none")
373 $(troll).append(
374 '<form><p><input class="content" type="text" size="50" maxlength="500" value="' +
375 thisTrolls.trolls[id].content +
376 '"></input><span class="modifier">modifier</span><span class="annuler">annuler</span></p></form>'
377 )
378 $("form input.content").focus()
379
380 var virerLeFormulaire = function()
381 {
382 $('form', troll).remove()
383 $('span', troll).css("display", "inline")
384 }
385 $("span.modifier", troll).click(
386 function()
387 {
388 var content = $("form input.content", troll).val()
389 virerLeFormulaire()
390 thisTrolls.modifier(id, content)
391 }
392 )
393 $("span.annuler", troll).click( virerLeFormulaire )
394 $("form", troll).submit(function(){ return false})
395 }
396 )
397 }
398 )
399
400 if (data["trolls"].length > 0)
401 thisTrolls.dernierTroll = data["trolls"][data["trolls"].length - 1]["troll_id"]
402 }
403
404 Trolls.prototype.modifierTrollEvent = function(data)
405 {
406 var thisTrolls = this
407 $("#trolls #troll" + data["troll_id"] + " .content").html(thisTrolls.formateur.traitementComplet(data["content"], thisTrolls.trolls[data["troll_id"]].author))
408 $("#trolls #troll" + data["troll_id"] + " a[@rel*=lightbox]").lightBox()
409 thisTrolls.trolls[data["troll_id"]].content = data["content"]
410 }
411
412 Trolls.prototype.supprimerTrollEvent = function(data)
413 {
414 $("#trolls #troll"+data["troll_id"]).remove()
415 }
416
417 Trolls.prototype.modifier = function(id, content)
418 {
419 var thisTrolls = this
420
421 var dataToSend =
422 {
423 "action" : "mod_troll",
424 "cookie" : this.client.cookie,
425 "troll_id" : id,
426 "content" : content
427 }
428
429 ;; dumpObj(dataToSend)
430 jQuery.ajax(
431 {
432 type: "POST",
433 url: "request",
434 dataType: "json",
435 data: this.util.jsonVersAction(dataToSend),
436 success:
437 function(data)
438 {
439 ;; dumpObj(data)
440 if (data["reply"] == "error")
441 {
442 thisTrolls.util.messageDialogue(data["error_message"])
443 }
444 }
445 }
446 )
447 }
448
449 /**
450 * Supprime un troll en fonction de son id.
451 */
452 Trolls.prototype.supprimer = function(id)
453 {
454 var thisTrolls = this
455
456 var dataToSend =
457 {
458 "action" : "del_troll",
459 "cookie" : this.client.cookie,
460 "troll_id" : id
461 }
462
463 ;; dumpObj(dataToSend)
464 jQuery.ajax(
465 {
466 type: "POST",
467 url: "request",
468 dataType: "json",
469 data: this.util.jsonVersAction(dataToSend),
470 success:
471 function(data)
472 {
473 ;; dumpObj(data)
474 if (data["reply"] == "error")
475 {
476 thisTrolls.util.messageDialogue(data["error_message"])
477 }
478 }
479 }
480 )
481 }