<?
if ( $user_auth && $user_level <= -1 )
{
 $blq1 ="anunciante, descr, url, tipo, nVistos, nVisitas, filename";

 $list = true;

 if ( isset( $sins ) )
 {
	displayForm( "./?dgo=adm/publicidad.php&ins=new" );
	$list = false;
 }

 if ( isset( $smod ) ) 
 {	
	$res = @mysql_query( "SELECT id,$blq1  FROM publicidad WHERE id=$smod", $conexion );
	$datos = @mysql_fetch_array( $res );

	list( $db_id, $db_anunciante, $db_descr, $db_url, $db_tipo, $db_nVistos, $db_nVisitas, $db_filename ) =$datos;

	displayForm( "./?dgo=adm/publicidad.php&mod=$smod" );
	$list = false;
 }

 if ( isset( $mod ) )
 {
	if ( !empty($imgData_name) && $imgData_size > 0 )
		  {
/*
			$img_prop = GetImageSize($imgData);
			$db_Foto_w = $img_prop[0];
			$db_Foto_h = $img_prop[1];
*/
			$addsql_p1 = ",filename='$imgData_name'"; //, Foto_w='$db_Foto_w', Foto_h='$db_Foto_h'";

		  }	

	$sql = "UPDATE publicidad SET ".
		"anunciante='$db_anunciante' ".
		",url='$db_url' ".
		",tipo='$db_tipo' ".
		",descr='$db_descr' $addsql_p1".
		" WHERE id=$mod";
	$res = @mysql_query( $sql, $conexion ) OR die("eror");

	if ( !empty($imgData_name) && $imgData_size > 0 )
	{
		$file_size = filesize($imgData);
		$imgDEL = "db/publicidad/$mod"."_i001@*";
		echo `rm  -rf $imgDEL`;
		fwrite( fopen("db/publicidad/".$mod."_i001@$imgData_name", "wb"),
			fread(fopen($imgData,"rb"),$file_size), $file_size);
	}
	$list = true;
 }

 if( isset( $ins ) )
 {
	if ( !empty($imgData_name) && $imgData_size > 0 )
		  {
			$img_prop = GetImageSize($imgData);
/*
			$db_Foto_w = $img_prop[0];
			$db_Foto_h = $img_prop[1];
			$addsql_p1 = ",Foto_mime, Foto_w, Foto_h";
*/
			$addsql_p2 = ",'$imgData_name'";//, '$db_Foto_w', '$db_Foto_h'";
		  }	
	$sql = "INSERT INTO publicidad (FAlta, $blq1 $addsql_p1) VALUES ".
		"(NOW(),'$db_anunciante','$db_descr','$db_url','$db_tipo','0','0','$imgData_name')";

	$res = @mysql_query( $sql, $conexion ) OR die( "Error<br>".@mysql_error($conexion) );

	if ( !empty($imgData_name) && $imgData_size > 0 )
	{
		$file_size = filesize($imgData);
		$last_id = @mysql_insert_id($conexion);
		fwrite( fopen("db/publicidad/".$last_id."_i001@$imgData_name", "wb"),
			fread(fopen($imgData,"rb"),$file_size), $file_size);
	}

        echo "<h1>Publicidad insertada</h1><br><br><br>";
        echo "<p class=remark>ID asignado = ".@mysql_insert_id($conexion)."</p>";
	$list = true;
 }
 if( isset( $del ) )
 {
		$imgDEL = "db/publicidad/$del"."_i001*";
		echo `rm  -rf $imgDEL`;

	$res = @mysql_query( "DELETE FROM artistas WHERE id=$del" );
	$list = true;
 }

 if ( isset( $list ) && $list )
   displayList($conexion);

 displayMenu();
}

function displayList($cnx, $orderBy, $orderDir)
{
 $thisLOCATION = "./?dgo=adm/publicidad.php";

 $res = @mysql_query( "SELECT id, anunciante, descr, nVistos, nVisitas FROM publicidad", $cnx );

 echo "<br><br><br><table width=100% cellpadding=0 cellspacing=0 class=td_lista>";

 echo "<td class=menuL>id</td>".
      "<td class=menuL>Anunciante</td>".
      "<td class=menuL>Descripci&oacuten</td>".
      "<td class=menuL>Vistos / Visitas</td>".
      "<td class=menuL>Borrar</td>";

 while( $datos=@mysql_fetch_array($res) )
 {
  list ( $db_id, $db_anunciante, $db_descr, $db_nVistos, $db_nVisitas )= $datos;
  echo "<tr><td><a href=\"./?dgo=adm/publicidad.php&smod=$db_id\">$db_id</a></td>".
	"<td>$db_anunciante</td><td>$db_descr</td><td>$db_nVistos / $db_nVisitas</td><td><a href=\"javascript::deleteUser($db_id)\"><img src=\"images/actions/cancel.gif\" border=0></a></td></tr>";
 }
 echo "</table>";

 echo '<script language="JavaScript"> 
	function deleteUser( which )
	{
	  var submitOK = confirm("Atencion: Esta a punto de eliminar al artista.");
	  if ( submitOK )
		  document.location.href = "'.$thisLOCATION.'&del="+which;
	}
	</script>';
}

function displayMenu()
{
 echo '<hr noshade color="#800000">';
 echo '<p align="center"><a href="./?dgo=adm/publicidad.php&sins=new">Insertar nuevo banner</a></p>';
 echo '<hr noshade color="#800000">';
}

function displayForm( $form_action )
{
 global $db_id, $db_url, $db_anunciante, $db_descr, $db_tipo, $db_nVistos, $db_nVisitas, $db_filename;
 global $imgData;

 echo '<form action="'.$form_action.'" method="POST" name="register" enctype="multipart/form-data">
       <table width=100%>
	<tr><td>ID: '.$db_id.'</td><td></td></tr>
	<tr><td>Anunciante:</td><td><input type=text name=db_anunciante value="'.$db_anunciante.'"></td></tr>
	<tr><td>Descripci&oacute;n:</td><td><input type=text name=db_descr value="'.$db_descr.'"></td></tr>
	<tr><td>Tipo:</td><td><select name=db_tipo>
				<option value="H">Horizontal</option>
				<option value="V">Vertical</option>
			  </select>
		      </td></tr>
	<tr><td>Visionados:</td><td>'.$db_nVistos.'</td></tr>
	<tr><td>Visitas:</td><td>'.$db_nVisitas.'</td></tr>
	<tr><td>url Publicidad:</td><td><input name=db_url value="'.$db_url.'"></td></tr>
	<tr><td>Fichero imagen:</td><td><input name="imgData" value="" type=file></td>
	<tr><td colspan=2><img src="db/publicidad/'.$db_id.'_i001@'.$db_filename.'"></td></tr>
       </table><center><input type=submit value=Enviar></center></form>';
}
?>
