summaryrefslogblamecommitdiffstats
path: root/library/Poolctrl/Search.php
blob: c798d38dd844d0515d53dc0aded9c4e893d1cdcf (plain) (tree)








































































































































































                                                                                                                                                                         
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class Poolctrl_Search{	

	private $searchTerm = '';
	private $searcha;
	private $searchb;
	private $module;
	private $countall;
	private $countresult;
	private $type;
	
	public function searchForm(){
		$str = "<form  style='float:left;' action='";
		if($this->type == ''){
			$str .= "/user/".$this->module."/search";
		}
		else{
			$str .= "/user/".$this->module."/search/type/".$this->type;
		}
		$str .= "'>
			<div><input type='text' id='search' name='search' ".(($this->searchTerm != '')?'value="'.htmlentities($this->searchTerm, ENT_QUOTES).'"':'')." />
			<button type='submit' class='searchbutton' value='search'> Search</button></div>";
			
		$highlight = array();
		if($this->searchTerm != ''){
			$str .= "<a href='/user/".$this->module."/'><img src='/media/img/delete.png' alt='Delete Client'/></a>";
			$str .= "<div class='searchsub'>$this->countresult result".(($this->countresult==1)?'':'s')." found</div>";
			$str .="<script type='text/javascript' src='/media/js/jquery.highlight-3.js'></script>";				
			foreach($this->getSearchTerms() as $term){
				$highlight[] = "$('table').highlight('".$term."');";				
				$highlight[] = "$('.element .number').highlight('".$term."');";
				$highlight[] = "$('.element .title').highlight('".$term."');";
				$highlight[] = "$('.element .subtitle').highlight('".$term."');";
				$highlight[] = "$('.element .item').highlight('".$term."');";
			}
		
		}
		$str .= "<div class='searchsub dsf'>Display searchfilter</div>";
		$str .= "<script type='text/javascript'>
					$(document).ready(function(){";
		$str .= implode("\n",$highlight);
		$str .= "$('.searchvars').data('m','0');\n";
		
		$str .= "$('.dsf')
			.click(function(){
				if($('.searchvars').is(':visible') == false){					
					$('.searchvars').data('m','1').slideDown();
				}
				else{
					$('.searchvars').data('m','0').slideUp();
				}
			});";
		$str .= "$('.searchvars .code').click(function(){
					$('#search').val($('#search').val()+' '+$(this).text()+':');
				});";	
		$str .= "$('#search')
			.focus(function() {
				$('table th .code').show();
				$('.searchvars').slideDown();
			}).focusout(function() {
				$('table th .code').hide();
				if($('.searchvars').data('m') == '0'){
					$('.searchvars').slideUp();
				}
			});
			$('table th').click(function(){
				$('#search').val($('#search').val()+' '+$(this).find('.code').text()+':');
			});";		
		$str .= "});
			</script>";
		$str .= "</form>";
		return $str;
	}	
	public function setModule($m){
		$this->module = $m;
		return $this;
	}
	public function setType($t){
		$this->type = $t;
		return $this;
	}
	public function setSearchTerm($search){		
		$this->searchTerm = trim($search);
		
		// search for "text"
		preg_match_all("!\"(.*?)\"!is",$this->searchTerm,$matches);		
		$tmpsearch = $this->searchTerm;
        for($i=0;$i<=count($matches[0]);$i++){
        	@$replace = str_replace(" ","<|>",$matches[0][$i]);
			@$tmpsearch = str_replace($matches[0][$i],$replace,$tmpsearch);
        }		
		$parts = explode(" ",$tmpsearch);
		foreach($parts as $search){
			if(stristr($search,":") && preg_match('/^[a-z_A-Z]+$/',$search) >= 0){
				$key = substr($search,0,strpos($search,":"));
				$value = substr($search,strpos($search,":")+1);
				if(stristr($value,'"')){
					$value = substr(str_replace('<|>',' ',$value),1,-1);
				}
				$searcha[$key] = $value;
			}
			else{
				if(stristr($search,'"')){
					$search = substr(str_replace('<|>',' ',$search),1,-1);
				}
				$searchb[] = $search;
			}
		}
		$this->searcha = @$searcha;
		$this->searchb = @$searchb;
		return $this;
	}
	public function getSearchTerm(){
		return $this->searchTerm;
	}
	public function getSearchTerms(){
		$beta = $this->searcha;
		foreach($this->searchb as $b)
			$beta[] = $b;
		return $beta;		
	}
	public function search($array){
		$this->countall = count($array);
		foreach($array as $counter => $cig){
			if(is_object($cig)){
				$cig = $cig->toArray();
			}
			foreach($cig as $k => $v){
				if(count($this->searcha) > 0){
					foreach($this->searcha as $sk => $sv){						
						if($k == $sk){
							if(stristr($v,$sv) || $v == $sv){		
								$com1[$counter] += 1;
							}
						}
					}
				}
				if(count($this->searchb) >= 0){
					foreach($this->searchb as $sk => $sv){	
						 $comm = stristr($v,$sv);
						 if($comm != false || $v == $sv){
						 	$com2[$counter] += 1;			 	
						 }
					}
				}
			}
			#print_a($com2[$counter]." >= ".count($this->searchb)." && ".$com1[$counter]." >= ".count($this->searcha));
			if($com2[$counter] >= count($this->searchb) && $com1[$counter] >= count($this->searcha)){
				// add item in resultlist
				$data[] = $counter;				
			}
		}		
		foreach( $data as $c)
			$ges[] = $array[$c];
		$this->countresult = count($ges);
		return $ges;
	}
}