to filter by amount, add the following code:
case 'filters':
$currencies = _money_parse_currencies($field['allowed_currencies']);
$options = array_combine($currencies, $currencies);

$filters = array(
'currency'=> array(
'name' => t('Filter by currency'),
// It seems Views requires your operator handler to contain at least
// an "OR" operator if you want the multiple select to mark the
// selected currencies as selected when a user revisits the form. It's
// stored correctly in the DB though. So, as a work-around, I opted to
// use "OR" and "NOR" and then use the desired operators in the actual
// filter handler.
'operator' => array('OR' => t('Is One Of'), 'NOR' => t('Is None Of')),
'value' => array(
'#type' => 'select',
'#multiple' => TRUE,
'#options' => $options,
),
'handler' => 'money_views_handler_filter_currency',
'help' => t('This filter allows you to filter by currency (or multiple currencies).'),
),
'amount' => array(
'name' => t('Filter by amount'),
'operator' => 'views_handler_operator_gtlt',
),
);

return $filters;

CommentFileSizeAuthor
#4 money-filter-amount.patch1.81 KBjohn morahan

Comments

M. P.’s picture

Status: Active » Needs review

Thanks for the start, here is the whole code - works for me so far.
NOTE: Not tested with multiple currencies and it has no support of filtering amount ranges

Modifing the Views filter:

case 'filters':
        $currencies = _money_parse_currencies($field['allowed_currencies']);
        $options = array_combine($currencies, $currencies);
        
        $filters = array(
          'currency'=> array(
            'name' => t('Filter by currency'),
            // It seems Views requires your operator handler to contain at least
            // an "OR" operator if you want the multiple select to mark the
            // selected currencies as selected when a user revisits the form. It's
            // stored correctly in the DB though. So, as a work-around, I opted to
            // use "OR" and "NOR" and then use the desired operators in the actual
            // filter handler.
            'operator' => array('OR' => t('Is One Of'), 'NOR' => t('Is None Of')),
            'value' => array(
              '#type' => 'select',
              '#multiple' => TRUE,
              '#options' => $options,
             ),
            'handler' => 'money_views_handler_filter_currency',
            'help' => t('This filter allows you to filter by currency (or multiple currencies).'),
          ),
          'amount' => array(
            'name' => t('Filter by amount'),
            'operator' => 'views_handler_operator_gtlt',
            'value' => array(
              '#type' => 'textfield',
              '#size' => 5,
             ),
            'handler' => 'money_views_handler_filter_amount',
            'help' => t('This filter allows you to filter by amount.'),
          ),
        );
        
        return $filters;

And added the Views filter handler:

/**
 * Views filter handler: amount filter.
 */
function money_views_handler_filter_amount($op, $filter_data, $filter_info, &$query) {
  // It's a query, so give the user more flexibility, e.g. 35 or 35,00 - must be adjusted to your needs
  $decimal_separator = preg_quote(_money_get_decimal_separator($field['decimal_separator']));
  $digit_group_separator = preg_quote(_money_get_digit_group_separator($field['digit_group_separator']));
  $converted_amount = str_replace(array($decimal_separator, $digit_group_separator), array('.', ''),  $filter_data['value']);
  $amount = $converted_amount * 100;
  
  $table = $filter_info['table'];
  $table_field = $filter_info['content_db_info']['columns']['amount']['column'];
  $operator = $filter_data['operator'];

  $query->add_table($table);
  $query->add_where("$table.$table_field $operator ('". $amount ."')");
}
wim leers’s picture

Title: filter by amount » Filter by amount (but not taking the currencies into account)
Status: Needs review » Needs work

That's what I was about to say... Anyway, I'll add this in. I'd appreciate it if you could roll a patch. See http://drupal.org/patch/create.

sleven’s picture

Nice but stupid.

Is that much complicated to say where to put this code?
Witch file, where.
It's not problem I am new here. Problem is that everybody is smrat guy.

This is incomplete.

john morahan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.81 KB

Just rolled a patch from the above, haven't reviewed this or anything

Gidgidonihah’s picture

I've applied the patch and it appears to be working correctly. Haven't noticed any flaws yet.

wim leers’s picture

Assigned: Unassigned » wim leers
Status: Needs review » Fixed

Implemented.

Thanks for the start, but there were several issues:
- the separators were not being picked up properly (by calling those functions you ensure that the default ones are used, but if you used custom ones it wouldn't work)
- preg_quote() was used where it shouldn't
- code duplication, I've abstracted the amount conversion into a separate function.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.