Location: PHPKode > projects > Top Music module > Top Music 3.0.7/html/modules/topMusic/admin/lib/com/sergids/topmusic/model/AlbumAdminDAO.php
<?php
require_once("CustomFieldAdminO.php");
require_once("AlbumAdminO.php");
require_once("ChartAdminDAO.php");
class AlbumAdminDAO{
	var $db;
	var $prefix;
	function AlbumAdminDAO($db,$prefix="nuke"){
		$this->db=$db;
		$this->prefix=$prefix;
	} 
	function insertAlbum($album,$language){
		
		$title=(!get_magic_quotes_gpc())?addslashes($album->getTitle()):$album->getTitle();
		$yeardate=$album->getYeardate();
		$genreid=$album->getGenreid();
		$frontcover_file=$album->getFrontcoverFile();
		$frontcover_url=$album->getFrontcoverUrl();
		$backcover_file=$album->getBackcoverFile();
		$backcover_url=$album->getBackcoverUrl();
		$shopurl=$album->getShopurl();
		$idartist=$album->getIdartist();
		$views=$album->getCount();
		$comments=(!get_magic_quotes_gpc())?addslashes($album->getComments()):$album->getComments();
		$musicmoz_url=(!get_magic_quotes_gpc())?addslashes($album->musicmoz_url):$album->musicmoz_url;
		$musicbrainz_url=(!get_magic_quotes_gpc())?addslashes($album->musicbrainz_url):$album->musicbrainz_url;
		if(eregi("'",$album->getComments()) && !eregi("\\\'",$album->getComments()))
			$comments=addslashes($album->getComments());
		$sql="INSERT INTO ".$this->prefix."_topmusic_album (title,shopurl,yeardate";
		if($frontcover_file!="")
			$sql.=",frontcover_file";
		if($frontcover_url!="")
			$sql.=",frontcover_url";
		if($backcover_file!="")
			$sql.=",backcover_file";
		if($backcover_url!="")
			$sql.=",backcover_url";
		if($idartist!="" && is_numeric($idartist))
			$sql.=",idartist";
		if($genreid!="" && is_numeric($genreid))
			$sql.=",idgenre";
		if($views!="" && is_numeric($views))
			$sql.=",views";
		if($musicmoz_url!="")
			$sql.=",musicmoz_url";
		if($musicbrainz_url!="")
			$sql.=",musicbrainz_url";
		$sql.=") VALUES ('".$title."','".$shopurl."','".$yeardate."'";
		if($frontcover_file!="")
			$sql.=",'".$frontcover_file."'";
		if($frontcover_url!="")
			$sql.=",'".$frontcover_url."'";
		if($backcover_file!="")
			$sql.=",'".$backcover_file."'";
		if($backcover_url!="")
			$sql.=",'".$backcover_url."'";
		if($idartist!="" && is_numeric($idartist))
			$sql.=",".$idartist;
		if($genreid!="" && is_numeric($genreid))
			$sql.=",".$genreid;
		if($views!="" && is_numeric($views))
			$sql.=",".$views;
		if($musicmoz_url!="")
			$sql.=",'".$musicmoz_url."'";
		if($musicbrainz_url!="")
			$sql.=",'".$musicbrainz_url."'";
		$sql.=")";
		//echo "\n\nsql1=".$sql."<br>\n\n";
		$this->db->sql_query($sql);
		$idalbum=$this->db->sql_nextid();
		$album->setId($idalbum);
		$error=$this->db->sql_error();
		if($error['message']==""){
			//insert biography currentlang
			$sql="INSERT INTO ".$this->prefix."_topmusic_album_lang (idalbum,language,comments) 
					VALUES (".$idalbum.",'".$language."','".$comments."')";
			//echo "sql2=".$sql;
			$this->db->sql_query($sql);
		}
	}
	function updateAlbum($album,$language){
		$idalbum=$album->getId();
		$idartist=$album->getIdartist();
		$title=(!get_magic_quotes_gpc())?addslashes($album->getTitle()):$album->getTitle();
		$yeardate=$album->getYeardate();
		$genreid=$album->getGenreid();
		$frontcover_file=$album->getFrontcoverFile();
		$frontcover_url=$album->getFrontcoverUrl();
		$backcover_file=$album->getBackcoverFile();
		$backcover_url=$album->getBackcoverUrl();
		$shopurl=$album->getShopurl();
		$views=$album->getCount();
		$comments=(!get_magic_quotes_gpc())?addslashes($album->getComments()):$album->getComments();
		$sql="UPDATE ".$this->prefix."_topmusic_album SET title='".$title."',shopurl='".$shopurl."',yeardate='".$yeardate."'";
		if($frontcover_file!="")
			$sql.=",frontcover_file='".$frontcover_file."'";
		$sql.=",frontcover_url='".$frontcover_url."'";
		if($backcover_file!="")
			$sql.=",backcover_file='".$backcover_file."'";
		$sql.=",backcover_url='".$backcover_url."'";
		if($idartist!="" && is_numeric($idartist))
			$sql.=",idartist=".$idartist;
		else
			$sql.=",idartist=NULL";
		if($genreid!="" && is_numeric($genreid))
			$sql.=",idgenre=".$genreid;
		else
			$sql.=",idgenre=NULL";
		if($views!="" && is_numeric($views))
			$sql.=",views=".$views;
		$sql.=" WHERE idalbum=".$idalbum;
		//echo "sql1=".$sql."<br>";
		$this->db->sql_query($sql);
		$error=$this->db->sql_error();
		if($error['message']==""){
			if($this->existsComments($idalbum,$language)){//if currentlang exists
				//update comments currentlang
				$sql="UPDATE ".$this->prefix."_topmusic_album_lang SET comments='".$comments."' WHERE idalbum=".$idalbum." AND language='".$language."'";
			}else{
				//insert biography
				$sql="INSERT INTO ".$this->prefix."_topmusic_album_lang (idalbum,language,comments) 
						VALUES (".$idalbum.",'".$language."','".$comments."')";
			}
			//echo "sql2=".$sql;
			$this->db->sql_query($sql);
		}
	}
	/**
	* deletes an album and its songs information
	*/
	function deleteAlbum($idartist,$idalbum){
		//del album from all charts
		$chartAdminDAO=new ChartAdminDAO($this->db,$this->prefix);
		$sSql="SELECT element.idchart, element.position FROM ".$this->prefix."_topmusic_chart_element element INNER JOIN ".$this->prefix."_topmusic_chart chart ON element.idchart=chart.idchart where chart.type='album' and element.idelement=".$idalbum;
		$stmt=$this->db->sql_query($sSql);
		while($rs=$this->db->sql_fetchrow($stmt)){
			$chartAdminDAO->delElement($rs['idchart'],$idalbum,$rs['position']);
		}
		$this->db->sql_freeresult($stmt);
		
		//del all songs related data
		$sql="SELECT idsong FROM ".$this->prefix."_topmusic_song WHERE idalbum=".$idalbum;
		$stmt2=$this->db->sql_query($sql,BEGIN_TRANSACTION);
		while($rs2=$this->db->sql_fetchrow($stmt2)){
			$idsong=$rs2['idsong'];
			//del songs_relations
			$sql="DELETE FROM ".$this->prefix."_topmusic_song_relation where idsong=".$idsong." OR idsong_related=".$idsong;
			//echo "sql=$sql<br>";
			$this->db->sql_query($sql);
			//del song custom fields values
			$sql="DELETE FROM ".$this->prefix."_topmusic_song_fields_values where idsong=".$idsong;
			//echo "sql=$sql<br>";
			$this->db->sql_query($sql);
			//delete song votes
			$sql="DELETE FROM ".$this->prefix."_topmusic_song_votes WHERE idsong=".$idsong;
			//echo "sql=$sql<br>";
			$this->db->sql_query($sql);
		}
		$this->db->sql_freeresult($stmt2);
		
		//del songs
		$sql="DELETE FROM ".$this->prefix."_topmusic_song WHERE idalbum=".$idalbum;
		//echo "sql=$sql<br>";
		$this->db->sql_query($sql);
		
		//del album relations
		$sql="DELETE FROM ".$this->prefix."_topmusic_album_relation where idalbum=".$idalbum." OR idalbum_related=".$idalbum;
		//echo "sql=$sql<br>";
		$this->db->sql_query($sql);
		//del album custom fields values
		$sql="DELETE FROM ".$this->prefix."_topmusic_album_fields_values where idalbum=".$idalbum;
		//echo "sql=$sql<br>";
		$this->db->sql_query($sql);
		//del albums lang
		$sql="DELETE FROM ".$this->prefix."_topmusic_album_lang WHERE idalbum=".$idalbum;
		//echo "sql=$sql<br>";
		$this->db->sql_query($sql);
		//delete album votes
		$sql="DELETE FROM ".$this->prefix."_topmusic_album_votes WHERE idalbum=".$idalbum;
		//echo "sql=$sql<br>";
		$this->db->sql_query($sql);
		//delete album
		$sql="DELETE FROM ".$this->prefix."_topmusic_album WHERE idalbum=".$idalbum;
		//echo "sql=$sql<br>";
		$this->db->sql_query($sql,END_TRANSACTION);
		
	}
	function getAlbum($id,$language){
		$albumO=new AlbumAdminO();
		$sSql="SELECT 
		  ".$this->prefix."_topmusic_album.idalbum,
		  ".$this->prefix."_topmusic_genre_lang.name as genrename,
		  ".$this->prefix."_topmusic_album.idartist,
		  ".$this->prefix."_topmusic_artist.name as artistname,
		  ".$this->prefix."_topmusic_album.title,
		  ".$this->prefix."_topmusic_album.frontcover_file,
		  ".$this->prefix."_topmusic_album.frontcover_url,
		  ".$this->prefix."_topmusic_album.backcover_file,
		  ".$this->prefix."_topmusic_album.backcover_url,
		  ".$this->prefix."_topmusic_album.yeardate,
		  ".$this->prefix."_topmusic_album.idgenre,
		  ".$this->prefix."_topmusic_album.shopurl,
		  ".$this->prefix."_topmusic_album.views
		FROM
		  ".$this->prefix."_topmusic_album
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_album.idgenre = ".$this->prefix."_topmusic_genre.idgenre)
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre_lang ON (".$this->prefix."_topmusic_genre.idgenre = ".$this->prefix."_topmusic_genre_lang.idgenre)
		  LEFT JOIN ".$this->prefix."_topmusic_artist ON (".$this->prefix."_topmusic_album.idartist = ".$this->prefix."_topmusic_artist.idartist)
		WHERE
		  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR
		  ".$this->prefix."_topmusic_genre_lang.language IS NULL) AND
		  ".$this->prefix."_topmusic_album.idalbum=".$id;
		$stmt=$this->db->sql_query($sSql);
		if($rs=$this->db->sql_fetchrow($stmt)){
			$albumO->setId($rs['idalbum']);
			$albumO->setTitle($rs['title']);
			$albumO->setYeardate($rs['yeardate']);
			$albumO->setGenreid($rs['idgenre']);
			$albumO->setGenrename($rs['genrename']);
			$albumO->setIdartist($rs['idartist']);
			$albumO->setArtistname($rs['artistname']);
			$albumO->setComments($rs['comments']);
			$albumO->setFrontcover_file($rs['frontcover_file']);
			$albumO->setFrontcover_url($rs['frontcover_url']);
			$albumO->setBackcover_file($rs['backcover_file']);
			$albumO->setBackcover_url($rs['backcover_url']);
			$albumO->setShopurl($rs['shopurl']);
			$albumO->setCount($rs['views']);
		}
		$this->db->sql_freeresult($stmt);
		$sSql="SELECT comments FROM ".$this->prefix."_topmusic_album_lang WHERE language='".$language."' AND idalbum=".$id;
		$stmt=$this->db->sql_query($sSql);
		if($rs=$this->db->sql_fetchrow($stmt)){
			$albumO->setComments($rs['comments']);
		}
		$this->db->sql_freeresult($stmt);
		return $albumO;
	}
	function getAlbumList($idartist,$language){
		$sSql="	SELECT 
		  ".$this->prefix."_topmusic_album.idalbum,
		  ".$this->prefix."_topmusic_genre_lang.name as genrename,
		  ".$this->prefix."_topmusic_album.idartist,
		  ".$this->prefix."_topmusic_artist.name as artistname,
		  ".$this->prefix."_topmusic_album.title,
		  ".$this->prefix."_topmusic_album.frontcover_file,
		  ".$this->prefix."_topmusic_album.frontcover_url,
		  ".$this->prefix."_topmusic_album.backcover_file,
		  ".$this->prefix."_topmusic_album.backcover_url,
		  ".$this->prefix."_topmusic_album.yeardate
		FROM
		  ".$this->prefix."_topmusic_album
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_album.idgenre = ".$this->prefix."_topmusic_genre.idgenre)
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre_lang ON (".$this->prefix."_topmusic_genre.idgenre = ".$this->prefix."_topmusic_genre_lang.idgenre)
		  INNER JOIN ".$this->prefix."_topmusic_artist ON (".$this->prefix."_topmusic_album.idartist = ".$this->prefix."_topmusic_artist.idartist)
		WHERE
		  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL ) AND
		  ".$this->prefix."_topmusic_album.idartist=".$idartist."
		ORDER BY yeardate";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$albumO=new AlbumAdminO();
			$albumO->setId($rs['idalbum']);
			$albumO->setTitle($rs['title']);
			$albumO->setYeardate($rs['yeardate']);
			$albumO->setGenreid($rs['idgenre']);
			$albumO->setGenrename($rs['genrename']);
			$albumO->setIdartist($rs['idartist']);
			$albumO->setArtistname($rs['artistname']);
			$albumO->setFrontcover_file($rs['frontcover_file']);
			$albumO->setFrontcover_url($rs['frontcover_url']);
			$albumO->setBackcover_file($rs['backcover_file']);
			$albumO->setBackcover_url($rs['backcover_url']);
			$albumList[$i]=$albumO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $albumList;
	}
	function searchAlbum($album,$freetext,$language,$page=1,$numppage=10){
		$sSql="	SELECT 
				  ".$this->prefix."_topmusic_album.idalbum,
				  ".$this->prefix."_topmusic_song.title as songtitle,
				  ".$this->prefix."_topmusic_album.idgenre,
				  ".$this->prefix."_topmusic_album.idartist,
				  ".$this->prefix."_topmusic_artist.name as artistname,
				  ".$this->prefix."_topmusic_album.title,
				  ".$this->prefix."_topmusic_album.yeardate
				FROM
				  ".$this->prefix."_topmusic_album
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_song ON (".$this->prefix."_topmusic_album.idalbum = ".$this->prefix."_topmusic_song.idalbum)
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_artist ON (".$this->prefix."_topmusic_album.idartist = ".$this->prefix."_topmusic_artist.idartist)
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_album_lang ON (".$this->prefix."_topmusic_album.idalbum = ".$this->prefix."_topmusic_album_lang.idalbum)";
		$junc=" AND ";
		$sWhere="";
		if($album->getGenreid()!="" && $album->getGenreid()!="...")
			$sWhere.=" ".$this->prefix."_topmusic_album.idgenre=".$album->getGenreid();
		if($album->getTitle()!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_album.title like '%".$album->getTitle()."%'";
		}
		if($album->getArtistname()!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_artist.name like '%".$album->getArtistname()."%'";
		}
		if($album->getSongtitle()!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_song.title like '%".$album->getSongtitle()."%'";
		}
		if($freetext!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_album_lang.comments like '%".$freetext."%'";
		}
		if($sWhere!="")
			$sWhere=" WHERE (".$this->prefix."_topmusic_album_lang.language='".$language."' || ".$this->prefix."_topmusic_album_lang.language IS NULL) AND ".$sWhere;
		$sSql.=$sWhere." GROUP BY idalbum";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		$numtotal=$this->db->sql_numrows();
		$pagetotal=ceil($numtotal/$numppage);
		if($page>1 && $page<=$pagetotal)
			$this->db->sql_rowseek(($page-1)*$numppage);
		while($i<$numppage+0 && $rs=$this->db->sql_fetchrow($stmt)){
			$albumO=new AlbumAdminO();
			$albumO->setId($rs['idalbum']);
			$albumO->setTitle($rs['title']);
			$albumO->setYeardate($rs['yeardate']);
			$albumO->setIdartist($rs['idartist']);
			$albumO->setArtistname($rs['artistname']);
			$albumO->setCount($numtotal);
			$albumList[$i]=$albumO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $albumList;
	}
	function existsComments($idalbum,$currentlang){
		$sql="SELECT * FROM ".$this->prefix."_topmusic_album_lang WHERE idalbum=".$idalbum." AND language='".$currentlang."'";
		$stmt=$this->db->sql_query($sql);
		if($this->db->sql_fetchrow($stmt))
			return 1;
		else
			return 0;
	}
	function getSelectList($idartist=null,$orphan=false){
		if($idartist!=null && is_numeric($idartist))
			$where = "where ".$this->prefix."_topmusic_artist.idartist=".$idartist;
		elseif($idartist=="orphan")
			$where = "where ".$this->prefix."_topmusic_artist.idartist is null";
		$sSql="SELECT 
				  ".$this->prefix."_topmusic_album.idalbum,
				  ".$this->prefix."_topmusic_album.title,
				  ".$this->prefix."_topmusic_artist.name as artistname
				FROM
				  ".$this->prefix."_topmusic_album
				  LEFT JOIN  ".$this->prefix."_topmusic_artist ON (".$this->prefix."_topmusic_album.idartist=".$this->prefix."_topmusic_artist.idartist)";
		if($where!="")
			$sSql.=" ".$where." ";
		$sSql.="ORDER BY artistname,title";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		if($orphan){
			$albumList[$i][0]='orphan';
			$albumList[$i][1]='no album';
			$i++;
		}
		while($rs=$this->db->sql_fetchrow($stmt)){
			$artistname=$rs['artistname'];
			$albumList[$i][0]=$rs['idalbum'];
			$albumList[$i][1]=$rs['title'];
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $albumList;
	}
	function getOrphanList($language){
		$sSql="	SELECT 
		  ".$this->prefix."_topmusic_album.idalbum,
		  ".$this->prefix."_topmusic_genre_lang.name as genrename,
		  ".$this->prefix."_topmusic_album.title,
		  ".$this->prefix."_topmusic_album.frontcover_file,
		  ".$this->prefix."_topmusic_album.frontcover_URL,
		  ".$this->prefix."_topmusic_album.backcover_file,
		  ".$this->prefix."_topmusic_album.backcover_url,
		  ".$this->prefix."_topmusic_album.yeardate
		FROM
		  ".$this->prefix."_topmusic_album
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_album.idgenre = ".$this->prefix."_topmusic_genre.idgenre)
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre_lang ON (".$this->prefix."_topmusic_genre.idgenre = ".$this->prefix."_topmusic_genre_lang.idgenre)
		WHERE
		  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL ) AND
		  (".$this->prefix."_topmusic_album.idartist IS NULL OR ".$this->prefix."_topmusic_album.idartist=0)
		ORDER BY title";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$albumO=new AlbumAdminO();
			$albumO->setId($rs['idalbum']);
			$albumO->setTitle($rs['title']);
			$albumO->setYeardate($rs['yeardate']);
			$albumO->setGenreid($rs['idgenre']);
			$albumO->setGenrename($rs['genrename']);
			$albumO->setArtistname($rs['artistname']);
			$albumO->setFrontcover_file($rs['frontcover_file']);
			$albumO->setFrontcover_url($rs['frontcover_url']);
			$albumO->setBackcover_file($rs['backcover_file']);
			$albumO->setBackcover_url($rs['backcover_url']);
			$albumList[$i]=$albumO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $albumList;
	}
	function getOrphanSelectList($language){
		$sSql="	SELECT 
		  ".$this->prefix."_topmusic_album.idalbum,
		  ".$this->prefix."_topmusic_genre_lang.name as genrename,
		  ".$this->prefix."_topmusic_album.title,
		  ".$this->prefix."_topmusic_album.frontcover_file,
		  ".$this->prefix."_topmusic_album.frontcover_URL,
		  ".$this->prefix."_topmusic_album.backcover_file,
		  ".$this->prefix."_topmusic_album.backcover_url,
		  ".$this->prefix."_topmusic_album.yeardate
		FROM
		  ".$this->prefix."_topmusic_album
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_album.idgenre = ".$this->prefix."_topmusic_genre.idgenre)
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre_lang ON (".$this->prefix."_topmusic_genre.idgenre = ".$this->prefix."_topmusic_genre_lang.idgenre)
		WHERE
		  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL ) AND
		  (".$this->prefix."_topmusic_album.idartist IS NULL OR ".$this->prefix."_topmusic_album.idartist=0)
		ORDER BY title";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$artistname=$rs["artistname"];
			if($artistname=="")
				$artistname="orphan";
			$albumList[$i][0]=$rs['idalbum'];
			$albumList[$i][1]=$rs['title']." (orphan)";
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $albumList;
	}
	function insertRelated($idalbum,$idrel){
		$sql="INSERT INTO ".$this->prefix."_topmusic_album_relation (idalbum,idalbum_related) 
						VALUES (".$idalbum.",".$idrel.")";
				$this->db->sql_query($sql);
				$error=$this->db->sql_error();
				if($error["message"]=="")
					return true;
				else
					return false;
	}
	function getRelated($idalbum){
		$sSql="	SELECT 
				album.idalbum,
				album.idartist,
			  	title,
				frontcover_file,
				frontcover_url
			 FROM
			  ".$this->prefix."_topmusic_album album
			  INNER JOIN ".$this->prefix."_topmusic_album_relation rel ON (album.idalbum = rel.idalbum_related)
			WHERE rel.idalbum=$idalbum
			ORDER BY yeardate desc";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$albumO=new AlbumAdminO();
			$albumO->setId($rs['idalbum']);
			$albumO->setIdartist($rs['idartist']);
			$albumO->setTitle($rs['title']);
			$albumO->setFrontcover_file($rs['frontcover_file']);
			$albumO->setFrontcover_url($rs['frontcover_url']);
			$albumList[$i]=$albumO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $albumList;
	}
	function deleteRelated($idalbum,$idrel){
		$sql="DELETE FROM ".$this->prefix."_topmusic_album_relation WHERE idalbum=".$idalbum." AND idalbum_related=".$idrel;
		$this->db->sql_query($sql);
		$error=$this->db->sql_error();
		if($error["message"]=="")
			return true;
		else
			return false;
	}
	function getCustomFields(){
		$sSql="	SELECT 
			  	idalbum_field,
				name,
				description,
				type,
				file_path,
				file_icon,
				image_width,
				select_values
			 FROM
			  ".$this->prefix."_topmusic_album_fields
			ORDER BY name";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		while($rs=$this->db->sql_fetchrow($stmt)){
			$albumFieldO=new CustomFieldAdminO();
			$albumFieldO->idfield=$rs['idalbum_field'];
			$albumFieldO->name=$rs['name'];
			$albumFieldO->type=$rs['type'];
			$albumFieldO->description=$rs['description'];
			$albumFieldO->file_icon=$rs['file_icon'];
			$albumFieldO->file_path=$rs['file_path'];
			$albumFieldO->image_width=$rs['image_width'];
			$albumFieldO->select_values=$rs['select_values'];
			$albumFields[]=$albumFieldO;
		}
		$this->db->sql_freeresult($stmt);
		return $albumFields;
	}
	function getCustomFieldsValues($idalbum){
		$fields=$this->getCustomFields();
		$sSql="	SELECT 
					idalbum_field,
					value
			 	FROM
					".$this->prefix."_topmusic_album_fields_values
				WHERE idalbum=$idalbum OR idalbum IS NULL";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$fields2=$fields;
		//print_r($fields2);
		while($rs=$this->db->sql_fetchrow($stmt)){
			foreach($fields as $key => $field){
				if($field->idfield==$rs['idalbum_field']){
					$field->value=$rs['value'];
					array_splice($fields2,$key,1,array($field));
					break;
				}
			}
		}
		$this->db->sql_freeresult($stmt);
		//print_r($fields2);
		return $fields2;
	}
	function updateCustomFields($idalbum,$POST){
		//all album custom fields with their original values
		$fieldsValues=$this->getCustomFieldsValues($idalbum);
		//those fields with not values still set
		$sql="SELECT camps.idalbum_field
			FROM ".$this->prefix."_topmusic_album_fields camps
			left outer JOIN ".$this->prefix."_topmusic_album_fields_values valors on camps.idalbum_field = valors.idalbum_field AND valors.idalbum=".$idalbum."
			where idalbum is null";
		//echo "sql=".$sql."<br>";
		$stmt=$this->db->sql_query($sql);
		while($rs=$this->db->sql_fetchrow($stmt)){
			foreach($fieldsValues as $field){
				if($field->idfield==$rs['idalbum_field']){
					switch($field->type){
						case "image":
						case "file":
							//print_r($_FILES);
							if($_FILES[$field->name]["size"]>0){
								$directory="modules/"._TOPMUSIC_MODULENAME."/".$field->file_path."/";
								//print($directory);
								if(move_uploaded_file($_FILES[$field->name]["tmp_name"],($directory.$_FILES[$field->name]["name"]))){
									chmod($directory.$_FILES[$field->name]["name"],0755);
							    	$POST[$field->name]=$_FILES[$field->name]["name"];
								}
								//print "post=".$POST[$field->name]."<br>";
							}
							break;
					}
					$value=(!get_magic_quotes_gpc())?addslashes($POST[$field->name]):$POST[$field->name];
					$sql="INSERT INTO ".$this->prefix."_topmusic_album_fields_values (idalbum_field,idalbum,value) values (".$field->idfield.",".$idalbum.",'".$value."')";
					$this->db->sql_query($sql);
					break;
				}
			}
			//echo "sql=".$sql."<br>";
		}
		$this->db->sql_freeresult($stmt);
		//update those values already set and changed
		if(count($fieldsValues)>0){
			foreach($fieldsValues as $field){
				//print "old=".$field->value." new=".$POST[$field->name];
				if($field->value!=$POST[$field->name]){
					switch($field->type){
						case "image":
						case "file":
							//print "postval=".$POST[$field->name]."<br>";
							//print "oldval=".$POST[$field->value]."<br>";
							//print "files_name=".$_FILES[$field->name]["name"]."<br>";
							if($field->value!=$POST[$field->name] || $field->value!=$_FILES[$field->name]["name"]){
								if($_FILES[$field->name]["size"]>0){
									$directory="modules/"._TOPMUSIC_MODULENAME."/".$field->file_path."/";
									//delete old file
									if($field->value!="")
										unlink($directory.$field->value);
									//upload new file
									if(move_uploaded_file($_FILES[$field->name]["tmp_name"],($directory.$_FILES[$field->name]["name"]))){
										chmod($directory.$_FILES[$field->name]["name"],0755);
									   	$POST[$field->name]=$_FILES[$field->name]["name"];
									}
									//print "post=".$POST[$field->name]."<br>";
								}
							}
							break;
					}
					if($POST[$field->name]!="" || ($POST[$field->name]=="" && $field->type!="image" && $field->type!="file")){
						$value=(!get_magic_quotes_gpc())?addslashes($POST[$field->name]):$POST[$field->name];
						$sql="UPDATE ".$this->prefix."_topmusic_album_fields_values SET value='".$value."' WHERE idalbum=".$idalbum." AND idalbum_field=".$field->idfield;
					}
					//echo "sql=".$sql."<br>";
					$this->db->sql_query($sql);
					$error=$this->db->sql_error();
					if($error["message"]!="")
						return false;
				}
			}
		}
		return true;
	}
	function addCustomField($field){
		if($field->image_width=="")
			$field->image_width=0;
		$field_description=(!get_magic_quotes_gpc())?addslashes($field->description):$field->description;
		$select_values=(!get_magic_quotes_gpc())?addslashes($field->select_values):$field->select_values;
		$sql="INSERT INTO ".$this->prefix."_topmusic_album_fields 
					(name,description,type,image_width,file_path,file_icon,select_values) 
				values 
					('".$field->name."','".$field_description."','".$field->type."',".$field->image_width.",'".$field->file_path."','".$field->file_icon."','".$select_values."')";
		//echo "sql=".$sql."<br>";
		$this->db->sql_query($sql);
		$error=$this->db->sql_error();
		if($error["message"]!="")
			return false;
		return true;
	}
	function delCustomField($idfield){
		if(is_numeric($idfield)){
			$sql="DELETE FROM ".$this->prefix."_topmusic_album_fields_values WHERE idalbum_field=".$idfield;
			//echo "sql=".$sql."<br>";
			$this->db->sql_query($sql);
			$sql="DELETE FROM ".$this->prefix."_topmusic_album_fields WHERE idalbum_field=".$idfield;
			//echo "sql=".$sql."<br>";
			$this->db->sql_query($sql);
			$error=$this->db->sql_error();
			if($error["message"]!="")
				return false;
			return true;
		}else
			return false;
	}
}
?>
Return current item: Top Music module