<?php

						/*************************************\
						
						    Handshakes Professional (3.3)
						            by DZOIC LTD
						       Copyright(c) 2005-2007
						        http://www.dzoic.com
						
						\*************************************/
						
						include_once(DOC_ROOT."/classes/Classifieds.class.php");

						///////////////////////////////////////
						
						admin_validate_section("manage",3);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':
									manage_classifieds();
									break;
									
								case 'delete':
									do_delete_classifieds();
									break;
									
								case 'edit':
									edit_classified_ad();
									break;
									
								case 'save':
									save_classified_ad();
									break;
									
								case 'update_categories':
									update_categories();
									break;
									
								case 'featured_add':
									featured_add_classified_ad();
									break;
									
								case 'featured_remove':
									featured_remove_classified_ad();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_classifieds(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 3;
	$data["title_id"] = "24";	
	$p = $GLOBALS["Get"]->val("p");
	
	$sql_query_total = "SELECT COUNT(cls_id) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query = "FROM classifieds,members
				  WHERE classifieds.mem_id=members.mem_id
				  AND classifieds.cls_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 'cls_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND cls_id=".$values["search_text"];
				break;
			case 'title':
				$sql_query.= " AND title LIKE '%".$values["search_text"]."%' ";
				break;
			case 'description':
				$sql_query.= " AND description LIKE '%".$values["search_text"]."%' ";
				break;
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND classifieds.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 classifieds.category=".$values["category"];
		
	if(!empty($values["subcategory"]))
		$sql_query.= " AND classifieds.subcategory=".$values["subcategory"];
	
	switch($values["sort_by"]){
		case '':
			$sql_query.= " ORDER BY posted DESC ";
			break;
		case 'title':
			$sql_query.= " ORDER BY title ".$values["sort_order"];
			break;
		case 'cls_id':
			$sql_query.= " ORDER BY cls_id ".$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["classifieds"] = 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=manage_classifieds";	
	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)."...";
			
		$classified["id"] = $result["cls_id"];
		$classified["title"] = $result["title"];
		$classified["author"] = implode(" ",$GLOBALS["Member"]->name($result["mem_id"]));
		$classified["date"] = extract_date($result["posted"]);
		$classified["view"] = build_link("classified_ad",$result["cls_id"]);		
		$classified["edit"] = "index.php?section=manage_classifieds&action=edit&cls_id=".$result["cls_id"]."&ret_link=".urlencode($data["link"]);
		$classified["delete"] = "index.php?section=manage_classifieds&action=delete&cls_id=".$result["cls_id"]."&ret_link=".urlencode($data["link"]);	
		$classified["featured"] = $result["featured"];
		$classified["featured_add"] = "index.php?section=manage_classifieds&action=featured_add&cls_id=".$result["cls_id"]."&ret_link=".urlencode($data["link"]);
		$classified["featured_remove"] = "index.php?section=manage_classifieds&action=featured_remove&cls_id=".$result["cls_id"]."&ret_link=".urlencode($data["link"]);
		$data["classifieds"][] = $classified;
	}//foreach
	
	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/cls_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.classifieds_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] = "classifieds_list";
	
	admin_output($layout,$data);
	
}//manage_classifieds

function edit_classified_ad(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 3;
	$data["title_id"] = "24";	
	
	$sql_query = "SELECT onload,code FROM javascripts WHERE (page='classifieds_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 = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$cls_id = $GLOBALS["Get"]->type_val("cls_id","integer");
	$data["cls_id"] = $cls_id;
	
	$sql_query = "SELECT photo_med FROM classifieds
				  WHERE cls_id=".$cls_id;
	
	$data["photo"] = $GLOBALS["DB"]->single($sql_query);
	
	build_form("classifieds_edit",$cls_id);
	
	$key = array_search("clb_id",$GLOBALS["forms"]["classifieds_edit"]["names"]);
	if($key){
		unset($GLOBALS["forms"]["classifieds_edit"]["names"][$key]);
		unset($GLOBALS["forms"]["classifieds_edit"]["types"][$key]);
		unset($GLOBALS["forms"]["classifieds_edit"]["labels"][$key]);
		unset($GLOBALS["forms"]["classifieds_edit"]["types"][$key]);
	}//if
	
	$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
	
	$layout[0] = "edit_classified_ad";
	
	admin_output($layout,$data);
	
}//edit_classified_ad

function save_classified_ad(){
	
	admin_validate();
	
	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/modules/classifieds.inc.php");
	include_once(DOC_ROOT."/functions/form_functions.php");
	
	$values = form_handle_input("classifieds_edit",1,1);
	
	if(!extract_null_value($values["err_id"])){
		$GLOBALS["uerr_id"] = $values["err_id"];
		edit_classified_ad();
		exit;
	}//if
	
	$values["cls_id"] = $GLOBALS["Get"]->type_val("cls_id","integer");
	
	$sql_query = "SELECT photo,photo_med,photo_small
				  FROM classifieds WHERE cls_id=".$values["cls_id"];
	$photo_ex = $GLOBALS["DB"]->row($sql_query);
	$sql_query = "SELECT folder FROM classifieds
				  WHERE cls_id=".$values["cls_id"];
	$folder = $GLOBALS["DB"]->single($sql_query);
	if($folder != $values["folder"]){
		$exists = $GLOBALS["Classifieds"]->verify_folder($values["folder"]);
		if($exists){
			$GLOBALS["uerr_id"] = 24;
			edit_classified_ad();
			exit;
		}//if
		if(file_exists($classifieds["path"]."/".$folder))
		rename($classifieds["path"]."/".$folder,$classifieds["path"]."/".$values["folder"]);
	}//if
			
	if(!empty($values["photo"][tmp])){

		if(hs_ereg("gif",$values["photo"][type])){
			$extension = ".gif";
		}//if
		elseif(hs_ereg("jpeg|jpg",$values["photo"][type])){
			$extension = ".jpeg";
		}//elseif
		
   		$photo = build_thumbnailes($values["photo"][tmp],$extension);
   	}//if
  	else{
		$photo = array(
   			"ex"    => $photo_ex["photo"],
   			"med"   => $photo_ex["photo_med"],
   			"small" => $photo_ex["photo_small"]
   		);
   	}//else
				
	$sql_query = "UPDATE classifieds SET title=':title:',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:,photo=':photo:',photo_med=':photo_med:',
				  photo_small=':photo_small:',audience=':audience:',
				  folder=':folder:',updated=:time:
				  WHERE cls_id=:cls_id:";				
	$values["time"]   = time();
	$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"];
	$values["photo"]       = $photo["ex"];
	$values["photo_med"]   = $photo["med"];
	$values["photo_small"] = $photo["small"];
	$values["description_plain"] = strip_tags(html_entity_decode($values["description"]));
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	load_page("index.php?section=manage_classifieds&action=edit&cls_id=".$values["cls_id"]."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));
	
}//save_classified_ad

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

function featured_add_classified_ad(){
	
	admin_validate();
	
	$cls_id = $GLOBALS["Get"]->val("cls_id");
	
	$sql_query = "UPDATE classifieds SET featured='1',featured_end=0 WHERE cls_id=".$cls_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_add_classified_ad

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

?>