<?php

define('_IN_JOHNCMS', 1);
require('../system/bootstrap.php');
/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::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\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

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


$headmod = 'notif';
$textl = 'Forum Notifikasi';
require ('../system/head.php');



echo '<div class="phdr"><b>Forum Notification</b></div>';

if($systemUser->notif_forum) {	
  echo '<div class="topmenu">New Notification : ' . $systemUser->notif_forum . '</div>';
  $db->exec("UPDATE `users` SET `notif_forum`='0' WHERE `id` = '$systemUser->id'");
}

$total = $db->query("SELECT COUNT(*) FROM `forum` WHERE `id_user`='$systemUser->id'" .
    ($systemUser->rights >= 7 ? "" : " AND `close` != '1'"))->fetchColumn();
if ($total) {
    $req = $db->query("SELECT `forum`.*, `users`.`sex`, `users`.`rights`, `users`.`lastdate`, `users`.`status`, `users`.`datereg`
     	FROM `forum` LEFT JOIN `users` ON `forum`.`user_id` = `users`.`id`
      	WHERE `forum`.`type` = 'm' AND `forum`.`id_user`='$systemUser->id'" . ($systemUser->rights >=
        7 ? "" : " AND `forum`.`close` != '1'") . " ORDER BY `forum`.`id` DESC LIMIT $start, $kmess");
    while (($res = $req->fetch()) !== false) {
    	if ($res['close'])
        	echo '<div class="mainblok"><div class="rmenu">';
        else
    		echo $i % 2 ? '<div class="mainblok"><div class="phdr">Notif Forum</div><div class="list2">' : '<div class="mainblok"><div class="phdr">Notif Forum</div><div class="list1">';
        $theme = $db->query("SELECT `text` FROM `forum` WHERE `id` = '" . $res['refid'] . "' ORDER BY `id` ASC LIMIT 1")->fetch();
       ###
        echo '<b>From: <a href="profile.php?user=' . $res['user_id'] . '">' . $res['from'] . '</a></b><br/>';
        echo '<b>Topic: <a href="../forum/index.php?id=' . $res['refid'] . '">' . $theme['text'] . '</a></b>';
        echo '<br/><b>Time:</b><small>' . date("d.m.Y / H:i", $res['time'] + $config['timeshift'] * 3600) . '</small>';
         echo '<br/><font color="red">Answared:</font><br/>';
        //echo '<span class="button-tag">';
        $text = $tools->checkout($res['text'], 1, 1);
        if(mb_strlen($res['text']) > 50)
           $text .= ' ...';
        if ($res['kedit'])
            $text .= '<br /><small>Edited <b>' . $res['edit'] . '</b> (' . date("d.m /H:i", $res['tedit'] + $config['timeshift'] * 3600) . ') <b>[' . $res['kedit'] . ']</b></small>';
    	      echo $text;
             //echo '</span>';
            echo '<br/><span class="button-tag"><a href="../forum/index.php?act=post&amp;id=' . $res['id'] . '">Read More</a></span>';
        echo '</div></div>';
        ++$i;
    }
} else
	echo '<div class="menu"><p>Not found!</p></div>';
echo '<div class="phdr">Total: ' . $total . '</div>';


if ($total > $kmess) {
	echo '<p>' . $tools->displayPagination('notif.php?', $start, $total, $kmess) . '</p>' .
	'<p><form action="notif.php?" method="post"><input type="text" name="page" size="2"/><input type="submit" value="To page &gt;&gt;"/></form></p>';
}

echo '<p><a href="index.php?act=office">Back</a></p>';

require_once ('../system/end.php');
?>