Location: PHPKode > projects > LibraryManager > include/functions.php
<?php
//versiune 5
//bsebi @ 2007-04-08 8:04 pm
require "conf/config.php";
require_once "conf/dbconnect.php";

	function load_script($script){
		global $base, $scripts;
		$scripts .= '<script type="text/javascript" src="'.$script.'"></script>';
	}
//********************************************************************************
	function imprumuta_carte($id_elev, $id_carte, $returnare){
		global $tp;
		$azi=strtotime("today");
		$sql="INSERT INTO ".$tp."imprumuturi(id_carte,id_elev,iesire,returnare,returnat) VALUES('".$id_carte."', '".$id_elev."', '".$azi."', '".$returnare."', '0')";
		if(mysql_query($sql)){
			$sql ="UPDATE ".$tp."carti SET status='1' WHERE id_carte='".$id_carte."'";
			if(mysql_query($sql)){
				return 1;
			}else{
				error_log(mysql_error());
				return 0;
			}
		}else{
			error_log(mysql_error());
			return 0;
		}
	}
//********************************************************************************
	function restituie_carte($id_elev, $id_carte){
		global $tp;
		$sql="UPDATE ".$tp."imprumuturi SET status='1' WHERE id_carte='".$id_carte."'";
		mysql_query($sql);
		$sql ="UPDATE ".$tp."carti SET status='0' WHERE id_carte='".$id_carte."'";
		mysql_query($sql);
	}
//********************************************************************************
	function modifica_elev($id_elev, $data){
		global $tp;
		$keys = array_keys($data);
		$values = array();
		$query = "UPDATE ".$tp."elevi SET ";
		foreach($keys as $key)
			$values[] = $key."='".$data[$key]."'";
		$query .= implode(', ', $values);
		$query .= " WHERE id_elev='".$id_elev."' LIMIT 1";
		$query = mysql_query($query);
		if(mysql_affected_rows() != 0){
			$error = 0;
		}else{
			$error = 1;
		}
		return array($error, mysql_error());
	}
//********************************************************************************
	function modifica_carte($id_carte, $data){
		global $tp;
		$keys = array_keys($data);
		$values = array();
		$query = "UPDATE ".$tp."carti SET ";
		foreach($keys as $key)
			$values[] = $key."='".$data[$key]."'";
		$query .= implode(', ', $values);
		$query .= " WHERE id_carte='".$id_carte."' LIMIT 1";
		mysql_query($query) or die(mysql_error());
		if(mysql_affected_rows()>0){
			$error = 0;
		}else{
			$error = 1;
		}
		return $error;
	}
//********************************************************************************
	function sterge_carte($id_carte){
		@session_start();
		$ses = array_search($id_carte, $_SESSION);
		if($ses)
			unset($_SESSION[$ses]);
		global $tp;
				
		$sql = "DELETE FROM ".$tp."carti WHERE id_carte='".$id_carte."' LIMIT 1";
		if($result = mysql_query($sql))
		    $ok = 1;
		else
		    $ok = 0;
		
		$sql = "DELETE FROM ".$tp."imprumuturi WHERE id_carte='".$id_carte."'";
		if($result = mysql_query($sql))
		    $ok = 1;
		else
		    $ok = 0;
	    return array($ok, mysql_error());
	}
	function sterge_elev($id_elev){
		@session_start();
		global $tp;
				
		$sql = "DELETE FROM ".$tp."elevi WHERE id_elev='".$id_elev."' LIMIT 1";
		if($result = mysql_query($sql))
		    $ok = 1;
		else
		    $ok = 0;
		
		$sql = "DELETE FROM ".$tp."imprumuturi WHERE id_elev='".$id_elev."'";
		if($result = mysql_query($sql))
		    $ok = 1;
		else
		    $ok = 0;
	    return array($ok, mysql_error());
	}
//********************************************************************************
	function test_fields($array){
		$ok = 1;
		$which = array();
		foreach($array as $elem=>$value){
			if(empty($value)){
				$ok = 0;
				$which[] = ucwords(str_replace("_", " ", $elem));
			}
		}
		if($ok)
			return array("ok"=>$ok);
		else
			return array("ok"=>$ok, "which"=>implode(', ', $which));
	}
//********************************************************************************
	function test_fields_elev($array){
		$ok = 1;
		$which = array();
		foreach($array as $elem=>$value){
			if(empty($value)){
				$ok = 0;
				$which[] = ucwords(str_replace("_", " ", $elem));
			}
		}
		if($ok)
			return array("ok"=>$ok);
		else
			return array("ok"=>$ok, "which"=>implode(', ', $which));
	}
//********************************************************************************
	function adaugare_carte($cod_carte, $categorie, $titlu, $nume_autor, $prenume_autor, $editura, $adresa, $nr_inventar, $limba, $locul_aparitiei,$anul_aparitiei){
		global $tp;
		$sql = "INSERT INTO ".$tp."carti(cod,categorie,titlu,nume_autor,prenume_autor,editura,anul_aparitiei,locul_aparitiei,limba,adresa,status,nr_inventar) VALUES('".$cod_carte."', '".$categorie."','".$titlu."','".$nume_autor."','".$prenume_autor."','".$editura."','".$anul_aparitiei."','".$locul_aparitiei."','".$limba."','".$adresa."', 0, '".$nr_inventar."')";
		$sql = mysql_query($sql);
		if($sql){
			return 1;
		}else{
			return 0;
		}
	}
	function adaugare_elev($nume, $prenume, $clasa, $litera_clasei, $diriginte){
		global $tp;
		$sql = "INSERT INTO ".$tp."elevi(nume,prenume,clasa,litera_clasei,diriginte) VALUES('".$nume."','".$prenume."','".$clasa."','".$litera_clasei."','".$diriginte."')";
		$sql = mysql_query($sql);
		if($sql){
			return 1;
		}else{
			return 0;
		}
	}
//********************************************************************************	

	function cauta_carte($mod=1, $exact=1, $cod_carte=0, $titlu=NULL, $nume_autor=NULL, $prenume_autor=NULL, $editura=NULL, $adresa=NULL, $nr_inventar=NULL){
	    global $tp;
	    $query = "SELECT ".(($mod==1)?"id_carte":"*")." FROM ".$tp."carti WHERE ";
	    $search_terms = array();
	    if($cod_carte)
			if($exact){
				$search_terms[] = "cod = '$cod_carte'";
			}else{
				$search_terms[] = "cod LIKE '%$cod_carte%'";
			}
			
	    if($titlu)
			if($exact){
				$search_terms[] = "titlu = '$titlu'";
			}else{
				$search_terms[] = "titlu LIKE '%$titlu%'";
			}
	    if($nume_autor)
			if($exact){
				$search_terms[] = "nume_autor = '$nume_autor'";
			}else{
				$search_terms[] = "nume_autor LIKE '%$nume_autor%'";
			}
	    if($prenume_autor)
			if($exact){
				$search_terms[] = "prenume_autor = '$prenume_autor'";
			}else{
				$serach_terms[] = "prenume_autor LIKE '%$prenume_autor%'";
			}
	    if($editura)
			if($exact){
				$search_terms[] = "editura = '$editura'";
			}else{
				$search_terms[] = "editura LIKE '%$editura%'";
			}
	    if($adresa)
			if($exact){
				$search_terms[] = "adresa = '$adresa'";
			}else{
				$search_terms[] = "adresa LIKE '%$adresa%'";
			}
	    if($nr_inventar)
			if($exact){
				$search_terms[] = "nr_inventar = '$nr_inventar'";
			}else{
				$search_terms[] = "nr_inventar LIKE '%$nr_inventar%'";
			}
	    $query .= implode(" AND ", $search_terms);
	    $query = mysql_query($query);
	    $data = array();
	    if($mod == 1)
		while($carte = mysql_fetch_assoc($query))
		    $data[] = $carte['id_carte'];
	    else
		while($carte = mysql_fetch_assoc($query))
		    $data[] = $carte;
	    return $data;
    }
//********************************************************************************
    function cauta_elev($mod=0, $exact=1, $id_elev=0, $nume=NULL, $prenume=NULL, $clasa=NULL, $litera_clasei=NULL, $diriginte=NULL){
		global $tp;
	    $query = "SELECT ".(($mod==1)?"id_elev":"*")." FROM ".$tp."elevi";
	    $search_terms = array();
	    if($id_elev)
			if($exact)
				$search_terms[] = "id_elev = '$id_elev'";
			else
				$search_terms[] = "id_elev LIKE '%$id_elev%'";
	    if($nume)
			if($exact)
				$search_terms[] = "nume = '$nume'";
			else
				$search_terms[] = "nume LIKE '%$nume%'";
	    if($prenume)
			if($exact)
		    	$search_terms[] = "prenume = '$prenume'";
			else
			    $search_terms[] = "prenume LIKE '%$prenume%'";
	    if($clasa)
			if($exact)
				$search_terms[] = "clasa = '$clasa'";
			else
				$search_terms[] = "clasa LIKE '%$clasa%'";
	    if($litera_clasei)
			$search_terms[] = "litera_clasei = '$litera_clasei'";
	    if($diriginte)
			if($exact)
				$search_terms[] = "diriginte='$diriginte'";
			else
				$search_terms[] = "diriginte LIKE '%$diriginte%'";
		if(count($search_terms))
			$query .= " WHERE ";
	    $query .= implode(" AND ", $search_terms);
	    $query = mysql_query($query);
	    
	    $data = array();
	    if($mod == 1)
			while($elev = mysql_fetch_assoc($query))
			    $data[] = $elev['id_elev'];
	    else
			while($elev = mysql_fetch_assoc($query))
		    	$data[] = $elev;
	    return $data;
    }
//**********************************************************
	function lista_carti(){
		global $tp;
		@session_start();
		if(isset($_SESSION['carti']))
			$carti = array_keys($_SESSION['carti']);
		else
			$carti = array();
		$data = array();
		if(!count($carti))
			return $data;
		else{
			$query = "SELECT * FROM ".$tp."carti WHERE id_carte='".implode("' OR id_carte='", $carti);
			$query .= "'";
			$query = mysql_query($query);
			while($row = mysql_fetch_assoc($query))
				$data[] = array($row["titlu"], $row["id_carte"]);
			return $data;
		}
	}
//**********************************************************
	function all_books($startIndex=0, $howMany=30){
		global $tp;
		if(!$startIndex)
			$startIndex = (int)0;
		$query = "SELECT * FROM ".$tp."carti LIMIT $startIndex, $howMany";
		$max = mysql_fetch_assoc(mysql_query("SELECT COUNT(id_carte) AS max FROM ".$tp."carti"));
		$query = mysql_query($query);
		$data = array();
		while($row = mysql_fetch_assoc($query)){
			$data["titlu"][] = '"'.addslashes($row['titlu']).'"';
			$data["clasificare"][] = '"'.addslashes($row['cod']).'"';
			$data["nume autor"][] = '"'.addslashes($row['nume_autor']).'"';
			$data["prenume autor"][] = '"'.addslashes($row['prenume_autor']).'"';
			$data["id_carte"][] = $row['id_carte'];
			$data["editura"][] = '"'.addslashes($row['editura']).'"';
			$data["adresa"][] = '"'.addslashes($row['adresa']).'"';
			$data["status"][] = $row['status'];
			$data["nr inventar"][] = $row['nr_inventar'];
		}
		return '{"titlu":['.implode(',',$data["titlu"]).'],"clasificare":['.implode(',',$data["clasificare"]).'],"nume autor":['.implode(',',$data["nume autor"]).'],"prenume autor":['.implode(',',$data["nume autor"]).'],"id_carte":['.implode(',',$data["id_carte"]).'],"editura":['.implode(',',$data["editura"]).'],"adresa":['.implode(',',$data["adresa"]).'],"status":['.implode(',',$data["status"]).'],"nr inventar":['.implode(',',$data["nr inventar"]).'],"max":'.$max['max'].'}';
	}
	function all_students($startIndex=0, $howMany=30){
		require_once "conf/dbconnect.php";
		require "conf/config.php";
		if(!$startIndex)
			$startIndex = (int)0;
		$query = "SELECT * FROM ".$tp."elevi LIMIT $startIndex, $howMany";
		$max = mysql_fetch_assoc(mysql_query("SELECT COUNT(id_elev) AS max FROM ".$tp."elevi"));
		$query = mysql_query($query);
		$data = array();
		while($row = mysql_fetch_assoc($query)){
			$data["nume"][] = '"'.addslashes($row['nume']).'"';
			$data["prenume"][] = '"'.addslashes($row['prenume']).'"';
			$data["clasa"][] = '"'.addslashes($row['clasa']).'"';
			$data["litera_clasei"][] = '"'.addslashes($row['litera_clasei']).'"';
			$data["id_elev"][] = $row['id_elev'];
			$data["diriginte"][] = '"'.addslashes($row['diriginte']).'"';
		}
		return '{"nume":['.implode(',',$data["nume"]).'],"prenume":['.implode(',',$data["prenume"]).'],"clasa":['.implode(',',$data["clasa"]).'],"litera_clasei":['.implode(',',$data["litera_clasei"]).'],"diriginte":['.implode(',',$data["diriginte"]).'],"id_elev":['.implode(',',$data["id_elev"]).'],"max":'.$max['max'].'}';
	}
//**********************************************************
	function get_config($row, $user=""){
		global $tp;
		if(isset($_SESSION['username']))
			$user = $_SESSION['username'];
		$query = mysql_query("SELECT * FROM {$tp}setari WHERE camp_setare='$row' AND user='$user'");
		if($query){
			if(mysql_num_rows($query)){
				$query = mysql_fetch_assoc($query);
				return $query['setare'];
			}else{
				$query = mysql_query("SELECT * FROM {$tp}setari WHERE camp_setare='$row' AND user=''");
				$query = mysql_fetch_assoc($query);
				return $query['setare'];
			}
		}else
			return false;
	}
//***********************************************************
function create_db_backup(){
	require_once 'include/mysql_backup.class.php';
	global $db_server, $db_user, $db_password, $db_db;
	$backup_obj = new MySQL_Backup();
	$backup_obj->server = $db_server;
	$backup_obj->username = $db_user;
	$backup_obj->password = $db_password;
	$backup_obj->database = $db_db;
	$backup_obj->tables = array();
	$backup_obj->drop_tables = true;
	$backup_obj->struct_only = false;
	$backup_obj->comments = true;
	$backup_obj->fname_format = 'U';
	$backup_obj->backup_dir = "include/backup/";
	$backup_obj->Execute(MSB_SAVE);
	copy("include/backup/".date("U").".sql", "include/backup/archive/".date("U").".sql");
}
//***********************************************************
$perms = array();
function set_user_perms(){
	global $smarty, $tp, $group, $perms;
	$query = mysql_query("SELECT * FROM {$tp}permisiuni WHERE grup <= $group");
	while($perm = mysql_fetch_assoc($query)){
		$perms[$perm['permisiune']] = true;
	}
	$smarty->assign($perms);
}
function update_option($key, $value, $user=""){
	global $tp;
	if(mysql_num_rows(mysql_query("SELECT * FROM {$tp}setari WHERE camp_setare='$key' AND user='$user'")) != 0){
		mysql_query("UPDATE {$tp}setari SET setare='$value' WHERE camp_setare='$key' AND user='$user'");
	}else{
		mysql_query("INSERT INTO {$tp}setari VALUES('$key', '$value', '$user')");
	}
}
function test_UsField($user,$nume,$password,$group){
	if(empty($user)||empty($nume)||empty($password)||empty($group))
		return 0;
	else 
		return 1;
}
function add_user($user,$nume,$password,$group){
	global $tp;
	$rez = mysql_query('SELECT username FROM '.$tp.'useri WHERE username="'.$user.'"') or die(mysql_error());
	if(mysql_num_rows($rez)>0)
		return 0;
	else{
		$rez = mysql_query('INSERT INTO '.$tp.'useri VALUES("'.$user.'","'.$password.'","'.$nume.'","4","'.$group.'")');
		if($rez)
			return 1;
		else
			return 2;
	}
}
	

?>
Return current item: LibraryManager