You are here

function ca_conditions_form in Ubercart 6.2

Build a form for adding and editing conditions on a predicate.

ca_conditions_form_remove_condition_group_submit() ca_conditions_form_add_condition_submit() ca_conditions_form_remove_condition_submit() ca_conditions_form_save_changes_submit()

See also

ca_conditions_form_add_condition_group_submit()

1 string reference to 'ca_conditions_form'
ca_menu in ca/ca.module
Implements hook_menu().

File

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

Code

function ca_conditions_form($form_state, $pid, $title = TRUE) {

  // Locate the specified predicate.
  $predicate = ca_load_predicate($pid);

  // Return an empty form if the predicate does not exist.
  if (empty($predicate)) {
    return array();
  }

  // Include the JS for conditional actions forms.
  drupal_add_js(drupal_get_path('module', 'ca') . '/ca.js');

  // Display the title if appropriate.
  if ($title) {
    drupal_set_title($predicate['#title']);
  }

  // Initialize the conditions available for this predicate.
  ca_load_trigger_conditions($predicate['#trigger']);

  // Add the predicate ID to the form array.
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => $pid,
  );

  // If the predicate conditions array isn't already a container of containers,
  // make it so.  i.e. group => array('group' => array('cond', 'cond'))
  if (empty($predicate['#conditions'])) {
    $predicate['#conditions'] = ca_new_conditions();
  }
  else {
    $keys = array_keys($predicate['#conditions']['#conditions']);
    $key = array_shift($keys);
    if (empty($predicate['#conditions']['#conditions'][$key]['#operator'])) {
      $predicate['#conditions'] = array(
        '#operator' => 'AND',
        '#conditions' => array(
          $predicate['#conditions'],
        ),
      );
    }
  }

  // Recurse through the predicate's conditions to build the form.
  $form['conditions'] = _ca_conditions_form_tree($predicate['#conditions'], $predicate['#trigger']);
  $form['conditions'] = $form['conditions'][0];

  // Always remove the top level condition group controls.
  unset($form['conditions']['condition']);
  unset($form['conditions']['add_condition']);
  unset($form['conditions']['remove_condition_group']);

  // If there are more than one top level condition groups...
  if (count($form['conditions']['conditions']) > 1) {

    // Update the top level fieldset.
    $form['conditions']['#title'] = t('Condition groups');
    $form['conditions']['#collapsible'] = FALSE;

    // Adjust the titles of the operator options to be more explicit.
    $form['conditions']['operator']['#options'] = array(
      'AND' => t('AND. If all of these condition groups are TRUE.'),
      'OR' => t('OR. If any of these condition groups are TRUE.'),
    );
  }
  else {

    // Otherwise remove the top level fieldset and operator.
    unset($form['conditions']['#type']);
    unset($form['conditions']['operator']);

    // Adjust the second level conditions group to not collapse.
    $form['conditions']['conditions'][0]['#collapsible'] = FALSE;
  }

  // Set the conditions array to a tree so the submitted form values mirror the
  // structure of a predicate's #conditions array.
  $form['conditions']['#tree'] = TRUE;
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save changes'),
    '#submit' => array(
      'ca_conditions_form_save_changes_submit',
    ),
  );
  $form['add_condition_group'] = array(
    '#type' => 'submit',
    '#value' => t('Add condition group'),
    '#submit' => array(
      'ca_conditions_form_add_condition_group_submit',
    ),
  );
  filter_configure_form($form);
  return $form;
}