Location: PHPKode > projects > Top Music module > Top Music 3.0.7/html/modules/topMusic/admin/lib/com/sergids/topmusic/model/SongAdminDAO.php
<?php
require_once("CustomFieldAdminO.php");
require_once("SongAdminO.php");
require_once("ChartAdminDAO.php");
class SongAdminDAO{
	var $db;
	function SongAdminDAO($db,$prefix="nuke"){
		$this->db=$db;
		$this->prefix=$prefix;
	} 
	function insertSong($song,$language){
		$title=(!get_magic_quotes_gpc())?addslashes($song->getTitle()):$song->getTitle();
		if(eregi("'",$song->getTitle()) && !eregi("\\\'",$song->getTitle()))
			$title=addslashes($song->getTitle());
		$number=$song->getNumber();
		$genreid=$song->getGenreid();
		$track=$song->getTrack();
		$idalbum=$song->idalbum;
		$lyrics=(!get_magic_quotes_gpc())?addslashes($song->getLyrics()):$song->getLyrics();
		$views=$song->getCount();
		$musicmoz_url=(!get_magic_quotes_gpc())?addslashes($song->musicmoz_url):$song->musicmoz_url;
		$musicbrainz_url=(!get_magic_quotes_gpc())?addslashes($song->musicbrainz_url):$song->musicbrainz_url;
		$sql="INSERT INTO ".$this->prefix."_topmusic_song (title,track,lyrics";
		if($idalbum!="" && is_numeric($idalbum))
			$sql.=",idalbum";
		if($genreid!="" && is_numeric($genreid))
			$sql.=",idgenre";
		if($number!="")
			$sql.=",number";
		if($views!="" && is_numeric($views))
			$sql.=",views";
		if($musicmoz_url!="")
			$sql.=",musicmoz_url";
		if($musicbrainz_url!="")
			$sql.=",musicbrainz_url";
		$sql.=") VALUES ('".$title."','".$track."','".$lyrics."'";
		if($idalbum!="" && is_numeric($idalbum))
			$sql.=",".$idalbum;
		if($genreid!="" && is_numeric($genreid))
			$sql.=",".$genreid;
		if($number!="")
			$sql.=",'".$number."'";
		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);
		$idsong=$this->db->sql_nextid();
		$song->setId($idsong);
		$error=$this->db->sql_error();
	}
	function updateSong($song,$language){
		
		$idsong=$song->getId();
		$idalbum=$song->getIdalbum();
		$title=(!get_magic_quotes_gpc())?addslashes($song->getTitle()):$song->getTitle();
		$number=$song->getNumber();
		$genreid=$song->getGenreid();
		$track=$song->getTrack();
		$lyrics=(!get_magic_quotes_gpc())?addslashes($song->getLyrics()):$song->getLyrics();
		$views=$song->getCount();
		$sql="UPDATE ".$this->prefix."_topmusic_song SET title='".$title."',track='".$track."',lyrics='".$lyrics."'";
		if($idalbum!="" && is_numeric($idalbum))
			$sql.=",idalbum=".$idalbum;
		else
			$sql.=",idalbum=NULL";
		if($genreid!="" && is_numeric($genreid))
			$sql.=",idgenre=".$genreid;
		else
			$sql.=",idgenre=NULL";
		if($number!="")
			$sql.=",number=".$number;
		if($views!="" && is_numeric($views))
			$sql.=",views=".$views;
		$sql.=" WHERE idsong=".$idsong;
		//echo "sql1=".$sql."<br>";
		$this->db->sql_query($sql);
	}
	function deleteSong($idsong){
		//del song 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='song' and element.idelement=".$idsong;
		$stmt=$this->db->sql_query($sSql);
		while($rs=$this->db->sql_fetchrow($stmt)){
			$chartAdminDAO->delElement($rs['idchart'],$idsong,$rs['position']);
		}
		$this->db->sql_freeresult($stmt);
		
		//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);
		//delete songs
		$sql="DELETE FROM ".$this->prefix."_topmusic_song WHERE idsong=".$idsong;
		$this->db->sql_query($sql);
	}
	function getSong($idsong,$language){
		$songO=new SongAdminO();
		$sSql="SELECT 
				  ".$this->prefix."_topmusic_song.idsong,
				  ".$this->prefix."_topmusic_song.idgenre,
				  ".$this->prefix."_topmusic_genre_lang.name as genrename,
				  ".$this->prefix."_topmusic_song.idalbum,
				  ".$this->prefix."_topmusic_album.title albumtitle,
				  ".$this->prefix."_topmusic_song.title,
				  ".$this->prefix."_topmusic_song.author,
				  ".$this->prefix."_topmusic_song.lyrics,
				  ".$this->prefix."_topmusic_song.number,
				  ".$this->prefix."_topmusic_song.track,
				  ".$this->prefix."_topmusic_album.idartist,
				  ".$this->prefix."_topmusic_album.frontcover_file as albumcover_file,
				  ".$this->prefix."_topmusic_album.frontcover_url as albumcover_url,
				  ".$this->prefix."_topmusic_song.views
				FROM
				  ".$this->prefix."_topmusic_song
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_song.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 OUTER JOIN ".$this->prefix."_topmusic_album ON (".$this->prefix."_topmusic_song.idalbum = ".$this->prefix."_topmusic_album.idalbum)
				WHERE
				  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL) AND
				  ".$this->prefix."_topmusic_song.idsong=".$idsong;
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		if($rs=$this->db->sql_fetchrow($stmt)){
			$songO->setId($rs['idsong']);
			$songO->setTitle($rs['title']);
			$songO->setGenreid($rs['idgenre']);
			$songO->setGenrename($rs['genrename']);
			$songO->setIdalbum($rs['idalbum']);
			$songO->setAlbumname($rs['albumtitle']);
			$songO->setLyrics($rs['lyrics']);
			$songO->setAuthor($rs['author']);
			$songO->setIdartist($rs['idartist']);
			$songO->setNumber($rs['number']);
			$songO->setTrack($rs['track']);
			if($rs['albumcover_file']!="")
				$songO->setAlbumcover($rs['albumcover_file']);
			else
				$songO->setAlbumcover($rs['albumcover_url']);
			$songO->setCount($rs['views']);
		}
		$this->db->sql_freeresult($stmt);
		return $songO;
	}
	function getSongList($idalbum,$language){
		$sSql="	SELECT 
				  ".$this->prefix."_topmusic_song.idsong,
				  ".$this->prefix."_topmusic_song.idgenre,
				  ".$this->prefix."_topmusic_genre_lang.name as genrename,
				  ".$this->prefix."_topmusic_song.idalbum,
				  ".$this->prefix."_topmusic_album.title albumtitle,
				  ".$this->prefix."_topmusic_song.title,
				  ".$this->prefix."_topmusic_song.author,
				  ".$this->prefix."_topmusic_song.lyrics,
				  ".$this->prefix."_topmusic_song.number,
				  ".$this->prefix."_topmusic_song.track,
				  ".$this->prefix."_topmusic_album.idartist
				FROM
				  ".$this->prefix."_topmusic_song
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_song.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 OUTER JOIN ".$this->prefix."_topmusic_album ON (".$this->prefix."_topmusic_song.idalbum = ".$this->prefix."_topmusic_album.idalbum)
				WHERE
				  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL)
				   AND  ".$this->prefix."_topmusic_album.idalbum=".$idalbum."
				ORDER BY number";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$songO=new SongAdminO();
			$songO->setId($rs['idsong']);
			$songO->setTitle($rs['title']);
			$songO->setGenreid($rs['idgenre']);
			$songO->setGenrename($rs['genrename']);
			$songO->setIdalbum($rs['idalbum']);
			$songO->setAlbumname($rs['albumtitle']);
			$songO->setLyrics($rs['lyrics']);
			$songO->setAuthor($rs['author']);
			$songO->setIdartist($rs['idartist']);
			$songO->setNumber($rs['number']);
			$songO->setTrack($rs['track']);
			$songList[$i]=$songO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $songList;
	}
	function searchSong($song,$freetext,$language,$page=1,$numppage=10){
		$sSql="SELECT 
		  ".$this->prefix."_topmusic_song.idsong,
		  ".$this->prefix."_topmusic_song.idgenre,
		  ".$this->prefix."_topmusic_song.idalbum,
		  ".$this->prefix."_topmusic_album.title as albumtitle,
		  ".$this->prefix."_topmusic_album.idartist,
		  ".$this->prefix."_topmusic_artist.name as artistname,
		  ".$this->prefix."_topmusic_song.title,
		  ".$this->prefix."_topmusic_song.track
		FROM
		  ".$this->prefix."_topmusic_song
		  LEFT OUTER JOIN ".$this->prefix."_topmusic_album ON (".$this->prefix."_topmusic_song.idalbum = ".$this->prefix."_topmusic_album.idalbum)
		  INNER JOIN ".$this->prefix."_topmusic_artist ON (".$this->prefix."_topmusic_album.idartist = ".$this->prefix."_topmusic_artist.idartist)";
		$junc=" AND ";
		if($song->getGenreid()!="" && $song->getGenreid()!="...")
			$sWhere.=" ".$this->prefix."_topmusic_song.idgenre=".$song->getGenreid();
		if($song->getTitle()!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_song.title like '%".$song->getTitle()."%'";
		}
		if($song->getAlbumname()!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_album.title like '%".$song->getAlbumname()."%'";
		}
		if($song->getArtistname()!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_artist.name like '%".$song->getArtistname()."%'";
		}
		if($freetext!=""){
			if($sWhere!="")
				$sWhere.=$junc;
			$sWhere.=" ".$this->prefix."_topmusic_song.lyrics like '%$freetext%'";
		}
		if($sWhere!="")
			$sWhere=" WHERE ".$sWhere;
		$sOrder=" ORDER BY ".$this->prefix."_topmusic_song.title";
		$sSql.=$sWhere.$sOrder;
		//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)){
			$songO=new SongAdminO();
			$songO->setId($rs['idsong']);
			$songO->setTitle($rs['title']);
			$songO->setGenreid($rs['idgenre']);
			$songO->setGenrename($rs['genrename']);
			$songO->setIdalbum($rs['idalbum']);
			$songO->setAlbumname($rs['albumtitle']);
			$songO->setLyrics($rs['lyrics']);
			$songO->setAuthor($rs['author']);
			$songO->setIdartist($rs['idartist']);
			$songO->setNumber($rs['number']);
			$songO->setTrack($rs['track']);
			$songO->setArtistname($rs['artistname']);
			$songO->setCount($numtotal);
			$songList[$i]=$songO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $songList;
	}
	function getOrphanList($language){
		$sSql="	SELECT 
				  ".$this->prefix."_topmusic_song.idsong,
				  ".$this->prefix."_topmusic_song.idgenre,
				  ".$this->prefix."_topmusic_genre_lang.name as genrename,
				  ".$this->prefix."_topmusic_song.idalbum,
				  ".$this->prefix."_topmusic_song.title,
				  ".$this->prefix."_topmusic_song.author,
				  ".$this->prefix."_topmusic_song.lyrics,
				  ".$this->prefix."_topmusic_song.number,
				  ".$this->prefix."_topmusic_song.track
				FROM
				  ".$this->prefix."_topmusic_song
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_song.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 OUTER JOIN ".$this->prefix."_topmusic_album ON (".$this->prefix."_topmusic_song.idalbum = ".$this->prefix."_topmusic_album.idalbum)
				WHERE
				  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL)
				   AND  (".$this->prefix."_topmusic_album.idalbum=0 OR ".$this->prefix."_topmusic_album.idalbum IS NULL) 
				ORDER BY number";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$songO=new SongAdminO();
			$songO->setId($rs['idsong']);
			$songO->setTitle($rs['title']);
			$songO->setGenreid($rs['idgenre']);
			$songO->setGenrename($rs['genrename']);
			$songO->setIdalbum($rs['idalbum']);
			$songO->setAlbumname($rs['albumtitle']);
			$songO->setLyrics($rs['lyrics']);
			$songO->setAuthor($rs['author']);
			$songO->setIdartist($rs['idartist']);
			$songO->setNumber($rs['number']);
			$songO->setTrack($rs['track']);
			$songList[$i]=$songO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $songList;
	}
	function getSelectList($idalbum=null,$orphan=false){
		if(is_numeric($idalbum))
			$where=" where ".$this->prefix."_topmusic_song.idalbum=".$idalbum." ";
		elseif($idalbum=="orphan")
			$where=" where ".$this->prefix."_topmusic_song.idalbum IS NULL";
		$sSql="SELECT 
				  ".$this->prefix."_topmusic_song.idsong,
				  ".$this->prefix."_topmusic_song.title,
				  ".$this->prefix."_topmusic_album.title as albumname,
				  ".$this->prefix."_topmusic_artist.name as artistname
				FROM
				  ".$this->prefix."_topmusic_song
				  	LEFT JOIN  ".$this->prefix."_topmusic_album ON (".$this->prefix."_topmusic_song.idalbum=".$this->prefix."_topmusic_album.idalbum)
					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;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$albumname=$rs["albumname"];
			$artistname=$rs["artistname"];
			$songList[$i][0]=$rs['idsong'];
			$songList[$i][1]=$rs['title'];
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $songList;
	}
	function getOrphanSelectList($language){
		$sSql="	SELECT 
				  ".$this->prefix."_topmusic_song.idsong,
				  ".$this->prefix."_topmusic_song.idgenre,
				  ".$this->prefix."_topmusic_genre_lang.name as genrename,
				  ".$this->prefix."_topmusic_song.idalbum,
				  ".$this->prefix."_topmusic_song.title,
				  ".$this->prefix."_topmusic_song.author,
				  ".$this->prefix."_topmusic_song.lyrics,
				  ".$this->prefix."_topmusic_song.number,
				  ".$this->prefix."_topmusic_song.track
				FROM
				  ".$this->prefix."_topmusic_song
				  LEFT OUTER JOIN ".$this->prefix."_topmusic_genre ON (".$this->prefix."_topmusic_song.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 OUTER JOIN ".$this->prefix."_topmusic_album ON (".$this->prefix."_topmusic_song.idalbum = ".$this->prefix."_topmusic_album.idalbum)
				WHERE
				  (".$this->prefix."_topmusic_genre_lang.language = '".$language."' OR ".$this->prefix."_topmusic_genre_lang.language IS NULL)
				   AND  (".$this->prefix."_topmusic_album.idalbum=0 OR ".$this->prefix."_topmusic_album.idalbum IS NULL) 
				ORDER BY number";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$songList[$i][0]=$rs['idsong'];
			$songList[$i][1]=$rs['title']." (orphan - orphan)";
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $songList;
	}
	function insertRelated($idsong,$idrel){
		$sql="INSERT INTO ".$this->prefix."_topmusic_song_relation (idsong,idsong_related) 
						VALUES (".$idsong.",".$idrel.")";
				$this->db->sql_query($sql);
				$error=$this->db->sql_error();
				if($error["message"]=="")
					return true;
				else
					return false;
	}
	function getRelated($idsong){
		$sSql="	SELECT 
				song.idsong,
				song.idalbum,
				album.idartist,
			  	song.title,
				song.track
			 FROM
			  ".$this->prefix."_topmusic_song song
			  INNER JOIN ".$this->prefix."_topmusic_song_relation rel ON (song.idsong = rel.idsong_related)
			 LEFT JOIN ".$this->prefix."_topmusic_album album ON (song.idalbum = album.idalbum)
			WHERE rel.idsong=$idsong
			ORDER BY song.title asc";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		$i=0;
		while($rs=$this->db->sql_fetchrow($stmt)){
			$songO=new SongAdminO();
			$songO->setId($rs['idsong']);
			$songO->setIdalbum($rs['idalbum']);
			$songO->setIdartist($rs['idartist']);
			$songO->setTitle($rs['title']);
			$songO->setTrack($rs['track']);
			$songList[$i]=$songO;
			$i++;
		}
		$this->db->sql_freeresult($stmt);
		return $songList;
	}
	function deleteRelated($idsong,$idrel){
		$sql="DELETE FROM ".$this->prefix."_topmusic_song_relation WHERE idsong=".$idsong." AND idsong_related=".$idrel;
		$this->db->sql_query($sql);
		$error=$this->db->sql_error();
		if($error["message"]=="")
			return true;
		else
			return false;
	}
	function getCustomFields(){
		$sSql="	SELECT 
			  	idsong_field,
				name,
				description,
				type,
				file_path,
				file_icon,
				image_width,
				select_values
			 FROM
			  ".$this->prefix."_topmusic_song_fields
			ORDER BY name";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		while($rs=$this->db->sql_fetchrow($stmt)){
			$songFieldO=new CustomFieldAdminO();
			$songFieldO->idfield=$rs['idsong_field'];
			$songFieldO->name=$rs['name'];
			$songFieldO->type=$rs['type'];
			$songFieldO->description=$rs['description'];
			$songFieldO->file_icon=$rs['file_icon'];
			$songFieldO->file_path=$rs['file_path'];
			$songFieldO->image_width=$rs['image_width'];
			$songFieldO->select_values=$rs['select_values'];
			$songFields[]=$songFieldO;
		}
		$this->db->sql_freeresult($stmt);
		return $songFields;
	}
	function getCustomFieldsValues($idsong){
		$fields=$this->getCustomFields();
		$sSql="	SELECT 
					idsong_field,
					value
			 	FROM
					".$this->prefix."_topmusic_song_fields_values
				WHERE idsong=$idsong OR idsong 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['idsong_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($idsong,$POST){
		//all album custom fields with their original values
		$fieldsValues=$this->getCustomFieldsValues($idsong);
		//those fields with not values still set
		$sql="SELECT camps.idsong_field
			FROM ".$this->prefix."_topmusic_song_fields camps
			left outer JOIN ".$this->prefix."_topmusic_song_fields_values valors on camps.idsong_field = valors.idsong_field AND valors.idsong=".$idsong."
			where idsong 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['idsong_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_song_fields_values (idsong_field,idsong,value) values (".$field->idfield.",".$idsong.",'".$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){
				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_song_fields_values SET value='".$value."' WHERE idsong=".$idsong." AND idsong_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;
		$select_values=(!get_magic_quotes_gpc())?addslashes($field->select_values):$field->select_values;
		$field_description=(!get_magic_quotes_gpc())?addslashes($field->description):$field->description;
		$sql="INSERT INTO ".$this->prefix."_topmusic_song_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_song_fields_values WHERE idsong_field=".$idfield;
			//echo "sql=".$sql."<br>";
			$this->db->sql_query($sql);
			$sql="DELETE FROM ".$this->prefix."_topmusic_song_fields WHERE idsong_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