-
Notifications
You must be signed in to change notification settings - Fork 28
/
logo_vote.php
218 lines (189 loc) · 5.39 KB
/
logo_vote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
require_once("bootstrap.inc.php");
require_once("include_pouet/box-bbs-post.php");
class PouetBoxLogoVote extends PouetBox
{
public $logo;
function __construct($logo)
{
parent::__construct();
$this->uniqueID = "";
$this->classes[] = "logovote";
$this->logo = $logo;
$this->title = $logo->file;
}
function RenderContent() {
echo "<img src=".POUET_CONTENT_URL."logos/"._html($this->logo->file)." alt='logo'/>";
}
function RenderFooter() {
echo " <div class='foot'>";
echo " <form action='".$_SERVER["REQUEST_URI"]."' method='post'>";
$csrf = new CSRFProtect();
$csrf->PrintToken();
echo " <input name='logoID' type='hidden' value='".(int)$this->logo->id."'/>";
echo " <input name='submit' type='submit' value='rulez'/>";
echo " <input name='submit' type='submit' value='sucks'/>";
echo " </form>\n";
echo " </div>\n";
echo "</div>\n";
}
};
class PouetBoxLogoLama extends PouetBox
{
function __construct()
{
parent::__construct();
$this->uniqueID = "pouetbox_logolama";
$this->title = "no logo left, you are now a l4m4h";
}
function RenderContent()
{
$lama_pictures = array(
'logos-lamer.jpg',
'logos-lama.jpg',
'logos-dalai-lama.jpg',
'logos-lamerbus.jpg',
'logos-lamerst-by-charlie.jpg',
'logos-lamercream.jpg',
'logos-lamerjewels.jpg',
'logos-lamercow-by-everybody.jpg',
);
echo "<img src='".POUET_CONTENT_URL."gfx/".$lama_pictures[array_rand($lama_pictures)]."' alt='Lamer picture'/>";
}
function RenderFooter() {
echo " <div class='foot'><a href='".POUET_ROOT_URL."'>get back</a></div>\n";
echo "</div>\n";
}
};
$sel = null;
if ($currentUser)
{
$sel = new SQLSelect();
$sel->AddField("logos.id as id");
$sel->AddField("logos.file as file");
$sel->AddTable("logos");
$sel->AddJoin("LEFT","logos_votes",sprintf_esc("logos_votes.logo = logos.id AND logos_votes.user = %d",$currentUser->id));
$sel->AddWhere("logos_votes.id IS NULL");
$sel->AddOrder("RAND()");
if (@$_POST["logoID"] && @$_POST["submit"])
{
$vote = 0;
if ($_POST["submit"] == "rulez") $vote = 1;
if ($_POST["submit"] == "sucks") $vote = -1;
$csrf = new CSRFProtect();
if ($vote && $csrf->ValidateToken())
{
SQLLib::Query(sprintf_esc("delete from logos_votes where logo = %d and user = %d",$_POST["logoID"],$currentUser->id));
$a = array();
$a["logo"] = (int)$_POST["logoID"];
$a["user"] = $currentUser->id;
$a["vote"] = $vote;
SQLLib::InsertRow("logos_votes",$a);
}
SQLLib::Query(sprintf_esc("update logos set vote_count = (select sum(vote) from logos_votes where logo = %d) where id = %d",(int)$_POST["logoID"],(int)$_POST["logoID"]));
// ajax
if ($_POST["partial"]==1)
{
$s = clone $sel;
$visibleLogos = $_POST["visibleLogos"];
foreach($visibleLogos as $k=>$v) $visibleLogos[$k] = (int)$v;
$s->AddWhere(sprintf_esc("logos.id not in (%s)",implode(",",$visibleLogos)));
$s->SetLimit(1);
$logo = SQLLib::SelectRow($s->GetQuery());
if ($logo)
{
$box = new PouetBoxLogoVote($logo);
$box->Render();
}
else
{
$box = new PouetBoxLogoLama();
$box->Render();
}
exit();
}
}
}
$TITLE = "vote for logos !";
require_once("include_pouet/header.php");
require("include_pouet/menu.inc.php");
echo "<div id='content'>\n";
if ($sel)
{
$s = clone $sel;
$s->SetLimit(5);
$logos = SQLLib::SelectRows($s->GetQuery());
if ($logos)
{
foreach($logos as $logo)
{
$box = new PouetBoxLogoVote($logo);
$box->Render();
}
}
else
{
$box = new PouetBoxLogoLama();
$box->Render();
}
?>
<script>
<!--
function InstrumentForm(form)
{
if (!form) return;
form.select("input[type=submit]").invoke("observe","click",function(e) {
form.select("input[type=submit]").invoke("setAttribute","clicked","");
e.element().setAttribute("clicked","true");
});
form.observe("submit",function(e){
e.stop();
var visibleLogos = $$("input[name='logoID'][type='hidden']").pluck("value");
var values = $H(Form.serialize(form,true));
values.set("submit",form.select("input[type=submit][clicked=true]").first().value);
values.set("partial",1);
values.set("visibleLogos[]",visibleLogos);
new Ajax.Request(form.action,{
method: form.method,
parameters: values,
onSuccess: function(transport) {
form.up(".logovote").remove();
if (transport.responseText.length)
{
var div = new Element("div").update(transport.responseText);
InstrumentForm( div.down("form") );
console.log( $("content").select("div").length );
console.log( div.down("#pouetbox_logolama") );
if (div.down("#pouetbox_logolama"))
{
if ($("content").select("div.logovote").length == 0)
$("content").insert(div);
}
else
{
$("content").insert(div);
}
}
},
});
});
}
document.observe("dom:loaded",function(){
$$("form").each(function(item){
InstrumentForm(item);
});
});
//-->
</script>
<?php
}
else
{
require_once("include_pouet/box-login.php");
$box = new PouetBoxLogin();
$box->Render();
}
echo "</div>\n";
require("include_pouet/menu.inc.php");
require_once("include_pouet/footer.php");
?>