<?php

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

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

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

$set['title'] = lang('Действия администрации');
require_once H . 'sys/inc/thead.php';

aut();
err();

if (isset($_GET['id'])) {
    $ank = get_user($_GET['id']);
} else {
    $ank = false;
}

if (isset($_GET['id_mod']) && isset($_GET['id_act'])) {
    $k = $db->super_query("SELECT COUNT(*) as count FROM `admin_log` WHERE `mod` = '" . intval($_GET['id_mod']) . "' AND `act` = '" . intval($_GET['id_act']) . "'" . ($ank ? " AND `id_user` = '$ank[id]'" : null));
    if ($k['count'] != 0) {
        $mod = $db->get_row($db->query("SELECT * FROM `admin_log_mod` WHERE `id` = '" . intval($_GET['id_mod']) . "' LIMIT 1"));
        $act = $db->get_row($db->query("SELECT * FROM `admin_log_act` WHERE `id` = '" . intval($_GET['id_act']) . "' LIMIT 1"));
        #Навигация
        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='?id_mod=$mod[id]" . ($ank ? "&amp;id=$ank[id]" : null) . "'>" . lang('Действия администрации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='?$passgen" . ($ank ? "&amp;id=$ank[id]" : null) . "'>" . lang('Список модулей') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Список');
        echo "</small></div><br />";

        $k_post = $db->super_query("SELECT COUNT(*) as count FROM `admin_log` WHERE `mod` = '$mod[id]' AND `act` = '$act[id]'" . ($ank ? " AND `admin_log`.`id_user` = '$ank[id]'" : null));
        $k_post = $k_post['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

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

        $q = $db->query("SELECT * FROM `admin_log` WHERE `mod` = '$mod[id]' AND `act` = '$act[id]'" . ($ank ? " AND `admin_log`.`id_user` = '$ank[id]'" : null) . " ORDER BY id DESC LIMIT $start, $set[p_str]");
        while ($post = $db->get_row($q)) {
            $ank2 = get_user($post['id_user']);
            echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td class='icon14'>";
            avatar($ank2['id'], '40', 'border-radius: 2px;');
            echo "</td><td class='null'>";
            echo "<small class='right grey'><span class='hint--left' data-hint='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</span></small>";
            user($ank2['id']);
            echo "<br /><small class='grey'>";
            echo toOutput($post['opis']);
            echo "</small></td></tr></table>";
        }

        if ($k_page > 1) {
            str('?id_mod=' . $mod['id'] . '&amp;id_act=' . $act['id'] . '&amp;', $k_page, $page);
        }
    }
} elseif (isset($_GET['id_mod']) && ($db->super_query("SELECT COUNT(*) as count FROM `admin_log` WHERE `mod` = '" . intval($_GET['id_mod']) . "'" . ($ank ? " AND `id_user` = '$ank[id]'" : null)) != 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='" . DIR_PANEL . "'>" . lang('Панель управления') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='?$passgen" . ($ank ? "&amp;id=$ank[id]" : null) . "'>" . lang('Действия администрации') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Действия в модуле');
    echo "</small></div>";

    $mod = $db->get_row($db->query("SELECT * FROM `admin_log_mod` WHERE `id` = '" . intval($_GET['id_mod']) . "' LIMIT 1"));
    $q = $db->query("SELECT `admin_log_act`.`name`, `admin_log_act`.`id`, COUNT(`admin_log`.`id`) AS `count` FROM `admin_log` LEFT JOIN `admin_log_act` ON `admin_log`.`act` = `admin_log_act`.`id` WHERE `admin_log`.`mod` = '$mod[id]'" . ($ank ? " AND `admin_log`.`id_user` = '$ank[id]'" : null) . " GROUP BY `admin_log`.`act`");

    if ($db->num_rows($q) == 0) {
        $doc->NoResult();
    }
    while ($act = $db->get_row($q)) {
        echo "<div class='list-group-item-null-mini list-group-item-grey dop'>";
        echo "<a href='?id_mod=$mod[id]&amp;id_act=$act[id]" . ($ank ? "&amp;id=$ank[id]" : null) . "'><i class='fa fa-chevron-right fa-fw'></i> $act[name]</a> <span class='tag tag-info right'>$act[count]</span>\n";
        echo "</div>";
    }
} else {
#действия по модулям
    #Навигация
    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>";

    if ($ank && access('adm_log_read') && ($ank['id'] == $user['id'] || $ank['level'] < $user['level'])) {
        $k = $db->super_query("SELECT COUNT(*) as count FROM `admin_log` WHERE `id_user` = '$ank[id]'");
        $adm_log_c_all = $k['count'];
        $mes = mktime(0, 0, 0, date('m') - 1); #время месяц назад
        $k = $db->super_query("SELECT COUNT(*) as count FROM `admin_log` WHERE `id_user` = '$ank[id]' AND `time` > '$mes'");
        $adm_log_c_mes = $k['count'];

        echo "<div class='list-group-item-null'>";
        echo "<a href='/id$ank[id]'>$ank[nick]</a> ($ank[group_name])<br />\n";
        echo lang('Вся активность') . ": $adm_log_c_all<br />";
        echo lang('Активность за месяц') . ": $adm_log_c_mes";
        echo "</div>";
    } else {
        $k = $db->super_query("SELECT COUNT(*) as count FROM `admin_log`");
        $adm_log_c_all = $k['count'];
        $mes = mktime(0, 0, 0, date('m') - 1); #время месяц назад
        $k = $db->super_query("SELECT COUNT(*) as count FROM `admin_log` WHERE `time` > '$mes'");
        $adm_log_c_mes = $k['count'];

        echo "<div class='list-group-item-null'>";
        echo lang('Вся активность') . ": $adm_log_c_all<br />";
        echo lang('Активность за месяц') . ": $adm_log_c_mes";
        echo "</div>";
    }

    $q = $db->query("SELECT `admin_log_mod`.`name`, `admin_log_mod`.`id`, COUNT(`admin_log`.`id`) AS `count` FROM `admin_log` LEFT JOIN `admin_log_mod` ON `admin_log`.`mod` = `admin_log_mod`.`id`" . ($ank ? " WHERE `admin_log`.`id_user` = '$ank[id]'" : null) . " GROUP BY `admin_log`.`mod`");

    if ($db->num_rows($q) == 0) {
        $doc->NoResult();
    }
    while ($mod = $db->get_row($q)) {
        echo "<div class='list-group-item-null-mini list-group-item-grey dop'>";
        echo "<a href='?id_mod=$mod[id]" . ($ank ? "&amp;id=$ank[id]" : null) . "'><i class='fa fa-chevron-right fa-fw'></i> $mod[name]</a> <span class='tag tag-info right'>$mod[count]</span>\n";
        echo "</div>";
    }
}

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