You are here

rules_forms.ui.inc in Rules Forms Support 7

Defines data type classes for forms and form elements.

File

includes/rules_forms.ui.inc
View source
<?php

/**
 * @file
 * Defines data type classes for forms and form elements.
 */

/**
 * Extends data to be used by Rules.
 */
abstract class RulesFormsDataUI extends RulesDataUI implements RulesDataDirectInputFormInterface {

  /**
   * Provides the default mode.
   */
  public static function getDefaultMode() {
    return 'input';
  }

  /**
   * Provides the default form.
   */
  public static function defaultForm($name, $info, $settings, $title, $type = 'select') {
    $form[$name] = array(
      '#type' => $type,
      '#title' => $title,
      '#default_value' => isset($settings[$name]) ? $settings[$name] : NULL,
      '#required' => empty($info['optional']),
    );
    return $form;
  }

  /**
   * Renders a parameter value.
   */
  public static function render($value) {
    return array(
      'content' => array(
        '#markup' => check_plain($value),
      ),
      '#attributes' => array(
        'class' => array(
          'rules-forms-parameter',
        ),
      ),
    );
  }

}

/**
 * Defines the form element data type UI.
 */
class RulesFormsDataElement extends RulesFormsDataUI {

  /**
   * An array of attributes and the form element types to which they apply.
   *
   * This array is used to filter the form element options available during
   * specific attribute setting actions.
   *
   * @var array
   */
  private static $attributes = array(
    'access' => 'all',
    'changed' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'password',
      'radio',
      'radios',
      'select',
    ),
    'clicked' => array(
      'button',
      'submit',
    ),
    'disabled' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'password',
      'radio',
      'radios',
      'select',
      'button',
      'submit',
    ),
    'default' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'radio',
      'radios',
      'select',
    ),
    'description' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'password',
      'radio',
      'radios',
      'select',
      'item',
    ),
    'error' => 'all',
    'options' => array(
      'select',
      'checkboxes',
    ),
    'required' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'password',
      'radio',
      'radios',
      'select',
    ),
    'suffix' => 'all',
    'title' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'password',
      'radio',
      'radios',
      'select',
      'item',
    ),
    'weight' => 'all',
    'value' => array(
      'textfield',
      'textarea',
      'checkbox',
      'checkboxes',
      'password',
      'radio',
      'radios',
      'select',
    ),
  );

  /**
   * Returns a select list with options including only available elements.
   */
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
    $options = array();

    // Get the type of attribute being set from the end of the element name.
    // The action name should directly correspond with the attribute it sets
    // for this reason.
    $element_name = $element
      ->getElementName();
    $attributes = explode('_', $element_name);
    $attribute = array_pop($attributes);

    // Loop through each variable and retrieve the options that apply to
    // this field type.
    foreach ($element
      ->availableVariables() as $key => $value) {

      // Populate the options list with only form elements.
      if (!in_array($key, array(
        'site',
        'form',
        'form_state',
        'form_id',
        'user',
      ))) {
        $type = substr($key, 0, strpos($key, ':'));

        // Filter the elements by determining whether this attribute
        // can be applied to them.
        if (isset(self::$attributes[$attribute]) && (self::$attributes[$attribute] == 'all' || in_array($type, self::$attributes[$attribute]))) {
          if (!isset($options[$type])) {
            $options[$type] = array();
          }
          $options[$type][$key] = $value['label'];
        }
      }
    }
    ksort($options);
    $form = parent::defaultForm($name, $info, $settings, 'Form element');
    $form[$name]['#options'] = $options;
    return $form;
  }

}

/**
 * Provides a form for the value in Condition: Element has value.
 */
class RulesFormsDataValue extends RulesFormsDataUI {

  /**
   * Sets the input type based on the selected form element type.
   */
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
    $form = parent::defaultForm($name, $info, $settings, 'Value');
    if (!empty($element->settings['element'])) {
      $type = substr($element->settings['element'], 0, strpos($element->settings['element'], ':'));
      if (in_array($type, array(
        'checkboxes',
        'radios',
        'select',
        'textfield',
      ))) {
        $form[$name]['#type'] = 'textarea';
        $form[$name]['#title'] = 'Value(s)';
      }
      else {
        $form[$name]['#type'] = $type;
      }
    }
    return $form;
  }

}

Classes

Namesort descending Description
RulesFormsDataElement Defines the form element data type UI.
RulesFormsDataUI Extends data to be used by Rules.
RulesFormsDataValue Provides a form for the value in Condition: Element has value.