Skip to content

Commit

Permalink
Fixed empty search causing an error when topic or user selected
Browse files Browse the repository at this point in the history
Fixed issue #730
  • Loading branch information
eSilverStrike committed Jan 11, 2017
1 parent cfb422f commit 38214f6
Showing 1 changed file with 51 additions and 45 deletions.
96 changes: 51 additions & 45 deletions system/classes/searchcriteria.class.php
Expand Up @@ -157,60 +157,66 @@ function getFTSQL()

function buildSearchSQL($keyType, $query, $columns, $sql = '')
{
if ($keyType === 'all') {
// must contain ALL of the keywords
$words = array_unique(explode(' ', $query));
$words = array_filter($words); // filter out empty strings
$sep = 'AND';

$ftwords['mysql'] = '+' . str_replace(' ', ' +', $query);
$ftwords['pgsql'] = '"' . str_replace(' ', '" AND "', $query) . '"';
} elseif ($keyType === 'any') {
// must contain ANY of the keywords
$words = array_unique(explode(' ', $query));
$words = array_filter($words); // filter out empty strings
$sep = 'OR ';
$ftwords['mysql'] = $query;
$ftwords['pgsql'] = $query;
} else {
// do an exact phrase search (default)
$words = array($query);
$sep = ' ';

// Puttings quotes around a single word in mysql really slows things down
if (strpos($query, ' ') !== false) {
$ftwords['mysql'] = '"' . $query . '"';
} else {
// Make sure query has at least 1 letter
if (!empty(trim($query))) {
if ($keyType === 'all') {
// must contain ALL of the keywords
$words = array_unique(explode(' ', $query));
$words = array_filter($words); // filter out empty strings
$sep = 'AND';

$ftwords['mysql'] = '+' . str_replace(' ', ' +', $query);
$ftwords['pgsql'] = '"' . str_replace(' ', '" AND "', $query) . '"';
} elseif ($keyType === 'any') {
// must contain ANY of the keywords
$words = array_unique(explode(' ', $query));
$words = array_filter($words); // filter out empty strings
$sep = 'OR ';
$ftwords['mysql'] = $query;
$ftwords['pgsql'] = $query;
} else {
// do an exact phrase search (default)
$words = array($query);
$sep = ' ';

// Puttings quotes around a single word in mysql really slows things down
if (strpos($query, ' ') !== false) {
$ftwords['mysql'] = '"' . $query . '"';
} else {
$ftwords['mysql'] = $query;
}
$ftwords['pgsql'] = '"' . $query . '"';
}
$ftwords['pgsql'] = '"' . $query . '"';
}

$titles = isset($_GET['title']) && isset($columns['title']);
$titles = isset($_GET['title']) && isset($columns['title']);

if ($titles) {
$strcol = $columns['title'];
} else {
$strcol = implode(',', $columns);
}
if ($titles) {
$strcol = $columns['title'];
} else {
$strcol = implode(',', $columns);
}

$ftsql['mysql'] = $sql . "AND MATCH($strcol) AGAINST ('{$ftwords['mysql']}' IN BOOLEAN MODE)";
$ftsql['mysql'] = $sql . "AND MATCH($strcol) AGAINST ('{$ftwords['mysql']}' IN BOOLEAN MODE)";

$tmp = 'AND (';
foreach ($words AS $word) {
$word = trim($word);
$tmp .= '(';
$tmp = 'AND (';
foreach ($words AS $word) {
$word = trim($word);
$tmp .= '(';

if ($titles) {
$tmp .= $columns['title'] . " LIKE '%$word%' OR ";
} else {
foreach ($columns AS $col) {
$tmp .= "$col LIKE '%$word%' OR ";
if ($titles) {
$tmp .= $columns['title'] . " LIKE '%$word%' OR ";
} else {
foreach ($columns AS $col) {
$tmp .= "$col LIKE '%$word%' OR ";
}
}
$tmp = substr($tmp, 0, -4) . ") $sep ";
}
$tmp = substr($tmp, 0, -4) . ") $sep ";
}
$sql .= substr($tmp, 0, -5) . ') ';

$sql .= substr($tmp, 0, -5) . ') ';
} else {
$ftsql['mysql'] = $sql;
}

return array($sql, $ftsql);
}
Expand Down

0 comments on commit 38214f6

Please sign in to comment.