-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-gather.pl
executable file
·255 lines (222 loc) · 5.81 KB
/
2-gather.pl
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env perl
use common::sense;
use utf8;
use Mojolicious::Lite;
use Validator::Custom;
use TwoGather::Vote;
use TwoGather::VoteSet;
my @poll_ids = qw (
20100316
20100309
20100302
20100223
);
my $current_poll_id = $poll_ids[0];
use KiokuDB;
sub kioku_dir {
my $date = $_[0] || $current_poll_id;
my $dir = KiokuDB->connect("dbi:SQLite:dbname=siatka-$date.db");
return ($dir, $dir->new_scope);
}
sub validate_params {
state $validator = Validator::Custom->new;
state $rules = {
create => [
title => [ [ { length => [ 0, 255 ] }, 'Title is too long' ] ],
brash => [
[ 'not_blank', 'Select brach' ],
[
{ 'in_array' => [qw/bash cpp c-sharp/] }, 'Brash is invalid'
]
],
content => [
[ 'not_blank', "Input content" ],
[ { length => [ 0, 4096 ] }, "Content is too long" ]
]
],
index => [
# ...
],
add => [
name => [
[ 'not_blank', "Nie podano nazwiska" ]
],
opt => [
[ { in_array => [ qw(yes no maybe dontknow) ] }, "Nie wybrano opcji" ],
],
min_people => [
[ 'uint', "Liczba osób nie jest prawidłowa" ],
]
],
show => [
pollid => [
[ { in_array => \@poll_ids } ]
]
]
};
my ($c) = @_;
my $name = $c->match->endpoint->name;
my $rule = $rules->{$name} // [ ];
my %params = map { $_ => $c->param($_) } $c->param;
$validator->validate(\%params, $rule);
}
get '/' => sub { shift->redirect_to('show'); } => 'index';
sub show_action {
my $self = shift;
my $vresu = validate_params($self);
my $pollid = ($vresu->is_ok) ? $self->param('pollid') : undef;
$pollid = undef if $pollid && $pollid eq $current_poll_id;
my ($dir, $scope) = kioku_dir($pollid);
my $vs = $dir->lookup(1); # a kind of magic
my %vs_will_be = map { $_ => 1 } @{$vs->hope_for};
$self->render('show',
votes => $vs,
votes_will_be => \%vs_will_be,
num_will_be => int(keys %vs_will_be),
kioku => $dir,
OptNames => { yes => 'Tak', no => 'Nie', maybe => 'Może...', dontknow => 'Nie wiem' },
pollid => $pollid,
render_form => !defined($pollid),
poll_ids => \@poll_ids,
current_poll_id => $current_poll_id
);
}
get '/show' => \&show_action => 'show';
post '/delete' => sub {
my $self = shift;
my ($dir, $scope) = kioku_dir;
my $vs = $dir->lookup(1); # a kind of magic
my $id = $self->param('id');
my $v = $dir->lookup($id) if $id;
if ($id && $v) {
$vs->del_vote($v);
$dir->delete($v);
}
$dir->update($vs);
$self->redirect_to('show');
} => 'delete';
post '/add' => sub {
my $self = shift;
$self->param('min_people', 0) unless length($self->param('min_people') // '') > 0;
my $vresu = validate_params($self);
if (! $vresu->is_ok) {
my $err = $vresu->messages;
$self->stash('errors', $err);
return show_action($self);
}
my ($dir, $scope) = kioku_dir;
my $vs = $dir->lookup(1); # a kind of magic
my $v = TwoGather::Vote->new(
map { $_ => $self->param($_) } qw(name opt min_people)
);
$vs->votes([ @{$vs->votes}, $v]);
$dir->store($vs);
$self->redirect_to('show');
} => 'add';
app->start;
__DATA__
@@ show.html.ep
% layout 'std';
<p>Wersja: <%== $pollid ? "$pollid (zamknięta)" : "$current_poll_id (aktualna)" %></p>
<p>Aktualna liczba osób, które powinny się stawić: <strong><%== $num_will_be %></strong></p>
<table class="players-table">
%== $self->render_partial('people-list');
% if ($render_form) {
<tr class="players-table-sep">
<form action="<%== url_for("add") %>" method="POST">
%== $self->render_partial('vote-form');
</form>
</tr>
% }
</table>
%== $self->render_partial('errors-pane');
<p>Wszystkie wersje:</p>
<ul>
% for (@$poll_ids) {
<li><a href="<%== url_for 'show' %>?pollid=<%== $_ %>"><%== $_ %></a><% if ($_ eq $current_poll_id) { %> (aktualna)<% } %></li>
% }
</ul>
@@ people-list.html.ep
<tr class="players-table-sep">
<th>Imię i nazwisko</th>
<th>Głos</th>
<th>Minimalna liczba osób</th>
<th>Pasuje liczba osób?</th>
</tr>
% for my $v (@{$votes->votes}) {
% my $vid = $kioku->object_to_id($v);
<tr>
<td><%== $v->name %></td>
<td class="vote-<%== $v->opt %>"><%== $OptNames->{$v->opt} %></td>
<td><%== $v->opt eq 'maybe' ? $v->min_people : '—' %></td>
% if ($votes_will_be->{$v}) {
<td class="vote-yes">Tak</td>
% } elsif ($v->opt eq 'dontknow') {
<td>?</td>
% } else {
<td class="vote-no">Nie</td>
% }
% if ($render_form) {
<td><form action="<%== url_for("delete") %>" method="POST"><input type="hidden" name="id" value="<%==$vid%>"><button type="submit">Usuń</button></form></td>
% }
</tr>
% }
@@ errors-pane.html.ep
% if (defined $self->stash('errors')) {
<div class="errors-list">
<p>Wystąpiły błędy:</p>
<ul>
% for (@{$self->stash('errors')}) {
<li><%== $_ %></li>
% }
</ul>
</div>
% }
@@ vote-form.html.ep
<td><input type="text" name="name"></td>
<td colspan="2" style="text-align: left">
<input type="radio" name="opt" value="dontknow">Nie wiem</input>
<input type="radio" name="opt" value="yes">Tak</input>
<input type="radio" name="opt" value="no">Nie</input><br>
<input type="radio" name="opt" value="maybe">Tak, jeśli będzie co najmniej <input type="text" name="min_people" size="2"></input> osób</input>
</td>
<td><button type="submit">Dodaj</button></td>
@@ layouts/std.html.ep
<!doctype html><html>
<head>
<title>Siatka</title>
<meta name="http-equiv" value="Content-Type: text/html; charset=UTF-8">
<style type="text/css">
.errors-list {
color: red;
}
.vote-yes {
background-color: green;
}
.vote-maybe {
background-color: yellow;
}
.vote-no {
background-color: red;
}
.players-table {
text-align: center;
border: 1pt solid black;
}
.players-table-sep {
background: #e0e0e0;
border: 1pt solid black;
}
.players-table td {
padding: 0.5em;
}
body, table {
font-family: sans-serif;
}
</style>
</head>
<body>
<h2>Lista obecności</h2>
<%== content %>
</body>
</html>