Location: PHPKode > projects > Top Music module > Top Music 3.0.7/html/modules/topMusic/admin/lib/com/sergids/topmusic/model/ChartAdminDAO.php
<?php
require_once("ChartAdminO.php");
require_once("ChartElementAdminO.php");
require_once("SongAdminO.php");
require_once("SongAdminDAO.php");
require_once("AlbumAdminO.php");
require_once("AlbumAdminDAO.php");
require_once("ArtistAdminO.php");
require_once("ArtistAdminDAO.php");
class ChartAdminDAO{
	var $db;
	var $prefix;
	
	function ChartAdminDAO($db,$prefix="nuke"){
		$this->db=$db;
		$this->prefix=$prefix;
	}
	 
	function getChart($idchart="",$language){
		$chartO=new ChartAdminO();
		if(is_numeric($idchart)){
			$sSql="SELECT chart.idchart, chart.type, chart.number_elements, chart_lang.title, chart_lang.description
					FROM ".$this->prefix."_topmusic_chart as chart
	  					INNER JOIN ".$this->prefix."_topmusic_chart_lang as chart_lang ON (chart.idchart=chart_lang.idchart)
					WHERE chart.idchart=".$idchart." AND
						chart_lang.language='".$language."'";
		}else{
			$sSql="SELECT chart.idchart, chart.type, chart.number_elements, chart_lang.title, chart_lang.description
					FROM ".$this->prefix."_topmusic_chart as chart
	  					INNER JOIN ".$this->prefix."_topmusic_chart_lang as chart_lang ON (chart.idchart=chart_lang.idchart)
					WHERE chart.default=1 AND
						chart_lang.language='".$language."'";
		}
	  	//print $sSql;
		$stmt=$this->db->sql_query($sSql);
		if($rs=$this->db->sql_fetchrow($stmt)){
			$chartO->id=$rs["idchart"];
			$chartO->title=$rs["title"];
			$chartO->description=$rs["description"];
			$chartO->type=$rs["type"];
			$chartO->number_elements=$rs["number_elements"];
		}
		$this->db->sql_freeresult($stmt);
		switch($chartO->type){
			case "song":
				$sSql="SELECT chart_element.idelement, chart_element.position, chart_element.position_ant, chart_element.createdon, DATEDIFF(CURDATE(),chart_element.positionsince) as pos_since,DATEDIFF(CURDATE(),chart_element.createdon) as chart_since, song.title as song_title, song.track, album.idalbum, album.title as album_title, artist.idartist, artist.name as artist_name
					FROM ".$this->prefix."_topmusic_chart as chart
					  INNER JOIN ".$this->prefix."_topmusic_chart_element as chart_element ON (chart.idchart=chart_element.idchart)
					  INNER JOIN ".$this->prefix."_topmusic_song as song ON (chart_element.idelement=song.idsong)
					  LEFT JOIN ".$this->prefix."_topmusic_album as album ON (song.idalbum=album.idalbum)
					  LEFT JOIN ".$this->prefix."_topmusic_artist as artist ON (album.idartist=artist.idartist)
					WHERE chart.idchart=".$chartO->id."
					ORDER BY position";
				//print $sSql;
				$stmt=$this->db->sql_query($sSql);
				while($rs=$this->db->sql_fetchrow($stmt)){
					$chartElementO=new ChartElementAdminO(); 
					$chartElementO->idchart=$chartO->id;
					$chartElementO->position=$rs["position"];
					$chartElementO->position_ant=$rs["position_ant"];
					$chartElementO->createdon=$rs["createdon"];
					$chartElementO->positionsince=$rs["pos_since"];
					$chartElementO->chartsince=$rs["chart_since"];
					$songO=new SongAdminO();
					$songO->id=$rs["idelement"];
					$songO->title=$rs["song_title"];
					$songO->track=$rs["track"];
					$songO->idalbum=$rs["idalbum"];
					$songO->albumname=$rs["album_title"];
					$songO->idartist=$rs["idartist"];
					$songO->artistname=$rs["artist_name"];
					$chartElementO->element=$songO;
					$chartO->elements[]=$chartElementO;
				}
				$this->db->sql_freeresult($stmt);
				break;
			case "album":
				$sSql="SELECT chart_element.idelement, chart_element.position, chart_element.position_ant, chart_element.createdon, DATEDIFF(CURDATE(),chart_element.positionsince) as pos_since,DATEDIFF(CURDATE(),chart_element.createdon) as chart_since, album.idalbum, album.title as album_title, artist.idartist, artist.name as artist_name
					FROM ".$this->prefix."_topmusic_chart as chart
					  INNER JOIN ".$this->prefix."_topmusic_chart_element as chart_element ON (chart.idchart=chart_element.idchart)
					  INNER JOIN ".$this->prefix."_topmusic_album as album ON (chart_element.idelement=album.idalbum)
					  LEFT JOIN ".$this->prefix."_topmusic_artist as artist ON (album.idartist=artist.idartist)
					WHERE chart.idchart=".$chartO->id."
					ORDER BY position";
				//print $sSql;
				$stmt=$this->db->sql_query($sSql);
				while($rs=$this->db->sql_fetchrow($stmt)){
					$chartElementO=new ChartElementAdminO(); 
					$chartElementO->idchart=$chartO->id;
					$chartElementO->position=$rs["position"];
					$chartElementO->position_ant=$rs["position_ant"];
					$chartElementO->createdon=$rs["createdon"];
					$chartElementO->positionsince=$rs["pos_since"];
					$chartElementO->chartsince=$rs["chart_since"];
					$albumO=new AlbumAdminO();
					$albumO->id=$rs["idelement"];
					$albumO->title=$rs["album_title"];
					$albumO->idartist=$rs["idartist"];
					$albumO->artistname=$rs["artist_name"];
					$chartElementO->element=$albumO;
					$chartO->elements[]=$chartElementO;
				}
				$this->db->sql_freeresult($stmt);
				break;
			case "artist":
				$sSql="SELECT chart_element.idelement, chart_element.position, chart_element.position_ant, chart_element.createdon, DATEDIFF(CURDATE(),chart_element.positionsince) as pos_since,DATEDIFF(CURDATE(),chart_element.createdon) as chart_since, artist.idartist, artist.name as artist_name
					FROM ".$this->prefix."_topmusic_chart as chart
					  INNER JOIN ".$this->prefix."_topmusic_chart_element as chart_element ON (chart.idchart=chart_element.idchart)
					  INNER JOIN ".$this->prefix."_topmusic_artist as artist ON (chart_element.idelement=artist.idartist)
					WHERE chart.idchart=".$chartO->id."
					ORDER BY position";
				//print $sSql;
				$stmt=$this->db->sql_query($sSql);
				while($rs=$this->db->sql_fetchrow($stmt)){
					$chartElementO=new ChartElementAdminO(); 
					$chartElementO->idchart=$chartO->id;
					$chartElementO->position=$rs["position"];
					$chartElementO->position_ant=$rs["position_ant"];
					$chartElementO->createdon=$rs["createdon"];
					$chartElementO->positionsince=$rs["pos_since"];
					$chartElementO->chartsince=$rs["chart_since"];
					$artistO=new ArtistAdminO();
					$artistO->id=$rs["idelement"];
					$artistO->name=$rs["artist_name"];
					$chartElementO->element=$artistO;
					$chartO->elements[]=$chartElementO;
				}
				$this->db->sql_freeresult($stmt);
				break;
		}  

	  //print_r($chartO);
		return $chartO;	
	}
	
	function getChartList($language){
			$sSql="	select *
				from ".$this->prefix."_topmusic_chart chart
  				inner join ".$this->prefix."_topmusic_chart_lang chart_lang on (chart.idchart=chart_lang.idchart)
				where chart_lang.language='".$language."'";
		//echo "sql=".$sSql."<br>";
		$stmt=$this->db->sql_query($sSql);
		while($rs=$this->db->sql_fetchrow($stmt)){
			$chartO=new ChartAdminO();
			$chartO->id=$rs['idchart'];
			$chartO->title=$rs['title'];
			$chartO->description=$rs['description'];
			$chartO->number_elements=$rs['number_elements'];
			$chartO->type=$rs['type'];
			$chartO->default=$rs['default'];
			$chartList[]=$chartO;
		}
		$this->db->sql_freeresult($stmt);
		return $chartList;
	}
	
	function newChart($chart,$language){
		$sSql="INSERT INTO ".$this->prefix."_topmusic_chart (type, number_elements) VALUES ('".$chart->type."',".$chart->number_elements.")";
		//print $sSql;
		$this->db->sql_query($sSql);
		$chart->id=$this->db->sql_nextid();
		$error=$this->db->sql_error();
		if($error['message']==""){
			$sSql="INSERT INTO ".$this->prefix."_topmusic_chart_lang (idchart,language,title,description) VALUES (".$chart->id.",'".$language."','".$chart->title."','".$chart->description."')";
			$this->db->sql_query($sSql);
		}
		if($chart->default){
			$sSql="UPDATE ".$this->prefix."_topmusic_chart chart set chart.default=0 where chart.default=1";
			$this->db->sql_query($sSql);
			$sSql="UPDATE ".$this->prefix."_topmusic_chart chart set chart.default=1 where idchart=".$chart->id;
			$this->db->sql_query($sSql);
		}
	}
	function setDefault($idchart){
		$sSql="UPDATE ".$this->prefix."_topmusic_chart chart set chart.default=0";
		//print $sSql;
		$this->db->sql_query($sSql);
		$error=$this->db->sql_error();
		if($error['message']==""){
			$sSql="UPDATE ".$this->prefix."_topmusic_chart chart set chart.default=1 where idchart=".$idchart;
			$this->db->sql_query($sSql);
		}
	}
	function delChart($idchart){
		$sSql="DELETE FROM ".$this->prefix."_topmusic_chart_element where idchart=".$idchart;
		//print $sSql;
		$this->db->sql_query($sSql);
		$error=$this->db->sql_error();
		if($error['message']==""){
			$sSql="DELETE FROM ".$this->prefix."_topmusic_chart_lang where idchart=".$idchart;
			$this->db->sql_query($sSql);
			//print $sSql;
			$error=$this->db->sql_error();
			if($error['message']==""){
				$sSql="DELETE FROM ".$this->prefix."_topmusic_chart where idchart=".$idchart;
				$this->db->sql_query($sSql);
			}
		}
	}
	function moveElements($chart,$newPos){
		for($i=0;$i<count($newPos);$i++){
			$pos_ant=($newPos[$i]+1);
			$pos=$i+1;
			$sSql="UPDATE ".$this->prefix."_topmusic_chart_element SET position=$pos,position_ant=$pos_ant,positionsince=now()  WHERE idchart=".$chart->id." and idelement=".$chart->elements[$newPos[$i]]->element->id;
			//echo $sSql."<br>";
			$this->db->sql_query($sSql);
		}
	}
	function newElement($chart,$idelement){
		$pos=count($chart->elements)+1;
		$sSql="INSERT INTO ".$this->prefix."_topmusic_chart_element (idchart,idelement,position,position_ant,createdon) VALUES (".$chart->id.",$idelement,$pos,$pos,now())";
		//echo "sql=$sSql";
		$this->db->sql_query($sSql);
	}
	function delElement($idchart,$idelement,$pos){
		$sSql="UPDATE ".$this->prefix."_topmusic_chart_element set position_ant=position,position=position-1,positionsince=now() where position>".$pos." AND idchart=".$idchart;
		//echo "sql=$sSql";
		$this->db->sql_query($sSql);
		$sSql="UPDATE ".$this->prefix."_topmusic_chart_element set position_ant=position,position=position-1,positionsince=now() where position>".$pos." AND idchart=".$idchart;
		//echo "sql=$sSql";
		$this->db->sql_query($sSql);
		$sSql="DELETE FROM ".$this->prefix."_topmusic_chart_element where idelement=$idelement AND idchart=".$idchart;
		//echo "sql=$sSql";
		$this->db->sql_query($sSql);
	}
}
?>
Return current item: Top Music module