<?php

/* DCMS Special
 * Дата последнего редактирования 26.09.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']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `diary_cat` WHERE `id` = '" . intval($_GET['r']) . "' LIMIT 1"), 0) == 1) {
    $r = intval($_GET['r']);
    $razdel = mysql_fetch_assoc(mysql_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']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `diary` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"), 0) != 0) {
            $del = mysql_fetch_assoc(mysql_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 = mysql_query("SELECT * FROM `diary_images` WHERE `id_diary`='$del[id]'");
                while ($delete = mysql_fetch_assoc($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'] . '');
                }
                mysql_query("DELETE FROM `diary_rating` WHERE `id_diary`='$del[id]'");
                mysql_query("DELETE FROM `diary_images` WHERE `id_diary`='$del[id]'");
                mysql_query("DELETE FROM `diary_komm` WHERE `id_diary`='$del[id]'");
                mysql_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 = mysql_fetch_assoc(mysql_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 />";

        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `diary` WHERE `id_cat`='$r'"), 0);
        $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 list-group-item-grey'>";
        echo "<i class='fa fa-sort fa-fw'></i> " . lang('Сортировать') . ":<br /> <a href='?r=$r&amp;sort=rating&amp;page=$page'>Рейтинг</a> | <a href='?r=$r&amp;sort=time&amp;page=$page'>Новые</a> | <a href='?r=$r&amp;sort=viewings&amp;page=$page'>Просмотры</a>";
        echo "</div>";

        if ($k_post == 0) {
            $doc->NoResult();
        }
        $q = mysql_query("SELECT * FROM `diary` WHERE `id_cat`='$r' ORDER BY $sort DESC LIMIT $start, $set[p_str]");
        while ($diary = mysql_fetch_assoc($q)) {
            $us = get_user($diary['id_user']);
            echo "<div class='list-group-item-null'>";
            if (isset($user) && $user['level'] > 2 && ($user['id'] == $us['id'] || $user['level'] > $us['level'])) {
                echo "<span style='float: right;' id='hides'><a href='?r=$r&amp;sort=$sort&amp;page=$page&amp;del=$diary[id]' title='" . lang('Удалить блог') . "' data-toggle='tooltip' data-placement='left'><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>";
            echo "<div class='hr'></div>";
        }

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


        if (isset($user)) {
            $doc->Link('list-group-item-null list-group-item-grey', "?r=$r&amp;new", 'plus', 'Создать блог');
        }
    }
} 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 "<a href='top.php' class='list-group-item-null list-group-item-grey'><i class='fa fa-star fa-fw'></i> " . lang('Топ блогов') . "</a>";
    echo "<a href='search.php' class='list-group-item-null list-group-item-grey'><i class='fa fa-search fa-fw'></i> " . lang('Поиск блогов') . "</a>";
    echo "<a href='tags.php' class='list-group-item-null list-group-item-grey'><i class='fa fa-tags fa-fw'></i> " . lang('Поиск по меткам') . "</a>";

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

    while ($cat = mysql_fetch_assoc($q2)) {
        $count = mysql_result(mysql_query("SELECT COUNT(*) FROM `diary` WHERE `id_cat`='$cat[id]'"), 0);

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


        echo "<a href='?r=$cat[id]'><i class='fa fa-book fa-fw'></i> $cat[name]</a> <span class='badge'>$count</span>";
        if (isset($user) && $user['level'] > 2) {
            echo " <a href='?edit=$cat[id]' data-toggle='tooltip' data-placement='bottom' title='" . lang('Редактировать') . "'><i class='fa fa-edit fa-fw'></i></a> <a href='?del=$cat[id]' data-toggle='tooltip' data-placement='bottom' title='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
        }


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

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