<?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">Publish News</h1>
<?php
$News = new News();
loadModuleConfigs('news');

// Check if News cache folder is writable
if($News->isNewsDirWritable()) {
	
	// Add news process::
	if(isset($_POST['news_submit'])) {
		$News->addNews($_POST['news_title'],$_POST['news_content'],$_POST['news_author'],0);
		$latestNews = $News->retrieveNews();
		if(is_array($latestNews) && isset($latestNews[0]['news_id'])) {
			nexoraNewsStudioSaveMeta($latestNews[0]['news_id'], array(
				'type'=>$_POST['news_type'] ?? 'anuncio','realm'=>$_POST['news_realm'] ?? 'both',
				'excerpt'=>$_POST['news_excerpt'] ?? '','cover'=>$_POST['news_cover'] ?? '',
				'featured'=>isset($_POST['news_featured'])
			));
		}
		$News->cacheNews();
		$News->updateNewsCacheIndex();
		redirect(1, 'admincp/?module=managenews');
	}
	
?>
	<form role="form" method="post" class="nex-news-studio-form">
		<div class="nex-news-studio-intro"><span>EDITORIAL NEXORA</span><h3>Construye una noticia con jerarquía, contexto y lectura clara.</h3><p>La portada atrae la mirada; el resumen explica por qué vale la pena entrar; el contenido desarrolla la información.</p></div>
		<div class="form-group">
			<label for="input_1">Title:</label>
			<input type="text" class="form-control" id="input_1" name="news_title" />
		</div>
		<div class="form-group">
			<label for="news_content">Contenido:</label>
			<textarea name="news_content" id="news_content"></textarea>
		</div>
		<div class="row"><div class="col-md-6 form-group"><label>Resumen para tarjetas:</label><textarea class="form-control" rows="3" maxlength="300" name="news_excerpt" placeholder="Síntesis clara de la noticia."></textarea></div><div class="col-md-6 form-group"><label>Imagen de portada:</label><input type="text" class="form-control" name="news_cover" placeholder="https://.../portada.webp o news/portada.webp"><small class="help-block">URL HTTPS o ruta relativa desde la carpeta img del template.</small></div></div>
		<div class="form-group">
			<label for="input_2">Author:</label>
			<input type="text" class="form-control" id="input_2" name="news_author" value="Administrator"/>
		</div>
		<div class="form-group">
			<label for="input_3">Tipo de Noticia:</label>
			<select class="form-control" id="input_3" name="news_type">
				<option value="anuncio">Anuncios</option>
				<option value="actualizacion">Actualizaciones</option>
				<option value="evento">Eventos</option>
				<option value="mantenimiento">Mantenimiento</option>
				<option value="comunidad">Comunidad</option>
			</select>
			<small class="help-block">Selecciona la categoría de esta noticia para que aparezca en el filtro correspondiente.</small>
		</div>
		<div class="form-group"><label>Reino:</label><select class="form-control" name="news_realm"><option value="both">Legado + Seasonal</option><option value="legacy">Legado</option><option value="seasonal">Seasonal</option></select></div>
		<label class="checkbox"><input type="checkbox" name="news_featured" value="1"> Marcar como noticia destacada</label>
		<button type="submit" class="btn btn-large btn-block btn-success" name="news_submit" value="ok">Publish</button>
	</form>
	<script>
	const useDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
	const isSmallScreen = window.matchMedia('(max-width: 1023.5px)').matches;
		
	document.addEventListener("DOMContentLoaded", function(){
		tinymce.init({
			selector: '#news_content',
			plugins: 'preview importcss searchreplace autolink autosave save directionality code visualblocks visualchars fullscreen image link media codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help charmap quickbars emoticons accordion',
			menubar: 'file edit view insert format tools table help',
			toolbar: "undo redo | accordion accordionremove | blocks fontfamily fontsize | bold italic underline strikethrough | align numlist bullist | link image | table media | lineheight outdent indent| forecolor backcolor removeformat | charmap emoticons | code fullscreen preview | save print | pagebreak anchor codesample | ltr rtl",
			promotion: false,
			license_key: 'gpl',
			toolbar_mode: 'sliding',
			contextmenu: 'link image table',
			skin: useDarkMode ? 'oxide-dark' : 'oxide',
			content_css: useDarkMode ? 'dark' : 'default',
		});
	});
	</script>
<?php	
} else {
	message('error','The news cache folder is not writable.');
}
?>
