<?php

						/*************************************\
						
						    Handshakes Professional (3.0)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////
						
						admin_validate_section("manage",7);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':
									manage_music();
									break;
									
								case 'delete':
									delete_music();
									break;
									
								case 'edit':
									edit_music();
									break;
									
								case 'save':
									save_music();
									break;
									
								case 'featured_add':
									featured_add_music();
									break;
									
								case 'featured_remove':
									featured_remove_music();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_music(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 7;
	$data["title_id"] = "53";	
	$p = $GLOBALS["Get"]->val("p");
	
	$sql_query_total  = "SELECT COUNT(*) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query = " FROM music WHERE mus_id > 100000 ";
	
	$valnames = array("search_text","search_for","sort_by","sort_order","genre");
	
	$values = $GLOBALS["Get"]->arr($valnames,1);
	
	if(!empty($values["search_text"]) && !empty($values["search_for"])){
		
		switch($values["search_for"]){
			case 'mus_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND mus_id=".$values["search_text"];
				break;
			case 'title':
				$sql_query.= " AND title LIKE '%".$values["search_text"]."%' ";
				break;
			case 'artist':
				$sql_query.= " AND artist LIKE '%".$values["search_text"]."%' ";
				break;
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND mem_id=".$values["search_text"];
				break;
		}//switch
		
	}//if
	
	if($values["genre"])
		$sql_query.= " AND genre LIKE '%|".$values["genre"]."|%'";
	
	switch($values["sort_by"]){
		case '':
			$sql_query.= " ORDER BY posted DESC ";
			break;
		case 'title':
			$sql_query.= " ORDER BY title ".$values["sort_order"];
			break;
		case 'artist':
			$sql_query.= " ORDER BY artist ".$values["sort_order"];
			break;
		case 'posted':
			$sql_query.= " ORDER BY posted ".$values["sort_order"];
			break;
		case 'featured':
			$sql_query.= " ORDER BY featured ".$values["sort_order"];
			break;
	}//switch
	
	$data["values"] = $values;
	$data["music"] = array();
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
	
	$sql_query.= " LIMIT ".$start.",30";
	$results = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$search_link = "index.php?section=music";	
	while(list($key,$val) = each($values)){
		$search_link.= "&".$key."=".urlencode(stripslashes(html_entity_decode($val)));
	}//while
	$data["pages"] = handle_multipages($p,$total,$search_link,30);
	
	$data["link"] = $search_link."&p=".$p;
	
	foreach($results as $result){
		if(hs_strlen($result["title"] > 30))
			$result["title"] = hs_substr($result["title"],0,27)."...";
			
		$media["id"]              = $result["mus_id"];
		$media["artist"]          = $result["artist"];
		$media["title"]           = $result["title"];
		$media["author"]          = $GLOBALS["Network"]->card($result["mem_id"]);
		$media["date"]            = extract_date($result["posted"]);
		$media["view"]            = URL."/index.php?page=music&section=view&mus_id=".$result["mus_id"];
		$media["edit"]            = "index.php?section=music&action=edit&mus_id=".$result["mus_id"]."&ret_link=".urlencode($data["link"]);
		$media["delete"]          = "index.php?section=music&action=delete&mus_id=".$result["mus_id"]."&ret_link=".urlencode($data["link"]);
		$media["featured"]        = $result["featured"];
		$media["featured_add"]    = "index.php?section=music&action=featured_add&mus_id=".$result["mus_id"]."&ret_link=".urlencode($data["link"]);
		$media["featured_remove"] = "index.php?section=music&action=featured_remove&mus_id=".$result["mus_id"]."&ret_link=".urlencode($data["link"]);
		
		$data["music"][] = $media;
	}//foreach
	
	$layout[0] = "music_list";
	
	admin_output($layout,$data);
	
}//manage_music

function edit_music(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 7;
	$data["title_id"] = "53";
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$mus_id = $GLOBALS["Get"]->type_val("mus_id","integer");
	$data["mus_id"] = $mus_id;
	
	build_form("edit_music",$mus_id);
	
	$layout[0] = "edit_music";
	
	admin_output($layout,$data);
	
}//edit_music

function save_music(){
	
	admin_validate();
	
	include_once(DOC_ROOT."/functions/form_functions.php");
	
	$values = form_handle_input("edit_music",1,1);
	
	if(!extract_null_value($values["err_id"])){
		$GLOBALS["uerr_id"] = $values["err_id"];
		edit_music();
		exit;
	}//if
	
	$values["mus_id"] = $GLOBALS["Get"]->type_val("mus_id","integer");
	
	$sql_query = "UPDATE music SET artist=':artist:',title=':title:',type=':type:',genre=':genre:',voting=':voting:'
				  WHERE mus_id=:mus_id:";
	
	$vals = $values;
	
	$GLOBALS["DB"]->values = $vals;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	load_page("index.php?section=music&action=edit&mus_id=".$values["mus_id"]."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));
	
}//save_music

function delete_music(){
	
	admin_validate();
	
	$mus_id = $GLOBALS["Get"]->val("mus_id");
	
	include_once(DOC_ROOT."/functions/delete_functions.php");
	
	if(is_array($mus_id) && count($mus_id) > 0){
		foreach($mus_id as $id){
			delete_media($id);
		}//foreach
	}//if
	elseif(!empty($mus_id))
		delete_media($mus_id);
		
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));		
	load_page($ret_link);
	
}//delete_music

function featured_add_music(){
	
	admin_validate();
	
	$mus_id = $GLOBALS["Get"]->val("mus_id");
	
	$sql_query = "UPDATE music SET featured='1' WHERE mus_id=".$mus_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_add_music

function featured_remove_music(){
	
	admin_validate();
	
	$mus_id = $GLOBALS["Get"]->val("mus_id");
	
	$sql_query = "UPDATE music SET featured='0' WHERE mus_id=".$mus_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_remove_music
						
?>