summaryrefslogtreecommitdiffstats
path: root/library/Pbs/Search.php
blob: eb9d198d95a995f4c81e066a8f9d3987a8184687 (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
<?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>$this->countresult results found in $this->countall</div>";
			$str .="<script type='text/javascript' src='/media/js/jquery.highlight-3.js'></script>";				
			foreach($this->getSearchTerms() as $term){
				$highlight[] = "$('table').highlight('".$term."');";
			}
		
		}
		$str .= "<script>
					$(document).ready(function(){";
		$str .= implode("\n",$highlight);
		$str .= "$('#search')
			.focus(function() {
				$('table th .code').show();
			}).focusout(function() {
				$('table th .code').hide();			
			});
			$('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;
	}
	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)){
								$com1[$counter] += 1;
							}
						}
					}
				}
				if(count($this->searchb) >= 0){
					foreach($this->searchb as $sk => $sv){	
						 $comm = stristr($v,$sv);
						 if($comm != false){
						 	$com2[$counter] += 1;			 	
						 }
					}
				}
			}
			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;
	}
}