<?php

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

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

if (isset($_GET['r'])) {
    $l = $db->super_query("SELECT COUNT(*) as count FROM `diary_cat` WHERE `id` = '" . intval($_GET['r']) . "' LIMIT 1");
    if ($l['count'] == 1) {
        $r = intval($_GET['r']);
        $razdel = $ank = $db->get_row($db->query("SELECT * FROM `diary_cat` WHERE `id` = '$r' LIMIT 1"));
    }
    if (isset($user) && isset($_GET['new'])) {
        $set['title'] = lang('Блоги') . ' - ' . $razdel['name'] . ' - ' . lang('Новый'); // заголовок страницы
        require_once H . 'sys/inc/thead.php';

        aut();
        include_once 'inc/new_act.php';
        err();
        include_once 'inc/new_form.php';
    } else {
        $set['title'] = lang('Блоги') . ' - ' . $razdel['name']; // заголовок страницы
        if ($razdel['desc'] != NULL) {
            $set['meta_description'] = '' . $razdel['desc'] . '';
        }
        require_once H . 'sys/inc/thead.php';

        aut();

        if (isset($user) && $user['level'] > 2 && isset($_GET['del'])) {
            $q = $db->super_query("SELECT COUNT(*) as count FROM diary WHERE id = '" . intval($_GET['del']) . "' LIMIT 1");
            if ($q['count'] != 0) {
                $del = $ank = $db->get_row($db->query("SELECT * FROM `diary` WHERE `id`='" . intval($_GET['del']) . "' LIMIT 1"));
                $avtor = get_user($del['id_user']);
            }
            if ($user['id'] == $avtor['id'] || $user['level'] > $avtor['level']) {
                $images = $db->query("SELECT * FROM `diary_images` WHERE `id_diary`='$del[id]'");
                while ($delete = $db->get_row($images)) {
                    unlink(H . 'modules/diary/images/48/' . $delete['id'] . '.' . $delete['ras'] . '');
                    unlink(H . 'modules/diary/images/128/' . $delete['id'] . '.' . $delete['ras'] . '');
                    unlink(H . 'modules/diary/images/640/' . $delete['id'] . '.' . $delete['ras'] . '');
                    unlink(H . 'modules/diary/images/' . $delete['id'] . '.' . $delete['ras'] . '');
                }
                $db->query("DELETE FROM `diary_rating` WHERE `id_diary`='$del[id]'");
                $db->query("DELETE FROM `diary_images` WHERE `id_diary`='$del[id]'");
                $db->query("DELETE FROM `diary_komm` WHERE `id_diary`='$del[id]'");
                $db->query("DELETE FROM `diary` WHERE `id`='$del[id]'");
                msg('Блог успешно удален');
            } else {
                $err[] = lang('Не хватает прав для удаления блога');
            }
        }
        if (isset($_GET['sort'])) {
            if ($_GET['sort'] == 'viewings') {
                $sort = 'viewings';
            } elseif ($_GET['sort'] == 'rating') {
                $sort = 'rating';
            } else {
                $sort = 'time';
            }
        } else {
            $sort = 'time';
        }

        $cat = $db->get_row($db->query("SELECT * FROM `diary_cat` WHERE `id`='$razdel[id]' 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='/modules/diary/'>" . lang('Блоги') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo cutStr($cat['name'], 30);
        echo "</small></div><br />";

        $q = $db->super_query("SELECT COUNT(*) as count FROM diary WHERE id_cat = '$r'");
        $k_post = $q['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

        echo "<div class='list-group-item-null'><ul class='nav nav-pills'>";
        if ($sort == 'time') {
            echo "<li class='nav-item'><a class='nav-link active'>" . lang('Новые') . "</a></li>";
        } else {
            echo "<li class='nav-item'><a class='nav-link' href='?r=$r&amp;sort=time&amp;page=$page'>" . lang('Новые') . "</a></li>";
        }
        if ($sort == 'rating') {
            echo "<li class='nav-item'><a class='nav-link active'>" . lang('Рейтинг') . "</a></li>";
        } else {
            echo "<li class='nav-item'><a class='nav-link' href='?r=$r&amp;sort=rating&amp;page=$page'>" . lang('Рейтинг') . "</a></li>";
        }
        if ($sort == 'viewings') {
            echo "<li class='nav-item'><a class='nav-link active'>" . lang('Просмотры') . "</a></li>";
        } else {
            echo "<li class='nav-item'><a class='nav-link' href='?r=$r&amp;sort=viewings&amp;page=$page'>" . lang('Просмотры') . "</a></li>";
        }
        echo "</ul></div>";

        if ($k_post == 0) {
            $doc->NoResult();
        }
        $q = $db->query("SELECT * FROM `diary` WHERE `id_cat`='$r' ORDER BY $sort DESC LIMIT $start, $set[p_str]");
        while ($diary = $db->get_row($q)) {
            $us = get_user($diary['id_user']);
            echo "<div class='list-group-item-null list-group-item-grey dop'>";
            if (isset($user) && $user['level'] > 2 && ($user['id'] == $us['id'] || $user['level'] > $us['level'])) {
                echo "<span style='float: right;'><a href='?r=$r&amp;sort=$sort&amp;page=$page&amp;del=$diary[id]' class='hint--left' data-hint='" . lang('Удалить блог') . "'><i class='fa fa-trash-o fa-fw'></i></a></span>";
            }
            echo "<a href='/modules/diary/$diary[name]/'><i class='fa fa-book fa-fw'></i> $diary[name]</a><br />";

            echo "<i class='fa fa-eye fa-fw'></i> " . lang('Просмотров') . ": $diary[viewings] | ";
            echo "<i class='fa fa-star fa-fw'></i> " . lang('Рейтинг') . ": $diary[rating]<br />";
            echo user($us['id']);
            echo " <small style='color: grey;'>" . date::times($diary['time']) . "</small>";
            echo "</div>";
        }

        if ($k_page > 1) {
            str("?r=$r&sort=$sort&", $k_page, $page);
        } // Вывод страниц


        if (isset($user)) {
            echo "<div class='list-group-item-null'>";
            $doc->Link('btn btn-secondary btn-sm', "?r=$r&amp;new", 'plus', 'Создать блог');
            echo "</div>";
        }
    }
} else {
    $set['title'] = lang('Блоги') . ' - ' . lang('Категории'); // заголовок страницы
    require_once H . 'sys/inc/thead.php';

    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 lang('Блоги');
    echo "</small></div><br />";

    if (isset($user) && $user['level'] > 2) {
        include_once 'inc/admin_act.php';
    }

    echo "<div class='list-group-item-null'>";
    echo "<a href='top.php' class='btn btn-secondary btn-sm'><i class='fa fa-line-chart  fa-fw'></i> " . lang('Топ блогов') . "</a> ";
    echo "<a href='search.php' class='btn btn-secondary btn-sm'><i class='fa fa-search fa-fw'></i> " . lang('Поиск') . "</a> ";
    echo "<a href='tags.php' class='btn btn-secondary btn-sm'><i class='fa fa-tags fa-fw'></i> " . lang('Метки') . "</a> ";
    echo "</div>";

    $q2 = $db->query("SELECT * FROM `diary_cat` ORDER BY `name` ASC");
    if ($db->num_rows($q2) == 0) {
        $doc->NoResult();
    }

    while ($cat = $db->get_row($q2)) {
        $q = $db->super_query("SELECT COUNT(*) as count FROM diary WHERE id_cat = '$cat[id]'");
        $count = $q['count'];

        echo "<div class='list-group-item-null list-group-item-grey dop'>";


        echo "<a href='?r=$cat[id]'><i class='fa fa-$cat[icon] fa-fw'></i> $cat[name]</a> <span class='tag tag-info'>$count</span>";

        if (isset($user) && $user['level'] > 2) {
            echo "<span style='float: right;'><a href='?edit=$cat[id]' class='hint--top' data-hint='" . lang('Редактировать') . "'><i class='fa fa-edit fa-fw'></i></a> <a href='?del=$cat[id]' class='hint--top' data-hint='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a></span>";
        }


        if ($cat['desc'] != NULL) {
            echo "<br /><small style='color: grey;'>" . toOutput($cat['desc']) . "</small>";
        }
        echo "</div>";
    }

    if (isset($user) && $user['level'] > 2) {
        include_once 'inc/admin_form.php';
    }
}
require_once H . 'sys/inc/tfoot.php';
