-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpi.search_fields.php
More file actions
567 lines (475 loc) · 17.1 KB
/
pi.search_fields.php
File metadata and controls
567 lines (475 loc) · 17.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$plugin_info = array(
'pi_name' => 'Search Fields',
'pi_version' =>'2.0.2',
'pi_author' =>'Mark Croxton',
'pi_author_url' => 'http://www.hallmark-design.co.uk/',
'pi_description' => 'Search channel entry titles, custom fields, category names, category descriptions and category custom fields.',
'pi_usage' => Search_fields::usage()
);
/**
* Search Fields
*
* Searches entry titles and custom fields within
* channel entries and categories for keywords/phrase.
* Outputs a delimited list of entry ids to a placeholder
*
* @version 2.0.2
*
*/
class Search_fields {
private $_custom_fields = array();
private $_cat_fields = array();
private $_ignore = array();
public $return_data = '';
public $min_length = 3;
/**
* Constructor
* @access public
* @return void
*/
function Search_fields()
{
$this->EE =& get_instance();
$this->EE->lang->loadfile('search');
// words to ignore in search terms
include(APPPATH.'config/stopwords'.EXT);
$this->_ignore = $ignore;
$channel = $this->EE->TMPL->fetch_param('channel', '*');
$delimiter = $this->EE->TMPL->fetch_param('delimiter', '|');
$operator = strtoupper($this->EE->TMPL->fetch_param('operator')) == 'OR' ? 'OR' : 'AND';
$ph = $this->EE->TMPL->fetch_param('placeholder', 'search_results');
$site = $this->EE->TMPL->fetch_param('site', $this->EE->config->item('site_id'));
$this->min_length = $this->EE->TMPL->fetch_param('min_length', $this->min_length);
// fetch the tagdata
$tagdata = $this->EE->TMPL->tagdata;
// get available custom fields
$this->_fetch_custom_channel_fields();
// some initial defaults
$sql_conditions = '';
$search_cat = $search_cat_fields = false;
// grab any posted search parameters and stick in an array where field => search phrase
if ($this->EE->TMPL->fetch_param('dynamic_parameters') !== FALSE)
{
foreach (explode('|', $this->EE->TMPL->fetch_param('dynamic_parameters')) as $var)
{
if (strncmp($var, 'search:', 7) == 0)
{
$this->EE->TMPL->search_fields[substr($var, 7)] = $this->EE->input->post($var);
}
}
}
/** ---------------------------------------
// Field searching
// => The Template parser stores parameters that start
// => with 'search:' in a special array, search_fields[]
/** ---------------------------------------*/
if (! empty($this->EE->TMPL->search_fields))
{
foreach ($this->EE->TMPL->search_fields as $field_name => $terms)
{
// search channel custom fields
if (isset($this->_custom_fields[$this->EE->config->item('site_id')][$field_name]))
{
$field_sql = 'wd.field_id_'.$this->_custom_fields[$this->EE->config->item('site_id')][$field_name];
// Tagger module support
if (strncmp($terms, 'tagger=', 7) == 0)
{
$tag = substr($terms, 7);
// Grab all entries with this tag
// Note that it doesn't matter what the custom field containing the tags is called
// Tagger's data model permits only one tag field per entry
$this->EE->db->select('tl.item_id');
$this->EE->db->from('exp_tagger_links tl');
$this->EE->db->join('exp_tagger t', 't.tag_id = tl.tag_id', 'left');
$this->EE->db->join('exp_channel_titles ct', 'ct.entry_id = tl.item_id', 'left');
$this->EE->db->where('t.tag_name', $tag);
// Fetch
$query = $this->EE->db->get();
if ($query->num_rows() > 0)
{
$matched_entry_sql = '';
foreach ($query->result() as $row)
{
$matched_entry_sql .= "'".$row->item_id."',";
}
$matched_entry_sql = rtrim($matched_entry_sql, ',');
// now add to our master search query...
$sql_conditions = "AND wt.entry_id IN({$matched_entry_sql})";
}
}
}
// search channel titles
else if ($field_name =="title")
{
$field_sql = 'wt.title';
}
// search category titles
else if ($field_name =="cat_name")
{
$field_sql = 'ct.cat_name';
$search_cat = true;
}
// search category description
else if ($field_name =="cat_description")
{
$field_sql = 'ct.cat_description';
$search_cat = true;
}
// search category custom fields
else if (!!strstr($field_name,'cat_'))
{
// get available custom category fields
$this->_fetch_custom_category_fields();
if (isset($this->_cat_fields[$this->EE->config->item('site_id')][ltrim($field_name,'cat_')]))
{
$field_sql = 'cd.field_id_'.$this->_cat_fields[$this->EE->config->item('site_id')][ltrim($field_name,'cat_')];
$search_cat_fields = true;
$search_cat = true;
}
}
// can't search this field because it doesn't exist
else
{
$field_sql = '';
}
if ($field_sql !== '' && $terms !== '' )
{
if (strncmp($terms, '=', 1) == 0)
{
/** ---------------------------------------
/** Exact Match e.g.: search:body="=pickle"
/** ---------------------------------------*/
$terms = substr($terms, 1);
// special handling for IS_EMPTY
if (strpos($terms, 'IS_EMPTY') !== FALSE)
{
$terms = str_replace('IS_EMPTY', '', $terms);
$terms = $this->_sanitize_search_terms($terms, TRUE);
$add_search = $this->EE->functions->sql_andor_string($terms, $field_sql);
// remove the first AND output by $this->EE->functions->sql_andor_string() so we can parenthesize this clause
$add_search = substr($add_search, 3);
$conj = ($add_search != '' && strncmp($terms, 'not ', 4) != 0) ? 'OR' : 'AND';
if (strncmp($terms, 'not ', 4) == 0)
{
$sql_conditions .= $operator.' ('.$add_search.' '.$conj.' '.$field_sql.' != "") ';
}
else
{
$sql_conditions .= $operator.' ('.$add_search.' '.$conj.' '.$field_sql.' = "") ';
}
}
else
{
$condition = $this->EE->functions->sql_andor_string($terms, $field_sql).' ';
// replace leading AND/OR with desired operator
$condition = preg_replace('/^AND|OR/', $operator, $condition,1);
$sql_conditions.=$condition;
}
}
else
{
/** ---------------------------------------
/** "Contains" e.g.: search:body="pickle"
/** ---------------------------------------*/
if (strncmp($terms, 'not ', 4) == 0)
{
$terms = substr($terms, 4);
$like = 'NOT LIKE';
}
else
{
$like = 'LIKE';
}
if (strpos($terms, '&&') !== FALSE)
{
$terms = explode('&&', $terms);
$andor = (strncmp($like, 'NOT', 3) == 0) ? 'OR' : 'AND';
}
else
{
$terms = explode('|', $terms);
$andor = (strncmp($like, 'NOT', 3) == 0) ? 'AND' : 'OR';
}
$sql_conditions .= ' '.$operator.' (';
foreach ($terms as $term)
{
if ($term == 'IS_EMPTY')
{
$sql_conditions .= ' '.$field_sql.' '.$like.' "" '.$andor;
}
elseif (strpos($term, '\W') !== FALSE) // full word only, no partial matches
{
$not = ($like == 'LIKE') ? ' ' : ' NOT ';
$term = $this->_sanitize_search_terms($term, TRUE);
$term = '([[:<:]]|^)'.addslashes(preg_quote(str_replace('\W', '', $term))).'([[:>:]]|$)';
$sql_conditions .= ' '.$field_sql.$not.'REGEXP "'.$this->EE->db->escape_str($term).'" '.$andor;
}
else
{
// starts with, ends with, or contains
$l_wildcard = substr($term, 0, 2) == '\^' ? '' : '%';
$r_wildcard = substr($term, -2, 2) == '\$' ? '' : '%';
$term = str_replace(array('\^', '\$'), '', $term);
$term = $this->_sanitize_search_terms($term);
$sql_conditions .= ' '.$field_sql.' '.$like.' "' .$l_wildcard.$this->EE->db->escape_like_str($term).$r_wildcard.'" '.$andor;
}
}
$sql_conditions = substr($sql_conditions, 0, -strlen($andor)).') ';
}
} // <- END if (strncmp($terms, '=', 1) == 0)
} // <- END foreach ($this->EE->TMPL->search_fields as $field_name => $terms)
} // <- END if (! empty($this->EE->TMPL->search_fields))
// check that we actually have some conditions to match
if ($sql_conditions == '')
{
// no valid fields to search
$this->return_data = $this->EE->TMPL->no_results();
return; // end the process here
}
// remove leading AND/OR. Clumsy - find a better way to do this?
$sql_conditions = preg_replace('/AND|OR/', '', $sql_conditions,1);
// limit to a channel?
if ($channel !== '*')
{
if(strpos($channel, '|') !== FALSE)
{
$channels = explode('|', $channel);
$sql_conditions = "(".$sql_conditions.") AND (";
foreach($channels as $ch)
{
$sql_conditions .= "wl.channel_name = '{$this->EE->db->escape_str($ch)}' OR ";
}
$sql_conditions = substr($sql_conditions, 0, -4);
$sql_conditions .= ")";
}
else
{
$sql_conditions = "(".$sql_conditions.") AND wl.channel_name = '{$this->EE->db->escape_str($channel)}'";
}
}
// let's build the query
$sql="SELECT distinct(wt.entry_id)
FROM exp_channel_titles AS wt
LEFT JOIN exp_channel_data AS wd
ON wt.entry_id = wd.entry_id
LEFT JOIN exp_channels AS wl
ON wt.channel_id = wl.channel_id
";
// join category tables, but only if required
if ($search_cat)
{
$sql .="LEFT JOIN exp_category_posts as cp
ON wt.entry_id = cp.entry_id
LEFT JOIN exp_categories as ct
ON cp.cat_id = ct.cat_id
";
// join category field table, again only if required
if ($search_cat_fields)
{
$sql .="LEFT JOIN exp_category_field_data as cd
ON ct.cat_id = cd.cat_id
";
}
}
// limit search to current site
$sql .= "WHERE wt.site_id = {$site} "."\n";
// add search conditions
$sql = $sql.'AND ('.$sql_conditions.')';
#echo $sql;
$results = $this->EE->db->query($sql);
// run the query
if ($results->num_rows() == 0)
{
// no results
$this->return_data = $this->EE->TMPL->no_results();
}
else
{
// loop through found entries
$found_ids = '';
foreach($results->result_array() as $row)
{
$found_ids .= ($found_ids=='' ? '' : $delimiter).$row['entry_id'];
}
$tagdata = $this->EE->TMPL->swap_var_single($ph, $found_ids, $tagdata);
// return data
$this->return_data = $tagdata;
}
}
/**
* Fetches custom channel fields from page flash cache.
* If not cached, runs query and caches result.
* @access private
* @return boolean
*/
private function _fetch_custom_channel_fields()
{
// as standard custom field data is used/stored in exactly the same way by the channel module
// we'll use the 'channel' class name as the cache key to avoid redundancy
if (isset($this->EE->session->cache['channel']['custom_channel_fields']))
{
$this->_custom_fields = $this->EE->session->cache['channel']['custom_channel_fields'];
return true;
}
// not found so cache them
$sql = "SELECT field_id, field_type, field_name, site_id
FROM exp_channel_fields
WHERE field_type != 'date'
AND field_type != 'rel'";
$query = $this->EE->db->query($sql);
if ($query->num_rows > 0)
{
foreach ($query->result as $row)
{
// assign standard custom fields
$this->_custom_fields[$row['site_id']][$row['field_name']] = $row['field_id'];
}
$this->EE->session->cache['channel']['custom_channel_fields'] = $this->_custom_fields;
return true;
}
else
{
return false;
}
}
/**
* Fetches custom category fields from page flash cache.
* If not cached, runs query and caches result.
* @access private
* @return boolean
*/
private function _fetch_custom_category_fields()
{
if (isset($this->EE->session->cache['search_fields']['custom_category_fields']))
{
$this->_cat_fields = $this->EE->session->cache['search_fields']['custom_category_fields'];
return true;
}
// not found so cache them
$sql = "SELECT field_id, field_name, site_id
FROM exp_category_fields";
$query = $this->EE->db->query($sql);
if ($query->num_rows > 0)
{
foreach ($query->result_array() as $row)
{
// assign standard fields
$this->_cat_fields[$row['site_id']][$row['field_name']] = $row['field_id'];
return true;
}
$this->EE->session->cache['search_fields']['custom_category_fields'] = $this->_cat_fields;
}
else
{
return false;
}
}
/**
* Sanitize earch terms
*
* @access private
* @param string $keywords
* @param boolean $exact_keyword
* @return boolean
*/
private function _sanitize_search_terms($keywords, $exact_keyword = false)
{
/** ----------------------------------------
/** Strip extraneous junk from keywords
/** ----------------------------------------*/
if ($keywords != "")
{
// Load the search helper so we can filter the keywords
$this->EE->load->helper('search');
$keywords = sanitize_search_terms($keywords);
/** ----------------------------------------
/** Is the search term long enough?
/** ----------------------------------------*/
if (strlen($keywords) < $this->min_length)
{
$text = $this->EE->lang->line('search_min_length');
$text = str_replace("%x", $this->min_length, $text);
return $this->EE->output->show_user_error('general', array($text));
}
// Load the text helper
$this->EE->load->helper('text');
$keywords = ($this->EE->config->item('auto_convert_high_ascii') == 'y') ? ascii_to_entities($keywords) : $keywords;
/** ----------------------------------------
/** Remove "ignored" words
/** ----------------------------------------*/
if (!$exact_keyword)
{
$parts = explode('"', $keywords);
$keywords = '';
foreach($parts as $num => $part)
{
// The odd breaks contain quoted strings.
if ($num % 2 == 0)
{
foreach ($this->_ignore as $badword)
{
$part = preg_replace("/\b".preg_quote($badword, '/')."\b/i","", $part);
}
}
$keywords .= ($num != 0) ? '"'.$part : $part;
}
if (trim($keywords) == '')
{
return $this->EE->output->show_user_error('general', array($this->EE->lang->line('search_no_stopwords')));
}
}
}
// finally, double spaces
$keywords = str_replace(" ", " ", $keywords);
return $keywords;
}
// usage instructions
function usage()
{
ob_start();
?>
-------------------
HOW TO USE
-------------------
Use this plugin to search channel entry titles, custom fields, category names, category descriptions and category custom fields.
Search parameter syntax is identical to the channel search parameter, see:
http://expressionengine.com/docs/modules/channel/parameters.html#par_search
In addition to entry custom fields you can search entry titles, category names, category descriptions and category custom fields. When searching categories references to category fields should be prefixed with 'cat_'.
For example:
search:cat_name="keyword"
search:cat_description="keyword"
search:cat_custom_field="keyword"
Returns a delimited list of entry ids.
Parameters
----------------
search:[field] = (optional) Field can be title, cat_name, cat_description, [custom_field_name], cat_[custom_field_name].
channel = (optional) Single channel name to search. Default is * (searches all channels).
operator = (optional) 'AND' or 'OR'. Operator for joining search field WHERE conditions. Default is 'OR'.
delimiter = (optional) Delimiter for returned entry id string. Default is pipe |.
placeholder = (optional) Single variable placeholder to replace with search results output. Default is search_results (use as {search_results}).
site = (optional) The site id. Default is current site id.
min_length = (optional) The minimum length for the search term. Default is 3.
dynamic_parameters = (optional) Allow specific search parameters to set via $_POST (form fields should have same name as the fields you wish to search). E.g. "title|custom_field".
This plugin is best used as a tag pair wrapping {exp:channel:entries}.
Example
------------
{exp:search_fields
search:title="keyword"
search:custom_field="keyword"
search:cat_name="keyword"
operator="OR"
channel="my_channel"
parse="inward"}
{exp:channel:entries entry_id="{search_results}" disable="member_data|categories" dynamic="no" orderby="title" sort="asc" limit="10"}
<a href="{page_url}">{title}</a>
{/exp:channel:entries}
{/exp:search_fields}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
} // END CLASS
/* End of file pi.search_fields.php */
/* Location: ./system/expressionengine/third_party/search_fields/pi.search_fields.php */