<?php
/**
 * WebEngine CMS
 * https://webenginecms.org/
 * 
 * @version 1.2.6
 * @author Lautaro Angelico <http://lautaroangelico.com/>
 * @copyright (c) 2013-2025 Lautaro Angelico, All Rights Reserved
 * 
 * Licensed under the MIT license
 * http://opensource.org/licenses/MIT
 */
?>
<h1 class="page-header">Manage News</h1>
<?php
$News = new News();

if($News->isNewsDirWritable()) {

	# News detele
	if(isset($_POST['delete_news'])) {
		$deleteNews = $News->removeNews($_POST['delete_news']);
		nexoraNewsStudioDeleteMeta($_POST['delete_news']);
		$News->cacheNews();
		$News->updateNewsCacheIndex();
		if($deleteNews) {
			redirect(1, 'admincp/?module=managenews');
		} else {
			message('error','Invalid news ID');
		}
	}
	
	# News translation delete
	if(isset($_POST['delete_translation']) && isset($_POST['language'])) {
		try {
			$News->setId($_POST['delete_translation']);
			$News->setLanguage($_POST['language']);
			$News->deleteNewsTranslation();
			$News->updateNewsCacheIndex();
			redirect(1, 'admincp/?module=managenews');
		} catch(Exception $ex) {
			message('error', $ex->getMessage());
		}
	}
	
	# News cache
	if(isset($_POST['refresh_news_cache']) && $_POST['refresh_news_cache'] == 1) {
		$cacheNews = $News->cacheNews();
		$News->updateNewsCacheIndex();
		if($cacheNews) {
			message('success','News successfully cached');
		} else {
			message('error','There are no news to cache.');
		}
	}
	
	$news_list = $News->retrieveNews();
	if(is_array($news_list)) {
		
		foreach($news_list as $row) {
			$newsMeta = nexoraNewsStudioGet($row['news_id']);
			$newsCover = isset($newsMeta['cover']) ? trim($newsMeta['cover']) : '';
			if($newsCover !== '' && !filter_var($newsCover, FILTER_VALIDATE_URL)) {
				$newsCover = __PATH_TEMPLATE_IMG__ . ltrim($newsCover, '/');
			}
			
			$News->setId($row['news_id']);
			
			echo '<div class="panel panel-default">';
				echo '<div class="panel-heading">';
					echo '<span class="nex-news-admin-badge nex-news-admin-badge--'.htmlspecialchars($newsMeta['type'] ?? 'anuncio').'">'.htmlspecialchars(strtoupper($newsMeta['type'] ?? 'ANUNCIO')).'</span> ';
					echo '<a class="nex-news-admin-title" href="'.__BASE_URL__.'news/'.$row['news_id'].'/" target="_blank">'.htmlspecialchars($row['news_title'], ENT_QUOTES, 'UTF-8').'</a>';
					echo '<form method="post" action="'.admincp_base('managenews').'" class="pull-right" style="margin:0" onsubmit="return confirm(\'¿Eliminar esta noticia?\')"><input type="hidden" name="delete_news" value="'.(int)$row['news_id'].'"><button class="btn btn-danger btn-xs" type="submit"><i class="fa fa-trash"></i> delete</button></form>';
					echo '<a class="btn btn-warning btn-xs pull-right" style="margin-right:5px;" href="'.admincp_base("editnews&id=".$row['news_id']).'"><i class="fa fa-edit"></i> edit</a>';
					echo '<a class="btn btn-xs btn-default pull-right" style="margin-right:5px;" href="'.admincp_base("addnewstranslation&id=".$row['news_id']).'"><i class="fa fa-plus"></i> Add Translation</a>';
				echo '</div>';
				echo '<div class="panel-body">';
					echo '<div class="row">';
						if($newsCover !== '') echo '<div class="col-xs-12"><div class="nex-news-admin-cover" style="background-image:url(\''.htmlspecialchars($newsCover, ENT_QUOTES, 'UTF-8').'\')"></div></div>';
						echo '<div class="col-xs-6">';
							echo '<table class="table">';
								echo '<tr>';
									echo '<th>News Id:</th>';
									echo '<td>'.$row['news_id'].'</td>';
								echo '</tr>';
								echo '<tr>';
									echo '<th>Author:</th>';
									echo '<td>'.$row['news_author'].'</td>';
								echo '</tr>';
								echo '<tr>';
									echo '<th>Date:</th>';
									echo '<td>'.date("Y-m-d H:i",$row['news_date']).'</td>';
								echo '</tr>';
							echo '</table>';
							echo '<p class="nex-news-admin-excerpt">'.htmlspecialchars($newsMeta['excerpt'] ?? 'Sin resumen editorial.', ENT_QUOTES, 'UTF-8').'</p>';
						echo '</div>';
						echo '<div class="col-xs-6">';
							echo 'Translations:';
							
							$newsTranslations = $News->getNewsTranslationsDataList();
							if(is_array($newsTranslations)) {
								echo '<ul>';
									foreach($newsTranslations as $translation) {
										echo '<li>[<span style="color:red;">'.$translation['news_language'].'</span>] '.base64_decode($translation['news_title']).' <a href="'.admincp_base('editnewstranslation&id='.$translation['news_id'].'&language='.$translation['news_language']).'" class="btn btn-xs btn-default">edit</a> <form method="post" action="'.admincp_base('managenews').'" style="display:inline"><input type="hidden" name="delete_translation" value="'.(int)$translation['news_id'].'"><input type="hidden" name="language" value="'.htmlspecialchars($translation['news_language'], ENT_QUOTES, 'UTF-8').'"><button type="submit" class="btn btn-xs btn-default">delete</button></form></li>';
									}
								echo '</ul>';
							}
						echo '</div>';
					echo '</div>';
				echo '</div>';
			echo '</div>';
			
		}
		
	}
	
	echo '<form method="post" action="'.admincp_base('managenews').'"><input type="hidden" name="refresh_news_cache" value="1"><button type="submit" class="btn btn-success">UPDATE NEWS CACHE</button></form>';

} else {
	message('error','The news cache folder is not writable.');
}

?>
