Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/DataSource/ArrayDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,18 @@ protected function applyFilter($row, Filter $filter)

$condition = $filter->getCondition();

$is_negation_search = false;
foreach ($condition as $column => $value) {
if ($filter->isSpecialChars()) {
if ($value === FilterText::TOKEN_EMPTY) { // Handle single '#'
return empty($row[$column]);
} else if ($value === FilterText::TOKEN_NEGATION . FilterText::TOKEN_EMPTY) {
return !empty($row[$column]);
} else if ($value === FilterText::TOKEN_EMPTY_ESCAPED) { // Handle '\#' for searching literal '#'
$value = FilterText::TOKEN_EMPTY;
}
}

if ($filter instanceof FilterText && $filter->isExactSearch()) {
return $row[$column] == $value;
}
Expand All @@ -182,12 +193,49 @@ protected function applyFilter($row, Filter $filter)
$row_value = strtolower(Strings::toAscii($row[$column]));

foreach ($words as $word) {
if (strpos($row_value, strtolower(Strings::toAscii($word))) === false) {
return false;
if ($filter instanceof FilterText && $filter->isSpecialChars()) {
if ($word === FilterText::TOKEN_NEGATION . FilterText::TOKEN_EMPTY) {
return !empty($row_value);
}
$allow_negation_filter = true;
if (strpos($word, FilterText::TOKEN_NEGATION_ESCAPED) !== false) {
//If the escaped negation token is in the beginning of text, explicitly forbid parsing it after it's replaced
if (strpos($word, FilterText::TOKEN_NEGATION_ESCAPED) === 0) {
$allow_negation_filter = false;
}

$word = str_replace(FilterText::TOKEN_NEGATION_ESCAPED, FilterText::TOKEN_NEGATION, $word);
}

if ($allow_negation_filter && strpos($word, FilterText::TOKEN_NEGATION) === 0) {
//exclamation point means negation - the word is NOT included in the searched string
$excludedWord = substr($word, 1);
if (empty($excludedWord)) {
return true;
}

$is_negation_search = true;
if (strpos($row_value, strtolower(Strings::toAscii($excludedWord))) !== false) {
return false;
}
}

if (!$is_negation_search) {
return strpos($row_value, strtolower(Strings::toAscii($word))) !== false;
}
} else {
if (strpos($row_value, strtolower(Strings::toAscii($word))) !== false) {
return true;
}
}
}
return $row;
}
if ($is_negation_search) {
//we're looking for rows that don't have specific rows, and we haven't aborted by this point
//-> should be good
return true;
}
}

return false;
Expand Down
39 changes: 36 additions & 3 deletions src/DataSource/DibiFluentPostgreDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ public function applyFilterText(Filter\FilterText $filter)
$driver = $this->data_source->getConnection()->getDriver();
$or = [];

$is_negation_search = false;
foreach ($condition as $column => $value) {

if ($filter->isSpecialChars()) {
if ($value === Filter\FilterText::TOKEN_EMPTY) { // Handle single '#'
$this->data_source->where("($column IS NULL OR $column = '')");
continue;
} else if ($value === Filter\FilterText::TOKEN_NEGATION . Filter\FilterText::TOKEN_EMPTY) {
$this->data_source->where("($column IS NOT NULL AND $column <> '')");
continue;
}
$value = str_replace(Filter\FilterText::TOKEN_EMPTY_ESCAPED, Filter\FilterText::TOKEN_EMPTY, $value);
}
$column = '[' . $column . ']::varchar';

if ($filter->isExactSearch()) {
Expand All @@ -42,12 +52,35 @@ public function applyFilterText(Filter\FilterText $filter)
$x = [];
foreach ($words as $word) {
$escaped = $driver->escapeLike((string) $word, 0);
$x[] = "public.unaccent($column) ILIKE public.unaccent('%" . substr( $escaped, 1, -1) . "%')";
if ($filter->isSpecialChars()) {
$allow_negation_filter = true;
if (strpos($word, Filter\FilterText::TOKEN_NEGATION_ESCAPED) !== false) {
//If the escaped negation token is in the beginning of text, explicitly forbid parsing it after it's replaced
if (strpos($word, Filter\FilterText::TOKEN_NEGATION_ESCAPED) === 0) {
$allow_negation_filter = false;
}

$word = str_replace(Filter\FilterText::TOKEN_NEGATION_ESCAPED, Filter\FilterText::TOKEN_NEGATION, $word);
$escaped = $driver->escapeLike($word, 0);
}

if ($allow_negation_filter && strpos($word, Filter\FilterText::TOKEN_NEGATION) === 0) {
//exclamation point means negation - the word is NOT included in the searched string
$escaped = $driver->escapeLike(substr($escaped, 2, -1),0);
$x[] = "($column IS NULL OR $column = '' OR public.unaccent($column) NOT ILIKE public.unaccent('%' || " . $escaped . " || '%'))";
continue;
}
}

$x[] = "public.unaccent($column) ILIKE public.unaccent('%" . substr($escaped, 1, -1) . "%')";
}
$or[] = "((" . implode(") AND (", $x) . "))";
}

if (sizeof($or) > 1) {
if ($is_negation_search) {
$condition = sprintf("(%s)", implode(' AND ', $or));
$this->data_source->where($condition);
} else if (sizeof($or) > 1) {
$this->data_source->where('(%or)', $or);
} else {
$this->data_source->where($or);
Expand Down
32 changes: 32 additions & 0 deletions src/Filter/FilterText.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

class FilterText extends Filter
{
/** Query that is exactly equal to '#' returns empty/null values */
const TOKEN_EMPTY = '#';
/** However, if your friend is named '#' and you really want to find him, you have to type this */
const TOKEN_EMPTY_ESCAPED = '\#';
/** Query that contains words that start with '!', excludes those words from search results */
const TOKEN_NEGATION = '!';
/** However, if your friend's name starts with '!', you have to type this */
const TOKEN_NEGATION_ESCAPED = '\!';

/**
* @var string
Expand All @@ -33,6 +41,11 @@ class FilterText extends Filter
*/
protected $split_words_search = true;

/**
* @var bool
*/
protected $enable_special_chars = true;


/**
* Adds text field to filter form
Expand Down Expand Up @@ -102,4 +115,23 @@ public function hasSplitWordsSearch()
{
return $this->split_words_search;
}

/**
* @return bool
*/
public function isSpecialChars()
{
return $this->enable_special_chars;
}

/**
* @param bool $enabled
* @return FilterText
*/
public function setSpecialChars($enabled)
{
$this->enable_special_chars = (bool) $enabled;

return $this;
}
}
Loading