viernes, 29 de mayo de 2009

Practica 6 Código

delNoticia.php


<?php
$accion=$_GET['accion'];
if($accion == 'Borrar'){
$id = "borrar";
}elseif($accion == 'Modificar'){
$id = "modificar";
}
?>
<head>
<title>Borrar Noticia</title>
<meta content="text/html; charset=[b]iso-8859-1[/b]" http-equiv="Content-Type" />
</head>

<body><!-- el formulario -->
<form name="noticia" id="noticia" action="" >
<blockquote>
<p class="formTitl">Seleccione la noticia que desee <?=$id?>:<br />
<select name="id">;
<?php
$xml = simplexml_load_file( 'noticias.xml' );
$result = $xml->xpath("//noticia");
foreach ($result as $noticia){
echo '<option value="'.$noticia["id"].'" >'.utf8_decode($noticia->titulo).'</option>';
}?>
</select>
<input type="submit" id="<?=$id?>" class="button" name="<?=$id?>" value="<?=$accion?>" />
</p>
</blockquote>
</form>
</body>

addNoticia.php


<html>
<head>
<title>Add Noticia</title>
</head>

<?php
$notID = $_GET['id'];
$notTitulo="";
$notDesc="";
$notCuerpo="";
$notAutor="";
$notFecha="";
$notCategoria="";
$categorias = array("Discos", "El Grupo", "Conciertos", "Otros");
if(isset($notID)){
$xml = simplexml_load_file( "noticias.xml" );
$result = $xml->xpath("//noticia[@id=".$notID."]");
foreach ($result as $noticia){
$notTitulo = utf8_decode($noticia->titulo);
$notDesc = utf8_decode($noticia->descripcion);
$notCuerpo = utf8_decode($noticia->cuerpo);
$notAutor = utf8_decode($noticia->autor);
$notFecha = $noticia->fecha;
$notCategoria = utf8_decode($noticia->categoria);
}
}?>

<body>
<form name="noticia" id="noticia" action="" >
<blockquote>
<p class="formTitl">T&iacute;tulo de la Noticia<br /><input type="text" name="notTitulo" size="50" value="<?=$notTitulo?>"/></p>
<p class="formTitl">Breve descripci&oacute;n: <br /><textarea name="notDesc" rows="5" cols="75"><?=$notDesc?></textarea></p>
<p class="formTitl">Cuerpo de la Noticia<br /><textarea name="notCuerpo" rows="10" cols="75"><?=$notCuerpo?></textarea></p>
<p class="formTitl">Autor: <input type="text" name="notAutor" size="20" value="<?=$notAutor?>"/></p>
<input type="hidden" name="notFecha" value="<?=$notFecha?>" />
<input type="hidden" name="notID" value="<?=$notID?>" />
<p class="formTitl">Categor&iacute;a<br />
<select name="notCategoria">
<?php
foreach ($categorias as $categoria){
echo '<option value="'.$categoria.'" ';
if(!strcmp($categoria, $notCategoria)){
echo 'selected="selected" ';
}
echo '>'.$categoria.'</option>';
}?>
</select>
<input type="submit" id="escribir" name="escribir" class="button" value="Enviar" />
</p>
</blockquote>
</form>
</body>
</html>

borrarNoticia.php


<?php

$notID= $_POST['id'];
$xml = simplexml_load_file( 'noticias.xml' );
$result = $xml->xpath("//noticia[@id!=".$notID."]");
$noticiones="<noticias>";
foreach($result as $new){
$noticiones = $noticiones.$new->asXML();
}
$noticiones = $noticiones."</noticias>";
$xml = simplexml_load_string($noticiones);
if(!$fp=fopen("noticias.xml","w+")) echo "No se ha podido abrir el fichero";
else{
file_put_contents("noticias.xml",$xml->asXML());
if(fclose($fp)) echo $notID;
}

?>

escribirNoticia.php


<?php
// comprobamos que el formulario no envie campos vacios
if(!empty($_POST['notTitulo']) && $_POST['notCuerpo'] && $_POST['notAutor'] && $_POST['notDesc'] && $_POST['notCategoria']){

// creamos las variables y les asignamos los valores a insertar
$notTitulo = utf8_encode($_POST['notTitulo']);
$notFecha = utf8_encode($_POST['notFecha']);
if(empty($notFecha)){
$notFecha= date("r");
}
$notAutor = utf8_encode($_POST['notAutor']);
$notCategoria = utf8_encode($_POST['notCategoria']);
$notDescripcion = utf8_encode($_POST['notDesc']);
$notCuerpo = utf8_encode($_POST['notCuerpo']);

// Aquí insertamos el valor del los datos al final de nuestro XML.
//En primer lugar, deberíamos ver cual es el último id que estamos usando
//en nuestra base de datos xml, o cual estamos sustituyendo
$notID=$_POST['notID'];
$xml = simplexml_load_file( 'noticias.xml' );
if(empty($notID)){
if(!$result = $xml->xpath("//noticia/@id")){
$fed = 0;
}else{
$max=0;
foreach($result as $var){
if($max<(int)$var['id']){
$max=(int)$var['id'];
}
}
$fed = $max;
}
$notID = $fed + 1;
}else{
$result = $xml->xpath("//noticia[@id!=".$notID."]");
$noticiones="<noticias>";
foreach($result as $new){
$noticiones = $noticiones.$new->asXML();
}
$noticiones = $noticiones."</noticias>";
$xml = simplexml_load_string($noticiones);
}

//Añadimos la noticia con ese id.
$noticia = $xml->addChild('noticia');
$noticia['id']= $notID;
$noticia->titulo = $notTitulo;
$noticia->fecha = $notFecha;
$noticia->autor = $notAutor;
$noticia->categoria = $notCategoria;
$noticia->descripcion = $notDescripcion;
$noticia->cuerpo = $notCuerpo;
$noticia->enlace = 'leerNoticia.php?id='.$notID;

//Y por último, lo guardamos en el archivo.
if(!$fp=fopen("noticias.xml","w+")) echo "No se ha podido abrir el fichero";
else{
file_put_contents("noticias.xml",$xml->asXML());
if(!fclose($fp)) {
echo "No se ha podido cerrar el fichero";
// enviamos un mensaje de exito
}else{
echo $notID;
}
}
}else{
// si el formulario envia algun campo vacio
// enviamos un mensaje de error
echo "Debe llenar todos los campos del formulario";
}
?>

Cliente


<html>
<head>
<title>Gestor de Noticias Hermes 0.5</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
var identificador = "";
$('#escribir').live("click", function(){
var datos = $("#noticia").serialize();
$.ajax({
type: "POST",
url: "escribirNoticia.php",
data: datos,
success: function(msg){
$("#editor").load("addNoticia.php?id="+msg);
$("#previsor").load("leerNoticia.php?id="+msg);
identificador = "id="+msg;
},
error: function(msg){
$("#editor").html("<h2>Error: </h2>"+msg);
}
});
return false;
});

$('#borrar').live("click", function(){
var datos = $("#noticia").serialize();
$.ajax({
type: "POST",
url: "borrarNoticia.php",
data: datos,
success: function(msg) {
$('#editor').html('<p>Los datos de la noticia '+msg+' fueron borrados</p>');
},
error: function(objeto, quepaso, otroobj){
alert("ERROR!! Mecachis... Esto es lo que ha pasado: "+quepaso);
}
});
return false;
});

$('#modificar').live("click", function(){
var datos = $("#noticia").serialize();
$.ajax({
type: "GET",
url: "addNoticia.php",
data: datos,
success: function(msg) {
$('#editor').html(msg);
$("#previsor").load("leerNoticia.php?"+datos);
identificador = datos;
},
error: function(objeto, quepaso, otroobj){
alert("ERROR!! Mecachis... Esto es lo que ha pasado: "+quepaso);
}
});
return false;
});
</script>
<style type="text/css">
body{background: #000033 none repeat scroll 0 0;}
h1{display:block; background:#FF9900; font-size: 20px; font-family: Georgia, "Times New Roman", Times, serif; font-weight: bold;}
h4{font-style: italic;}
p{text-align:justify}
.noticia{border-bottom: 1px dotted #666666;}
#head {display:block; text-align:center; width:100%; height:10%;position:absolute;top:0px;}
#editor {display:block; background:LemonChiffon; width:50%; height:90%; position:absolute;top:10%; left:0; overflow:auto;}
#previsor {display:block; background:red; width:50%; height:90%; position:absolute; top:10%; left:50%; padding-left:10px; overflow;auto;}
#menu {list-style:none; margin:0; padding:0;}
#menu li {margin:2px; padding:2px; border:1px solid #CCCCCC; float:left;}
#menu li a {display:block; width:100px;padding:4px 0;text-decoration:none;text-align:center;font-size:11px;color:#FFFFFF;background-color:#000033; cursor:pointer;}
#menu li a:hover {color:#000000;background-color:#FF9900;}
</style>
</head>

<body>
<div id="head">
<ul id="menu">
<li><a onClick="$('#editor').load('delNoticia.php?accion=Modificar');">Editar una noticia</a></li>
<li><a onClick="$('#editor').load('delNoticia.php?accion=Borrar');">Borrar una noticia</a></li>
<li><a onClick="$('#editor').load('addNoticia.php'); $('#previsor').empty();identificador='';">A&ntilde;adir una noticia</a></li>
<li><a onClick="$('#previsor').load('leerNoticia.php?'+identificador);">Recargar Noticias</a></li>
</ul>
</div>
<div id="editor"></div>
<div id="previsor">
</div>
</body>
</html>

No hay comentarios:

Publicar un comentario