query("SELECT id, title, category, body, vote_num, start, end, grouplevel FROM " . PREFIX . "_vote WHERE approve");
while ($row = $db->get_row()) {
$vote_info[$row['id']] = $row;
}
set_vars("vote", $vote_info);
$db->free();
}
// ===== ВЫБОР ОПРОСА =====
if (!$vote_id OR empty($vote_info[$vote_id]['id'])) {
$find_vote = array();
foreach ($vote_info as $votes) {
if ($votes['start'] AND $_TIME < $votes['start']) continue;
if ($votes['end'] AND $_TIME > $votes['end']) continue;
$votes['grouplevel'] = explode(',', $votes['grouplevel']);
if ($votes['grouplevel'][0] != "all" AND !in_array($member_id['user_group'], $votes['grouplevel'])) continue;
$find_vote[] = $votes;
}
if (!count($find_vote)) {
$tpl->result['vote'] = "";
return;
}
$rand_keys = array_rand($find_vote);
$rid = $find_vote[$rand_keys]['id'];
} else {
$rid = $vote_id;
}
// ===== РЕЗУЛЬТАТЫ =====
if ($vote_action == "results" OR $flag) {
$db->query("SELECT answer, count(*) as count FROM " . PREFIX . "_vote_result WHERE vote_id='$rid' GROUP BY answer");
$flag = 1;
$pn = 0;
$answer = array();
while ($row = $db->get_row()) {
$answer[$row['answer']]['count'] = $row['count'];
}
$db->free();
}
// ===== AJAX (ВАЖНО!) =====
$ajax_script = <<
function doVote(event){
var vote_check = document.querySelector('#dle-vote input[name=vote_check]:checked');
if (!vote_check) return;
ShowLoading('');
fetch(dle_root + "engine/ajax/vote.php?vote_id={$rid}&vote_action=" + event + "&vote_check=" + vote_check.value + "&vote_skin=" + dle_skin + "&user_hash=" + dle_login_hash)
.then(res => res.text())
.then(data => {
HideLoading('');
document.getElementById('vote-layer').innerHTML = data;
});
}
HTML;
// ===== ВЫВОД =====
switch ($flag) {
// ===== ГОЛОСОВАНИЕ =====
case 0:
$title = dle_get_lang_text(stripslashes($vote_info[$rid]['title']));
$body = stripslashes($vote_info[$rid]['body']);
$body = str_replace("
", "
", $body);
$body = explode("
", $body);
for ($i = 0; $i < sizeof($body); $i++) {
$text = dle_get_lang_text(trim($body[$i]));
if (!$text) continue;
$sel = ($i == 0) ? 'checked="checked"' : "";
$entry .= "
{$text}
";
}
$entry = "
$entry
";
$tpl->load_template('vote.tpl');
$tpl->copy_template = $ajax_script . "
" . $tpl->copy_template . "
";
$tpl->set('
', $entry);
$tpl->set('95', $rid);
$tpl->set('Когда закончится война?', $title);
$tpl->set('', '');
$tpl->set('', '');
$tpl->set_block("'\\[voteresult\\].*?\\[/voteresult\\]'si", "");
$tpl->compile('vote');
$tpl->clear();
break;
// ===== РЕЗУЛЬТАТЫ =====
case 1:
$result = $db->super_query("SELECT * FROM " . PREFIX . "_vote WHERE id='$rid'");
$title = dle_get_lang_text(stripslashes($result['title']));
$body = stripslashes($result['body']);
$body = str_replace("
", "
", $body);
$body = explode("
", $body);
$max = $result['vote_num'];
for ($i = 0; $i < sizeof($body); $i++) {
++$pn;
if ($pn > 5) $pn = 1;
$text = dle_get_lang_text(trim($body[$i]));
if (!$text) continue;
$num = $answer[$i]['count'] ?? 0;
$proc = ($max != 0) ? round((100 * $num) / $max, 2) : 0;
$entry .= "
{$text} - {$num} ({$proc}%)
{$proc}%
";
}
$entry = "
$entry
";
$tpl->load_template('vote.tpl');
$tpl->set('
', $entry);
$tpl->set('95', $rid);
$tpl->set('Когда закончится война?', $title);
$tpl->set('{votes}', $max);
$tpl->set('', '');
$tpl->set_block("'\\[votelist\\].*?\\[/votelist\\]'si", "");
$tpl->compile('vote');
$tpl->clear();
break;
}
if (!$rid) $tpl->result['vote'] = "";
?>