'formater' -> 'formatter'
[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 /*jslint laxbreak:true */
22
23
24 euphorik.PageAdmin = function(client, formatter, util, communication) {
25 this.name = "admin";
26
27 this.client = client;
28 this.formatter = formatter;
29 this.util = util;
30 this.communication = communication;
31
32 this.comet = this.communication.createCometConnection("admin");
33
34 // a timer which will periodically refresh the banned IP list
35 this.timeoutIDmajIPs = null;
36 };
37
38 /**
39 * Interface des pages.
40 */
41 euphorik.PageAdmin.prototype.contenu = function() {
42 return '<h1>IPs bannies</h1><div id="ips"></div>';
43 };
44
45 /**
46 * Interface des pages.
47 */
48 euphorik.PageAdmin.prototype.charger = function() {
49 var thisPage = this;
50
51 this.waitEvent();
52
53 this.majIPs();
54 };
55
56 /**
57 * Interface des pages.
58 */
59 euphorik.PageAdmin.prototype.decharger = function() {
60 this.comet.stopAttenteCourante();
61
62 // supprime le rafraichissement période des ips
63 if (this.timeoutIDmajIPs) {
64 clearTimeout(this.timeoutIDmajIPs);
65 }
66 };
67
68 /**
69 * Met à jour la liste des IP bannies.
70 */
71 euphorik.PageAdmin.prototype.majIPs = function() {
72 if (this.timeoutIDmajIPs) {
73 clearTimeout(this.timeoutIDmajIPs);
74 }
75
76 var thisPageAdmin = this;
77
78 this.communication.requete(
79 "list_banned_ips",
80 {"cookie" : this.client.cookie},
81 function(data) {
82 var XHTML = "";
83 data.list.each(function(i, ip) {
84 XHTML += '<div class="ban"><span class="ip">' + ip.ip + '</span>|' +
85 '<span class="temps">' +
86 ip.remaining_time +
87 '</span>|';
88 ip.users.each(function(j, user) {
89 XHTML += (j > 0 ? ", " : "") +
90 '<span class="nick">' + thisPageAdmin.formatter.completeProcessing(user.nick) + '</span>' +
91 (user.login === "" ? "" : '<span class="login">(' + thisPageAdmin.formatter.completeProcessing(user.login) + ')</span>');
92 });
93 XHTML += '<span class="deban">débannir</span></div>';
94 });
95
96 if (data.list.length === 0) {
97 XHTML += '<p>Aucune IP bannie</p>';
98 }
99
100 $("#ips").html(XHTML);
101
102 $(".ban").each(function() {
103 var ip = $(".ip", this).html();
104 $(".deban", this).click(
105 function() {
106 thisPageAdmin.util.messageDialog("Êtes-vous sur de vouloir débannir l'IP ''" + ip + "'' ?", euphorik.Util.messageType.question,
107 {"Oui" : function() {
108 thisPageAdmin.deban(ip);
109 },
110 "Non" : function(){}
111 }
112 );
113 }
114 );
115 });
116
117 // rafraichissement toutes les minutes (je sais c'est mal)
118 // le problème est le rafraichissement des temps restant de bannissement qui doit être fait du coté client
119 thisPageAdmin.timeoutIDmajIPs = setTimeout(function(){ thisPageAdmin.majIPs(); }, 60 * 1000);
120 }
121 );
122 };
123
124 /**
125 * Débannie une ip donnée.
126 */
127 euphorik.PageAdmin.prototype.deban = function(ip) {
128 var thisPageAdmin = this;
129
130 this.communication.requete(
131 "unban",
132 {"cookie" : this.client.cookie, "ip" : ip}
133 );
134 };
135
136 /**
137 * Attente d'événement de la part du serveur.
138 */
139 euphorik.PageAdmin.prototype.waitEvent = function() {
140 var thisPageAdmin = this;
141
142 this.comet.waitEvent(
143 {
144 "banned_ips_refresh" : function(data){ thisPageAdmin.majIPs(); },
145 "error" :
146 function(data) {
147 thisPage.util.messageDialog(data.error_message);
148 }
149 }
150 );
151 };