<?php

/**
 * WebEngine CMS
 * https://webenginecms.org/
 * 
 * @version 1.2.0
 * @author Lautaro Angelico <http://lautaroangelico.com/>
 * @copyright (c) 2013-2019 Lautaro Angelico, All Rights Reserved
 * 
 * Licensed under the MIT license
 * http://opensource.org/licenses/MIT
 */

echo '<div class="page-title flex-s-c"> <span>LAST NEWS</span> <a href="' . __BASE_URL__ . 'news" class="news-title-more">+</a></div>';

try {
	loadModuleConfigs('news');

	// Module status
	if (!mconfig('active')) throw new Exception(lang('error_47', true));

	// News object
	$News = new News();
	$cachedNews = loadCache('news.cache');
	if (!is_array($cachedNews)) throw new Exception(lang('error_61'));

	// Set news language
	if (config('language_switch_active', true)) {
		if (check_value($_SESSION['language_display'])) {
			$News->setLanguage($_SESSION['language_display']);
		}
	}

	// Single news
	$showSingleNews = false;
	$requestedNewsId = $_GET['subpage'];
	if (check_value($requestedNewsId) && $News->newsIdExists($requestedNewsId)) {
		$showSingleNews = true;
		$newsID = $requestedNewsId;
	}

	// Helper: decode base64 safely
	function we_safe_b64decode($value) {
		if (!is_string($value) || $value === '') return '';
		$decoded = base64_decode($value, true);
		return ($decoded === false) ? $value : $decoded; // si no es base64 válido, devuelve original
	}

	// News list
	$i = 0;
	foreach ($cachedNews as $newsArticle) {
		if ($showSingleNews) {
			if ($newsArticle['news_id'] != $newsID) continue;
		}

		$News->setId($newsArticle['news_id']);

		if ($i > mconfig('news_list_limit')) continue;

		$news_id     = $newsArticle['news_id'];
		$news_author = $newsArticle['news_author'];
		$news_date   = $newsArticle['news_date'];
		$news_url    = __BASE_URL__ . 'news/' . $news_id . '/';

		// ✅ FIX: decode base64 title by default
		$news_title = we_safe_b64decode($newsArticle['news_title']);

		// ✅ FIX: translated news title (also base64)
		if (config('language_switch_active', true)) {
			if (check_value($_SESSION['language_display']) && isset($newsArticle['translations']) && is_array($newsArticle['translations'])) {
				if (array_key_exists($_SESSION['language_display'], $newsArticle['translations'])) {
					$news_title = we_safe_b64decode($newsArticle['translations'][$_SESSION['language_display']]);
				}
			}
		}

		// ✅ Make title safe for HTML output
		$news_title_safe = htmlspecialchars($news_title, ENT_QUOTES, 'UTF-8');

		if (mconfig('news_short')) {
			if ($showSingleNews) {
				$loadNewsCache = $News->LoadCachedNews();
			} else {
				$loadNewsCache = $News->LoadCachedNews(true);
				$loadNewsCache .= '<br /><a href="' . $news_url . '" class="button-small flex-c-c">' . lang('news_txt_3') . '</a>';
			}
		} else {
			$loadNewsCache = $News->LoadCachedNews();
		}

		if (mconfig('news_expanded') > $i) {

			if ($showSingleNews) {

				// single news (expanded)
				echo '<div class="row news-article">';
					echo '<div class="news-content-title-title">';
						echo '<h3><a href="' . $news_url . '">' . $news_title_safe . '</a></h3>';
						echo '<div class="news-text-text">';
							echo $loadNewsCache;
						echo '</div>';
						echo '<span class="time-news-news">' . langf('news_txt_1', array($news_author, date("F j, Y", $news_date))) . '</span>';
					echo '</div>';
				echo '</div>';

			} else {

				// news list

				$newsImagePath = __PATH_TEMPLATE_IMG__ . 'news/' . $news_id . '.jpg';
				if (!file_exists(__PATH_TEMPLATE_ROOT__ . 'img/news/' . $news_id . '.jpg')) {
					$newsImagePath = __PATH_TEMPLATE_IMG__ . 'img_news.jpg';
				}

				// expanded news
				echo '<div class="last-news-content">';
					echo '<div class="img-news">';
						echo '<a href="' . $news_url . '"><img src="' . $newsImagePath . '" /></a>';
					echo '</div>';

					echo '<div class="news-content-title">';
						echo '<h3><a href="' . $news_url . '">' . $news_title_safe . '</a></h3>';
					echo '</div>';

					echo $loadNewsCache;
					echo '<br>';
					echo '<span class="news-date pull-right">' . langf('news_txt_1', array($news_author, date("F j, Y", $news_date))) . '</span>';
				echo '</div>';
			}

		} else {

			// non expanded news
			echo '<div class="last-news-content">';
				echo '<div class="news-content-title">';
					echo '<h3><a href="' . $news_url . '">' . $news_title_safe . '</a></h3>';
				echo '</div>';
			echo '</div>';

		}

		$i++;
	}

} catch (Exception $ex) {
	message('warning', $ex->getMessage());
}
