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