Skip to content

Commit

Permalink
Allow publish filtering to work with multiple values (using commas). F…
Browse files Browse the repository at this point in the history
…ixes #2290
  • Loading branch information
brendo committed Dec 15, 2014
1 parent 96de65d commit a6c86f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
28 changes: 19 additions & 9 deletions symphony/content/content.publish.php
Expand Up @@ -387,16 +387,25 @@ public function __viewIndex()
}

foreach ($filters as $handle => $value) {
$handle = Symphony::Database()->cleanValue($handle);
// Handle multiple values through filtering. RE: #2290
if ((is_array($value) && empty($value)) || trim($value) == '') {
continue;
}

if (!is_array($value)) {
$filter_type = Datasource::determineFilterType($value);
$value = preg_split('/'.($filter_type == Datasource::FILTER_AND ? '\+' : '(?<!\\\\),').'\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
$value = array_map('trim', $value);
$value = array_map(array('Datasource', 'removeEscapedCommas'), $value);
}

// Handle date meta data #2003
$handle = Symphony::Database()->cleanValue($handle);
if (in_array($handle, array('system:creation-date', 'system:modification-date'))) {
require_once TOOLKIT . '/fields/field.date.php';

$date_joins = '';
$date_where = '';
$date = new fieldDate();
$date->buildDSRetrievalSQL(array($value), $date_joins, $date_where, true);
$date = new FieldDate();
$date->buildDSRetrievalSQL($value, $date_joins, $date_where, ($filter_type == Datasource::FILTER_AND ? true : false));

// Replace the date field where with the `creation_date` or `modification_date`.
$date_where = preg_replace('/`t\d+`.date/', ($field_id !== 'system:modification-date') ? '`e`.creation_date_gmt' : '`e`.modification_date_gmt', $date_where);
Expand All @@ -410,11 +419,12 @@ public function __viewIndex()
);

$field = FieldManager::fetch($field_id);

if ($field instanceof Field) {
$field->buildDSRetrievalSQL(array($value), $joins, $where, true);
$filter_querystring .= sprintf("filter[%s]=%s&amp;", $handle, rawurlencode($value));
$prepopulate_querystring .= sprintf("prepopulate[%d]=%s&amp;", $field_id, rawurlencode($value));
$field->buildDSRetrievalSQL($value, $joins, $where, ($filter_type == Datasource::FILTER_AND ? true : false));

$encoded_value = rawurlencode(implode(',' , $value));
$filter_querystring .= sprintf("filter[%s]=%s&amp;", $handle, $value);
$prepopulate_querystring .= sprintf("prepopulate[%d]=%s&amp;", $field_id, $value);
} else {
unset($filters[$handle]);
}
Expand Down
22 changes: 21 additions & 1 deletion symphony/lib/toolkit/class.datasource.php
Expand Up @@ -214,7 +214,7 @@ public function execute(array &$param_pool = null)
* @return integer
* Datasource::FILTER_OR or Datasource::FILTER_AND
*/
public function __determineFilterType($value)
public static function determineFilterType($value)
{
return (preg_match('/\s+\+\s+/', $value) ? Datasource::FILTER_AND : Datasource::FILTER_OR);
}
Expand Down Expand Up @@ -536,4 +536,24 @@ public static function findParameterInEnv($needle, $env)

return null;
}

/**
* By default, all Symphony filters are considering to be AND filters, that is
* they are all used and Entries must match each filter to be included. It is
* possible to use OR filtering in a field by using an + to separate the values.
* eg. If the filter is test1 + test2, this will match any entries where this field
* is test1 OR test2. This function is run on each filter (ie. each field) in a
* datasource
*
* @deprecated Since Symphony 2.6.0 it is recommended to use the static version,
* `Datasource::determineFilterType`
* @param string $value
* The filter string for a field.
* @return integer
* Datasource::FILTER_OR or Datasource::FILTER_AND
*/
public function __determineFilterType($value)
{
return self::determineFilterType($value);
}
}
Expand Up @@ -366,7 +366,7 @@ public function processFilters(&$where, &$joins, &$group)
}

if (!is_array($filter)) {
$filter_type = $this->__determineFilterType($filter);
$filter_type = Datasource::determineFilterType($filter);
$value = preg_split('/'.($filter_type == Datasource::FILTER_AND ? '\+' : '(?<!\\\\),').'\s*/', $filter, -1, PREG_SPLIT_NO_EMPTY);
$value = array_map('trim', $value);
$value = array_map(array('Datasource', 'removeEscapedCommas'), $value);
Expand Down

0 comments on commit a6c86f6

Please sign in to comment.