<?php

/* No empty dropdowns please */
if ($field != '' AND $id != '') {
	$filtermd = $this->filtermd($filter);
	$out = wp_cache_get( $filtermd.'wct_select_'.$salt.$id.'_'.$field, 'wct');
	if ($out == false) {
		$limit = (integer)$limit;
		if ($limit <= '0') { $limit = '1'; }

		$qry = $wpdb->get_row("SHOW COLUMNS FROM `".$wpdb->prefix."wct".$id."` LIKE '".$field."';");
		if (substr($qry->Type,0,4) == "set(") {
			$sets = explode(",",substr($qry->Type,4,strlen($qry->Type)-5));
			unset($qry);
			$m = '0';
			foreach ($sets as $var => $wert) {
				$wert = substr($wert,1,strlen($wert)-2);
				$sql .= " sum( CASE WHEN LOCATE(\",".$wert.",\",CONCAT(',',".$field.", ',')) THEN 1 ELSE 0 END) as `feld_".$wert."`,";
			}
			$abfrage = "SELECT ".substr($sql,0,strlen($sql)-1)." FROM `".$wpdb->prefix."wct".$id."` WHERE `status`='active'".$filter." AND `setfeld`!='';";
			$abfrage = $wpdb->get_row($abfrage);
			foreach ($sets as $var => $wert) {
				$wert = substr($wert,1,strlen($wert)-2);
				$g = "feld_".$wert;
				if ($abfrage->$g >= '1') {
					$n = new stdClass();
					if ($field != '') { $n->item = $wert; }
					$n->anz = $abfrage->$g;
					$qry[$m] = $n;
					$m++;
				}
			}

			global $k, $l, $j;
			$j = 'anz'; $k='1'; $l='-1';

			if ($sort == 'anz DESC') { $j = 'anz'; $k='1'; $l='-1'; }
			elseif ($sort == 'anz ASC') { $j = 'anz'; $k='-1'; $l='1';  }
			elseif ($sort == 'name DESC') { $j = $field; $k='1'; $l='-1'; }
			else { $j = $field; $k='-1'; $l='1'; }

			usort($qry,array( &$this, 'wct_sort_object'));

		}
		else {
			$sort = str_replace('name',$field,$sort);
			$qry = $wpdb->get_results("SELECT `".mres($field)."` AS `item`,count(".mres($field).") AS `anz`  FROM `".$wpdb->prefix."wct".mres($id)."` WHERE `status`='active'".$filter." GROUP BY `".mres($field)."` ORDER BY ".$sort." LIMIT ".$limit.";");
		}
		if (count($qry) >= '1') {
			if ($multiselectjs == '1') { $feldnameaddon = rand(1000,9999); }
			$out = "<select class=\"wct-select\" ".($multiselectjs == '1' ? "" : "onChange=\"selector".$salt."('".$field."')\"")." style=\"font-weight: bold;\" id=\"".$salt."wct".($multiselectjs == '1' ? "mu" : "")."drop_".$field.($multiselectjs == '1' ? $feldnameaddon : "")."\" name=\"".$salt."wct".($multiselectjs == '1' ? "mu" : "")."drop_".$field.($multiselectjs == '1' ? "[]\" multiple=\"multiple" : "")."\">". ($multiselectjs == '1' ? "" : "<option style=\"font-weight: bold;\" value=\"NULL\">".$maintext."</option>");
			foreach ($qry as $row) {
				$jsvalue = $row->item;
				$out .= "<option class=\"wct-option\" style=\"font-weight: normal;\" value=\"".rawurlencode(base64_encode($jsvalue))."\" ";

				if ((base64_decode($_GET[$salt.'wctdrop']) == $jsvalue AND $row->item != '') OR ($_GET[$salt.'wctdrof'] != '' AND $_GET[$salt.'wctdrop'] != 'NULL' AND $row->item == '' AND $field == $_GET[$salt.'wctdrof'])) { $out .= " selected"; }
				if ($_GET[$salt.'wctmudrof'] != '') {
					if (strpos($_GET[$salt.'wctmudrop'],",") !== false) {
						$drops = explode(",",$_GET[$salt.'wctmudrop']);
					}
					else {
						$drops = array('0' => $_GET[$salt.'wctmudrop']);
					}
					foreach ($drops as $wert) {
						$wert = base64_decode($wert);
						if ($jsvalue == $wert AND $field == $_GET[$salt.'wctmudrof']) { $out .= " selected"; }
					}
				}

				$out .= ">";
				if ($row->item == '') { $out .= "-- ".__('Empty','wct')." --"; } else { $out .= $row->item; }
 
				$out .= " (".$row->anz.")</option>";
			}
			$out .= "</select>";
		}
		/* If field not existing or no options to dropdown, no cache! */
		if ($out != '') { wp_cache_set( $filtermd.'wct_select_'.$id.'_'.$salt.$field.$feldnameaddon, $out, 'wct', $this->settings['wct_cachetime']); }
	}
}

?>