You are here

class RulesDataUICommercePrice in Commerce Core 7

Defines a commerce_price input form for Rules actions altering price fields.

Hierarchy

Expanded class hierarchy of RulesDataUICommercePrice

1 string reference to 'RulesDataUICommercePrice'
commerce_price_rules_data_info in modules/price/commerce_price.rules.inc
Implements hook_rules_data_info().

File

modules/price/commerce_price.rules.inc, line 29
Rules integration for the Price module.

View source
class RulesDataUICommercePrice extends RulesDataUI implements RulesDataDirectInputFormInterface {
  public static function getDefaultMode() {
    return 'input';
  }
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
    $settings += array(
      $name => isset($info['default value']) ? $info['default value'] : array(
        'amount' => NULL,
        'currency_code' => NULL,
      ),
    );
    $value = $settings[$name];

    // Legacy data stored price amount as a scalar value, so we convert it here
    // to the expected data structure.
    if (is_scalar($value)) {
      $value = array(
        'amount' => $value,
        'currency_code' => NULL,
      );
    }
    $currency_code = empty($value['currency_code']) || $value['currency_code'] == 'default' ? commerce_default_currency() : $value['currency_code'];
    $currency = commerce_currency_load($currency_code);
    if (isset($value['amount']) && is_numeric($value['amount'])) {

      // Price amount should always be saved as integers (minor units), but in
      // case they're not we round them.
      if (strpos($value['amount'], '.') === FALSE) {
        $default_amount = $value['amount'];
      }
      else {
        $default_amount = commerce_round(COMMERCE_ROUND_HALF_UP, $value['amount']);
      }

      // Format the number to the proper decimal places for the textfield.
      $default_amount = commerce_currency_amount_to_decimal($default_amount, $currency_code);
      $currency = commerce_currency_load($currency_code);
      $default_amount = number_format($default_amount, $currency['decimals'], '.', '');
    }
    else {
      $default_amount = NULL;
    }
    $form[$name]['#element_validate'] = array(
      '_commerce_price_rules_data_ui_element_validate',
    );
    $form[$name]['amount'] = array(
      '#type' => 'textfield',
      '#default_value' => $default_amount,
      '#size' => 10,
      '#required' => TRUE,
    );

    // Build a currency options list from all enabled currencies.
    $options = array();
    foreach (commerce_currencies(TRUE) as $currency_key => $currency_data) {
      $options[$currency_key] = check_plain($currency_data['code']);
    }

    // If the current currency value is not available, add it now with a message
    // in the help text explaining it.
    if (empty($options[$currency['code']])) {
      $options[$currency['code']] = check_plain($currency['code']);
      $description = t('The currency set for this action is not currently enabled. If you change it now, you will not be able to set it back.');
    }
    else {
      $description = '';
    }

    // If only one currency option is available, don't use a select list.
    if (count($options) == 1) {
      $form[$name]['amount']['#field_suffix'] = reset($options);
      $form[$name]['currency_code'] = array(
        '#type' => 'value',
        '#default_value' => key($options),
      );
    }
    else {
      $form[$name]['#attached']['css'][] = drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css';
      $form[$name]['amount']['#prefix'] = '<div class="commerce-price-full">';
      $form[$name]['currency_code'] = array(
        '#type' => 'select',
        '#description' => $description,
        '#options' => $options,
        '#default_value' => $currency_code,
        '#suffix' => '</div>',
      );
    }
    return $form;
  }
  public static function render($value) {
    return array(
      'content' => array(
        '#markup' => commerce_currency_format($value['amount'], $value['currency_code']),
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesDataUI::getTypeInfo public static function Returns the data type and parameter information for the given arguments.
RulesDataUI::renderOptionsLabel public static function Renders the value with a label if an options list is available.
RulesDataUI::selectionForm public static function Provides the selection form for a parameter.
RulesDataUICommercePrice::getDefaultMode public static function Specifies the default input mode per data type. Overrides RulesDataUI::getDefaultMode
RulesDataUICommercePrice::inputForm public static function Constructs the direct input form. Overrides RulesDataDirectInputFormInterface::inputForm
RulesDataUICommercePrice::render public static function Render the configured value. Overrides RulesDataDirectInputFormInterface::render