<?php

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

						///////////////////////////////////////
						
						admin_validate_section("manage",10);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									manage_hotornot();
									break;
									
								case 'voting':
									remove_from_voting();
									break;
									
								case 'edit':
									edit_photo();
									break;
									
								case 'save':
									save_photo();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_hotornot(){
	
	admin_validate();
	
	$p = $GLOBALS["Get"]->val("p");
	
	$data["section"] = "manage";
	$data["subsection"] = 10;
	$data["title_id"] = "29";	
	$search_link = "index.php?section=manage_hotornot";
	
	$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
	
	$data["values"] = $values;
	
	$start = $p*30;
	
	$ret_link = $search_link."&p=".$p;	
	
	$sql_query_total = "SELECT COUNT(pho_id) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query="FROM photos
				WHERE voting='1'";
	
	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 mem_id IN (".$values["search_text"].")";
				break;
			case 'pho_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND pho_id IN (".$values["search_text"].")";
				break;
			case 'caption':
				$captions = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($captions as $caption){
					$sql_query.= " caption LIKE '".$caption."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
		}//switch
	}//if
	
	if(!empty($values["sort_by"])){
		switch($values["sort_by"]){
			case 'rating':
				$sql_query.= " ORDER BY rating/votes ";
				break;
			case 'votes':
				$sql_query.= " ORDER BY votes ";
				break;
			case 'mem_id':
				$sql_query.= " ORDER BY mem_id ";
				break;
			case 'pho_id':
				$sql_query.= " ORDER BY pho_id ";
				break;
		}//switch
		if(!empty($values["sort_order"])){
			$sql_query.= $values["sort_order"];
		}//if
	}//if
	else{
		$sql_query.= " ORDER BY rating/votes DESC ";
	}//else
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
				
	$sql_query.= " LIMIT ".$start.",30";
	
	$result = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$data["pages"] = handle_multipages($p,$total,$search_link,30);	
	
	$photos = array();
	foreach($result as $res){
		$photo["photo"]   = $res["photo_small"];
		$photo["caption"] = hs_wordwrap($res["caption"],50);
		$photo["pho_id"]  = $res["pho_id"];		
		$photo["mem_id"]  = $res["mem_id"];
		$photo["rating"]  = @round($res["rating"]/$res["votes"],2);
		$photo["votes"]   = $res["votes"];
		$photo["delete"]  = "index.php?section=manage_photos&action=delete&pho_id=".$res["pho_id"]."&ret_link=".urlencode($ret_link);
		$photo["edit"]    = "index.php?section=manage_hotornot&action=edit&pho_id=".$res["pho_id"]."&ret_link=".urlencode($ret_link);
		$photo["voting"]  = "index.php?section=manage_hotornot&action=voting&pho_id=".$res["pho_id"]."&ret_link=".urlencode($ret_link);
		$photos[] = $photo;
	}//foreach
	
	$data["photos"] = $photos;
	
	$data["link"] = stripslashes(html_entity_decode($ret_link));
	
	$layout[0] = "hotornot_list";
	admin_output($layout,$data);
	
}//manage_hotornot

function edit_photo(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 10;
	$data["title_id"] = "29";	
	$pho_id = $GLOBALS["Get"]->type_val("pho_id","integer");
	$ret_link  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	$data["link"] = $ret_link;
	
	$sql_query = "SELECT * FROM photos WHERE pho_id=".$pho_id;
	$photo = $GLOBALS["DB"]->row($sql_query);
	$photo["rating"]  = @round($photo["rating"]/$photo["votes"],2);
	$data["photo"] = $photo;

	$layout[0] = "hotornot_edit_photo";
	
	admin_output($layout,$data);
	
}//edit_photo

function save_photo(){
	
	admin_validate();
		
	$ret_link  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	
	$vals = array("pho_id","caption","rating","votes");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$values["rating"] = round($values["rating"]*$values["votes"]);
	
	$sql_query = "UPDATE photos SET caption=':caption:',rating=':rating:',votes=':votes:'
				  WHERE pho_id=:pho_id:";
	
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page($ret_link);
	
}//save_photo

function remove_from_voting(){
	
	admin_validate();
	
	$pho_id = $GLOBALS["Get"]->val("pho_id");
	
	if(is_array($pho_id) && count($pho_id)){
		$sql_query = "UPDATE photos SET voting='0' WHERE pho_id IN (".implode(",",$pho_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif(!empty($pho_id)){
		$sql_query = "UPDATE photos SET voting='0' WHERE pho_id=".$pho_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//elseif
	
	$ret_link  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//remove_from_voting