<?php

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

						///////////////////////////////////////
							
						admin_validate_section("tools",4);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									manage_moderators();
									break;
									
								case 'add':
									add_moderator();
									break;
									
								case 'edit':
									edit_moderator();
									break;
								
								case 'save':
									save_moderator();
									break;
									
								case 'delete':
									delete_moderator();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_moderators(){
	
	admin_validate();
	
	$data["section"] = "tools";
	$data["subsection"] = 4;
	$data["title_id"] = "37";	
	$sql_query = "SELECT * FROM moderators";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$moderators = array();
	
	foreach($result as $res){
		$moderator["card"]   = $GLOBALS["Network"]->card($res["mem_id"]);
		$moderator["edit"]   = "index.php?section=moderators&action=edit&mem_id=".$res["mem_id"];
		$moderator["delete"] = "index.php?section=moderators&action=delete&mem_id=".$res["mem_id"];
		$moderators[] = $moderator;
	}//foreach
	
	$data["moderators"] = $moderators;
	
	$layout[0] = "moderators_list";
	
	admin_output($layout,$data);
	
}//manage_moderators

function add_moderator(){
	
	admin_validate();	
	
	$data["section"] = "tools";
	$data["subsection"] = 4;
	$data["title_id"] = "37";	$search_link = "index.php?section=moderators&action=add";
	
	$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(members.mem_id) ";
	$sql_query_result = "SELECT members.mem_id ";
	
	$sql_query="FROM members,profiles,network
				WHERE members.mem_id=profiles.mem_id
				AND members.mem_id=network.mem_id
				AND members.mem_id > 100000";
	
	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;				
		}//switch
	}//if
	
	if(!empty($values["sort_by"])){
		switch($values["sort_by"]){
			case 'email':
				$sql_query.= " ORDER BY members.email ";
				break;
			case 'fname':
				$sql_query.= " ORDER BY members.fname ";
				break;
			case 'lname':
				$sql_query.= " ORDER BY members.lname ";
				break;
			case 'jdate':
				$sql_query.= " ORDER BY profiles.joined ";
				break;
			case 'activity':
				$sql_query.= " ORDER BY profiles.last_click ";
				break;
			case 'handshakes':
				$sql_query.= " ORDER BY LENGTH(network.friends) ";
				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";
	$result = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$data["cards"] = admin_build_cards($result,$search_link."&p=".$p);
	$data["cards_total"] = count($result);	
	
	$data["pages"] = handle_multipages($p,$total,$search_link,30);	
	$data["values"] = $values;	
	
	$data["link"] = $search_link."&p=".$p;
	
	$layout[0] = "add_moderator";
	
	admin_output($layout,$data);
	
}//add_moderator

function edit_moderator(){
	
	admin_validate();
	
	$mem_id = $GLOBALS["Get"]->type_val("mem_id","integer");
	
	if(!$mem_id){
		$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
		load_page($ret_link."&err_id=37");
	}//if
	
	$data["section"] = "tools";
	$data["subsection"] = 4;
	$data["title_id"] = "37";	
	$data["mem_id"] = $mem_id;
	
	$data["moderator"] = $GLOBALS["Network"]->card($mem_id);
	$data["access"] = admin_sections();
	
	$sql_query = "SELECT access FROM moderators WHERE mem_id=".$mem_id;
	$access = $GLOBALS["DB"]->single($sql_query);
	$access_sections = handle_empties(explode("|",$access));
	
	foreach($access_sections as $sec){
		$dets = explode("_",$sec);
		$data["values"][$dets[0]][$dets[1]] = 1;
	}//foreach
	
	$data["link"] = "index.php?section=moderators";
	
	$data["javascript"] = <<<HTMLCODE
<script language="JavaScript">
<!--
function HS_TickAllAdvanced(SetName,FormHandler,Tracker){
	var FormElements = FormHandler.elements;
	var TickTracker = Tracker.checked;		
	for(i=0;i<FormElements.length;i++){
		FElement = FormHandler.elements[i];
		if( (FElement.type == "checkbox") && (FElement.name == SetName || FElement.name.indexOf(SetName) != -1) ){
			if(TickTracker)
				FElement.checked = true;
			else
				FElement.checked = false;
		}//if
	}//for
}//HS_TickAllAdvanced
//-->
</script>
HTMLCODE;
	
	$layout[0] = "edit_moderator";
	
	admin_output($layout,$data);
	
}//edit_moderator

function save_moderator(){
	
	admin_validate();
	
	$mem_id = $GLOBALS["Get"]->type_val("mem_id","integer");
	$subsections = $GLOBALS["Get"]->val("subsections");
	
	$access = "";
	
	while(list($key,$val) = each($subsections)){
		while(list($k,) = each($val)){
			$access.= $key."_".$k."|";
		}//while
	}//while
	
	$access = rtrim($access,"|");
	
	$sql_query = "REPLACE INTO moderators (mem_id,access) VALUES (:mem_id:,':access:')";
	$GLOBALS["DB"]->values = array("mem_id" => $mem_id, "access" => $access);
	$rows = $GLOBALS["DB"]->update($sql_query);
	
	if($rows > 1)
		load_page("index.php?section=moderators&action=edit&mem_id=".$mem_id);
	elseif($rows == 1){
		$sql_query = "UPDATE members SET member_type='m' WHERE mem_id=".$mem_id;
		$GLOBALS["DB"]->execute($sql_query);
		load_page("index.php?section=moderators");
	}//else
	
}//save_moderator

function delete_moderator(){
	
	admin_validate();
	
	$mem_id = $GLOBALS["Get"]->type_val("mem_id","integer");
	
	$sql_query = "DELETE FROM moderators WHERE mem_id=".$mem_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query = "UPDATE members SET member_type='u' WHERE mem_id=".$mem_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=moderators");
	
}//delete_moderator
						
?>