/
search.php
353 lines (292 loc) · 9.83 KB
/
search.php
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
* Page Name: Search
* Purpose: Primary search page
* Last Updated: 6/5/2014
* Signature: Tyler Nickerson
* Copyright 2014 eDart
*
* [Do not remove this header. One MUST be included at the start of every page/script]
*
*/
include_once $_SERVER["DOC_ROOT"]."/scripts/php/core.php"; //Import core functionality
//If there is no keyword
if(!isset($_GET["keyword"]))
{
header("Location:/"); //Go home
exit;
}
$keyword = $_GET["keyword"];
//Break down the keyword by spaces
$srch = explode(' ', trim(strtolower($keyword)));
HTML::begin();
//If the keyword is blank, just make it 'Search'
if (trim($_GET["keyword"])=="")
{
Head::make("Search");
}
else //If there is a keyword, show it in the title
{
Head::make($_GET["keyword"]);
}
Body::begin();
//Because the search bar is the same on all pages, we have to change it via JavaScript
$headsearch_script = <<<HSRCH
<script>
var hsea = document.getElementById('headsearch');
hsea.style.color="white";
hsea.value = "$keyword"
</script>
HSRCH;
echo $headsearch_script;
?>
<div class="layout-978 uk-container-center">
<div class="uk-grid">
<?php
$cnt = 0; //Count of the items found
//Words to disregard in the search
$nowords = array("the", "a", "an", "it", "its", "it's");
//If you can't connect...
if(mysqli_connect_errno())
{
//Throw an error
echo "Failed to connect: " . mysqli_connect_error();
}
$itemarry = array(); //Array of found items
//Check for any filters
$filter_array = getFilters($_GET["keyword"]);
//Make an API call to get all matching items
$items = new Item(array("action"=>"get", "filter"=>$filter_array));
//Get info on all of the items
$items_info = $items->run(true);
$keyword = trim(removeFilters($keyword));
//Loop through each item
foreach($items_info as $item)
{
//If there were additional terms instead of just filters
if($keyword!="")
{
//Loop through each property of the item
foreach($item as $k=>$v)
{
//As long as it's not the image binary
if($k!="image")
{
//Get info about the user who owns the current item
$owner = new User(array("action"=>"get", "id"=>$item["usr"]));
$ownerInfo = $owner->run(true);
//Check to make sure the value isn't empty
if(trim($v)!="")
{
//echo $keyword . "," . $v . "<br/>";
//Returns true if the keyword is in the current item column
$item_match = (strpos(strtolower($v), strtolower($keyword))!==false);
//Returns true if the keyword is in the owner's name
$user_match = (strpos(strtolower($keyword), strtolower($ownerInfo[0]["fname"])))||
(strpos(strtolower($keyword), strtolower($ownerInfo[0]["lname"])));
//If either of these are true
if($user_match||$item_match)
{
//Print the item to the board
printItem(trim($item["id"]));
//Increment the item count
$cnt++;
//Break out of the parent for loop
break 1;
}
}
}
}
}
else
{
printItem(trim($item["id"]));
}
}
//If no items were found
if($cnt==0&&$keyword!="")
{
//Aplogize
echo "We're sorry, but it seems no one has posted ";
//If there was a keyword to begin with
if(trim($keyword)!="")
{
//Append the keyword to the apology string
echo "'" . $keyword . "'";
}
else //If it was blank
{
echo " this item "; //Be generic
}
//Finish off the apology string with a link to Amazon
echo " yet! Why not <a target=\"_blank\" href=\"http://www.amazon.com/s/?field-keywords=".urlencode(trim($_GET["keyword"]))."\">buy</a> it?";
}
//Removes any filter strings from the input
function removeFilters($input)
{
//First, convert it to lower case
$input = strtolower($input);
//Our final string
$final = "";
//Then, divide it by spaces
$break_by_space = explode(' ', $input);
//Loop through each term
foreach($break_by_space as $term)
{
//Then break it by colons
$break_by_colon = explode(':', $term);
$matched = false;
//Let us make sure this is an actual array, not just one term
if(count($break_by_colon)>=2)
{
//Get the first word. This will be our key.
$key = $break_by_colon[0];
//Let's see what it is...
switch(trim($key))
{
case "user":
$matched = true;
break;
}
}
//If it's not a filter...
if(!$matched)
{
//Add it to the output
$final .= $term . " ";
}
}
return $final;
}
//Returns an array of filters depending on the input
//(e.g. user:1, category:books, etc)
function getFilters($input)
{
//Initialize the empty array
$filter_array = array();
//First, convert it to lower case
$input = strtolower($input);
//Then, divide it by spaces
$break_by_space = explode(' ', $input);
//Loop through each term
foreach($break_by_space as $term)
{
//Then break it by colons
$break_by_colon = explode(':', $term);
//Let us make sure this is an actual array, not just one term
if(count($break_by_colon)>=2)
{
//Get the first word. This will be our key.
$key = $break_by_colon[0];
$matched = false;
//Let's see what it is...
switch(trim($key))
{
case "user":
$key = "usr";
$matched = true;
break;
}
//This prevents it from matching anything irrelevant
if($matched)
{
//Remove the first element (key)
array_shift($break_by_colon);
$value = implode("", $break_by_colon);
//Merge the key/values with the master filter array
$filter_array = array_merge($filter_array, array($key=>$value));
}
}
}
return $filter_array;
}
//Prints an item
function printItem($itemid)
{
//Get the item info
$item = new Item(array("action"=>"get", "filter"=>array("id"=>$itemid)));
$item_info = $item->run(true);
//Load them into variables
$item_img_url = "/imageviewer/?id=".$itemid;
$item_name = $item_info[0]["name"];
$item_desc = $item_info[0]["description"];
$item_price = $item_info[0]["emv"];
$item_adddate = $item_info[0]["adddate"];
$item_duedate = $item_info[0]["duedate"];
$item_dodue = ($item_duedate != 0);
$item_expires = $item_info[0]["expiration"];
$item_owner = $item_info[0]["usr"];
//Format the due date
//If the due date isn't this year, include the year
if(date("Y", $item_duedate)==date("Y"))
{
$item_duedate = date("F jS",$item_duedate);
}
else
{
$item_duedate = date("n/j/Y", $item_duedate);
}
//Format the add date
//Follow the same year rule as the due date
if(date("Y", $item_adddate)==date("Y"))
{
$item_adddate = date("F jS",$item_adddate);
}
else
{
$item_adddate = date("n/j/Y", $item_adddate);
}
//Format the expiration date
//Again, follow the year rule
if(date("Y",$item_expires)==date("Y"))
{
$item_expires = date("F jS", $item_expires);
}
else
{
$item_expires = date("n/j/Y", $item_expires);
}
//Get the owner's info
$owner = new User(array("action"=>"get", "id"=>$item_owner));
$owner_info = $owner->run(true);
$owner_name = $owner_info[0]["fname"] . " " . $owner_info[0]["lname"];
$item_offer_count = (is_array(json_encode($item_info[0]["offers"], true))) ? count(json_encode($item_info[0]["offers"])) : 0;
$item_emv = (strlen($item_info[0]["emv"])!=0) ? $item_info[0]["emv"] : 0;
$item_html = <<<ITEM1
<div class="uk-width-1-1 uk-align-center">
<div class="item" onclick="window.location='/view.php?itemid=$itemid&userid=$item_owner';">
<div class="uk-grid uk-grid-preserve reset_padding">
<div class="uk-width-4-6 info">
<div class="header">$item_name </div>
<div class="description">$item_desc</div>
<div class="overview uk-grid">
<div class="uk-width-1-3" title="Number of Offers">
<span class="uk-icon-cube"></span> $item_offer_count
</div>
<div class="uk-width-1-3" title="View Count">
<span class="uk-icon-eye"></span> {$item_info[0]["views"] }
</div>
<div class="uk-width-1-3" title="Estimated Market Value (EMV)">
<span class="uk-icon-usd"></span> $item_emv
</div>
</div>
</div>
<div class="uk-width-2-6">
<div style="background:url('/imageviewer/?id=$itemid&size=medium') no-repeat center center;" class="thumbnail">
<div class="gradient"></div>
</div>
</div>
</div>
</div>
</div>
ITEM1;
//Print the HTML
echo $item_html;
}
?>
</div>
</div>
<?php
Body::end();
HTML::end();
?>