<?php

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

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'adm_check', 'user') as $inc) {
    require_once "../sys/inc/$inc.php";
}

access('adm_games', null, 'index.php?' . SID);
adm_check();

$set['title'] = lang('Игры и приложения');

require_once H . 'sys/inc/thead.php';

aut();
err();

#Навигация
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_PANEL . "'>" . lang('Панель управления') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Игры и приложения');
echo "</small></div><br />";

if (isset($_POST['add']) && isset($_POST['name']) && $_POST['name'] != NULL && isset($_POST['url']) && $_POST['url'] != NULL && isset($_POST['counter'])) {
    $name = esc(stripcslashes(htmlspecialchars($_POST['name'])));
    $opis = esc(stripcslashes(htmlspecialchars($_POST['opis'])));
    $url = esc(stripcslashes(htmlspecialchars($_POST['url'])));
    $counter = esc(stripcslashes(htmlspecialchars($_POST['counter'])));
    $ps = $db->query("SELECT MAX(pos) FROM `games`")->fetch_row();
    $pos = $ps[0] + 1;
    $icon = preg_replace('#[^a-z0-9 _\-\.]#i', null, $_POST['icon']);

    $db->query("INSERT INTO `games` (`name`, `opis`, `url`, `counter`, `pos`, `icon`) VALUES ('$name', '$opis', '$url', '$counter', '$pos', '$icon')");

    $_SESSION['message'] = lang('Приложение успешно добавлено');
    header("Location: ?");
}

if (isset($_POST['change']) && isset($_GET['id']) && isset($_POST['name']) && $_POST['name'] != NULL && isset($_POST['url']) && isset($_POST['counter'])) {
    $id = intval($_GET['id']);
    $name = esc(stripcslashes(htmlspecialchars($_POST['name'])));
    $opis = esc(stripcslashes(htmlspecialchars($_POST['opis'])));
    $url = esc(stripcslashes(htmlspecialchars($_POST['url'])));
    $counter = esc(stripcslashes(htmlspecialchars($_POST['counter'])));
    $icon = preg_replace('#[^a-z0-9 _\-\.]#i', null, $_POST['icon']);

    $db->query("UPDATE `games` SET `name` = '$name', `opis` = '$opis', `url` = '$url', `counter` = '$counter', `icon` = '$icon' WHERE `id` = '$id' LIMIT 1");
    $_SESSION['message'] = lang('Изменения сохранены');
    header("Location: ?");
}

if (isset($_GET['id']) && isset($_GET['act'])) {
    $countm = $db->super_query("SELECT COUNT(*) as count FROM `games` WHERE `id` = '" . intval($_GET['id']) . "'");
    if ($countm['count']) {
        $menu = $db->get_row($db->query("SELECT * FROM `games` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1"));
    }
    if ($_GET['act'] == 'up') {
        $db->query("UPDATE `games` SET `pos` = '" . ($menu['pos']) . "' WHERE `pos` = '" . ($menu['pos'] - 1) . "' LIMIT 1");
        $db->query("UPDATE `games` SET `pos` = '" . ($menu['pos'] - 1) . "' WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1");

        $_SESSION['message'] = lang('Изменения сохранены');
        header("Location: ?");
    }
    if ($_GET['act'] == 'down') {
        $db->query("UPDATE `games` SET `pos` = '" . ($menu['pos']) . "' WHERE `pos` = '" . ($menu['pos'] + 1) . "' LIMIT 1");
        $db->query("UPDATE `games` SET `pos` = '" . ($menu['pos'] + 1) . "' WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1");

        $_SESSION['message'] = lang('Изменения сохранены');
        header("Location: ?");
    }
    if ($_GET['act'] == 'del') {
        $db->query("DELETE FROM `games` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1");
        $_SESSION['message'] = lang('Пункт удален');
        header("Location: ?");
    }
}

$k_post = $db->super_query("SELECT COUNT(*) as count FROM games");
$k_post = $k_post['count'];

#Если нет разделов меню
if ($k_post == 0) {
    $doc->NoResult();
}

$q = $db->query("SELECT * FROM `games` ORDER BY `pos` ASC");
while ($post = $db->get_row($q)) {

    if (isset($_GET['id']) && $_GET['id'] == $post['id'] && isset($_GET['act']) && $_GET['act'] == 'edit') {
        echo "<div class='list-group-item-null'>";

        echo "<img src='/modules/games/img/$post[icon]' width='20'/>";
        echo "<div style='float:right;color:grey;'>$post[pos]</div>";

        echo "$post[pos]. $post[name] ($post[url])";

        echo "</div>";

        echo "<form class='list-group-item-null' action='?id=$post[id]' method='post'>";

        echo "<input placeholder='Название' type='text' name='name' value='$post[name]' class='form-control'/><br />";
        echo "<textarea placeholder='Описание' name='opis' class='form-control'/>$post[opis]</textarea><br />";
        echo "<input placeholder='Ссылка' type='text' name='url' value='$post[url]' class='form-control'/><br />";
        echo "<input placeholder='Счетчик' type='text' name='counter' value='$post[counter]' class='form-control'/><br />";
        echo "<input placeholder='Иконка 48x48' type='text' name='icon' value='$post[icon]' class='form-control'/>";
        echo "<br />";
        echo "<button name='change' class='btn btn-success btn-sm'><i class='fa fa-save fa-fw'></i> Сохранить</button> ";
        echo "<a class='btn btn-sm btn-secondary' href='?$passgen'>Отмена</a>";

        echo "</form>";
    } else {
        echo "<div class='list-group-item-null'>";

        echo "<span style='float:right;color:grey;'>";
        echo "<a href='?id=$post[id]&amp;act=edit&amp;$passgen' title='Редактировать'><i class='fa fa-edit fa-fw'></i></a> ";
        echo "<a href='?id=$post[id]&amp;act=up&amp;$passgen' title='Выше'><i class='fa fa-arrow-up fa-fw'></i></a> ";
        echo "</span>";

        echo "<img src='/modules/games/img/$post[icon]' width='20'/>";
        echo " $post[name]<br />";

        echo "<font color='grey'>";
        echo "<small>Ссылка: $post[url]</small><br />";
        echo "<small>Счетчик: " . ($post['counter'] == null ? 'отсутствует' : $post['counter']) . "</small>";
        echo "</font>\n";

        echo "<span style='float:right;color:grey;'>";
        echo "<a href='?id=$post[id]&amp;act=del&amp;$passgen' title='Удалить'><i class='fa fa-trash-o fa-fw'></i></a>";
        echo "<a href='?id=$post[id]&amp;act=down&amp;$passgen' title='Ниже'><i class='fa fa-arrow-down fa-fw'></i></a>";
        echo "</span>";

        echo "</div>\n";
    }
}

if (isset($_GET['add'])) {
    echo "<form action='?add=$passgen' class='list-group-item-null' method='post'>";

    echo "<input placeholder='Название' type='text' name='name' value='' class='form-control'/><br />";
    echo "<textarea placeholder='Описание' name='opis' class='form-control'/></textarea><br />";
    echo "<input placeholder='Ссылка' type='text' name='url' value='' class='form-control'/><br />";
    echo "<input placeholder='Счетчик' type='text' name='counter' value='' class='form-control'/>";
    echo "<small style='color: grey;'>Имя иконки (gorod.png) иконку кинуть в /modules/games/img/</small>";
    echo "<input placeholder='Иконка 48x48' type='text' name='icon' value='' class='form-control'/>";
    echo "<br />";
    echo "<button name='add' class='btn btn-primary btn-sm'><i class='fa fa-plus fa-fw'></i> Добавить</button> ";
    echo "<a class='btn btn-sm btn-secondary' href='?$passgen'>Отмена</a>";

    echo "</form>";
} else {
    echo "<form action='?add=$passgen' class='list-group-item-null' method='post'>";

    echo "<button class='btn btn-primary btn-sm'><i class='fa fa-plus fa-fw'></i> Добавить пункт</button>";

    echo "</form>";
}

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