You are here

function _ca_conditions_form_tree in Ubercart 6.2

Recursively add logical groups to the conditions form as fieldsets.

Parameters

$condition: An array consisting of an #operator and another array of #conditions, or a condition array with data pertaining to its callback function.

$trigger: The trigger name and data concerning the arguments that are passed in to the condition.

Return value

A form array representing the tree of conditions.

1 call to _ca_conditions_form_tree()
ca_conditions_form in ca/ca.admin.inc
Build a form for adding and editing conditions on a predicate.

File

ca/ca.admin.inc, line 680
Conditional actions overview UI.

Code

function _ca_conditions_form_tree($condition, $trigger) {

  // We need an index so condition group fieldsets have unique keys.
  static $index = 0;
  $form = array();

  // If we're dealing with a conditions group and not a single condition.
  if (isset($condition['#operator']) && is_array($condition['#conditions'])) {
    $level = $index;
    $form[$level] = array(
      '#type' => 'fieldset',
      '#title' => t('Conditions group'),
      '#collapsible' => TRUE,
    );

    // Add an operator radio select for the group.
    $form[$level]['operator'] = array(
      '#type' => 'radios',
      '#title' => t('Operator'),
      '#options' => array(
        'AND' => t('AND. If all of these conditions are TRUE.'),
        'OR' => t('OR. If any of these conditions are TRUE.'),
      ),
      '#default_value' => $condition['#operator'],
    );
    $form[$level]['conditions'] = array();

    // For each condition in #conditions, add a condition fieldset to the form.
    foreach ($condition['#conditions'] as $sub_condition) {
      $result = _ca_conditions_form_tree($sub_condition, $trigger);
      if (is_array($result)) {
        $form[$level]['conditions'] = array_merge($form[$level]['conditions'], $result);
      }
    }
    $form[$level]['condition'] = array(
      '#type' => 'select',
      '#title' => t('Available conditions'),
      '#options' => ca_load_trigger_conditions(),
    );
    $form[$level]['add_condition'] = array(
      '#type' => 'submit',
      '#value' => t('Add condition'),
      '#submit' => array(
        'ca_conditions_form_add_condition_submit',
      ),
      '#name' => 'add_condition_' . $level,
    );
    $form[$level]['remove_condition_group'] = array(
      '#type' => 'submit',
      '#value' => t('Remove group'),
      '#submit' => array(
        'ca_conditions_form_remove_condition_group_submit',
      ),
      '#attributes' => array(
        'class' => 'ca-remove-confirm',
      ),
      '#name' => 'remove_condition_group_' . $level,
    );
    $index++;
    return $form;
  }
  elseif (!empty($condition)) {

    // Load the data for the conditions as defined by modules.
    $condition_data = ca_load_condition();

    // Otherwise add a fieldset for the individual condition.
    $form[0] = array(
      '#type' => 'fieldset',
      '#title' => t('Condition: @title', array(
        '@title' => $condition_data[$condition['#name']]['#title'],
      )),
      '#description' => t('@description', array(
        '@description' => $condition_data[$condition['#name']]['#description'],
      )),
      '#collapsible' => TRUE,
      '#collapsed' => isset($condition['#expanded']) ? FALSE : TRUE,
    );

    // Add form elements to the fieldset to adjust the condition's settings.
    $form[0] += _ca_conditions_form_condition($condition, $trigger);
    return $form;
  }
}