<?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
 */

try {
	
	// 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
	$requestedNewsId = $_GET['subpage'];
	if(check_value($requestedNewsId) && $News->newsIdExists($requestedNewsId)) {
		$showSingleNews = true;
		$newsID = $requestedNewsId;
	}
	
	// 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;
		if($i > 8) continue;
		
		$news_id = $newsArticle['news_id'];
		$news_title = base64_decode($newsArticle['news_title']);
		$news_author = $newsArticle['news_author'];
		$news_date = $newsArticle['news_date'];
		$news_url = __BASE_URL__.'news/'.$news_id.'/';
		$news_type = $newsArticle['news_type'];
		
		// translated news title
		if(config('language_switch_active',true)) {
			if(check_value($_SESSION['language_display']) && is_array($newsArticle['translations'])) {
				if(array_key_exists($_SESSION['language_display'], $newsArticle['translations'])) {
					$news_title = $newsArticle['translations'][$_SESSION['language_display']];
				}
			}
		}
		
		if(mconfig('news_short')) {
			if($showSingleNews) {
				$loadNewsCache = $News->LoadCachedNews();
			} else {
				$loadNewsCache = $News->LoadCachedNews(true);
				//$loadNewsCache .= '</br><div class="news-info"><a href="'.$news_url.'"class="read-more button">' . lang('news_txt_3') . '</a></div>';
			}
		} else {
			$loadNewsCache = $News->LoadCachedNews();
		}
		echo '<div class="news">';
			echo '<div class="">';
			    if($news_type == 1){
				    echo '<h3 class="row news-article"> <img src="https://mu-kyniem.com/templates/Skybow/img/huongdan7.png" alt="" style="width: 50px;">   <a  style="text-transform: none;font-size: 16px;color: #fff;" href="'.$news_url.'">'.$news_title.'</a></h3>';
			    } else  if($news_type == 3){
				    echo '<h3 class="row news-article"> <img src="https://mu-kyniem.com/templates/Skybow/img/thongbao.png" alt="" style="width: 50px;">   <a  style="text-transform: none;font-size: 16px;color: #fff;" href="'.$news_url.'">'.$news_title.'</a></h3>';
			    }
			    else {
			        echo '<h3 class="row news-article"> <img src="https://mu-kyniem.com/templates/Skybow/img/event.png" alt="" style="width: 50px;">   <a  style="text-transform: none;font-size: 16px;color: #fff;" href="'.$news_url.'">'.$news_title.'</a></h3>';
			    }
				
			echo '</div>';
			if(mconfig('news_expanded') > $i && $showSingleNews) {
				echo '<div>';
					echo $loadNewsCache;
				echo '</div>';
			}
		echo '</div>';
		$i++;
	}

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