<?php

include 'system/sys.php';
include 'system/funcs.php';

if (!isset ($us)) {
        header ('location: /');
        exit;
}

H ('Новости', 'Новости');

echo '<div class="list1"><a href="/len.php">Все новости</a></div>';

$items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `new` LEFT JOIN `podp` ON `podp`.`us`='" . $us['id'] . "' WHERE (`new`.`us`=`podp`.`na`) AND (`new`.`us`!='" . $us['id'] . "')");
$items_per_page = 5;
$pages = ceil ( $items / $items_per_page );
$page = (int) $_GET['page'];

if ($page < 1) {
        $page = 1;
}

if ($page > $pages) {
        $page = $pages;
}

$start = $page * $items_per_page - $items_per_page;
if ($items == 0) {
        echo 'Новостей нет..';
}
else {
        
        $query = DB :: $dbs -> query ("SELECT `new`.* FROM `new` LEFT JOIN `podp` ON `podp`.`us`='" . $us['id'] . "' WHERE (`new`.`us`=`podp`.`na`) AND (`new`.`us`!='" . $us['id'] . "') ORDER BY `new`.`id` DESC LIMIT $start, $items_per_page");
        while ($new = $query -> fetch ()) {
                
                $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $new['us'] . "')");
                
                //
                echo '<div class="list1">';
                echo uNick ($new['kem']) . ' (' . datef ($new['t']) . ')<br/>';
                
                // 
                if (!empty ($new['komm'])) {
                        echo 'Комментарий: <b>' .bb ($new['komm']) . '</b><br/>';
                }
                
                // 
                if ($new['id_p']!=0) {                        
                        $p = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_p` WHERE (`id`='" . $new['id_p'] . "')");
                        $t = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t'] . "')");
                        echo '<img src="/images/re.png" alt=""/> ' . uNick ($p['us']) . '<br/>';
                        echo 'Тема: <a href="/forum/thema' . $t['id'] . '"><b>' . $t['name'] . '</b></a><br/>';
                        echo 'Сообщение: ' . bb ($p['text']);                        
                }
                
                
                //
                if ($new['id_f']!=0) {        
                        $f = DB :: $dbs -> queryFetch ("SELECT * FROM `zc_f` WHERE (`id`='" . $new['id_f'] . "')");
                        echo '<img src="/images/re.png" alt=""/> ' . uNick ($f['us']) . '<br/>';
                        echo 'Файл: <a href="/zc/file' . $f['id'] . '"><b>' . $f['name'] . '</b></a>';
                }

                
                echo '<br/>';
                
                // 
                if (!empty ($new['text'])) {
                        echo bb ($new['text']);
                }
                
                
                
                        // 
                if ($u['id']==$us['id'] || $u['level']<$us['level'] && $us['level']>1) {
                        echo ' <a href="/usnews.php?id=' . $u['id'] . '&del=' . $new['id'] . '">удал.</a>';
                }


                echo '</div>';
                
                // 
                echo '<div class="lst">';
                echo '<form action="/newkomm.php?id=' . $new['id'] . '&add" method="post">';
                echo 'Комментарий: <input name="komm"/> <input type="submit" value="Добавить"/>';
                echo '</form>';
                echo '</div>';
                
                
                
                $itemss = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `new_komm` WHERE (`id_new`='" . $new['id'] . "')");
                if ($itemss != 0) {

                        echo '<div style="padding-left:15%;">';

                        
                        $q = DB :: $dbs -> query ("SELECT * FROM `new_komm` WHERE (`id_new`='" . $new['id'] . "') ORDER BY `id` DESC LIMIT 3");
                        while ($komm = $q -> fetch ()) {
                                echo '<div class="lst">';
                                echo uNick ($komm['us']) . ' (' . datef ($komm['t']) . ')';

                                if ($komm['us']!=$us['id']) {
                                        echo ' [<a href="/newkomm.php?id=' . $new['id'] . '&komu=' . $komm['us'] . '">отв</a>] ';
                                }

                                if ($komm['us']==$us['id'] && $us['level']>1 || $u['level']<$us['level'] && $us['level']>1) {
                                        echo ' [<a href="/newkomm.php?id=' . $new['id'] . '&del=' . $komm['id'] . '">x</a>] ';
                                }
                                            
                                echo '<br/>';

                                echo bb ($komm['text']);
                                echo '</div>';
                        }
                        
                        //
                        if ($itemss>=5) {
                                echo '<div class="list1"><a href="/newkomm.php?id=' . $new['id'] . '">Показать все комментарии</a> (' . $itemss . ')</div>';
                        }
                        
                        echo '</div>';
                        
                }
                
                echo '<hr/>';
                
                
        }
        
        echo '<div class="lst">' . pages ('/feeds') . '</div>';        
        
        
}


F ();
