Location: PHPKode > projects > NAR contents manager > software/libs/controlador.php
<?
//Implementación de la clase controlador del patrón C-V-M
require_once("BD.php");

//variables de conexión a la BD para cambiarlas solo aquí en caso de variar la localización o parámetros de ésta
//$servidor="localhost";
//$usuario="root";
//$password="";
//$basedatos="gestornoticias";

function login ($username,$password,&$cticket){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
	$BDcontrolador->query("SELECT password FROM user where username='$username'");
	@$BDcontrolador->one_result("0","password");
	
	if ($BDcontrolador->finalresult == "") return "2"; //no existe el usuario
	else                                               //existe el usuario
	{
		if ($BDcontrolador->finalresult == $password){
			//asignamos un ticket al usuario autentificado si no tiene sesión abierta
			$BDcontrolador->query("SELECT username FROM ticket where username='$username'");
			@$BDcontrolador->one_result("0","username");
			if ($BDcontrolador->finalresult != "") return '3'; //El usuario ya tiene sesión abierta
			else{
				$BDcontrolador->query("INSERT INTO ticket(username)VALUES('$username')");
				//devolvemos el ticket asignado para futuras comprobaciones
				$BDcontrolador->query("SELECT id_ticket FROM ticket where username='$username'");
				@$BDcontrolador->one_result("0","id_ticket");
				$cticket=$BDcontrolador->finalresult;
				return "1";  //la contraseña es correcta y el usuario no tenía sesión abierta
			}
		}
		else return "0";     //la contraseña no es correcta
	}
}

function check_userticket($ticket){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
	$BDcontrolador->query("SELECT id_ticket FROM ticket where id_ticket='$ticket'");
	@$BDcontrolador->one_result("0","id_ticket");
	if ($BDcontrolador->finalresult == "") return "0";  //el usuario no tiene ticket válido 
	else return "1";         //el usuario tiene ticket válido 
}


function logout($ticket){
	if ($ticket==""){
		return '0'; //No existe el ticket (es un caso prácticamente imposible, ya que solo se ofrece la opción de salir cuando ya estás dentro, y eso implica tener un ticket asignado)
	}else{
		$BDcontrolador = new db_handling;
		$BDcontrolador->connection();
		$BDcontrolador->query("DELETE FROM ticket WHERE id_ticket='$ticket'");
		return '1'; //Existe el ticket
	}
}

function open($itemtype,$itemkey){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();

	$BDcontrolador->query("SELECT * FROM $itemtype");
	//sacamos el nombre del campo que contiene la clave primaria (sabemos que es siempre el primero)
	$BDcontrolador->field_name('0'); 
	$BDcontrolador->query("SELECT * FROM $itemtype where $BDcontrolador->finalresult='$itemkey'"); //nos quedamos  con la fila de la tabla que corresponde al objeto que se solicita.
	$BDcontrolador->all_results_into_array();
	
		
	if ($itemtype=='user')
	{
		$objeto = new user();
		$objeto->fill_user($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3],$BDcontrolador->arrayfinalresult[4],$BDcontrolador->arrayfinalresult[5],$BDcontrolador->arrayfinalresult[6],$BDcontrolador->arrayfinalresult[7]);
	}
	if ($itemtype=='news') 
	{
		$objeto = new news();
		$objeto->fill_news($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3],$BDcontrolador->arrayfinalresult[4],$BDcontrolador->arrayfinalresult[5],$BDcontrolador->arrayfinalresult[6]);
	}
	if ($itemtype=='report') 
	{
		$objeto = new report();
		$objeto->fill_report($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3],$BDcontrolador->arrayfinalresult[4],$BDcontrolador->arrayfinalresult[5],$BDcontrolador->arrayfinalresult[6]);
	}
	if ($itemtype=='material') 
	{
		$objeto = new material();
		$objeto->fill_material($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3],$BDcontrolador->arrayfinalresult[4],$BDcontrolador->arrayfinalresult[5]);
	}
	if ($itemtype=='structure') 
	{
		$objeto = new structure();
		$objeto->fill_structure($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3],$BDcontrolador->arrayfinalresult[4],$BDcontrolador->arrayfinalresult[5],$BDcontrolador->arrayfinalresult[6],$BDcontrolador->arrayfinalresult[7],$BDcontrolador->arrayfinalresult[8]);
	}
	if ($itemtype=='structdivision') 
	{
		$objeto = new structdivision();
		$objeto->fill_structdivision($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3],$BDcontrolador->arrayfinalresult[4]);
	}
	
	if ($itemtype=='publication') 
	{
		$objeto = new publication();
		$objeto->fill_publication($BDcontrolador->arrayfinalresult[0],$BDcontrolador->arrayfinalresult[1],$BDcontrolador->arrayfinalresult[2],$BDcontrolador->arrayfinalresult[3]);
	}

	return $objeto;
}


function create($itemtype,$item,&$clave){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	if ($itemtype=='user')
	{
		$BDcontrolador->query("insert into user (username,name,surnames,address,email,rol,telephone,password) values ('$item->username','$item->name','$item->surnames','$item->address','$item->email','$item->rol','$item->telephone','$item->password')");

		$clave="";

	}
	if ($itemtype=='news') 
	{
		$BDcontrolador->query("insert into news (matter,state,ndate,title,contents,comment) values ('$item->matter','$item->state','$item->ndate','$item->title','$item->contents','$item->comment')");

		$BDcontrolador->query("select id_news from news where matter='$item->matter' and state='$item->state' and ndate='$item->ndate' and title='$item->title' and contents='$item->contents' and comment='$item->comment'");

		$BDcontrolador->one_result('0','id_news');
		$clave=$BDcontrolador->finalresult;
		

	}
	if ($itemtype=='report') 
	{
		$BDcontrolador->query("insert into report (title,type,state,rdate,contents,username) values ('$item->title','$item->type','$item->state','$item->rdate','$item->contents','$item->username')");

		$clave="";

	}
	if ($itemtype=='material') 
	{
		$BDcontrolador->query("insert into material (descriptor,type,mdate,attachednote,contents,username) values ('$item->descriptor','$item->type','$item->mdate','$item->attachednote','$item->contents','$item->username')");

		$clave="";

	}
	if ($itemtype=='structure') 
	{
		$BDcontrolador->query("insert into structure (name,state,type,category,pagewi,pagehe,marginsize,username) values ('$item->name','$item->state','$item->type','$item->category','$item->pagewi','$item->pagehe','$item->marginsize','$item->username')");

		$clave="";

	}
	if ($itemtype=='structdivision') 
	{
		$BDcontrolador->query("insert into structdivision (structdivisionname,toppos,leftpos,height,width) values ('$item->structdivisionname','$item->toppos','$item->leftpos','$item->height','$item->width')");

		$clave="";

	}
	
	if ($itemtype=='publication') 
	{
		$BDcontrolador->query("insert into publication (publicationname,pdate,sections,state) values ('$item->publicationname','$item->pdate','$item->sections','$item->state')");

		$clave="";

	}

}


function erase($itemtype,$itemkey){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	if ($itemtype=='user')
	{
		//Borramos el usuario y todas sus ocurrencias en la tabla que lo asocia con una noticia (delete cascade)
		$BDcontrolador->query("delete from user where username='$itemkey'");
		$BDcontrolador->query("delete from mnewsuser where username='$itemkey'");
	}
	if ($itemtype=='news') 
	{
		$BDcontrolador->query("delete from news where id_news='$itemkey'");
		$BDcontrolador->query("delete from mmaterialnews where id_news='$itemkey'");
		$BDcontrolador->query("delete from mnewspublication where id_news='$itemkey'");
		$BDcontrolador->query("delete from mnewsuser where id_news='$itemkey'");
	}
	if ($itemtype=='report') 
	{
		$BDcontrolador->query("delete from report where id_report='$itemkey'");
		$BDcontrolador->query("delete from mmaterialreport where id_report='$itemkey'");
	}
	if ($itemtype=='material') 
	{
		$BDcontrolador->query("delete from material where descriptor='$itemkey'");
		$BDcontrolador->query("delete from mmaterialreport where descriptor='$itemkey'");
		$BDcontrolador->query("delete from mmaterialnews where descriptor='$itemkey'");
	}
	if ($itemtype=='structure') 
	{
		$BDcontrolador->query("delete from structure where id_structure='$itemkey'");
		$BDcontrolador->query("delete from mstructurepublication where id_structure='$itemkey'");
	}
	if ($itemtype=='structdivision') 
	{
		$BDcontrolador->query("delete from structdivision where structdivisionname='$itemkey'");
	}
	if ($itemtype=='publication') 
	{
		$BDcontrolador->query("delete from publication where publicationname='$itemkey'");
		$BDcontrolador->query("delete from mstructurepublication where publicationname='$itemkey'");
		$BDcontrolador->query("delete from mnewspublication where publicationname='$itemkey'");
	}

}

function erase_mat($itemtype,$itemkey,$matkey){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();

	if ($itemtype=='news') 
	{
		$BDcontrolador->query("delete from mmaterialnews where id_news='$itemkey' and descriptor='$matkey'");
	}
	if ($itemtype=='report') 
	{
		$BDcontrolador->query("delete from mmaterialreport where id_report='$itemkey' and descriptor='$matkey'");
	}

}


function modify($itemtype,$olditemkey,$newitem){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	if ($itemtype=='user')
	{
		$BDcontrolador->query("update user set username='$newitem->username',name='$newitem->name',surnames='$newitem->surnames',address='$newitem->address',email='$newitem->email',rol='$newitem->rol',telephone='$newitem->telephone',password='$newitem->password' where username='$olditemkey' ");

	}
	if ($itemtype=='news') 
	{
		$BDcontrolador->query("update news set matter='$newitem->matter',state='$newitem->state',ndate='$newitem->ndate',title='$newitem->title',contents='$newitem->contents',comment='$newitem->comment' where id_news='$olditemkey' ");
		
	}
	if ($itemtype=='report') 
	{
		$BDcontrolador->query("update report set title='$newitem->title',type='$newitem->type',state='$newitem->state',rdate='$newitem->rdate',contents='$newitem->contents',username='$newitem->username' where id_report='$olditemkey' ");

	}
	if ($itemtype=='material') 
	{
		$BDcontrolador->query("update material set descriptor='$newitem->descriptor',type='$newitem->type',mdate='$newitem->mdate',attachednote='$newitem->attachednote',contents='$newitem->contents',username='$newitem->username' where descriptor='$olditemkey' ");

	}
	if ($itemtype=='structure') 
	{
		$BDcontrolador->query("update structure set name='$newitem->name',state='$newitem->state',type='$newitem->type',category='$newitem->category',pagewi='$newitem->pagewi',pagehe='$newitem->pagehe',marginsize='$newitem->marginsize',username='$newitem->username' where id_structure='$olditemkey' ");

	}
	if ($itemtype=='structdivision') 
	{
		$BDcontrolador->query("update structdivision set structdivisionname='$newitem->structdivisionname',toppos='$newitem->toppos',leftpos='$newitem->leftpos',height='$newitem->height',width='$newitem->width' where structdivisionname='$olditemkey' ");

	}
	
	if ($itemtype=='publication') 
	{
		$BDcontrolador->query("update publication set publicationname='$newitem->publicationname',pdate='$newitem->pdate',sections='$newitem->sections',state='$newitem->state' where publicationname='$olditemkey' ");

	}
}


function assignnews_to_pub($news,$pubkey){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	$BDcontrolador->query("insert into mnewspublication (id_news,publicationname) values ('$news->id_news','$pubkey')");
}

function addmaterial_from_DB($itemtype,$item,$materialkey){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	if ($itemtype=='news') 
	{
		$BDcontrolador->query("insert into mmaterialnews (descriptor,id_news) values ('$materialkey','$item->id_news')");
	}
	if ($itemtype=='report') 
	{
		$BDcontrolador->query("insert into mmaterialreport (descriptor,id_report) values ('$materialkey','$item->id_report')");

	}
}


function addmaterial_from_file($itemtype,$item,$material,$filetemplocation){
	$clave="";
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    create('material',$material,$clave);
	
	//Vamos a guardar el fichero de material que se ha subido en un lugar seguro
	$nuevodirectorio="./plantilla/htmls/materiales/";
	$separador="/";
	$partes=explode($separador,$material->contents);
	$elems=count($partes);
	$pos=$elems-1;
	$nuevofichero=$partes[$pos];
	
	//Metemos el nuevo material y la asociación con el item correspondiente (noticia o informe)
	
	if (copy($filetemplocation, $nuevodirectorio . $nuevofichero)){
		
		
		$BDcontrolador->query("insert into material (descriptor,type,mdate,attachednote,contents,username) values ('$material->descriptor','$material->type','$material->mdate','$material->attachednote','$material->contents','$material->username')");
		
		if ($itemtype=='news') 
		{
			$BDcontrolador->query("insert into mmaterialnews (descriptor,id_news) values ('$material->descriptor','$item->id_news')");
		}
		if ($itemtype=='report') 
		{
			$BDcontrolador->query("insert into mmaterialreport (descriptor,id_report) values ('$material->descriptor','$item->id_report')");

		}
	}
} 


function circulate_publication($publicationkey){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	$BDcontrolador->query("update publication set state='publicar' where publicationname='$publicationkey' ");
}


function assignstruct_to_pubication($pubkey,$struct,$fromsection,$section){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	$BDcontrolador->query("insert into mstructurepublication (id_structure,publicationname,isfromsection,section) values ('$struct->id_structure','$pubkey','$fromsection','$section')");	

}


function assignnews_to_user($newskey,$username){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	$BDcontrolador->query("insert into mnewsuser (id_news,username) values ('$newskey','$username')");	

}


function show_list($itemtype,$filters){
	$BDcontrolador = new db_handling;
	$BDcontrolador->connection();
    
	if ($filters!=""){
		$numele=count($filters);
	}else{
		$numele='0';
	}

	if ($numele==0) $sentencia="select * from $itemtype";
	else{
		$sentencia= "select * from $itemtype where ";
		for ($i=0;$i<$numele;$i++){
			if ($i==$numele - 1) $sentencia=$sentencia.$filters[$i];
			else $sentencia=$sentencia.$filters[$i].' and ';
		}
	}
	
	$BDcontrolador->query($sentencia);
	$BDcontrolador->numrows();
	$num = $BDcontrolador->finalresult;

	$BDcontrolador->field_name(0);
	$claveprimaria = $BDcontrolador->finalresult;
	$resultado="";
		
	for ($i=0;$i<$num;$i++){
		$BDcontrolador->one_result($i,$claveprimaria);
		$resultado[$i]=$BDcontrolador->finalresult;
	}
	return $resultado;  //vector con las claves primarias de los elementos encontrados con esas características

}  

?>
Return current item: NAR contents manager