<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/FrontendAutocomplete.inc.php" );

	class NativeLanguage extends FrontendAutocomplete {

		protected function getLanguageKeys() {
			return FNTH_db_get_associative(
				sprintf( "select language_id,lower(langdef_name) as language from language_langdefs where site_language_id=%d",$GLOBALS["langcode"] ),
				"language",
				"language_id" );
		}


		protected function getAutocompleteDatas( $filter ) {
			$retval = array();
			$retval["numofitems"]=0;
			$retval["items"]=array();
			if (empty( $filter )) return $retval;

			$prefixpos = (strrpos( $filter , "," ));
			$exclude_ids = array();
			$exstring="";
			if ($prefixpos===false) {
				$prefix="";
			} else {
				$prefix=trim( substr( $filter , 0 , $prefixpos + 1 ) ) . " ";	// prefix => minden az utolso vesszo elott + space
				$langs = array_map( "trim" , explode( "," , FNTH_strtolower( trim( $prefix ) ) ) );	// lowercase trimmelt eddigi nyelvek
				$alllang = $this->getLanguageKeys();		// osszes nyelv asszociativ tombben
				foreach ($langs as $lang) if (isset( $alllang[$lang] )) $exclude_ids[]=$alllang[$lang];	// meglevo nyelvek az exclude idk
				if (!empty( $exclude_ids )) $exstring=sprintf( " and language_id not in (%s)",implode(",",$exclude_ids) );	// exclude_id string

				$filter = trim( substr( $filter , $prefixpos + 1 ) );
			}
			$query = sprintf( "select language_id,langdef_name as language from language_langdefs where site_language_id=%d and langdef_name ilike '%s%%' %s order by langdef_ordering",
				$GLOBALS["langcode"],
				FNTH_db_addslashes( $filter ),
				$exstring );
			$ds = FNTH_db_get_resultset( $query );
			$i=0;
			foreach ($ds as $row) {
				$name = $row["language"];
				$tooltip = str_replace( $filter , sprintf( "<span class=\"hl\">%s</span>" , $filter ) , $name );
				$retval["items"][]=array(
					"value" => $prefix.$name,
					"title" => $prefix.$tooltip );
				$i++;
			}
			$retval["numofitems"]=$i;
			return $retval;
		}
	}

?>
