Location: PHPKode > projects > Top Music module > Top Music 3.0.7/html/modules/topMusic/admin/lib/com/sergids/topmusic/model/MusicMozAdminDAO.php
<?php
require_once("lib/com/sergids/common/Xml.php");
require_once("ArtistAdminO.php");
require_once("ArtistAdminDAO.php");
require_once("AlbumAdminO.php");
require_once("AlbumAdminDAO.php");
require_once("SongAdminO.php");
require_once("SongAdminDAO.php");
require_once("GenreAdminDAO.php");
require_once("GenericAdminDAO.php");
require_once("GenreAdminO.php");

class MusicMozAdminDAO{
	var $prefix;
	var $db;
	var $error;
	
	function MusicMozAdminDAO($db,$prefix){
		$language="english";
		$this->prefix=$prefix;
		$this->db=$db;
	}
	function searchArtist($artist){
		$url="http://musicmoz.org/search/search.cgi?search=8:".urlencode($artist)."+t:profileinfo&xml=1";
		$data = new XMLParser($url);
		//print_r($data->data);
		$data = $data->data[0]['child'][0]['child'];
		//print_r($data);
		if(count($data)>0){
			foreach($data as $item){
				if($item['name']=="SEARCH:ITEM"){
					$artistO=new ArtistAdminO();
					$artistO->musicmoz_url="http://musicmoz.org/".$item['attributes']['REF']."index.xml";
					foreach($item['child'] as $item2){
						if($item2['name']=="NAME"){
							$artistO->name=$item2['content'];
						}
					}
					$artists[]=$artistO;
				}
			}
		}
		return $artists;
	}
	function searchAlbum($album,$artist){
		$url="http://musicmoz.org/search/search.cgi?search=";
		if($album!="")
			$url.="i:".urlencode($album);
		if($artist!=""){
			if($album!="")
				$url.="+";
			$url.="k:".urlencode($artist);
		}
		$url.="+t:release&xml=1";
		//print "url=".$url."<br>";
		$data = new XMLParser($url);
		//print_r($data->data);
		$data = $data->data[0]['child'][0]['child'];
		//print_r($data);
		if(count($data)>0){
			foreach($data as $item){
				if($item['name']=="SEARCH:ITEM"){
					if($item['child'][0]['attributes']['TYPE']=="release"){
						$albumO=new AlbumAdminO();
						$albumO->musicmoz_url="http://musicmoz.org/".$item['attributes']['REF']."index.xml";
						foreach($item['child'][0]['child'] as $item2){
							switch($item2['name']){
								case "TITLE":
									$albumO->title=$item2['content'];
									break;
								case "COVER":
									$cover=$item2['attributes']['SRC'];
									if(substr($cover,0,4)=="http")
										$albumO->frontcover_url=$cover;
									else
										$albumO->frontcover_url="http://musicmoz.org".$cover;
									break;
							}
						}
						$albums[]=$albumO;
					}
				}
			}
		}
		return $albums;
	}
	function getArtist($url){
		$data= new XMLParser($url);
		//print_r($data);
		
		$data_root=$data->data[0]['child'][0]['child'];
		//print_r($data_root);
		$artistO=new ArtistAdminO();
		$artistO->musicmoz_url=addslashes($url);
		foreach($data_root as $elem){
			switch($elem['name']){
				case 'RESOURCE':
					if($elem['attributes']['NAME']=='musicbrainz')
						$artistO->musicbrainz_url=addslashes($elem['attributes']['LINK']);
					break;
				case 'NAME':
					$artistO->name=addslashes($elem['content']);
					break;
				case 'FOUNDED':
					if($artistO->bornyear=="")
						$artistO->bornyear=$elem['content'];
					break;
				case 'BORN':
					if($artistO->bornyear=="")
						$artistO->bornyear=$elem['content'];
					break;
				case 'DISBANDED':
					if($artistO->deadyear=="")
						$artistO->deadyear=$elem['content'];
					break;
				case 'DIED':
					if($artistO->deadyear=="")
						$artistO->deadyear=$elem['content'];
					break;
				case 'LOGO':
					$photo=$elem['attributes']['SRC'];
					if(substr($photo,0,4)=="http")
						$artistO->photo=$photo;
					else
						$artistO->photo="http://musicmoz.org".$photo;
					break;
				case 'MEMBER':
					if($elem['attributes']['NAME']!="")
						$artistO->members.=addslashes($elem['attributes']['NAME']." (".$elem['attributes']['ROLE'].")<br>");
					break;
				case 'STYLE':
					if($elem['attributes']['NUMBER']=='1'){
						$genreO=new GenreAdminO();
						$genreO->name=$elem['content'];
						$genreDAO=new GenreAdminDAO($this->db,$this->prefix);
						if(!$genreO->id=$genreDAO->getId($genreO->name)){
							$genericDAO=new GenericAdminDAO($this->db,$this->prefix);
							$languageList=$genericDAO->getLanguageList();
							foreach($languageList as $num=>$lang){
								if($num==0)
									$genreDAO->insertGenre(&$genreO,$lang);
								else
									$genreDAO->insertGenreLang($genreO,$lang);
							}
						}
						$artistO->genreid=$genreO->id;
					}
					break;
				case 'RELEASE':
					if(($elem['attributes']['TYPE']=='album' || $elem['attributes']['TYPE']=='compilation' || $elem['attributes']['TYPE']=='soundtrack') && $elem['attributes']['REF']!=""){
						$album_url="http://musicmoz.org/".$elem['attributes']['REF']."/index.xml";
						//print("album_url=".$album_url."<br>");
						$albumO=$this->getAlbum($album_url);
						if($albumO->title!="")
							$artistO->albums[]=$albumO;
					}
					break;
			}
		}
		return $artistO;
	}
	function getAlbum($album_url){
		$data= new XMLParser($album_url);
		//print_r($data);
		$data_root=$data->data[0]['child'][0]['child'];
		//print_r($data_root);
		$albumO=new AlbumAdminO();
		if(count($data_root)>0){
			foreach($data_root as $elem2){
				if($elem2['name']=="ITEM"){
					foreach($elem2['child'] as $elem3){
						switch($elem3['name']){
							case 'TITLE':
								$albumO->title=addslashes($elem3['content']);
								break;
							case 'GENRE':
								$genreO=new GenreAdminO();
								$genreO->name=$elem3['content'];
								$genreDAO=new GenreAdminDAO($this->db,$this->prefix);
								if($genreO->id!=$genreDAO->getId($genreO->name)){
									$genericDAO=new GenericAdminDAO($this->db,$this->prefix);
									$languageList=$genericDAO->getLanguageList();
									foreach($languageList as $num=>$lang){
										if($num==0)
											$genreDAO->insertGenre(&$genreO,$lang);
										else
											$genreDAO->insertGenreLang($genreO,$lang);
									}
								}
								$albumO->genreid=$genreO->id;
								break;
							case 'RELEASEDATE':
								$albumO->yeardate=$elem3['content'];
								break;
							case 'DESCRIPTION':
								$albumO->comments=addslashes($elem3['content']);
								break;
							case 'COVER':
								if(substr($elem3['attributes']['SRC'],0,4)=="http")
									$albumO->frontcover_url=$elem3['attributes']['SRC'];
								else
									$albumO->frontcover_url="http://musicmoz.org".$elem3['attributes']['SRC'];
								break;
							case 'TRACKLISTING':
								if($elem3['child'][0]['name']=="TRACK"){ //only 1 vol.
									foreach($elem3['child'] as $elem4){
										if($elem4['name']=='TRACK' && $elem4['attributes']['TITLE']!=""){
											$songO=new SongAdminO();
											$songO->title=addslashes($elem4['attributes']['TITLE']);
											$songO->number=$elem4['attributes']['NUMBER'];
											$albumO->songs[]=$songO;
										}
									}
								}else{
									foreach($elem3['child'] as $elem4){
										foreach($elem4['child'] as $elem5){
											if($elem5['name']=='TRACK' && $elem5['attributes']['TITLE']!=""){
												$songO=new SongAdminO();
												$songO->title=addslashes($elem5['attributes']['TITLE']);
												(strlen($elem5['attributes']['NUMBER'])==1)?$num="0".$elem5['attributes']['NUMBER']:$num=$elem5['attributes']['NUMBER'];
												$songO->number=$elem4['attributes']['NUMBER'].$num;
												$albumO->songs[]=$songO;
											}
										}
									}
								}
								break;
						}
					}
				}
			}
		}
		return $albumO;
	}
	function insertArtist($artist){
		$artistDAO=new ArtistAdminDAO($this->db,$this->prefix);
		$artistDAO->insertArtist(&$artist,"english");
		$error=$this->db->sql_error();
		if($error['message']==""){
			return true;
		}else{
			$this->error=$error['message'];
			return false;
		}
	}
	function insertEntireArtist($artist){
		$artistDAO=new ArtistAdminDAO($this->db,$this->prefix);
		$artistDAO->insertArtist(&$artist,"english");
		$error=$this->db->sql_error();
		if($error['message']==""){
			$albumDAO=new AlbumAdminDAO($this->db,$this->prefix);
			$songDAO=new SongAdminDAO($this->db,$this->prefix);
			if(count($artist->albums)>0){
				foreach($artist->albums as $album){
					$album->idartist=$artist->id;
					if(!$this->insertEntireAlbum($album)){
						$this->error=$error['message'];
						return false;
					}
				}
			}
			return true;
		}else{
			$this->error=$error['message'];
			return false;
		}
	}
	function insertAlbum($album){
		$albumDAO=new AlbumAdminDAO($this->db,$this->prefix);
		$albumDAO->insertAlbum(&$album,"english");
		$error=$this->db->sql_error();
		if($error['message']==""){
			return true;
		}else{
			$this->error=$error['message'];
			return false;
		}
	}
	function insertEntireAlbum($album){
		$albumDAO=new AlbumAdminDAO($this->db,$this->prefix);
		$songDAO=new SongAdminDAO($this->db,$this->prefix);
		$albumDAO->insertAlbum(&$album,"english");
		$error=$this->db->sql_error();
		if($error['message']==""){
			if(count($album->songs)>0){
				foreach($album->songs as $song){
					if(strpos($song->title,"/"))
						$song->title=str_replace("/","-",$song->title);
					$song->idalbum=$album->id;
					$songDAO->insertSong(&$song,"english");
					$error=$this->db->sql_error();
					if($error['message']!=""){
						$this->error=$error['message'];
						return false;
					}
				}
			}
			return true;
		}else{
			$this->error=$error['message'];
			return false;
		}
	}
	function insertSong($album,$title){
		$albumDAO=new AlbumAdminDAO($this->db,$this->prefix);
		$songDAO=new SongAdminDAO($this->db,$this->prefix);
		if(count($album->songs)>0){
			foreach($album->songs as $song){
				if(urlencode($song->title)==urlencode($title)){
					if(strpos($song->title,"/"))
						$song->title=str_replace("/","-",$song->title);
					$song->idalbum=$album->id;
					$songDAO->insertSong(&$song,"english");
					$error=$this->db->sql_error();
					if($error['message']!=""){
						$this->error=$error['message'];
						return false;
					}
				}
			}
			return true;
		}
	}
}
?>
Return current item: Top Music module