<?php

define('_IN_JOHNCMS', 1);


//require('bootstrap.php');
 

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);

/** @var Johncms\Api\EnvironmentInterface $env */
$env = $container->get(Johncms\Api\EnvironmentInterface::class);

/** @var Johncms\Api\BbcodeInterface $bbcode */
$bbcode = $container->get(Johncms\Api\BbcodeInterface::class);

/** @var Johncms\Api\ConfigInterface $config */
$config = $container->get(Johncms\Api\ConfigInterface::class);



$tongbv = $db->query("SELECT COUNT(*) FROM `forum` WHERE `type`='t' AND `close` != '1'")->fetchColumn();

$newtopic = $db->query("SELECT * FROM `forum` WHERE `type` = 't' and kedit='0' AND `close`!='1' ORDER BY `time` DESC LIMIT $start, $kmess");
$themesOut = '';

while ($arr = $newtopic->fetch()) {


if ($arr['close']) $themesOut .= '<div class="rmenu">';
		else $themesOut .=  $i % 2 ? '<div class="list2"><table cellpadding="0" cellspacing="0" width="100%"><tr><td width="auto" align="left">' : '<div class="list1"><table cellpadding="0" cellspacing="0" width="100%"><tr><td width="auto" align="left">';

 $np = $db->query("SELECT COUNT(*) FROM `cms_forum_rdm` WHERE `time` >= '" . $arr['time'] . "' AND `topic_id` = '" . $arr['id'] . "' AND `user_id` = " . $systemUser->id)->fetchColumn(); 

        $icons = [
                            ($np ? (!$arr['vip'] ? $tools->image('op.gif') : '') : $tools->image('np.gif')),
                            ($arr['vip'] ? $tools->image('pt.gif') : ''),
                            ($arr['realid'] ? $tools->image('rate.gif') : ''),
                            ($arr['edit'] ? $tools->image('tz.gif') : ''),
                        ]; 
        $themesOut .= implode('', array_filter($icons));
        $prfx = $db->query("SELECT * FROM `prefix_forum` ORDER BY `id` ");
            if ($prfx) {
                while ($prefix = $prfx->fetch()) {
                   if ($arr['nircable'] == $prefix['id']) {
	                   $themesOut .= '&#160;<span class="' . $prefix['label'] . '">' . $prefix['name'] . '</span>&#160;';
                    } elseif ($arr['nircable'] == 0) {
                       $themesOut .= '';
                    }
                  }
            }




$gettop = $db->query ( "SELECT * FROM `forum` WHERE `type` = 'r' and id = '" . $arr [ 'refid' ]."'" )->fetch();

$total = $db->query("select count(`id`) from `forum` where `type` = 'm' and `refid` = '".$arr['id']."'")->fetchColumn();

$comment = $db->query("SELECT `user_id` FROM `forum` WHERE `type` = 'm' AND `close` != '1' AND `refid` = '" . $arr['id'] . "'ORDER BY time DESC");

$nick = $comment->fetch();

$endPage = ceil($total / $kmess); 

if ($_SESSION['agent_web'] == 'wap') {         
		$themesOut .= '&#160;<a href="forum/index.php?id=' . $arr['id'] . '&amp;page=' . $endPage . '" data-tooltip="' . $arr['text'] . '" title="' . $arr['text'] . '">' . $arr['text'] . '</a></td><td width="auto" align="right"> (<span style="color:red">'.$total.'</span>) <a href="'.$home.'/users/profile.php?id='.$nick['user_id'].'">'.$tools->nick($nick['user_id']).'</a>';
}
if ($_SESSION['agent_web'] == 'web') {
   $themesOut .= '&#160;<a href="forum/index.php?id=' . $arr['id'] . '&amp;page=' . $endPage . '" data-tooltip="' . $arr['text'] . '" title="' . $arr['text'] . '">' . $arr['text'] . '</a></td><td width="auto" align="right">';
}
        $themesOut .= '</td></tr></table></div>';
        
	++$i;
 
}


echo $themesOut;
    if ($tongbv > $kmess){
        echo '<div class="topmenu">' . $tools->displayPagination('/index.php?', $start, $tongbv, $kmess) . '</div>';
    } 

 





?>