<?php

/*
 * @name        JMY CMS
 * @link        https://jmy.su/
 * @copyright   Copyright (C) 2012-2025 JMY LTD
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      Komarov Ivan
 * @edited      Orlov Alexsey
 */

if (!defined('ACCESS')) {
    header('Location: /');
    exit;
}
loadConfigBLOCK('poll');
global $core, $db, $poll_conf, $lang;
require ROOT . 'usr/plugins/poll.plugin.php';
$rows = $core->row_data('polls');
if ($poll_conf['on']) {
    if ($rows['startdate'] == $rows['enddate'] && !$rows['startdate'] && !$rows['enddate'] && $rows['active'] != 0) {
        $core->upd_data('polls', 'active = 1', 'id = "' . $rows['id'] . '"');
    } elseif ($rows['enddate'] <= time() && $rows['active'] != 0) {
        $core->upd_data('polls', 'active = 2', 'id = "' . $rows['id'] . '"');
    } elseif ($rows['enddate'] >= time() && $rows['active'] != 0) {
        $core->upd_data('polls', 'active = 1', 'id = "' . $rows['id'] . '"');
    } else {
        $core->upd_data('polls', 'active = 0', 'id = "' . $rows['id'] . '"');
    }
    if ($poll_conf['poll_rand'] == 1) {
        $query2 = $core->select_data('polls', 'active = 1');
        if ($db->numRows($query2) > 0) {
            while ($rows2 = $db->getRow($query2)) {
                $id_arr[] = $rows2['id'];
            }
            $max = rand(0, ($db->numRows($query2) - 1));
            $id = $id_arr[$max];
            show_poll($type = false, $id);
        } else {
            $core->tpl->info($lang['poll_empty']);
        }
    } else {
        show_poll($type = false, $poll_conf['poll_id']);
    }
} else {
    if ($poll_conf['info_off']) $core->tpl->info($lang['poll_off']);
}