<?php
function list_entries($categ_uid, $district_location="", $page=1, $limit = 100) {
	$ret = array(
		"total_records"	=> 0,
		"data"			=> array()
	);
	$from = 0;

	
	if($page > 1) {
		$from = ($page-1) * $limit;
	}

	
	$q = 	"select 
				b.uid, b.name, b.website, b.friendly_name, b.description
			from 
				yp_businesses b, yp_businesses_categories c
			where
				b.uid=c.business_uid ";
				

	
	if($categ_uid != null && $categ_uid != "") {
		$q .= " and c.category_uid='$categ_uid' ";
	}
	
	if(trim($district_location) != "/" && trim($district_location) != "") {
		if(is_numeric($district_location)) {
			$q .= " and b.uid IN ( select l.business_uid from yp_businesses_locations l where l.business_uid=b.uid and l.district_id='$district') ";
		}
		else {
			$dl = explode("/", trim($district_location));
			if(count($dl) > 1) {
				$district = trim($dl[0]);
				$location = trim($dl[1]);
			}
			else {
				$district = trim($dl[0]);
				$location = "";
			}
			
			if($district != "" && $location != "") {
				$q .= " and b.uid IN ( select l.business_uid from yp_businesses_locations l where l.business_uid=b.uid and l.district='$district' and l.location='$location') ";
			}
			else {
				$q .= " and b.uid IN ( select l.business_uid from yp_businesses_locations l where l.business_uid=b.uid and l.district='$district') ";
			}
		}
		
	}
	
	$q .= " group by b.uid ";
	$q .= " order by b.name asc ";
	
	$rtmp = @mysql_query($q);
	if($rtmp && mysql_num_rows($rtmp) > 0) {
		$ret['total_records'] = mysql_num_rows($rtmp);
	}
	
	$q .= " limit $from, $limit";
	
	//echo $q;
	
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$b = array();
		while ($row = mysql_fetch_assoc($r)) {
			$uid = $row['uid'];
			$name = stripslashes($row['name']);
			$friendly_name = $row['friendly_name'];
			
			$b[$uid] = array(
				"name"		=> $name,
				"friendly_name"		=> $friendly_name,
				"locations"	=> array()
			);
		}
		
		$uids = array();
		foreach (array_keys($b) as $i=>$buid) {
			$uids [] = "'" . $buid . "'";
		}
		
		$uids_str = implode(",", $uids);
		$q = "select * from yp_businesses_locations where business_uid in ($uids_str) ";
		if(trim($district_location) != "" && trim($district_location) != "/") {
			if(is_numeric(trim($district_location))) {
				$district_id = intval(trim($district_location));
				$q .= " and district_id='$district_id' ";
			}
			else {
				if(isset($district) && $district != "") {
					$q .= " and district='$district' ";
				}
				
				if(isset($location) && $location != "") {
					$q .= " and location='$location' ";
				}
			}
		}
		
		$q .= " order by district asc;";
		
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			while ($row = mysql_fetch_assoc($r)) {
				$buid = $row['business_uid'];
				$loc_tmp = $row;
				if(isset($location) && $location != "") {
					$loc_tmp['show_location'] = 1;
				}
				
				$b[$buid]['locations'][] = array_map('stripslashes', $loc_tmp);
			}
		}
		
		$ret['data'] = $b;
		
	}
	
	return $ret;
}

function list_all_entries($categs_str, $district="", $page=1, $limit=10) {
	$ret = array(
		"total_records"	=> 0,
		"data"			=> array()
	);
	$from = 0;
	if(!isset($limit)) $limit = 10;
	if($page > 1) {
		$from = $page * $limit;
	}
	
	$q = 	"select 
				b.uid, b.name, b.website, b.friendly_name, b.description
			from 
				yp_businesses b, yp_businesses_categories c
			where
				b.uid=c.business_uid ";
				

	
	if($categs_str != null && $categs_str != "") {
		$q .= " and c.category_uid in ($categs_str) ";
	}
	
	if($district != "") {
		if(is_numeric($district)) {
			$q .= " and exists ( select * from yp_businesses_categories l where l.business_uid=b.uid and l.district_id='$district') ";
		}
		else {
			$q .= " and exists ( select * from yp_businesses_categories l where l.business_uid=b.uid and l.district='$district') ";
		}
	}
	
	$q .= " group by b.uid ";
	$q .= " order by b.name asc ";
	
	$rtmp = @mysql_query($q);
	if($rtmp && mysql_num_rows($rtmp) > 0) {
		$ret['total_records'] = mysql_num_rows($rtmp);
	}
	$q .= " limit $from, $limit";
	
	
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$b = array();
		while ($row = mysql_fetch_assoc($r)) {
			$uid = $row['uid'];
			$name = stripslashes($row['name']);
			$friendly_name = $row['friendly_name'];
			
			$b[$uid] = array(
				"name"		=> $name,
				"friendly_name"		=> $friendly_name,
				"locations"	=> array()
			);
		}
		
		$uids = array();
		foreach (array_keys($b) as $i=>$buid) {
			$uids [] = "'" . $buid . "'";
		}
		
		$uids_str = implode(",", $uids);
		$q = "select * from yp_businesses_locations where business_uid in ($uids_str) ";
		if($district != "") {
			if(is_numeric($district)) {
				$q .= " and district_id='$district' ";
			}
			else {
				$q .= " district='$district' ";
			}
		}
		
		$q .= " order by district asc;";
		
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			while ($row = mysql_fetch_assoc($r)) {
				$buid = $row['business_uid'];
				$b[$buid]['locations'][] = array_map('stripslashes', $row);
			}
		}
		
		$ret['data'] = $b;
		
	}
	
	return $ret;
}

?>