Skip to content

Instantly share code, notes, and snippets.

@doekenorg
Created June 24, 2019 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doekenorg/5755c563e54f02af7e31e90cfbfc2a14 to your computer and use it in GitHub Desktop.
Save doekenorg/5755c563e54f02af7e31e90cfbfc2a14 to your computer and use it in GitHub Desktop.
Filter results with sets via URL
<?php
// ..
// filetrs for form ID: 1
add_filter('gfexcel_output_search_criteria_1', function ($search) {
// Add a group with filters
// An operator can be: <, >, =, >=, <=, <>, contains, like, is, is not, in, not in
$filters['groupA'] = [
// mode says: <mode> of the following rules should apply for me to filter
'mode' => 'any',
// or 'all'. 'all' is default, and then this can be omitted.
// [Field id, operator, value]
['1', 'contains', 'Onion'],
// a single field with a value containing "Onion"
['2.4', 'contains', 'Peanut'],
// A multipart field (like name or address) (2) where the subfield (4) contains "Peanut"
];
// Retrieve the filterset from the url (?subset=<filfter-group>)
$subset = rgar($filters, rgar($_GET, 'subset'), []);
if (!$subset) {
// none found, so no filtering.
return $search;
}
// remap the filters so it's following the rules.
$field_filters = rgar($search, 'field_filters', []);
$search['field_filters'] = array_merge($field_filters, array_map(function ($row) {
if (!is_array($row)) {
return $row;
}
list($key, $operator, $value) = $row;
return compact('key', 'operator', 'value');
}, $subset));
return $search;
});
@doekenorg
Copy link
Author

doekenorg commented Jun 24, 2019

@websherpa Is this something you can work with?

It's not as nice as I would like it to be, but I can't seem to find the time to do it less verbose without introducing some new files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment