-
Notifications
You must be signed in to change notification settings - Fork 2
/
pi.live_search.php
166 lines (128 loc) · 3.67 KB
/
pi.live_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
<?php
/*
=====================================================
Copyright (c) 2008 Pascal Kriete
-----------------------------------------------------
http://pascalkriete.com/
=====================================================
File: pi.live_search.php
-----------------------------------------------------
Purpose: Generate live-search json results
=====================================================
Changelog:
- Version 1.1.0
Removed trailing comma from js array
Updated javascript to accept optional parameters
- Version 1.0.2
Fixed PHP Notice bug
- Version 1.0.1
Added 'No Results' text
- Version 1.0.0
Plugin Created
*/
$plugin_info = array(
'pi_name' => 'Live Search',
'pi_version' => '1.1.0',
'pi_author' => 'Pascal Kriete',
'pi_author_url' => 'http://pascalkriete.com/',
'pi_description' => 'Generate search results for the accompanying jQuery plugin.',
'pi_usage' => Live_search::usage()
);
class Live_search {
/**
* Display Search Results
*
* @access public
* @return string
*/
function results()
{
global $IN, $DB, $LOC, $TMPL;
if ( ! $IN->QSTR)
{
return '';
}
$search_phrase =& $IN->QSTR;
$search_pharse = $DB->escape_str( urldecode($search_phrase) );
/** ---------------------------------------
/** Build and run the query
/** ---------------------------------------*/
$sql = "SELECT distinct(a.entry_id), a.url_title, a.title, b.blog_url, b.comment_url
FROM exp_weblog_titles a, exp_weblogs b
WHERE a.weblog_id = b.weblog_id
AND a.status != 'closed'
AND (a.expiration_date > '".$LOC->now."' OR a.expiration_date = '0')
AND a.title LIKE '%{$search_phrase}%'
ORDER BY a.title ASC LIMIT 0,10";
$query = $DB->query($sql);
/** ---------------------------------------
/** Empty, no point in continuing
/** ---------------------------------------*/
if ($query->num_rows == 0)
{
echo '[{ "title" : "No results", "path" : "#" }]';
exit;
}
/** ---------------------------------------
/** Process parameters
/** ---------------------------------------*/
if( $TMPL->fetch_param('link') == 'comment')
{
$link_to = 'comment_url';
}
else
{
$link_to = 'blog_url';
}
/** ---------------------------------------
/** Create javascript json array
/** ---------------------------------------*/
$data = '[';
foreach($query->result as $row)
{
$data .= '{ "title" : "'.$row['title'].'", "path" : "'.$row[$link_to].$row['url_title'].'" }, ';
}
// Take off the last comma
$data = substr($data, 0, -2);
$data .= ']';
echo $data;
exit;
}
function _safe_json($val)
{
}
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
// This function describes how the plugin is used.
// Make sure and use output buffering
function usage()
{
ob_start();
?>
EE Template:
{exp:live_search:results}
------------------------
EE Parameters:
link="comment"
- The weblog path to link to. Options are "blog" and "comment". Default is "blog".
------------------------
Javascript:
Make sure you have an up-to-date version of jQuery included before the plugin javascript.
Parameters:
width : width of the displayed results
center_results : boolean to center the result box
Examples:
$('#keywords').livesearch('/search/livesearch/');
$('#keywords').livesearch('/search/livesearch/', { width: '200' });
$('#keywords').livesearch('/search/livesearch/', { center_results: true });
$('#keywords').livesearch('/search/livesearch/', { width: '200', center_results: true });
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
// END
}
// END CLASS
?>