You are here

function money_field_settings in Money field 5

Same name and namespace in other branches
  1. 6 money.module \money_field_settings()

Implementation of hook_field_settings().

File

./money.module, line 27
This module defines the "money" CCK field. It uses the Currency API, which is included in the Currency module, to get a list of valid currencies.

Code

function money_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['decimal_separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Decimal separator'),
        '#default_value' => _money_get_decimal_separator($field['decimal_separator']),
        '#size' => 5,
        '#maxlength' => 255,
        '#description' => t('Three decimal separators are used across the planet: the dot
          (English-speaking countries), the comma (Europe) and the momayyez
          (Arab world and Iran). ISO 31-0 specifies both the dot and the comma
          as valid, but prefers the comma, this is also the default.'),
      );
      $form['digit_group_separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Digit group separator'),
        '#default_value' => _money_get_digit_group_separator($field['digit_group_separator']),
        '#size' => 5,
        '#maxlength' => 255,
        '#description' => t('Three digit group separators are used across the planet: the comma
          (English-speaking countries), the dot (Europe) and the space. ISO
          31-0 specifies only the space as valid, this is also the default.'),
      );
      $form['displayed_decimals'] = array(
        '#type' => 'select',
        '#title' => t('Displayed decimals'),
        '#options' => array(
          0,
          1,
          2,
        ),
        '#default_value' => _money_get_displayed_decimals($field['displayed_decimals']),
        '#description' => t('The number of decimals that will be displayed in the formatters.
          This does <em>not</em> affect the stored number of decimals, or the
          number of decimals that can be entered, which will always be 2.'),
      );
      $form['currency_list'] = array(
        '#value' => theme('money_field_settings_currency_list', currency_api_get_list()),
      );
      $form['allowed_currencies'] = array(
        '#type' => 'textarea',
        '#rows' => 5,
        '#title' => t('Currencies'),
        '#description' => t('Enter the 3-letter ISO codes for the currencies that you want to allow, separated by commas. Leave empty to allow all currencies.'),
        '#default_value' => isset($field['allowed_currencies']) ? $field['allowed_currencies'] : '',
      );
      return $form;
    case 'validate':
      if (!in_array($field['displayed_decimals'], array(
        0,
        1,
        2,
      ))) {
        form_set_error('displayed_decimals', t('Invalid number of displayed decimals. Valid numbers are 0, 1 and 2.'));
      }

      // Empty list of allowed currencies means that *all* currencies will be
      // used!
      if (!empty($field['allowed_currencies'])) {
        $valid_currencies = array_keys(currency_api_get_list());
        $allowed_currencies = _money_parse_currencies($field['allowed_currencies']);
        foreach ($allowed_currencies as $currency) {
          if (!in_array($currency, $valid_currencies)) {
            form_set_error('allowed_currencies', t('The currency %currency is not a valid currency.', array(
              '%currency' => $currency,
            )));
          }
        }
      }
      break;
    case 'save':
      return array(
        'decimal_separator',
        'digit_group_separator',
        'displayed_decimals',
        'allowed_currencies',
      );
    case 'database columns':
      $columns['amount'] = array(
        'type' => 'bigint',
        'length' => 13,
        'not null' => TRUE,
        'default' => 0,
        'unsigned' => FALSE,
        'sortable' => TRUE,
      );
      $columns['currency'] = array(
        'type' => 'varchar',
        'length' => 3,
      );
      return $columns;
    case 'filters':
      $currencies = _money_parse_currencies($field['allowed_currencies']);
      $options = array_combine($currencies, $currencies);
      $filters['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).'),
      );
      $filters['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;
  }
}