You are here

function ca_actions_form in Ubercart 6.2

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

ca_actions_form_add_action_submit() ca_actions_form_save_changes_submit()

Parameters

$form_state: Used by FAPI; the current state of the form as it processes.

$pid: The ID of the predicate whose actions are being modified.

$title: TRUE or FALSE to specify whether or not to set the predicate's title to be the title of the page.

Return value

The form array for the actions form.

See also

ca_actions_form_add_action_submit()

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

File

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

Code

function ca_actions_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']);
  }

  // Load up any actions that could possibly be on this predicate.
  $action_data = ca_load_action();
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => $pid,
  );
  $form['actions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Actions'),
    '#description' => t('These actions will be performed in order when this predicate passes the conditions evaluation.'),
    '#tree' => TRUE,
  );

  // Loop through the actions and add them to the form if valid.
  $i = 0;
  foreach ($predicate['#actions'] as $key => $action) {

    // Add it to the form if the action's callback exists.
    $callback = isset($action_data[$action['#name']]['#callback']) ? $action_data[$action['#name']]['#callback'] : '';
    if (function_exists($callback)) {
      $form['actions'][$i] = array(
        '#type' => 'fieldset',
        '#title' => t('Action: @title', array(
          '@title' => $action_data[$action['#name']]['#title'],
        )),
        '#description' => isset($action_data[$action['#name']]['#description']) ? $action_data[$action['#name']]['#description'] : '',
        '#collapsible' => TRUE,
        '#collapsed' => isset($_SESSION['ca_action_focus']) && $i == $_SESSION['ca_action_focus'] ? FALSE : TRUE,
      );
      $form['actions'][$i]['name'] = array(
        '#type' => 'value',
        '#value' => $action['#name'],
      );
      $form['actions'][$i]['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Title'),
        '#default_value' => $action['#title'],
      );
      $form['actions'][$i]['argument_map'] = array(
        '#type' => 'fieldset',
        '#title' => t('Arguments'),
        '#description' => t('Some triggers pass in multiple options for arguments related to a particular action, so you must specify which options to use in the fields below.'),
        '#collapsible' => TRUE,
      );

      // Setup a variable to decide if we can collapse the arguments fieldset.
      $collapsed = TRUE;
      foreach ($action_data[$action['#name']]['#arguments'] as $key => $value) {

        // Load the available arguments for each entity as received by the
        // trigger when it was pulled.
        $options = ca_load_trigger_arguments($predicate['#trigger'], $value['#entity']);

        // If we have more than one option for any argument, do not collapse.
        if (count($options) > 1) {
          $collapsed = FALSE;
        }
        $form['actions'][$i]['argument_map'][$key] = array(
          '#type' => 'select',
          '#title' => check_plain($value['#title']),
          '#options' => $options,
          '#default_value' => $action['#argument_map'][$key],
        );
      }
      $form['actions'][$i]['argument_map']['#collapsed'] = $collapsed;

      // Add the action's form elements if any exist.
      $callback .= '_form';
      if (function_exists($callback)) {

        // Load the trigger data.
        $trigger_data = ca_load_trigger($predicate['#trigger']);
        $form['actions'][$i]['settings'] = $callback(array(), $action['#settings'], $trigger_data['#arguments']);
      }
      $form['actions'][$i]['remove'] = array(
        '#type' => 'submit',
        '#value' => t('Remove this action'),
        '#name' => 'ca_remove_action_' . $i,
        '#submit' => array(
          'ca_actions_form_remove_action_submit',
        ),
        '#attributes' => array(
          'class' => 'ca-remove-confirm',
        ),
      );
      $i++;
    }
  }

  // Unset the session variable used to focus on the new action.
  unset($_SESSION['ca_action_focus']);
  $form['actions']['add_action'] = array(
    '#type' => 'select',
    '#title' => t('Available actions'),
    '#options' => ca_load_trigger_actions($predicate['#trigger']),
  );
  $form['actions']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add action'),
    '#submit' => array(
      'ca_actions_form_add_action_submit',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save changes'),
    '#submit' => array(
      'ca_actions_form_save_changes_submit',
    ),
  );
  filter_configure_form($form);
  return $form;
}