summaryrefslogtreecommitdiffstats
path: root/library/Pbs/Search.php
blob: 9f25d03a2fdaed84b9cc7e412f066f54fc62ba6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

class Pbs_Search{	

	private $searchTerm = '';
	private $searcha;
	private $searchb;
	private $module;
	private $countall;
	private $countresult;
	
	public function searchForm(){
		$str = "<form  style='float:left;' action='/user/".$this->module."/search'>
			<input type='text' id='search' name='search' ".(($this->searchTerm != '')?'value="'.htmlentities($this->searchTerm, ENT_QUOTES).'"':'').">
			<button type='submit' class='searchbutton' value='search'> Search</button>";
			
		$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>
					$(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;
	}
	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;
		#print_a($searcha, $searchb);
	}
	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;
	}
}