<?php

/**
 * This file is part of JohnCMS Content Management System.
 *
 * @copyright JohnCMS Community
 * @license   https://opensource.org/licenses/GPL-3.0 GPL-3.0
 * @link      https://johncms.com JohnCMS Project
 */

declare(strict_types=1);

use Johncms\Counters;
use Johncms\System\Legacy\Tools;
use Johncms\System\View\Render;
use Johncms\NavChain;
use Johncms\System\i18n\Translator;

defined('_IN_JOHNCMS') || die('Error: restricted access');
define('_IS_HOMEPAGE', 1); // Пометка главной страницы

/**
 * @var Render $view
 * @var NavChain $nav_chain
 * @var Tools $tools
 */

$view = di(Render::class);
$news_config = di('config')['johncms']['news'];
$tools = di(Tools::class);
$nav_chain = di(NavChain::class);
$nav_chain->showHomePage(false);

// Register Namespace for module templates
$view->addFolder('homepage', __DIR__ . '/templates/');

// Register the module languages domain and folder
di(Translator::class)->addTranslationDomain('homepage', __DIR__ . '/locale');

$data = [];

// Последние темы
$total_topics = $db->query("SELECT `id` FROM `forum_topic` WHERE `deleted` != '1' OR deleted IS NULL")->rowCount();
if ($total_topics > 0) {
    $forum = $db->query("SELECT * FROM `forum_topic` WHERE `deleted` != '1' OR deleted IS NULL ORDER BY `last_post_date` DESC LIMIT 5");
    $last_posts = [];

    while ($f = $forum->fetch()) {
        $last_posts[] = [
            'id' => $f['id'],
            'title' => $f['name'],
            'posts' => $f['post_count'],
            'author' => $f['user_name'],
            'last_user' => $f['last_post_author_name'],
            'date' => $f['last_post_date'],
        ];
    }

}else {
    $last_posts = 'none';
}

// Последние Статьи
if ($db->query("SELECT `id` FROM `library_texts`")->rowCount() > 0) {
    $lib = $db->query("SELECT * FROM `library_texts` ORDER BY `time` DESC LIMIT 5");
    $last_lib = [];

    while ($l = $lib->fetch()) {
        $last_lib[] = [
            'id' => $l['id'],
            'name' => $l['name'],
            'user' => $l['uploader'],
            'views' => $l['count_views'],
            'coms' => $l['comm_count'],
        ];
    }

}else {
    $last_lib = 'none';
}


// Последние файлы
$total_files = $db->query("SELECT COUNT(*) FROM `download__files`")->fetchColumn();
if ($total_files > 0) {
    $downs = $db->query("SELECT * FROM `download__files` ORDER BY `time` DESC LIMIT 5");
    $last_down = [];

    while ($d = $downs->fetch()) {
        $us = $db->query("SELECT `name` FROM `users` WHERE `id` = '".$d['user_id']."'")->fetch();
        $last_down[] = [
            'uid' => $d['id'],
            'rus_name' => $d['rus_name'],
            'coms' => $d['comm_count'],
            'user' => $us['name'],
        ];
    }

}else {
    $last_down = 'none';
}


if ($news_config['view'] > 0) {
    $reqtime = $news_config['days'] ? time() - ($news_config['days'] * 86400) : 0;
    $req = $db->query(
        "SELECT * FROM `news` WHERE `time` > '${reqtime}' ORDER BY `time` DESC LIMIT " .
        $news_config['quantity']
    );

    if ($req->rowCount()) {
        $i = 0;
        $news = '';

        $items = [];
        while ($res = $req->fetch()) {
            $text = $res['text'];
            $moreLink = '';

            // Если текст больше заданного предела, обрезаем
            if (mb_strlen($text) > $news_config['size']) {
                $text = mb_substr($text, 0, $news_config['size']);
                $text = htmlentities($text, ENT_QUOTES, 'UTF-8') . '...';
            }

            $text = $tools->checkout($text, $news_config['breaks'] ? 1 : 2, $news_config['tags'] ? 1 : 2);

            if ($news_config['smileys']) {
                $text = $tools->smilies($text);
            }

            // Ссылка на каменты
            $comments_url = '';
            $comments_count = 0;

            if (! empty($res['kom']) && $news_config['view'] !== 2 && $news_config['kom'] > 0) {
                $res_mes = $db->query("SELECT * FROM `forum_topic` WHERE `id` = '" . $res['kom'] . "'");
                if ($mes = $res_mes->fetch()) {
                    $comments_count = $mes['post_count'] - 1;
                }
                if ($comments_count >= 0) {
                    $comments_url = '/forum/?type=topic&id=' . $res['kom'];
                }
            }

            $items[] = [
                'text'         => $news_config['view'] !== 2 ? $text : '',
                'title'        => $res['name'],
                'comments'     => $comments_count ?? 0,
                'comments_url' => $comments_url,
            ];
        }
    }
}

$data['news'] = $items ?? [];
$data['last_posts'] = $last_posts ?? [];
$data['last_down'] = $last_down ?? [];
$data['last_lib'] = $last_lib ?? [];

// TODO: Если приживется, объединить со счетчиками в меню для избежания лишних запросов
/** @var Counters $counters */
$counters = di('counters');
$count['forum'] = $counters->forumCounters();
$count['guestbook'] = $counters->guestbookCounters();
$count['downloads'] = $counters->downloadsCounters();
$count['library'] = $counters->libraryCounters();
$count['users'] = $counters->usersCounters();
$count['news'] = $counters->news();
$count['total_topics'] = $total_topics;
$count['total_files'] = $total_files;

$data['counters'] = $count;

echo $view->render('homepage::index', ['data' => $data, 'last_posts' => $last_posts, 'last_down' => $last_down, 'last_lib' => $last_lib]);