<?php

/* DCMS Special
 * Дата последнего редактирования 25.09.2016
 * Модифицировал densnet
 * Автор yadalay
 */

if (isset($_POST['save'])) {
    $name = mysql_real_escape_string(trim($_POST['name']));

    if ($_POST['time_end'] == 1 || ($_POST['time_end'] != 1 && $_POST['time_end'] != 2 && $_POST['time_end'] != 3 && $_POST['time_end'] != 4 && $_POST['time_end'] != 5 && $_POST['time_end'] != 6)) {
        $time_end = 0;
    } elseif ($_POST['time_end'] == 1) {
        $time_end = 0;
    } elseif ($_POST['time_end'] == 2) {
        $time_end = ($vote->time_end < time()) ? $vote->time_end + time() + 60 * 60 * 24 : time() + 60 * 60 * 24;
    } elseif ($_POST['time_end'] == 3) {
        $time_end = ($vote->time_end < time()) ? $vote->time_end + time() + 60 * 60 * 72 : time() + 60 * 60 * 72;
    } elseif ($_POST['time_end'] == 4) {
        $time_end = ($vote->time_end < time()) ? $vote->time_end + time() + 60 * 60 * 24 * 7 : time() + 60 * 60 * 24 * 7;
    } elseif ($_POST['time_end'] == 5) {
        $time_end = ($vote->time_end < time()) ? $vote->time_end + time() + 60 * 60 * 24 * 30 : time() + 60 * 60 * 24 * 30;
    } elseif ($_POST['time_end'] == 6) {
        $time_end = ($vote->time_end < time()) ? $vote->time_end + time() + 60 * 60 * 24 * 90 : time() + 60 * 60 * 24 * 90;
    }
    if (mb_strlen($name) < 5) {
        echo "<div class='alert alert-danger'>" . lang('Слишком короткое содержание опроса') . ".</div>";
    } else {
        unset($_SESSION['name']);
        unset($_SESSION['time_end']);

        $var = $_POST['var'];
        $count_var = count($var);

        for ($i = 0; $i < $count_var; $i++) {
            unset($_SESSION['var'][$i]);

            $var[$i] = mysql_real_escape_string(trim($var[$i]));
            $id = mysql_fetch_object(mysql_query('SELECT `id` FROM `forum_votes_var` WHERE `id_vote` = ' . $vote->id . ' AND `variant` = "' . $var[$i] . '"'));

            if (!$id) {
                mysql_query('INSERT INTO `forum_votes_var` SET `variant` = "' . mysql_real_escape_string($var[$i]) . '", `id_vote` = ' . $vote->id . ', `id_theme` = ' . $theme->id);
            } else {
                mysql_query('UPDATE `forum_votes_var` SET `variant` = "' . mysql_real_escape_string($var[$i]) . '" WHERE `id` = ' . $id->id);
            }

            mysql_query('DELETE FROM `forum_votes_var` WHERE `id_theme` = ' . $theme->id . ' AND `variant` = ""');
        }

        $_SESSION['message'] = lang('Изменения сохранены');

        mysql_query('UPDATE `forum_votes` SET  `name` = "' . $name . '", `time_end` = ' . $time_end . ' WHERE `id_theme` = ' . $theme->id);
        header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '.html');
        exit;
    }
} elseif (isset($_POST['add_var']) && $vars < 9) {
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['time_end'] = $_POST['time_end'];
    $var = $_POST['var'];
    $count_var = count($var);

    for ($i = 0; $i < $count_var; $i++) {
        $_SESSION['var'][$i] = mysql_real_escape_string(trim($var[$i]));
    }

    mysql_query('INSERT INTO `forum_votes_var` SET `id_theme` = ' . $theme->id . ', `id_vote` = ' . $vote->id . ', `variant` = ""');
    header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '/edit_vote.html');
    exit;
} elseif (isset($_POST['delete_var']) && $vars > 2) {
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['time_end'] = $_POST['time_end'];
    $var = $_POST['var'];
    $count_var = count($var) - 1;

    unset($_SESSION['var'][$count_var]);

    $variant = mysql_result(mysql_query('SELECT MAX(`id`) FROM `forum_votes_var` WHERE `id_theme` = ' . $theme->id), 0);

    mysql_query('DELETE FROM `forum_votes_var` WHERE `id_theme` = ' . $theme->id . ' AND `id` = ' . $variant);
    mysql_query('DELETE FROM `forum_vote_voices` WHERE `id_variant` = ' . $variant);

    header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '/edit_vote.html');
    exit;
} elseif (isset($_POST['cancel']) && ($theme->type == 0 || ($theme->type == 1 && access('forum_post_close')))) {
    $_SESSION['message'] = lang('Изменения сохранены');
    $var = $_POST['var'];
    $count_var = count($var);

    for ($i = 0; $i < $count_var; $i++) {
        unset($_SESSION['var'][$i]);
    }
    unset($_SESSION['name']);
    unset($_SESSION['time_end']);

    mysql_query('DELETE FROM `forum_votes_var` WHERE `id_theme` = ' . $theme->id . ' AND `variant` = ""');

    header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '.html');
    exit;
} elseif (isset($_POST['delete'])) {
    $_SESSION['message'] = lang('Голосование успешно удалено');
    $var = $_POST['var'];
    $count_var = count($var);

    for ($i = 0; $i < $count_var; $i++) {
        unset($_SESSION['var'][$i]);
    }
    unset($_SESSION['name']);
    unset($_SESSION['time_end']);

    mysql_query('DELETE FROM `forum_vote_voices` WHERE `id_vote` = ' . $vote->id);
    mysql_query('DELETE FROM `forum_votes_var` WHERE `id_theme` = ' . $theme->id);
    mysql_query('DELETE FROM `forum_votes` WHERE `id_theme` = ' . $theme->id);

    header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '.html');
    exit;
}

#Навигация
echo "<div class='list-group-item-null list-group-item-grey'><small>";
echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FORUM . "'>" . lang('Форум') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FORUM . "$forum->id/'>" . toOutput(cutStr($forum->name, 30)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/'>" . toOutput(cutStr($razdel->name, 30)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id.html'>" . toOutput(cutStr($theme->name, 30)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Редактирование опроса');
echo "</small></div><br />";

echo "<form action='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id/edit_vote.html' class='list-group-item-null' method='post'>";

echo lang('Содержание опроса') . "<br />";
echo "<textarea name='name' class='form-control'>" . ((isset($_SESSION['name'])) ? toOutput($_SESSION['name']) : toOutput($vote->name)) . "</textarea><br />";

echo lang('Варианты ответов') . ":<br />";

$i = 0;
$all_vars = mysql_query('SELECT `variant` FROM `forum_votes_var` WHERE `id_vote` = ' . $vote->id . ' ORDER BY `id` ASC');
while ($var = mysql_fetch_object($all_vars)) {
    echo "<input name='var[]' type='text' value='" . ((isset($_SESSION['var'][$i])) ? toOutput($_SESSION['var'][$i]) : toOutput($var->variant)) . "' class='form-control'>";
    $i++;
}
echo "<button class='btn btn-primary btn-sm' type='submit' name='add_var' " . (($vars > 7) ? 'disabled="disabled"' : NULL) . ">" . lang('Ещё вариант') . "</button> ";
echo "<button class='btn btn-primary btn-sm' type='submit' name='delete_var' " . (($vars < 3) ? 'disabled="disabled"' : NULL) . ">" . lang('Убрать вариант') . "</button><br />";
echo "" . (($vote->time_end < time()) ? '' . lang('Продлить на') . ':' : '' . lang('Дата окончания через') . ':') . "<br />";
echo "<select name='time_end' class='form-control'>";
if ($vote->time_end < time()) {
    echo "<option value='0'>" . lang('Без изменений') . "</option>";
}
if (isset($_SESSION['time_end']) && $_SESSION['time_end'] == 1) {
    $sel = "selected='selected'";
} elseif (isset($_SESSION['time_end']) && $_SESSION['time_end'] == 2) {
    $sel = "selected='selected'";
} elseif (isset($_SESSION['time_end']) && $_SESSION['time_end'] == 3) {
    $sel = "selected='selected'";
} elseif (isset($_SESSION['time_end']) && $_SESSION['time_end'] == 4) {
    $sel = "selected='selected'";
} elseif (isset($_SESSION['time_end']) && $_SESSION['time_end'] == 5) {
    $sel = "selected='selected'";
} elseif (isset($_SESSION['time_end']) && $_SESSION['time_end'] == 6) {
    $sel = "selected='selected'";
} else {
    $sel = null;
}

echo "
    <option value='1' $sel>" . lang('Бессрочно') . "</option>
    <option value='2' $sel>1 " . lang('День') . "</option>
    <option value='3' $sel>3 " . lang('Дня') . "</option>
    <option value='4' $sel>1 " . lang('Неделю') . "</option>
    <option value='5' $sel>1 " . lang('Месяц') . "</option>
    <option value='6' $sel>3 " . lang('Месяца') . "</option>";

echo "</select>";

echo "<hr>";

$doc->Button('btn btn-success btn-sm', 'save', 'save', 'Сохранить');
$doc->Button('btn btn-danger btn-sm', 'delete', 'trash-o', 'Удалить');
$doc->Button('btn btn-secondary btn-sm', 'cancel', null, 'Отменить');

echo "</form>";

require_once H . 'sys/inc/tfoot.php';
exit;
