<?php

function #entity#_countfield($fieldname,$limit=null){
	global $db;
	
	$filters=#entity#_makefilters();
		
	$query="select count(#mastertable#.#primarykey#) as c, $fieldname from #mastertable# ";
#iterator-multifields-multitable#			
		
	$query.=" where #mastertable#.#primarykey#!=0 ";
	
#iterator-multifields-multibridge#

	$params=array();
	$sqlfilters=#entity#_sqlfilters();
	$query.=$sqlfilters['clauses'];
	$params=array_merge($params,$sqlfilters['params']);
	
	$query.=" group by $fieldname ";
	
	if ($limit!=null) $query.=" order by c desc limit $limit ";
	else $query.=" order by $fieldname ";
			
	$rs=sql_prep($query,$db,$params);
	$counts=array();
	
	while ($myrow=sql_fetch_array($rs)){
		$c=$myrow['c'];
		if ($c<=0) continue;
		$key=$myrow[$fieldname];
		if (trim($key)=='') continue;
		$counts[$key]=$c;	
	}
	return $counts;	
	
}

function #entity#_sqlfilters(){
	$filters='';
	$params=array();
	$key=GETSTR('key');
	if ($key!='') $filters.=" and (#dispfield# like '%$key%') ";
		
#iterator-dimlist-gdimlist#
#iterator-multifields-multisqlfilter#				
#iterator-rangelist-gsrangesqlfilter#							
#iterator-daterangelist-gsdaterangesqlfilter#							
	
	return array('clauses'=>$filters,'params'=>$params);
}

function #entity#_makefilters(){
	$filters=array();
	$nfilters=array();
	
	$key=GETSTR('key');
	if ($key!='') {
		$filters['searchterm']=$key;
	}
	if (isset($_GET['visible'])) $filters['visible']=$_GET['visible'];

		
#iterator-dimlist-gdimfilter#	
#iterator-multifields-multifilter#	
#iterator-rangelist-gsrangefilter#	
#iterator-daterangelist-gsrangefilter#	
			
		
	return array('filters'=>$filters,'nfilters'=>$nfilters);
}

function #entity#_strfilters($filters,$nfilters=null){
	
	$filter='';
	foreach ($filters as $key=>$val){
		if (is_array($val)) foreach ($val as $k=>$v) $filter.='&'.$key.'['.urlencode($k).']=1';
		else $filter.='&'.urlencode($key).'='.urlencode($val);

	}
	
	if (is_array($nfilters)){
		foreach ($nfilters as $key=>$vals) {
			foreach ($vals as $val) $filter.="&neg__${key}[]=$val";	
		}
	}
		
	return $filter;
}

function #entity#_shownav($container, $cmd, $title,$fieldname,$multi=null){
	
	$bfilters=#entity#_makefilters();
	$basefilters=$bfilters['filters'];
	$nbasefilters=$bfilters['nfilters'];

	
	if (isset($basefilters[$fieldname])&&!$multi){
		$myfilters=$basefilters;
		unset($myfilters[$fieldname]);
		$filter=#entity#_strfilters($myfilters,$nbasefilters);
?>
<div class="navgroup ng_<?echo $fieldname;?>">
	<div class="navtitle"><?echo $title;?></div>
<?
	#entity#_shownegfilters($fieldname,$container,$cmd);
?>
	<div class="navfilter">
		<a href=# onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filter;?>');return false;">[x]</a> <?echo #entity#_dispname($fieldname,$basefilters[$fieldname]);?>
	</div>
</div><!-- navgroup -->
<?		
		return;
	}

// end inline breadcrumb

	$counts=#entity#_countfield($fieldname);
	if (count($counts)<=0) {
		if (count($nbasefilters[$fieldname])>0){
		?>
		<div class="navgroup ng_<?echo $fieldname;?>">
		<div class="navtitle"><?echo $title;?></div>
		<?	
			#entity#_shownegfilters($fieldname,$container,$cmd);
		?>
		</div><!-- navgroup -->
		<?
		}
		return;
	}
?>
<div class="navgroup ng_<?echo $fieldname;?>">
<div class="navtitle"><?echo $title;?></div>
<?	
	$bfilters=#entity#_makefilters();
	$basefilters=$bfilters['filters'];
	$nbasefilters=$bfilters['nfilters'];
	
	#entity#_shownegfilters($fieldname,$container,$cmd);
	
	foreach ($counts as $key=>$count){
		$dispname=#entity#_dispname($fieldname,$key);
		$myfilters=$basefilters;
		if ($multi){
			if (!is_array($myfilters[$multi])) $myfilters[$multi]=array();
			$myfilters[$multi][$key]=1;
			if ($basefilters[$multi][$key]) unset($myfilters[$multi][$key]);
		} else $myfilters[$fieldname]=$key;
		$filters=#entity#_strfilters($myfilters,$nbasefilters);
		//echo '<pre>';print_r($filters);echo '</pre>';
		/*
		todo: use your container ids for navigation, breadcrumb and record list
		*/
		
		?>
<div class="navfilter">
		<?
		if ($multi){
	?>		
		<input <?if ($basefilters[$multi][$key]) echo 'checked';?> type="checkbox" href=# onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filters;?>');return false;"> 
	<?		
		}

?>
	<a href=# onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filters;?>');return false;"><?echo htmlspecialchars($dispname);?></a> 
	<?
	if (!$multi||!$basefilters[$multi][$key]||true){ //remove true to hide refinement count for selected multi fields
	?>
	<em>(<?echo $count;?>)</em>
	<?}?>
	
	<?if (!$basefilters[$multi][$key]){?>
	<a class="filterneg" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filters;?>&neg__<?echo $fieldname;?>[]=<?echo htmlspecialchars($key);?>');return false;">
	<acronym title="exclude from results">[-]</acronym>
	</a>
	<?}?>
		
</div>
<?
	}
?>
</div><!-- navgroup -->
<?		
}


function #entity#_dispname($fieldname,$key){
	global $db;
	
	switch ($fieldname){
		/*
		todo: add field value to field name mapping
		*/
		default: return $key;
	}	
}

function #entity#_shownegfilters($fieldname,$container,$cmd){
	$bfilters=#entity#_makefilters();
	$filters=$bfilters['filters'];
	$nfilters=$bfilters['nfilters'];
			
	if (!is_array($nfilters)||!is_array($nfilters[$fieldname])||count($nfilters[$fieldname])==0) return;
?>
<div class="neglabel">Excluding:</div>
<div class="neggroup">
		<?foreach ($nfilters[$fieldname] as $item){
			$mynfilters=$nfilters;
			$key=array_search($item,$mynfilters[$fieldname]);
			if ($key!==false){
				unset($mynfilters[$fieldname][$key]);	
			}
			$strfilters=#entity#_strfilters($filters,$mynfilters);
		?>
		<div class="negfilter"><a class="filterclear" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $strfilters;?>');return false;">[x]</a> <?echo #entity#_dispname($fieldname,$item);?></div>
		<?}?>
</div>
<?	
}

function #entity#_showrange($container,$cmd, $title,$fieldname){
	global $db;
	
	$bfilters=#entity#_makefilters();
	$basefilters=$bfilters['filters'];
	$nbasefilters=$bfilters['nfilters'];
	$filters=$basefilters;
		

// start inline breadcrumb		
	if (is_numeric($filters[$fieldname.'_a'])||is_numeric($filters[$fieldname.'_b'])){
		$myfilters=$filters;
		unset($myfilters[$fieldname.'_a']);
		unset($myfilters[$fieldname.'_b']);
		$filter=#entity#_strfilters($myfilters,$nbasefilters);		
?>
<div class="navgroup ng_<?echo $fieldname;?>">
	<div class="navtitle"><?echo $title;?></div>
	<div class="navfilter">
<a class="filterclear" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filter;?>');return false;">[x]</a>
<?echo $filters[$fieldname.'_a'].' - '.$filters[$fieldname.'_b'];?>
	</div>
</div> 
<?	
		return;	
	}
// end inline breadcrumb

	$sqlfilters=#entity#_sqlfilters();	

	$params=array();
	
	$query="select min($fieldname) as a, max($fieldname) as b from #mastertable# where #mastertable#.#primarykey#!=0 ".$sqlfilters['clauses'];
	$params=array_merge($params,$sqlfilters['params']);
	
	$rs=sql_prep($query,$db,$params);
	$myrow=sql_fetch_array($rs);
	
	$min=$myrow['a'];
	$max=$myrow['b'];

	if ($min==$max) return;

	$filter=#entity#_strfilters($filters,$nbasefilters);		
			
?>
<div class="navgroup ng_<?echo $fieldname;?>">
	<div class="navtitle"><?echo $title;?></div>
		<input class="inpshort num" id="<?echo $fieldname?>_a" name="<?echo $fieldname;?>_a" value="<?echo $min;?>" style="width:60px;"> - 
		<input class="inpshort num" id="<?echo $fieldname?>_b" name="<?echo $fieldname;?>_b" value="<?echo $max;?>" style="width:60px;">
		<button onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filter;?>&<?echo $fieldname;?>_a='+gid('<?echo $fieldname;?>_a').value+'&<?echo $fieldname;?>_b='+gid('<?echo $fieldname;?>_b').value);return false;">Set</button>
</div>
<?	
		
}

function #entity#_showdaterange($container,$cmd, $title,$fieldname){
	global $db;
	
	$bfilters=#entity#_makefilters();
	$basefilters=$bfilters['filters'];
	$nbasefilters=$bfilters['nfilters'];
	$filters=$basefilters;
		

// start inline breadcrumb		
	if (isset($filters[$fieldname.'_a'])||isset($filters[$fieldname.'_b'])){
		$myfilters=$filters;
		unset($myfilters[$fieldname.'_a']);
		unset($myfilters[$fieldname.'_b']);
		$filter=#entity#_strfilters($myfilters,$nbasefilters);		
?>
<div class="navgroup ng_<?echo $fieldname;?>">
	<div class="navtitle"><?echo $title;?></div>
	<div class="navfilter">
<a class="filterclear" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filter;?>');return false;">[x]</a>
<?echo $filters[$fieldname.'_a'].' - '.$filters[$fieldname.'_b'];?>

	<?if ($filters[$fieldname.'_a']==$filters[$fieldname.'_b']){
		$nextfilters=$myfilters;
		$nextfilters[$fieldname.'_a']=date('Y-n-j',date2stamp($filters[$fieldname.'_a'])+3600*24);
		$nextfilters[$fieldname.'_b']=$nextfilters[$fieldname.'_a'];
		$nextfilter=#entity#_strfilters($nextfilters,$nbasefilters);
	
		$prevfilters=$myfilters;
		$prevfilters[$fieldname.'_a']=date('Y-n-j',date2stamp($filters[$fieldname.'_a'])-3600*24);
		$prevfilters[$fieldname.'_b']=$prevfilters[$fieldname.'_a'];
		$prevfilter=#entity#_strfilters($prevfilters,$nbasefilters);
			
		?>
			<div style="padding-top:5px;font-size:11px;text-align:center;">
				<a class="hovlink" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $prevfilter;?>');return false;">&laquo; Prev Day</a>
				&nbsp; | &nbsp;
				<a class="hovlink" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $nextfilter;?>');return false;">Next Day &raquo;</a>
			</div>
	<?}
	$stampa=date2stamp($filters[$fieldname.'_a']);
	$stampb=date2stamp($filters[$fieldname.'_b']);
	$ndays=round(($stampb-$stampa)/3600/24);
	if ($ndays>=4&&$ndays<=8){
		$nextfilters=$myfilters;
		$nextfilters[$fieldname.'_a']=date('Y-n-j',$stampa+3600*24*7);
		$nextfilters[$fieldname.'_b']=date('Y-n-j',$stampb+3600*24*7);
		$nextfilter=#entity#_strfilters($nextfilters,$nbasefilters);
	
		$prevfilters=$myfilters;
		$prevfilters[$fieldname.'_a']=date('Y-n-j',$stampa-3600*24*7);
		$prevfilters[$fieldname.'_b']=date('Y-n-j',$stampb-3600*24*7);
		$prevfilter=#entity#_strfilters($prevfilters,$nbasefilters);
			
		?>
			<div style="padding-top:5px;font-size:11px;text-align:center;">
				<a class="hovlink" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $prevfilter;?>');return false;">&laquo; Prev Week</a>
				&nbsp; | &nbsp;
				<a class="hovlink" onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $nextfilter;?>');return false;">Next Week &raquo;</a>
			</div>
		<?
		
	}
	?>

	</div>
</div> 
<?	
		return;	
	}
// end inline breadcrumb

	$sqlfilters=#entity#_sqlfilters();	

	$params=array();
	
	$query="select min($fieldname) as a, max($fieldname) as b from #mastertable# where #mastertable#.#primarykey#!=0 ".$sqlfilters['clauses'];
	$params=array_merge($params,$sqlfilters['params']);
	
	$rs=sql_prep($query,$db,$params);
	$myrow=sql_fetch_array($rs);
	
	$min=date('Y-n-j',$myrow['a']);
	$max=date('Y-n-j',$myrow['b']);

	if ($min==$max) return;

	$filter=#entity#_strfilters($filters,$nbasefilters);		
			
?>
<div class="navgroup ng_<?echo $fieldname;?>">
	<div class="navtitle"><?echo $title;?></div>
		<input onfocus="pickdate(this);" onkeyup="_pickdate(this);" class="inp" id="<?echo $fieldname?>_a" name="<?echo $fieldname;?>_a" value="<?echo $min;?>" style="width:80px;"> - 
		<input onfocus="pickdate(this);" onkeyup="_pickdate(this);" class="inp" id="<?echo $fieldname?>_b" name="<?echo $fieldname;?>_b" value="<?echo $max;?>" style="width:80px;">
		<button onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $filter;?>&<?echo $fieldname;?>_a='+gid('<?echo $fieldname;?>_a').value+'&<?echo $fieldname;?>_b='+gid('<?echo $fieldname;?>_b').value);return false;">Set</button>
</div>
<?	
		
}
	
function #entity#_shownavs($container, $cmd){
	$bfilters=#entity#_makefilters();
	$basefilter=$bfilters['filters'];
	$nbasefilter=$bfilters['nfilters'];
	$filters=#entity#_strfilters($basefilter,$nbasefilter);

	$vfilters=$basefilter;
	unset($vfilters['visible']);
	$vfilter=#entity#_strfilters($vfilters,$nbasefilter);

?>
	<div style="padding:10px;display:nonea;">
		<input id="searchfilter_#entity#" type="hiddena" value="<?echo $filters;?>" style="border:dashed 1px #dedede;width:100%;">
	</div>
<?	
	if (!$basefilter['visible']){
		
?>	
	<div class="navopen"><a onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $vfilter;?>&visible=1');">show filters</a></div>
<?			
	} else {
?>
	<div class="navclose"><a onclick="nav_setfilter('<?echo $container;?>','#searchkey#','<?echo $cmd;?>','<?echo $vfilter;?>&visible=0');">hide filters</a></div>	
<?			
		
#iterator-dimlist-gsshowdim#	
#iterator-multifields-gsshowmulti#
#iterator-rangelist-gsrangeshownav#
#iterator-daterangelist-gsdaterangeshownav#
	}
	
}


