-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
154 lines (143 loc) · 6.79 KB
/
index.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
<?php ?><html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='icon' href='/favicon.png'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type='text/javascript' src='/jquery.min.js'></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>E-Vote</title>
</head>
<?php
$vid = $_GET['vid'];
$correctVID = isset($vid) && is_numeric($vid) && !is_null($vid);
echo '<body class=\'container\'>';
if(!$correctVID) {
echo '<header class="text-center">Доступные голосования:</header>';
} else {
echo '<header class="text-center"><b id="voteName"></b><br><span id="publicKey">[PublicKey]</span></header>';
}
echo '<main><div class=\'list-group\' id=\'';
if(!$correctVID) { echo 'votes'; } else { echo 'rivals'; }
echo '\'></div></main>';
if(!$correctVID) { echo '<footer>Адрес: <b><span id=\'address\'></span></b></footer>'; }
else { echo '<div id="wait" class="modal fade" role="dialog"><div class="modal-dialog modal-sm"><div class="modal-content text-center" style="padding: 10px;">Ваш голос будет учтен через <b><span id="timer"></span></b> секунд<br><br><button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Отмена</button></div></div></div>'; } // <--------------------- MODAL
echo '</body>';
if(!$correctVID) {
?>
<script type='text/javascript'>
var vBASE = 'https://genco.one:5432';
$(document).ready(function() {
if(navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
$.ajax({
url: vBASE + '/address?full=1&lon=' + lon + '&lat=' + lat,
dataType: 'json',
success: function(data, status, xhr) {
if(data.length > 0) {
fullAddress = '';
for(var i = 0; i < data.length; ++i)
fullAddress = data[i].name + ', ' + fullAddress;
fullAddress = fullAddress.substr(0, fullAddress.length - 2);
var secret = prompt('Контрольное слово:', '');
$('#address').text(fullAddress);
getVotes(lon, lat, secret);
}
},
type: 'get',
crossDomain: true
});
}, function(){alert('Включите GPS и обновите страницу');}, { timeout: 3000, enableHighAccuracy: true });
} else {
alert('Устройство не поддерживает функции геолокации');
}
});
function getVotes(lon, lat, secret) {
$.ajax({
url: vBASE + '/vote?lon=' + lon + '&lat=' + lat + '&secret=' + encodeURI(secret),
dataType: 'json',
success: function(data, status, xhr) {
$('#votes').text('');
if(data.length === 0) {
$('#votes').text('По Вашему адресу нет доступных голосований');
return;
}
for(var i = 0; i < data.length; ++i) {
start = new Date(data[i].start * 1000);
stop = new Date(data[i].stop * 1000);
start = start.getUTCHours().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + ':' +
start.getUTCMinutes().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + ' ' +
start.getUTCDate().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + '.' +
(start.getUTCMonth() + 1).toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + '.' + start.getUTCFullYear();
stop = stop.getUTCHours().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + ':' +
stop.getUTCMinutes().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + ' ' +
stop.getUTCDate().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + '.' +
(stop.getUTCMonth() + 1).toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) + '.' + stop.getUTCFullYear();
$('#votes').append('<a href=\'?vid=' + data[i].id + '\' class=\'list-group-item list-group-item-action\'>' + data[i].name + ' <br><span class="label label-primary">' + start + ' - ' + stop + '</span></a>');
}
},
type: 'get',
crossDomain: true
});
}
</script>
<?php
} else { // Load Rivals ?>
<script type='text/javascript'>
var vBASE = 'https://genco.one:5432';
var maxSelected = 1;
$(document).ready(function() {
$.ajax({
url: vBASE + '/rival?vid=<?=$vid ?>',
dataType: 'json',
success: function(data, status, xhr) {
$('#rivals').text('');
if(data.length === 0) {
$('#rivals').text('нет альтернатив');// <--------------- Если попадает сюда, то произошла ошибка
return;
}
$.ajax({ url: vBASE + '/vote?id=' + data[0].vid, dataType: 'json', type: 'get', crossDomain: true, success: function(d, s, x) { $('#voteName').text(d[0].name); maxSelected = d[0].max; } });
for(var i = 0; i < data.length; ++i) {
// {id: 1, name: "ФИО", description: "Длинное описание", position: 0, vid: 2}
$('#rivals').append('<div style="cursor:pointer;" class=\'list-group-item list-group-item-action\' rid=' + data[i].id + ' onclick="setactive(this)"><b>' + data[i].name +
'</b><br><div style="font-size:9pt;">' + data[i].description + '</div></div>');
}
},
type: 'get',
crossDomain: true
});
});
function setactive(t) {
var rivalCount = $('.list-group-item-action.active').size();
if(maxSelected === rivalCount) { // && !$(t).hasClass('active')) {
setTimeout(function() {
$('#wait').modal('show');
}, 300);
$('#timer').text(10);//10 seconds
var _Seconds = $('#timer').text();
var interval = setInterval(function() {
if (_Seconds > 0) {
_Seconds--;
$('#timer').text(_Seconds);
if(!$('#wait').is(':visible')) {
$('.list-group-item-action').removeClass('active');
clearInterval(interval);
}
} else {
clearInterval(interval);
$('#wait').modal('hide');
//DO /poll
alert('TA-DA! Your vote is polled');//DEBUG
$('.list-group-item-action').removeClass('active');//DEBUG ???
}
}, 1000);
} else {
$(t).toggleClass('active');
setactive(null);
}
}
</script>
<?php }
echo '</html>';
?>