<div id="center-column">
<div class="top-bar">
				<h1>Cpanel - Noticias</h1>
				<div class="breadcrumbs"><a href="index.php?act=newsblogcomments" title="News Blog Comments">Noticias blog Comentarios</a></div>
			</div><br />
		  <div class="select-bar">
		    <label>
		    <h3>Administrar Noticias</h3>
		    </label>
		  </div>
<?php
if(!empty($_GET['edit'])) {

	$query = yasDB_select("SELECT * FROM news WHERE id = '{$_GET['edit']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No puede editar la noticia que no existe.</center>';
	} else {
		$news = $query->fetch_array(MYSQLI_ASSOC);
		?><div class="table">
		<img src="img/bg-th-left.gif" width="8" height="7" alt="" class="left" />
		<img src="img/bg-th-right.gif" width="7" height="7" alt="" class="right" />
		<form name="edit" method="post" action="index.php?act=news">
		<table class="listing form" cellpadding="0" cellspacing="0">
		<tr>
	        <th class="full" colspan="2">Edit - News</th>
			</tr>
			<tr>
			<td class="first" width="172"><strong>Fecha</strong></td>
			<td class="last"><input type="text" name="date" value="<?php echo $news['date'];?>"  /></td>
			</tr>
			<tr>
			<td class="first" width="172">Noticias de la entrada</strong></td>
			<td class="last"><textarea name="news_text" cols="40" rows="5"><?php echo $news['news_text'];?></textarea></td>
			</tr>
			<tr>
			<td class="first" width="172"><strong>Topic</strong></td>
			<td class="last"><input type="text" name="topic" value="<?php echo $news['topic'];?>"  /></td>
			</tr>
			<tr>
			<td class="first" width="172"></td>
			<td class="last"><input type="hidden" name="id" value="<?php echo $news['id'];?>" /><input type="submit" class="button" name="edit" value="Editar!" /></td>
			</tr>
        </form>
		</table>
	    </div>
	<?php
	}
	$query->close();
	
} elseif(isset($_POST['edit'])) {
	if(empty($_POST['date']) || empty($_POST['news_text'])) {
		echo '<center>Uno o mas campos se quedo vacio.<br />';
		echo '<a href="index.php?act=news?edit=' . $_POST['id'] . '">Haga clic aqui para volver</a></center>';
	} else {
		yasDB_update("UPDATE news SET date = '{$_POST['date']}', news_text = '{$_POST['news_text']}', topic = '{$_POST['topic']}' WHERE id = '{$_POST['id']}'",false);
		echo '<center>News Successfully edited!<br />';
		echo '<a href="index.php?act=news">Haga clic aqui para continuar</a></center>';
	}
} elseif(!empty($_GET['delete'])) {
	$query = yasDB_select("SELECT id FROM news WHERE id = '{$_GET['delete']}'");
	if($query->num_rows == 0) {
		echo '<center>No se puede eliminar la noticia de que no existe!<br />';
		echo '<a href="index.php?act=news">Haga clic aqui para volver</a></center>';
		$query->close();
		
	} else {
		$query->close();
		
		yasDB_delete("DELETE FROM news WHERE id = '{$_GET['delete']}'");
		echo '<center>La noticias se ha eliminado correctamente.<br />';
		echo '<a href="index.php?act=news">Haga clic aqui para continuar</a></center>';
	}
} elseif(isset($_POST['deletechecked'])) {
    $count = count($_POST['checkbox']);
    ?>
    <script type = "text/javascript">
        var response = confirm("Esta seguro que desea eliminar la<?php echo $count;?> Noticia(s)?");
        if (!response) {
            window.location.href = '<?php echo $setting['siteurl'];?>admin/index.php?act=news&remove=notta';
            
        }
    </script>
    <?php
    foreach ($_POST['checkbox'] as $box) {
        $query = yasDB_select("SELECT id, date, news_text, topic FROM news WHERE id = '$box'",false);
        if($query->num_rows == 0) {
            echo '<center>No se puede eliminar esta noticia que no existe!<br />';
            echo '<a href="index.php?act=news">Haga clic aqui para volver</a></center>';
        } else {
            if ($_GET['remove'] != 'notta') {
                $row = $query->fetch_array(MYSQLI_ASSOC);
                $query->close();            
                yasDB_delete("DELETE FROM news WHERE id = '$box'",false);
                ?>
                <meta http-equiv="refresh" content="0;URL=<?php echo $setting['siteurl'];?>admin/index.php?act=news" />
                <?php
            }
        } 
	}
} elseif(isset($_POST['add'])) {
      $news = yasDB_clean($_POST['news_text'],true);
	  $topic = yasDB_clean($_POST['topic']);
	  yasDB_insert("INSERT INTO news (date, news_text, topic) values ('{$_POST['date']}', '$news', '{$_POST['topic']}')");
      echo 'Noticias de la entrada con éxito añadido!<br />';
      echo '<a href="index.php?act=news">Haga clic aqui para continuar!</a>';
} else {

	if (isset($_GET['page'])) {
		$pageno = $_GET['page'];
	} else {
		$pageno = 1;
	} 
	$result = yasDB_select("SELECT count(id) FROM news",false);
	$query_data = $result->fetch_array(MYSQLI_NUM);
	$numrows = $query_data[0];
	$result->close();
	
	$rows_per_page = 15;
	$lastpage = ceil($numrows/$rows_per_page);
	$pageno = (int)$pageno;
	if ($pageno > $lastpage) {
	   $pageno = $lastpage;
	} 
	if ($pageno < 1) {
	   $pageno = 1;
	} 
	$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
	
	echo '<center>Añadir entrada de la Noticia<br/><br/>';?>	
   <form name="add" method="post" action="index.php?act=news">
   Fecha: <br /><input type="text" name="date" size="35" value="<?php echo date("D, j F Y"); ?>"><br />   
   Noticias de la entrada: <br /><center><textarea name="news_text" rows="4" cols="40"></textarea></center><br />
   Tema de la entrada: <br /><input type="text" name="topic" size="35" /><br />
   <input type="submit" class="button" value="Añadir noticias" name="add">
   </form><br /></center>
<?php   
	$query = yasDB_select("SELECT * FROM news order by id DESC $limit",false);
	if($query->num_rows == 0) {
		echo 'No hay noticias en la base de datos!';
	} else {
		?>
		<div class="table">
		<img src="img/bg-th-left.gif" width="8" height="7" alt="" class="left" />
		<img src="img/bg-th-right.gif" width="7" height="7" alt="" class="right" />
		<table class="listing" cellpadding="0" cellspacing="0">
		<tr>
						<th class="first" style="width:40px">ID</th>
						<th style="width:100px;">Tema</th>
						<th style="width:100px;">Fecha</th>
						<th >Noticias</th>
						<th style="width:40px;">Editar</th>
						<th class="last" style="width:40px;">borrar</th>
					</tr>
		<?php
		while($news = $query->fetch_array(MYSQLI_ASSOC)) {
			?><form name="deleteform" method="post" action="">
            <tr>		
			<td class="first style1"><?php echo $news['id'];?></td>
			<td style="overflow:hidden;"><?php echo $news['topic'];?></td>
			<td><?php echo $news['date'];?></td>
			<td style="overflow:hidden;"><?php echo $news['news_text'];?></td>
			<td><a href="index.php?act=news&edit=<?php echo $news['id'];?>"><font color="#cc0000">Editar</font></a></td>
		    <td class="last"><a href="index.php?act=news&delete=<?php echo $news['id'];?>" onclick="return confirm(\'Esta seguro que desea eliminar esta noticia?\')"><font color="#cc0000">borrar</font></a><br/><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $news['id'];?>"></td>
			</tr>
			<?php
		}
		$query->close();
		
		?>
		</table>
		</div>
		<div style="text-align:right;padding-right:20px;padding-top:10px;">
        <input name="deletechecked" class="button" type="submit" id="deletechecked" value="Chequear la eliminacion">
        </form>
    </div>
    <br /><div style="text-align:center;padding-bottom:10px;">
		<?php
		if ($pageno == 1) {
			echo ' PRiMERA ANTERIOR';
		} else {
			echo ' <a href="index.php?act=news&page=1">FIRST</a> ';
			$prevpage = $pageno-1;
			echo ' <a href="index.php?act=news&page=' . $prevpage . '">PREV</a> ';
		} 
		echo ' ( Pagina ' . $pageno . ' de ' . $lastpage . ' ) ';
		if ($pageno == $lastpage) {
			echo ' SIGUIENTE ULTIMA ';
		} else {
			$nextpage = $pageno+1;
			echo ' <a href="index.php?act=news&page=' . $nextpage . '">NEXT</a> ';
			echo ' <a href="index.php?act=news&page=' . $lastpage . '">LAST</a> ';
		} ?> 
	</div>
	<?php
	}
}
?>
</div>