Skip to content

Instantly share code, notes, and snippets.

@heathdutton
Created April 17, 2018 16:43
Show Gist options
  • Save heathdutton/a552f321c5aabf4e3181ab90a9cdf235 to your computer and use it in GitHub Desktop.
Save heathdutton/a552f321c5aabf4e3181ab90a9cdf235 to your computer and use it in GitHub Desktop.
From 23a41e73f13cfbc9bc0156ee8bcb69ff15fa29c1 Mon Sep 17 00:00:00 2001
From: heathdutton <hdutton@thedmsgrp.com>
Date: Mon, 9 Apr 2018 16:47:13 -0400
Subject: [PATCH 1/5] Support includes/excludes with text fields for bulk
filtering.
---
app/bundles/LeadBundle/Entity/LeadListRepository.php | 10 ++++++++++
app/bundles/LeadBundle/Entity/OperatorListTrait.php | 2 ++
2 files changed, 12 insertions(+)
diff --git a/app/bundles/LeadBundle/Entity/LeadListRepository.php b/app/bundles/LeadBundle/Entity/LeadListRepository.php
index 8cdfbae108..67dabf0a28 100644
--- a/app/bundles/LeadBundle/Entity/LeadListRepository.php
+++ b/app/bundles/LeadBundle/Entity/LeadListRepository.php
@@ -1677,6 +1677,16 @@ public function getListFilterExpr($filters, &$parameters, QueryBuilder $q, $isNo
case 'in':
case 'notIn':
+ if (is_string($details['filter'])) {
+ // Support common formats received when pasting in from spreadsheets.
+ foreach (["\n", ',', ' '] as $delimiter) {
+ if (strpos($details['filter'], $delimiter) !== false) {
+ break;
+ }
+ }
+ $details['filter'] = explode($delimiter, $details['filter']);
+ $details['filter'] = array_map('trim', $details['filter']);
+ }
foreach ($details['filter'] as &$value) {
$value = $q->expr()->literal(
InputHelper::clean($value)
diff --git a/app/bundles/LeadBundle/Entity/OperatorListTrait.php b/app/bundles/LeadBundle/Entity/OperatorListTrait.php
index 156a6d8d5e..4fa8bb6ce5 100644
--- a/app/bundles/LeadBundle/Entity/OperatorListTrait.php
+++ b/app/bundles/LeadBundle/Entity/OperatorListTrait.php
@@ -27,6 +27,8 @@
'startsWith',
'endsWith',
'contains',
+ 'in',
+ '!in',
],
],
'select' => [
From 9c5a5c4130639943357b65f0e00d5a4a4a70fdec Mon Sep 17 00:00:00 2001
From: heathdutton <hdutton@thedmsgrp.com>
Date: Mon, 9 Apr 2018 16:47:40 -0400
Subject: [PATCH 2/5] Prevent duplicates within the same in/!in query.
---
app/bundles/LeadBundle/Entity/LeadListRepository.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/app/bundles/LeadBundle/Entity/LeadListRepository.php b/app/bundles/LeadBundle/Entity/LeadListRepository.php
index 67dabf0a28..c2841ef1ca 100644
--- a/app/bundles/LeadBundle/Entity/LeadListRepository.php
+++ b/app/bundles/LeadBundle/Entity/LeadListRepository.php
@@ -1692,6 +1692,7 @@ public function getListFilterExpr($filters, &$parameters, QueryBuilder $q, $isNo
InputHelper::clean($value)
);
}
if ($details['type'] == 'multiselect') {
foreach ($details['filter'] as $filter) {
$filter = trim($filter, "'");
From 1028e6e6352597a503171bb73ecb25eae7d38226 Mon Sep 17 00:00:00 2001
From: heathdutton <hdutton@thedmsgrp.com>
Date: Mon, 9 Apr 2018 17:09:08 -0400
Subject: [PATCH 3/5] Add support for delimited/wrapped/terminated array
notation.
---
.../LeadBundle/Entity/LeadListRepository.php | 28 ++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/app/bundles/LeadBundle/Entity/LeadListRepository.php b/app/bundles/LeadBundle/Entity/LeadListRepository.php
index c2841ef1ca..4f98b12c3b 100644
--- a/app/bundles/LeadBundle/Entity/LeadListRepository.php
+++ b/app/bundles/LeadBundle/Entity/LeadListRepository.php
@@ -1678,14 +1678,34 @@ public function getListFilterExpr($filters, &$parameters, QueryBuilder $q, $isNo
case 'in':
case 'notIn':
if (is_string($details['filter'])) {
- // Support common formats received when pasting in from spreadsheets.
- foreach (["\n", ',', ' '] as $delimiter) {
+ // Expound the string to an array based on common string array notations.
+ foreach ([',', ';', "\t", ' '] as $delimiter) {
if (strpos($details['filter'], $delimiter) !== false) {
break;
}
}
- $details['filter'] = explode($delimiter, $details['filter']);
- $details['filter'] = array_map('trim', $details['filter']);
+ foreach (["'", '"'] as $enclosure) {
+ if (strpos($details['filter'], $enclosure) !== false) {
+ break;
+ }
+ }
+ foreach (['"', "\\"] as $escape) {
+ if ($escape !== $enclosure && strpos($details['filter'], $enclosure) !== false) {
+ break;
+ }
+ }
+ foreach (["\r\n", "\n", "\r"] as $terminator) {
+ if (strpos($details['filter'], $terminator) !== false) {
+ break;
+ }
+ }
+ $lines = explode($terminator, $details['filter']);
+ $details['filter'] = [];
+ foreach ($lines as $line) {
+ foreach (str_getcsv($line, $delimiter, $enclosure, $escape) as $value) {
+ $details['filter'][] = $value;
+ }
+ }
}
foreach ($details['filter'] as &$value) {
$value = $q->expr()->literal(
From c2025333976e328dd85ded88c77bfc48e2f85701 Mon Sep 17 00:00:00 2001
From: heathdutton <hdutton@thedmsgrp.com>
Date: Mon, 9 Apr 2018 17:10:29 -0400
Subject: [PATCH 4/5] Exclude NULL values.
These may be accidentally pasted in from spreadsheets, which would
produce erroneous results.
---
app/bundles/LeadBundle/Entity/LeadListRepository.php | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/app/bundles/LeadBundle/Entity/LeadListRepository.php b/app/bundles/LeadBundle/Entity/LeadListRepository.php
index 4f98b12c3b..dd5e26e76c 100644
--- a/app/bundles/LeadBundle/Entity/LeadListRepository.php
+++ b/app/bundles/LeadBundle/Entity/LeadListRepository.php
@@ -1703,7 +1703,9 @@ public function getListFilterExpr($filters, &$parameters, QueryBuilder $q, $isNo
$details['filter'] = [];
foreach ($lines as $line) {
foreach (str_getcsv($line, $delimiter, $enclosure, $escape) as $value) {
- $details['filter'][] = $value;
+ if ($value && $value !== 'NULL') {
+ $details['filter'][] = $value;
+ }
}
}
}
From a0b9463b7dea0756c3c89662357cc185162868bf Mon Sep 17 00:00:00 2001
From: heathdutton <hdutton@thedmsgrp.com>
Date: Mon, 9 Apr 2018 17:49:10 -0400
Subject: [PATCH 5/5] PHPCS suggestions.
---
app/bundles/LeadBundle/Entity/LeadListRepository.php | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/app/bundles/LeadBundle/Entity/LeadListRepository.php b/app/bundles/LeadBundle/Entity/LeadListRepository.php
index dd5e26e76c..234535214f 100644
--- a/app/bundles/LeadBundle/Entity/LeadListRepository.php
+++ b/app/bundles/LeadBundle/Entity/LeadListRepository.php
@@ -1680,30 +1680,30 @@ public function getListFilterExpr($filters, &$parameters, QueryBuilder $q, $isNo
if (is_string($details['filter'])) {
// Expound the string to an array based on common string array notations.
foreach ([',', ';', "\t", ' '] as $delimiter) {
- if (strpos($details['filter'], $delimiter) !== false) {
+ if (false !== strpos($details['filter'], $delimiter)) {
break;
}
}
foreach (["'", '"'] as $enclosure) {
- if (strpos($details['filter'], $enclosure) !== false) {
+ if (false !== strpos($details['filter'], $enclosure)) {
break;
}
}
- foreach (['"', "\\"] as $escape) {
- if ($escape !== $enclosure && strpos($details['filter'], $enclosure) !== false) {
+ foreach (['"', '\\'] as $escape) {
+ if ($escape !== $enclosure && false !== strpos($details['filter'], $enclosure)) {
break;
}
}
foreach (["\r\n", "\n", "\r"] as $terminator) {
- if (strpos($details['filter'], $terminator) !== false) {
+ if (false !== strpos($details['filter'], $terminator)) {
break;
}
}
- $lines = explode($terminator, $details['filter']);
+ $lines = explode($terminator, $details['filter']);
$details['filter'] = [];
foreach ($lines as $line) {
foreach (str_getcsv($line, $delimiter, $enclosure, $escape) as $value) {
- if ($value && $value !== 'NULL') {
+ if ($value && 'NULL' !== $value) {
$details['filter'][] = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment