<?php

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

if (isset($_GET['id_group'])) {
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user_group` WHERE `id` = '" . intval($_GET['id_group']) . "'");
    if ($k['count']) {

        $group = $db->get_row($db->query("SELECT * FROM `user_group` WHERE `id` = '" . intval($_GET['id_group']) . "'"));

        $set['title'] = toOutput('Группа "' . $group['name'] . '" - Привилегии');
        require_once H . 'sys/inc/thead.php';

        if (isset($_POST['accesses'])) {
            $db->query("DELETE FROM `user_group_access` WHERE `id_group` = '$group[id]'");

            $q = $db->query("SELECT * FROM `all_accesses`");

            while ($post = $db->get_row($q)) {
                $type = $post['type'];
                if (isset($_POST[$type]) && $_POST[$type] == 1) {
                    $db->query("INSERT INTO `user_group_access` (`id_group`, `id_access`) VALUES ('$group[id]', '$post[type]')");
                }
            }

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

            header("Location: ?id_group=$group[id]");
            exit();
        }

        aut();

        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 "<a href='".DIR_PANEL."accesses.php'>" . lang('Привилегии') . "</a>";
        echo "</small></div><br />";

        echo "<form method='post' class='list-group-item-null' action='?id_group=$group[id]&amp;$passgen'>\n";
        $q = $db->query("SELECT * FROM `all_accesses` ORDER BY `name` ASC");

        while ($post = $db->get_row($q)) {
            $k = $db->super_query("SELECT COUNT(*) as count FROM `user_group_access` WHERE `id_group` = '$group[id]' AND `id_access` = '$post[type]' LIMIT 1");
            $u_g = $k['count'];
            echo "<label class='custom-control custom-checkbox'><input type='checkbox'" . ($u_g == 1 ? " checked='checked'" : null) . " name='$post[type]' value='1' class='custom-control-input'/><span class='custom-control-indicator'></span> <span class='custom-control-description'>$post[name]</span></label><br />\n";
        }

        echo "<button class='btn btn-success btn-sm' name='accesses'><i class='fa fa-save fa-fw'></i> " . lang('Сохранить') . "</button>";
        echo "</form>\n";

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

$set['title'] = lang('Группы пользователей');
require_once H . 'sys/inc/thead.php';

aut();

#Навигация
echo "<div class='list-group-item-null list-group-item-info'><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 />";

$accesses = $db->query("SELECT * FROM `user_group` ORDER BY `id` ASC");
while ($res = $db->get_row($accesses)) {
    $u_g = $db->super_query("SELECT COUNT(*) as count FROM `user_group_access` WHERE `id_group` = '$res[id]'");

    echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
    echo "<i class='fa fa-user-secret fa-3x'></i>";
    echo "</td><td class='null'>";
    echo "<small style='color: grey;' class='right'>" . lang('Уровень') . " $res[level]</small>";
    echo "<a href='?id_group=$res[id]'><b>$res[name]</b></a><br />";
    echo "<small style='color: grey;'>" . lang('Привелегий') . " $u_g[count]</small>";
    echo "</td></tr></table>";
}

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

