<?php

						/*************************************\

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

						\*************************************/

						include_once(DOC_ROOT."/classes/Clubs.class.php");

						///////////////////////////////////////

						admin_validate_section("manage",5);
						$action = $GLOBALS["Get"]->val("action");

							switch($action){

								case '':
									manage_clubs();
									break;

								case 'delete':
									do_delete_clubs();
									break;

								case 'edit':
									edit_club();
									break;

								case 'save_club':
									save_club();
									break;

								case 'remove_member':
									remove_member();
									break;

								case 'delete_photo':
									do_delete_photo();
									break;

								case 'edit_photo':
									edit_photo();
									break;

								case 'do_edit_photo':
									do_edit_photo();
									break;

								case 'update_categories':
									update_categories();
									break;

								case 'featured_add':
									featured_add_club();
									break;

								case 'featured_remove':
									featured_remove_club();
									break;

							}//switch

						///////////////////////////////////////

function manage_clubs(){

	admin_validate();

	$data["section"] = "manage";
	$data["subsection"] = 5;
	$data["title_id"] = "25";
	$p = $GLOBALS["Get"]->val("p");

	$sql_query_total  = "SELECT COUNT(*) FROM clubs,members WHERE clubs.mem_id=members.mem_id AND clubs.clb_id > 100000 ";
	$sql_query_result = "SELECT clubs.featured,clubs.clb_id,clubs.name,clubs.mem_id,clubs.created,count(members_clubs.mem_id) AS total_members ";

	$sql_query_result.= "FROM clubs,members,members_clubs
				  		 WHERE clubs.mem_id=members.mem_id
				  		 AND clubs.clb_id=members_clubs.clb_id
				  		 AND clubs.clb_id > 100000 ";

	$valnames = array("search_text","search_for","sort_by","sort_order","category","subcategory");

	$values = $GLOBALS["Get"]->arr($valnames,1);

	if(!empty($values["search_text"]) && !empty($values["search_for"])){

		switch($values["search_for"]){
			case 'clb_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND clubs.clb_id=".$values["search_text"];
				break;
			case 'name':
				$sql_query.= " AND clubs.name LIKE '%".$values["search_text"]."%' ";
				break;
			case 'description':
				$sql_query.= " AND clubs.description LIKE '%".$values["search_text"]."%' ";
				break;
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND clubs.mem_id=".$values["search_text"];
				break;
			case 'fname':
				$sql_query.= " AND members.fname LIKE '".$values["search_text"]."%' ";
				break;
			case 'lname':
				$sql_query.= " AND members.lname LIKE '".$values["search_text"]."%' ";
				break;
			case 'email':
				$sql_query.= " AND members.email LIKE '".$values["search_text"]."%' ";
				break;
		}//switch

	}//if

	if(!empty($values["category"]))
		$sql_query.= " AND clubs.category=".$values["category"];

	if(!empty($values["subcategory"]))
		$sql_query.= " AND clubs.subcategory=".$values["subcategory"];

	$sql_query_total.= $sql_query;

	$sql_query.= " GROUP BY clubs.clb_id ";

	switch($values["sort_by"]){
		case '':
			$sql_query.= " ORDER BY clubs.created DESC ";
			break;
		case 'name':
			$sql_query.= " ORDER BY clubs.name ".$values["sort_order"];
			break;
		case 'clb_id':
			$sql_query.= " ORDER BY clubs.clb_id ".$values["sort_order"];
			break;
		case 'created':
			$sql_query.= " ORDER BY clubs.created ".$values["sort_order"];
			break;
		case 'members':
			$sql_query.= " ORDER BY total_members ".$values["sort_order"];
			break;
		case 'featured':
			$sql_query.= " ORDER BY featured ".$values["sort_order"];
			break;
	}//switch

	$data["values"] = $values;
	$data["clubs"] = array();

	$start = $p*30;
	$total = $GLOBALS["DB"]->single($sql_query_total);

	$sql_query.= " LIMIT ".$start.",30";
	$results = $GLOBALS["DB"]->result($sql_query_result.$sql_query);


	$search_link = "index.php?section=manage_clubs";
	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["name"] > 30))
			$result["name"] = hs_substr($result["name"],0,27)."...";

		$club["id"] = $result["clb_id"];
		$club["name"] = $result["name"];
		$club["moderator"] = implode(" ",$GLOBALS["Member"]->name($result["mem_id"]));
		$club["date"] = extract_date($result["created"]);
		$club["view"] = build_link("club",$result["clb_id"]);
		$club["edit"] = "index.php?section=manage_clubs&action=edit&clb_id=".$result["clb_id"]."&ret_link=".urlencode($data["link"]);
		$club["delete"] = "index.php?section=manage_clubs&action=delete&clb_id=".$result["clb_id"]."&ret_link=".urlencode($data["link"]);
		$club["featured"] = $result["featured"];
		$club["featured_add"] = "index.php?section=manage_clubs&action=featured_add&clb_id=".$result["clb_id"]."&ret_link=".urlencode($data["link"]);
		$club["featured_remove"] = "index.php?section=manage_clubs&action=featured_remove&clb_id=".$result["clb_id"]."&ret_link=".urlencode($data["link"]);
		$club["members"] = $result["total_members"];
		$data["clubs"][] = $club;
	}//foreach

	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/clb_categories.inc.php");

	$jscode = "";
	while(list($key,$val) = each($categories)){
		$jscode.= "HS_Categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
	}//while
	while(list($key,$val) = each($subcategories)){
		$jscode.= "HS_SubCategories[".$key."] = new Array;\n";
		while(list($k,$v) = each($val)){
			$jscode.= "HS_SubCategories[".$key."][".$k."] = \"".hs_ereg_replace('"','\"',$v)."\";\n";
		}//while
	}//while

	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	var HS_Categories = new Array;
	var HS_SubCategories = new Array;
	var Category = 0;
	var SubCategory = 0;
	function HS_InitCategories(){
		$jscode
	}//HS_InitCategories
	function HS_PopulateSubCategoriesSimple(){
		var FormHandler = document.clubs_search;
		Category = FormHandler.category.options[FormHandler.category.selectedIndex].value;
		for(var i=FormHandler.subcategory.options.length-1; i>=0; i--){
			FormHandler.subcategory.options[i] = null;
		}//for
		FormHandler.subcategory.options[0] = new Option("Please, select...","0");
		var k=1;
		for(var Item in HS_SubCategories[Category]){
			FormHandler.subcategory.options[k] = new Option(HS_SubCategories[Category][Item],Item);
			k++;
		}//for
	}//HS_PopulateSubCategoriesSimple
//-->
</script>
JAVASCRIPTCODE;

	$data["onload"] = "HS_InitCategories();";

	$data["categories"] = $categories;
	$data["subcategories"] = $subcategories;

	$layout[0] = "clubs_list";

	admin_output($layout,$data);

}//manage_clubs

function edit_club(){

	admin_validate();

	$data["section"] = "manage";
	$data["subsection"] = 5;
	$data["title_id"] = "25";

	$sql_query = "SELECT onload,code FROM javascripts WHERE (page='clubs_edit') AND theme='".$GLOBALS["THEME"]."'";
	$result = $GLOBALS["DB"]->row($sql_query);
	$result["code"] = ereg_replace('"index.php"','"../index.php"',$result["code"]);

	$data["javascript"] = handle_js_patterns($result["code"]);
	$data["onload"]     = handle_js_patterns($result["onload"]);

	$ret_link  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	$data["link"] = $ret_link;

	$clb_id = $GLOBALS["Get"]->type_val("clb_id","integer");
	$data["clb_id"] = $clb_id;

	$ret_link2 = "index.php?section=manage_clubs&action=edit_member&clb_id=".$clb_id."&ret_link=".urlencode($ret_link);

	build_form("manage_club",$clb_id);
	build_form("manage_moderator",$clb_id);

	$data["htmlareas"] = "";
	$forms = $GLOBALS["forms"];
	foreach($forms as $form){
		while(list($key,$val) = each($form["types"])){
			if($val == "htmlarea"){
				$data["htmlareas"].= "html_".$form["names"][$key].",";
			}//if
		}//while
	}//foreach

	$club_members = $GLOBALS["Clubs"]->members($clb_id);
	$owner = $GLOBALS["Clubs"]->owner($clb_id);
	foreach($club_members as $member){
		if($member != $owner){
			$card["name"]   = implode(" ",$GLOBALS["Member"]->name($member));
			$card["mem_id"] = $member;
			$card["delete"] = "index.php?section=manage_clubs&action=remove_member&clb_id=".$clb_id."&per_id=".$member."&ret_link=".urlencode($ret_link2);
			$data["cards"][] = $card;
		}//if
	}//foreach

	$sql_query="SELECT *
				FROM club_photos
				WHERE clb_id=".$clb_id;

	$result = $GLOBALS["DB"]->result($sql_query);
	$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["delete"]  = "index.php?section=manage_clubs&action=delete_photo&pho_id=".$res["pho_id"]."&ret_link=".urlencode($ret_link2);
		$photo["edit"]    = "javascript:HS_Popup('index.php?section=manage_clubs&action=edit_photo&pho_id=".$res["pho_id"]."','width=400,height=400,scrollbars=no,menubar=no,resizable=no,location=no')";
		$photos[] = $photo;
	}//foreach

	$data["photos"] = $photos;

	$layout[0] = "edit_club";

	admin_output($layout,$data);

}//edit_club

function remove_member(){

	admin_validate();
	$clb_id = $GLOBALS["Get"]->val("clb_id");
	$per_id = $GLOBALS["Get"]->val("per_id");

	$GLOBALS["Clubs"]->remove_member($per_id,$clb_id);

	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page("index.php?section=manage_clubs&action=edit&clb_id=".$clb_id."&ret_link=".urlencode($ret_link));

}//remove_member

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");

	$clb_id = delete_club_photo($pho_id);

	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);
	load_page("index.php?section=manage_clubs&action=edit&clb_id=".$clb_id."&ret_link=".urlencode($ret_link));

}//do_delete_photo

function save_club(){

	admin_validate();

	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/modules/clubs.inc.php");
	include_once(DOC_ROOT."/functions/form_functions.php");

	$values = form_handle_input("manage_club",1,1);
	$moderator = form_handle_input("manage_moderator",1,1);
	$values["moderator"] = $moderator["moderator"];

	if(!extract_null_value($values["err_id"])){
		$GLOBALS["uerr_id"] = $values["err_id"];
		edit_club();
		exit;
	}//if

	$sql_query = "SELECT folder FROM clubs
				  WHERE clb_id=".$values["clb_id"];
	$folder = $GLOBALS["DB"]->single($sql_query);
	if($folder != $values["folder"]){
		$exists = $GLOBALS["Clubs"]->verify_folder($values["folder"]);
		if($exists){
			$GLOBALS["uerr_id"] = 24;
			edit_club();
			exit;
		}//if
		if(file_exists($clubs["path"]."/".$folder))
		rename($clubs["path"]."/".$folder,$clubs["path"]."/".$values["folder"]);
	}//if

	$geo = build_geo_position($values["country"],$values["state"],$values["city"],$values["postal_code"]);
	$values["latitude"]  = $geo["latitude"];
	$values["longitude"] = $geo["longitude"];
	$values["state"]     = $geo["state"];
	$values["city"]      = $geo["city"];

	$sql_query = "UPDATE clubs SET
                  name=':name:',mem_id=:moderator:,brief=':brief:',description=':description:',description_plain=':description_plain:',
                  country=':country:',state=':state:',city=':city:',postal_code=':postal_code:',
                  latitude=':latitude:',longitude=':longitude:',
                  category=:category:,subcategory=:subcategory:,
                  membership=':membership:',folder=':folder:',updated=:time:
                  WHERE clb_id=:clb_id:";

	$values["time"] = time();
	$values["description_plain"] = strip_tags(html_entity_decode($values["description"]));
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);

	$sql_query = "UPDATE forums SET title=':title:' WHERE clb_id=:clb_id:";
	$vals["clb_id"] = $values["clb_id"];
	$vals["title"] = $values["name"];
	$GLOBALS["DB"]->values = $vals;
	$GLOBALS["DB"]->execute($sql_query);

	$ret_link = $GLOBALS["Get"]->val("ret_link");
	load_page("index.php?section=manage_clubs&action=edit&clb_id=".$values["clb_id"]."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));

}//save_club

function do_delete_clubs(){

	admin_validate();

	$clb_id = $GLOBALS["Get"]->val("clb_id");

	include_once(DOC_ROOT."/functions/delete_functions.php");

	if(is_array($clb_id) && count($clb_id) > 0){
		foreach($clb_id as $id){
			delete_club($id);
		}//foreach
	}//if
	elseif(!empty($clb_id))
		delete_club($clb_id);

	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);

}//do_delete_clubs

function edit_photo(){

	$admin = admin_validate();
	$pho_id = $GLOBALS["Get"]->val("pho_id");

	$sql_query="SELECT photo,caption
				FROM club_photos
				WHERE pho_id=".$pho_id;

	$result = $GLOBALS["DB"]->row($sql_query);

	$data["image"] = $result["photo"];
	$data["caption"] = $result["caption"];
	$data["pho_id"] = $pho_id;
	$data["section"] = "manage_clubs";
	$data["action"] = "do_edit_photo";
	$data["title_id"] = "26";
	$layout[0] = "edit_club_photo";
	admin_popup_output($layout,$data);

}//edit_photo

function do_edit_photo(){

	$admin = admin_validate();
	$pho_id = $GLOBALS["Get"]->val("pho_id");
	$caption = $GLOBALS["Get"]->val("caption");

	$sql_query="UPDATE club_photos
				SET caption=':0:'
				WHERE pho_id=".$pho_id;

	$GLOBALS["DB"]->values = array ($caption);
	$GLOBALS["DB"]->execute($sql_query);

	admin_popup_close(0);

}//do_edit_photo

function update_categories(){

	admin_validate();

	$cats = stripslashes(html_entity_decode($GLOBALS["Get"]->val("res_categories")));
	$subcats = stripslashes(html_entity_decode($GLOBALS["Get"]->val("res_subcategories")));
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	$cats = hs_ereg_replace(preg_quote("&#124;"),"|",$cats);
	$subcats = hs_ereg_replace(preg_quote("&#124;"),"|",$subcats);

	$cats_exp = handle_empties(explode("==|==",$cats));
	$subcats_exp = handle_empties(explode("==|==",$subcats));

	$e_cats = array();
	$e_subcats = array();

	foreach($cats_exp as $cat){
		$its = explode("|",$cat);
		$e_cats[$its[0]] = $its[1];
	}//foreach

	foreach($subcats_exp as $subcat){
		$its = explode("|",$subcat);
		$e_subcats[$its[0]][$its[1]] = $its[2];
	}//foreach

	$str = "<?php\n\n";
	while(list($key,$val) = each($e_cats)){
		$str.= "\$categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
		if(is_array($e_subcats[$key])){
			while(list($k,$v) = each($e_subcats[$key])){
				$str.= "\$subcategories[".$key."][".$k."] = \"".hs_ereg_replace('"','\"',$v)."\";\n";
			}//while
		}//if
	}//while
	$str.= "\n?>";

	$file_handler = fopen(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/clb_categories.inc.php","w");
	fwrite($file_handler,$str);
	fclose($file_handler);

	load_page($ret_link);

}//update_categories

function featured_add_club(){

	admin_validate();

	$clb_id = $GLOBALS["Get"]->val("clb_id");

	$sql_query = "UPDATE clubs SET featured='1',featured_end=0 WHERE clb_id=".$clb_id;
	$GLOBALS["DB"]->execute($sql_query);

	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);

}//featured_add_club

function featured_remove_club(){

	admin_validate();

	$clb_id = $GLOBALS["Get"]->val("clb_id");

	$sql_query = "UPDATE clubs SET featured='0' WHERE clb_id=".$clb_id;
	$GLOBALS["DB"]->execute($sql_query);

	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);

}//featured_remove_club

?>