<?php

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

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

if (!isset($user)) {
    header("Location: /aut.php");
}
if (isset($_GET['admin']) && $user['level'] >= 3) {
    $doc->Title('Панель ссылок');

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

    aut();
    err();

    if (isset($_GET['new'])) {
        if (isset($_POST['name']) && isset($_POST['url']) && isset($_POST['icon'])) {
            $name = $_POST['name'];
            $url = $_POST['url'];
            $icon = preg_replace('#[^a-z0-9 _\-\.]#i', null, $_POST['icon']);

            mysql_query("INSERT INTO `links_foot` (`name`, `url`, `icon`) VALUES ('" . mysql_real_escape_string($name) . "', '$url', '$icon')");

            $_SESSION['new'] = 1;

            header("location: ?admin");
            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='/?umenu'>" . lang('Кабинет') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='/user/links/'>" . lang('Панель навигации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='?admin'>" . lang('Управление') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Добавление ссылок');
        echo "</small></div><br />";

        echo "<form class='list-group-item-null' method='post' action=''>\n";

        $doc->Input('name', 'Название', 100);
        $doc->Input('url', 'Ссылка', 100);
        $doc->Input('icon', 'Иконка', 100);
        $doc->Link(null, 'http://fontawesome.io/icons/', null, 'Список иконок');
        echo "<br />";
        $doc->Button('btn btn-success btn-sm', 'ok', 'plus', 'Добавить');

        echo "</form>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    } elseif (isset($_GET['edit']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot` WHERE `id` = '" . intval($_GET['edit']) . "' LIMIT 1"), 0) != 0) {
        $link = mysql_fetch_array(mysql_query("SELECT * FROM `links_foot` WHERE `id` = '" . intval($_GET['edit']) . "' LIMIT 1"));
        if (isset($_POST['name']) && isset($_POST['url']) && isset($_POST['icon'])) {
            $name = $_POST['name'];
            $url = $_POST['url'];
            $icon = preg_replace('#[^a-z0-9 _\-\.]#i', null, $_POST['icon']);

            mysql_query("UPDATE `links_foot` SET `name` = '" . mysql_real_escape_string($name) . "', `url` = '$url', `icon` = '$icon' WHERE `id` = '$link[id]' LIMIT 1");

            $_SESSION['edit'] = 1;
            header("location: ?admin");
            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='/?umenu'>" . lang('Кабинет') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='/user/links/'>" . lang('Панель навигации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='?admin'>" . lang('Управление') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Редактирование');
        echo "</small></div><br />";

        echo "<form class='list-group-item-null' method='POST' action='?admin&amp;edit=$link[id]&amp;ok'>";

        $doc->Input('name', 'Название', 100, "$link[name]");
        $doc->Input('url', 'Ссылка', 100, "$link[url]");
        $doc->Input('icon', 'Иконка', 100, "$link[icon]");
        $doc->Link(null, 'http://fontawesome.io/icons/', null, 'Список иконок');
        echo "<br />";
        $doc->Button('btn btn-success btn-sm', 'ok', 'save', 'Сохранить');

        echo "</form>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    } elseif (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"), 0) != 0) {
        $link = mysql_fetch_array(mysql_query("SELECT * FROM `links_foot` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
        if (isset($_GET['ok'])) {
            mysql_query("DELETE FROM `links_foot` WHERE `id` = '$link[id]'");
            mysql_query("DELETE FROM `links_foot_user` WHERE `id_link` = '$link[id]'");
            $_SESSION['del'] = 1;
            header("location: ?admin");
            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='/?umenu'>" . lang('Кабинет') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='/user/links/'>" . lang('Панель навигации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='?admin'>" . lang('Управление') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Удаление');
        echo "</small></div><br />";

        echo "<div class='list-group-item-null'>";
        echo lang('Вы уверены, что хотите удалить эту ссылку') . "?<br />";

        $doc->Link('btn btn-success btn-sm', "?admin&amp;del=$link[id]&amp;ok", null, 'Да');
        $doc->Link('btn btn-secondary btn-sm', '?admin', null, 'Нет');
        echo "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
    if (isset($_SESSION['new'])) {
        $_SESSION['message'] = lang('Ссылка успешно добавлена');

        unset($_SESSION['new']);
    }
    if (isset($_SESSION['edit'])) {
        $_SESSION['message'] = lang('Изменения сохранены');

        unset($_SESSION['edit']);
    }
    if (isset($_SESSION['del'])) {
        $_SESSION['message'] = lang('Ссылка успешно удалена');

        unset($_SESSION['del']);
    }

    #Навигация
    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='/?umenu'>" . lang('Кабинет') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/user/links/'>" . lang('Панель навигации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Управление');
    echo "</small></div><br />";

    $doc->Link('list-group-item-null list-group-item-warning', '?admin&amp;new', 'plus', 'Добавить ссылку');
    $links = mysql_query("SELECT * FROM `links_foot` ORDER BY `name` DESC");

    if (mysql_num_rows($links) == 0) {
        $doc->NoResult();
    }
    while ($post = mysql_fetch_array($links)) {
        echo "<div class='list-group-item-null'>\n";
        echo "<div style='float: right;'>";
        echo "<a href='?admin&amp;edit=$post[id]' class='hint--left' data-hint='" . lang('Редактировать ссылку') . "'><i class='fa fa-edit fa-fw'></i></a> ";
        echo "<a href='?admin&amp;del=$post[id]' class='hint--left' data-hint='" . lang('Удалить ссылку') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
        echo "</div>";
        echo "<i class='fa fa-$post[icon] fa-fw'></i> $post[name] ($post[url])";
        echo "</div>";
        echo "<div class='hr'></div>";
    }

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

$doc->Title('Нижние ссылки');

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

aut();
err();

if (isset($_GET['add'])) {
    if (isset($_GET['link']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot` WHERE `id` = '" . intval($_GET['link']) . "' LIMIT 1"), 0) != 0) {
        $link = mysql_fetch_array(mysql_query("SELECT * FROM `links_foot` WHERE `id` = '" . intval($_GET['link']) . "' LIMIT 1"));
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot_user` WHERE `id_link` = '$link[id]' AND `id_user` = '$user[id]' LIMIT 1"), 0) == 0) {
            $pos = mysql_result(mysql_query("SELECT MAX(`pos`) FROM `links_foot_user` WHERE `id_user` = '$user[id]'"), 0) + 1;
            mysql_query("INSERT INTO `links_foot_user` (`id_link`, `id_user`, `pos`) VALUES ('$link[id]', '$user[id]', '$pos')");
            $_SESSION['new_user'] = 1;
        }
        header("location: /user/links/");
        exit;
    }
    $links = mysql_query("SELECT * FROM `links_foot` ORDER BY `name` DESC");
    $cr = 0;

    #Навигация
    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='/?umenu'>" . lang('Кабинет') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/user/links/'>" . lang('Панель навигации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Добавление ссылок');
    echo "</small></div><br />";

    while ($post = mysql_fetch_array($links)) {

        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot_user` WHERE `id_link` = '$post[id]' AND `id_user` = '$user[id]' LIMIT 1"), 0) == 0) {
            $cr++;

            echo "<a class='list-group-item-null list-group-item-grey' href='?add&amp;link=$post[id]'><i class='fa fa-$post[icon] fa-fw'></i> $post[name]</a>";
        }
    }

    if ($cr == 0) {
        $doc->NoResult();
    }

    require_once H . 'sys/inc/tfoot.php';
    exit;
} elseif (isset($_GET['up'])) {
    $link = mysql_fetch_assoc(mysql_query("SELECT * FROM `links_foot_user` WHERE `id` = '" . intval($_GET['up']) . "' AND `id_user` = '$user[id]' LIMIT 1"));
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot_user` WHERE `pos` < '$link[pos]' AND `id_user` = '$user[id]' LIMIT 1"), 0) != 0) {
        mysql_query("UPDATE `links_foot_user` SET `pos` = '" . ($link['pos']) . "' WHERE `pos` = '" . ($link['pos'] - 1) . "' AND `id_user` = '$user[id]' LIMIT 1");
        mysql_query("UPDATE `links_foot_user` SET `pos` = '" . ($link['pos'] - 1) . "' WHERE `id` = '" . intval($_GET['up']) . "' AND `id_user` = '$user[id]' LIMIT 1");

        $_SESSION['up'] = 1;
        header("location: /user/links/");
    } else {
        header("Location: /user/links/");
    }
} elseif (isset($_GET['down'])) {
    $link = mysql_fetch_assoc(mysql_query("SELECT * FROM `links_foot_user` WHERE `id` = '" . intval($_GET['down']) . "' AND `id_user` = '$user[id]' LIMIT 1"));
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `links_foot_user` WHERE `pos` > '$link[pos]' AND `id_user` = '$user[id]' LIMIT 1"), 0) != 0) {
        mysql_query("UPDATE `links_foot_user` SET `pos` = '" . ($link['pos']) . "' WHERE `pos` = '" . ($link['pos'] + 1) . "' AND `id_user` = '$user[id]' LIMIT 1");
        mysql_query("UPDATE `links_foot_user` SET `pos` = '" . ($link['pos'] + 1) . "' WHERE `id` = '" . intval($_GET['down']) . "' AND `id_user` = '$user[id]' LIMIT 1");
        $_SESSION['down'] = 1;
        header("location: /user/links/");
    } else {
        header("Location: /user/links/");
    }
} elseif (isset($_GET['del'])) {
    $link = mysql_fetch_array(mysql_query("SELECT * FROM `links_foot_user` WHERE `id` = '" . intval($_GET['del']) . "' AND `id_user` = '$user[id]' LIMIT 1"));
    $links = mysql_query("SELECT * FROM `links_foot_user` WHERE `id_user` = '$user[id]' AND `pos` > '$link[pos]' ORDER BY `pos` ASC");
    while ($p = mysql_fetch_array($links)) {
        mysql_query("UPDATE `links_foot_user` SET `pos` = '" . ($p['pos'] - 1) . "' WHERE `id` = '$p[id]' LIMIT 1");
    }
    mysql_query("DELETE FROM `links_foot_user` WHERE `id` = '$link[id]' AND `id_user` = '$user[id]'");

    $_SESSION['del_user'] = 1;

    header("Location: ?");
} elseif (isset($_GET['show_icons'])) {
    mysql_query("UPDATE `user` SET `show_foot_type` = 'icons' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_icons'] = 1;
    header("Location: ?");
} elseif (isset($_GET['show_text'])) {
    mysql_query("UPDATE `user` SET `show_foot_type` = 'text' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_text'] = 1;
    header("Location: ?");
} elseif (isset($_GET['show_on'])) {
    mysql_query("UPDATE `user` SET `show_foot` = 'on' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_on'] = 1;
    header("Location: ?");
} elseif (isset($_GET['show_off'])) {
    mysql_query("UPDATE `user` SET `show_foot` = 'off' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_off'] = 1;
    header("Location: ?");
} elseif (isset($_GET['show_left'])) {
    mysql_query("UPDATE `user` SET `foot_sit` = 'left' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_left'] = 1;
    header("Location: ?");
} elseif (isset($_GET['show_center'])) {
    mysql_query("UPDATE `user` SET `foot_sit` = 'center' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_center'] = 1;
    header("Location: ?");
} elseif (isset($_GET['show_right'])) {
    mysql_query("UPDATE `user` SET `foot_sit` = 'right' WHERE `id` = '$user[id]' LIMIT 1");
    $_SESSION['show_right'] = 1;
    header("Location: ?");
}
if (isset($_SESSION['new_user'])) {
    $_SESSION['message'] = lang('Ссылка успешно добавлена');

    unset($_SESSION['new_user']);
}
if (isset($_SESSION['up'])) {
    $_SESSION['message'] = lang('Ссылка перемещена выше');

    unset($_SESSION['up']);
}
if (isset($_SESSION['down'])) {
    $_SESSION['message'] = lang('Ссылка перемещена ниже');

    unset($_SESSION['down']);
}
if (isset($_SESSION['del_user'])) {
    $_SESSION['message'] = lang('Ссылка успешно удалена');

    unset($_SESSION['del_user']);
}
if (isset($_SESSION['show_on'])) {
    $_SESSION['message'] = lang('Показ нижних ссылок успешно включен');

    unset($_SESSION['show_on']);
}
if (isset($_SESSION['show_off'])) {
    $_SESSION['message'] = lang('Показ нижних ссылок успешно выключен');

    unset($_SESSION['show_off']);
}
if (isset($_SESSION['show_left'])) {
    $_SESSION['message'] = lang('Включен показ ссылок слева');

    unset($_SESSION['show_left']);
}
if (isset($_SESSION['show_center'])) {
    $_SESSION['message'] = lang('Включен показ ссылок в центре');

    unset($_SESSION['show_center']);
}
if (isset($_SESSION['show_right'])) {
    $_SESSION['message'] = lang('Включен показ ссылок справа');

    unset($_SESSION['show_right']);
}

#Навигация
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='/?umenu'>" . lang('Кабинет') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Панель навигации');
echo "</small></div><br />";

if ($user['level'] >= 3) {
    $doc->Link('list-group-item-null list-group-item-warning', '?admin', 'th-large', 'Управление ссылками');
}
$doc->Link('list-group-item-null list-group-item-grey', '?add', 'plus', 'Добавить ссылку');

if ($user['show_foot'] == 'on') {
    $doc->Link('list-group-item-null list-group-item-grey', '?show_off', 'power-off', 'Выключить панель навигации');
} else {
    $doc->Link('list-group-item-null list-group-item-grey', '?show_on', 'power-off', 'Включить панель навигации');
}


$links = mysql_query("SELECT * FROM `links_foot_user` WHERE `id_user` = '$user[id]' ORDER BY `pos` ASC");
if (mysql_num_rows($links) == 0) {
    $doc->NoResult();
}

while ($post = mysql_fetch_array($links)) {
    echo "<div class='list-group-item-null'>";
    $link = mysql_fetch_array(mysql_query("SELECT * FROM `links_foot` WHERE `id` = '$post[id_link]' LIMIT 1"));
    echo "<div style='float: right;'>";
    echo "<a href = '?up=$post[id]' title = '" . lang('Поднять выше') . "'><i class='fa fa-arrow-up fa-fw'></i></a> ";
    echo "<a href = '?down=$post[id]' title = '" . lang('Опустить ниже') . "'><i class='fa fa-arrow-down fa-fw'></i></a> ";
    echo "<a href = '?del=$post[id]' title = '" . lang('Удалить ссылку') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
    echo "</div>";
    echo "<i class='fa fa-$link[icon] fa-fw'></i> " . lang("$link[name]");
    echo "</div>";
    echo "<div class='hr'></div>";
}

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