-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.php
More file actions
185 lines (145 loc) · 5.1 KB
/
Search.php
File metadata and controls
185 lines (145 loc) · 5.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
class Search extends Eloquent {
public static $table = "searches";
public $results = array();
public function fulltext($q) {
$this->s_filters["fulltext"] = $q;
$es = new FullTextSearch();
$params = array();
$params["type"] = "something";
$params["size"] = 100;
if(strlen($q)) {
$params["body"]["query"]["multi_match"]["query"] = $q;
$params["body"]["query"]["multi_match"]["fields"][] = "title^2";
$params["body"]["query"]["multi_match"]["fields"][] = "description";
$params["body"]["query"]["multi_match"]["fields"][] = "company_name";
$params["body"]["query"]["multi_match"]["fields"][] = "skill_tags^2";
$params["body"]["query"]["multi_match"]["operator"] = "and";
}
$params["body"]["fields"][] = "_id";
$params["body"]["filter"]["range"]["display_to"]["gte"] = date("Y-m-d");
$res = $es->search($params);
$jobs = array_map(function($hit) {
return (int) $hit["_id"];
},$res["hits"]["hits"]);
$this->fulltext_results = $jobs;
return $this;
}
public function location($l,$u_radius=10) {
$this->s_filters["location"] = $l;
$u_radius = (int) $u_radius;
if(!in_array($u_radius, array(10,15,20,25))) {
$u_radius = 10;
}
$lat = 54.17143;
$lng = -4.13085;
$radius = 375;
if(strlen($l) > 0) {
$gc = new Geocoder();
$res = $gc->geocode($l);
if($res) {
$lat = $gc->lat;
$lng = $gc->lng;
$radius = $u_radius;
}
}
$jobs = JobLocation::filter(array(
"loc" => array(
"\$geoWithin" => array(
"\$centerSphere" => array(
array($lat,$lng),
$radius / 3959
)
)
)
));
$this->location_results = $jobs;
return $this;
}
public function of_type($types) {
$this->s_filters["types"] = $types;
return $this;
}
public function per_page($n) {
$this->s_filters["pp"] = $n;
return $this;
}
public function page($n) {
$n = $n - 1;
$this->s_filters["p"] = $n;
return $this;
}
public function paginate() {
$this->results = array_intersect($this->fulltext_results, $this->location_results);
if(count($this->results)) {
$this->_total_results = count($this->results);
$jq = Job::where_in("id",$this->results)
->where("is_posted","=",true)
->where("display_to",">=",date(FORMAT_MYSQL_DATETIME));
if(isset($this->s_filters["types"]) && count($this->s_filters["types"])) {
$jq->where_in("type",$this->s_filters["types"]);
}
$jq->order_by("display_to");
if($this->s_filters["pp"] != 0) {
$jq->take($this->s_filters["pp"])
->skip($this->s_filters["pp"]*$this->s_filters["p"]);
}
$this->results = $jq->get();
} else {
$this->results = array();
}
return $this;
}
public function get_page_count() {
if($this->s_filters["pp"] == 0) {
return 1;
} else {
return floor($this->_total_results / $this->s_filters["pp"]);
}
}
public function save() {
$user_id = 0;
if(Sentry::check()) {
$user_id = Sentry::user()->id;
}
$this->set_attribute("user_id",$user_id);
$this->set_attribute("type","job");
$this->set_attribute("count",count($this->result_ids));
$s = parent::save();
foreach($this->s_filters as $filter_key => $filter_val) {
$f = new SearchFilter();
if(gettype($filter_val) == "array") {
foreach($filter_val as $v) {
$f->create(array(
"search_id" => $this->id,
"k" => $filter_key,
"v" => $v
));
}
} else {
$f->create(array(
"search_id" => $this->id,
"k" => $filter_key,
"v" => $filter_val
));
}
}
}
public static function fmt_permalink($p) {
$permalink = strtolower($p);
$permalink = preg_replace("/[^a-z0-9\-\.\+]/i","-",$permalink);
$words = array("a","in","for","to","and","at","ltd","limited","plc");
$concise = preg_replace("/\b(".implode("|",$words).")\b/","-",$permalink);
$concise = trim(preg_replace("/[-]+/","-",$concise),"-");
return $concise;
}
private $fulltext_results = array();
private $location_results = array();
private $s_filters = array(
"pp" => 10,
"p" => 1,
"type" => array()
);
private $_total_results = 0;
}
?>