<?php

/* DCMS Special
 * Дата последнего редактирования 18.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_menu', null, 'index.php?' . SID);
adm_check();

$set['title'] = lang('Панель управления');

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

aut();
err();

#Навигация
echo "<div class='card-header'>";
echo "<a href='/' data-toggle='tooltip' data-placement='right' title='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='/panel/'>" . lang('Панель управления') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Панель управления');
echo "</div>";

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'])));
    $url = esc(stripcslashes(htmlspecialchars($_POST['url'])));
    $counter = esc(stripcslashes(htmlspecialchars($_POST['counter'])));
    $pos = mysql_result(mysql_query("SELECT MAX(`pos`) FROM `adm_menu`"), 0) + 1;
    $icon = preg_replace('#[^a-z0-9 _\-\.]#i', null, $_POST['icon']);

    mysql_query("INSERT INTO `adm_menu` (`name`, `url`, `counter`, `pos`, `icon`) VALUES ('$name', '$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'])));
    $url = esc(stripcslashes(htmlspecialchars($_POST['url'])));
    $counter = esc(stripcslashes(htmlspecialchars($_POST['counter'])));
    $icon = preg_replace('#[^a-z0-9 _\-\.]#i', null, $_POST['icon']);

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

if (isset($_GET['id']) && isset($_GET['act']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `adm_menu` WHERE `id` = '" . intval($_GET['id']) . "'"), 0)) {

    $menu = mysql_fetch_assoc(mysql_query("SELECT * FROM `adm_menu` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1"));
    if ($_GET['act'] == 'up') {
        mysql_query("UPDATE `adm_menu` SET `pos` = '" . ($menu['pos']) . "' WHERE `pos` = '" . ($menu['pos'] - 1) . "' LIMIT 1");
        mysql_query("UPDATE `adm_menu` SET `pos` = '" . ($menu['pos'] - 1) . "' WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1");

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

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

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `adm_menu`"), 0);

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

$q = mysql_query("SELECT * FROM `adm_menu` ORDER BY `pos` ASC");
while ($post = mysql_fetch_assoc($q)) {

    if (isset($_GET['id']) && $_GET['id'] == $post['id'] && isset($_GET['act']) && $_GET['act'] == 'edit') {
        echo "<div class='list-group-item' style='margin-top: 2px;margin-bottom: 2px;'>";

        if (!isset($post['icon'])) {
            mysql_query('ALTER TABLE `adm_menu` ADD `icon` VARCHAR( 32 ) NULL DEFAULT NULL');
        }

        echo icons($post['icon']);
        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' action='?id=$post[id]' method='post'>";

        echo "<input placeholder='Название' type='text' name='name' value='$post[name]' class='form-control'/><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='Иконка' type='text' name='icon' value='$post[icon]' class='form-control'/>";
        echo "<a href='http://fontawesome.io/icons/'>Список иконок</a><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'>";

        if (!isset($post['icon'])) {
            mysql_query('ALTER TABLE `adm_menu` ADD `icon` VARCHAR( 32 ) NULL DEFAULT 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 icons($post['icon']) . " $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' method='post'>";

    echo "<input placeholder='Название' type='text' name='name' value='' class='form-control'/><br />";
    echo "<input placeholder='Ссылка' type='text' name='url' value='' class='form-control'/><br />";
    echo "<input placeholder='Счетчик' type='text' name='counter' value='' class='form-control'/><br />";
    echo "<input placeholder='Иконка' type='text' name='icon' value='' class='form-control'/>";
    echo "<a href='http://fontawesome.io/icons/'>Список иконок</a><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' 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';
