<?php

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

						///////////////////////////////////////
						
						admin_validate_section("manage","albums");
						$action = $GLOBALS["Get"]->val("action");
						
							switch($action){								
										
								case 'albums':
									manage_albums();
									break;
									
								case 'delete_album':
									do_delete_album();
									break;
									
								case 'edit_album':
									edit_album();
									break;
									
								case 'do_edit_album':
									do_edit_album();
									break;
									
								case 'edit_photo':
									edit_photo();
									break;
									
								case 'do_edit_photo':
									do_edit_photo();
									break;
									
								case 'delete_photo':
									do_delete_photo();
									break;									
									
							}//switch
							
						///////////////////////////////////////
						
function manage_albums(){
	
	admin_validate();	
	
	$data["section"] = "manage";
	$data["subsection"] = "albums";
	$data["title_id"] = "Manage Albums";
	
	$search_link = "index.php?section=manage_albums&action=albums";	
	
	$form = array (
		"search_for",
		"search_text",
		"sort_by",
		"sort_order",
	);	
	
	foreach($form as $el){		
		$values[$el] = $GLOBALS["Get"]->val($el);
		$search_link.= "&".$el."=".urlencode(stripslashes(html_entity_decode($values[$el])));
	}//foreach
	
	$p = $GLOBALS["Get"]->val("p");
		
	$sql_query_total = "SELECT count(albums.alb_id) ";
	$sql_query_result = "SELECT members.mem_id, members.email, albums.title, albums.alb_id,albums.active ";
	
	$sql_query="FROM members,albums
				WHERE members.mem_id > 100000
				AND albums.mem_id = members.mem_id";
	
	if(!empty($values["search_for"]) && hs_strlen($values["search_text"]) > 0){
		$values["search_text"] = admin_replace_search_string($values["search_text"]);
		switch($values["search_for"]){
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND members.mem_id IN (".$values["search_text"].")";
				break;
			case 'email':
				$emails = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($emails as $email){
					$sql_query.= " members.email LIKE '".$email."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'fname':
				$fnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($fnames as $fname){
					$sql_query.= " members.fname LIKE '".$fname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'lname':
				$lnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($lnames as $lname){
					$sql_query.= " members.lname LIKE '".$lname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'title':
				$titles = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($titles as $title){
					$sql_query.= " albums.title LIKE '%".$title."%' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;				
		}//switch
	}//if
	$sql_query.= " GROUP BY alb_id";
	if(!empty($values["sort_by"])){
		switch($values["sort_by"]){
			case 'title':
				$sql_query.= " ORDER BY albums.title ";
				break;
			case 'mem_id':
				$sql_query.= " ORDER BY members.mem_id ";
				break;
		
		}//switch
		if(!empty($values["sort_order"])){
			$sql_query.= $values["sort_order"];
		}//if
	}//if
	
	$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);
	
	$cards = array ();	
	$i = 0;
	foreach($results as $result){
		$cards[$i]["name"] = implode(" ",$GLOBALS["Member"]->name($result["mem_id"]));
		if(hs_strlen($cards[$i]["name"]) > 18)
			$cards[$i]["name"] = hs_substr($cards[$i]["name"],0,16)."...";
				
		$cards[$i]["mem_id"]   = $result["mem_id"];
		$cards[$i]["view"]     = "../index.php?page=photos&section=browse&alb_id=".$result["alb_id"]."&per_id=".$result["mem_id"];
		$cards[$i]["edit"]     = "index.php?section=manage_albums&action=edit_album&alb_id=".$result["alb_id"]."&ret_link=".urlencode($search_link);
		$cards[$i]["delete"]   = "index.php?section=manage_albums&action=delete_album&alb_id=".$result["alb_id"]."&ret_link=".urlencode($search_link);
		$cards[$i]["member_edit"] = "index.php?section=manage_members&action=edit_member&mem_id=".$result["mem_id"]."&ret_link=".urlencode($link);
		$cards[$i]["email"] = $result["email"];
		$cards[$i]["title"] = $result["title"];
		$cards[$i]["active"] = $result["active"];
		$cards[$i]["alb_id"] = $result["alb_id"];
		$i++;

	}//foreach
	
	$data["cards"] = $cards;
	$data["cards_total"] = count($results);	
	
	$data["pages"] = handle_multipages($p,$total,$search_link,30);	
	$data["values"] = $values;	
	
	$data["link"] = $search_link."&p=".$p;
	
	$layout[0] = "albums_list";
	
	admin_output($layout,$data);
	
}//manage_members

function edit_album(){

	$admin = admin_validate();
	$data["section"] = "manage";
	$data["subsection"] = "albums";
	$data["title_id"] = "Manage Albums";
	$alb_id = $GLOBALS["Get"]->val("alb_id");
	
	$sql_query="SELECT title, description, active, pho_id
				FROM albums
				WHERE alb_id=".$alb_id;
	
	$result = $GLOBALS["DB"]->row($sql_query);
	
	$data["pho_small"] = extract_photo($result["pho_id"]);
	$data["title"] = $result["title"];
	$data["description"] = $result["description"];
	$data["active"] = $result["active"];
	$data["alb_id"] = $alb_id;
	$data["action"] = "do_edit_album";
	$data["title_id"] = "Edit Album";
	$data["ret_link"] = $GLOBALS["Get"]->val("ret_link");
	$layout[0] = "edit_album";
	admin_output($layout,$data);
	
}//edit_photo

function do_edit_album(){
	
	$admin = admin_validate();
	$alb_id = $GLOBALS["Get"]->val("alb_id");
	$data["title"] = $GLOBALS["Get"]->val("title");
	$data["description"] = $GLOBALS["Get"]->val("description");
	$data["active"] = $GLOBALS["Get"]->val("active");
	$data["ret_link"] = $GLOBALS["Get"]->val("ret_link");
	if (!$data["active"])
		$data["active"] = 0;
	//$values = array(addslashes($data["title"]),addslashes($data["description"]),$data["active"]);
	$sql_query="UPDATE albums
				SET title='".addslashes($data["title"])."',
				description = '".addslashes($data["description"])."',
				active = ".$data["active"]."
				WHERE alb_id=".$alb_id;
	
	//$GLOBALS["DB"]->values = array ($values);
	$GLOBALS["DB"]->execute($sql_query);
	$data["section"] = "manage";
	$data["subsection"] = 17;
	$data["title_id"] = "Manage Albums";
	$layout[0] = "albums_list";
	load_page(html_entity_decode($data["ret_link"]));
	
}//do_edit_photo

function do_delete_photo(){
	
	$admin = admin_validate();
	$pho_id = $GLOBALS["Get"]->val("pho_id");
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	
	include_once(DOC_ROOT."/functions/delete_functions.php");
	
	$mem_id = delete_photo($pho_id);
	
	load_page("index.php?section=manage_members&action=edit_member&mem_id=".$mem_id."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));
	
}//do_delete_photo

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


?>