You are here

function hansel_ui_rule_form in Hansel breadcrumbs 8

Same name and namespace in other branches
  1. 7 hansel_ui/hansel_ui.module \hansel_ui_rule_form()

Generates the form for adding and editing rules.

_state

Parameters

array $form:

int $rid:

Return value

array

2 string references to 'hansel_ui_rule_form'
hansel_ui_page in hansel_ui/hansel_ui.module
Page callback
_hansel_ui_menu in hansel_ui/hansel_ui.registry.inc
Registry function for hook_menu().

File

hansel_ui/hansel_ui.module, line 246
Hansel UI module

Code

function hansel_ui_rule_form($form, &$form_state, $rid = NULL) {
  $form = array();
  if ($form_state['submitted']) {
    !empty($form_state['storage']) or $form_state['storage'] = array();
    $form_state['storage'] += $form_state['values'];
  }
  if ($rid) {
    $form['#rid'] = $rid;
    $sql = "SELECT r.rid, r.pid, r.name, r.crumb_action, r.crumb_action_arguments,\n      rag.destination as goto_destination, ral.restore_original as leave_restore,\n      ras.handler as switch_handler, ras.arguments as switch_arguments\n      FROM {hansel_rule} r\n      LEFT JOIN {hansel_rule_action_goto} rag ON rag.rid = r.rid\n      LEFT JOIN {hansel_rule_action_leave} ral ON ral.rid = r.rid\n      LEFT JOIN {hansel_rule_action_switch} ras ON ras.rid = r.rid\n      WHERE r.rid = :rid";
    $res = db_query($sql, array(
      ':rid' => $rid,
    ));
    if (!($rule = $res
      ->fetchObject())) {
      drupal_set_message(t('Rule not found'), 'error');
      drupal_goto('admin/config/search/hansel');
    }
    $rule->crumb_action_arguments = unserialize($rule->crumb_action_arguments);
    $rule_action = 'leave';
    empty($rule->goto_destination) or $rule_action = 'goto';
    empty($rule->switch_handler) or $rule_action = 'switch';
  }
  else {
    $rule = new stdClass();
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
    '#default_value' => isset($form_state['storage']['name']) ? $form_state['storage']['name'] : isset($rule->name) ? $rule->name : '',
  );
  $form['pid'] = array(
    '#type' => 'select',
    '#title' => t('Parent'),
    '#options' => _hansel_get_rules_tree(-1),
    '#default_value' => isset($form_state['storage']['pid']) ? $form_state['storage']['pid'] : isset($rule->pid) ? $rule->pid : 0,
  );
  $form['config'] = array(
    '#prefix' => '<div id="hansel-config">',
    '#suffix' => '</div>',
  );
  if (empty($form_state['values']['action'])) {
    $options = array(
      '' => 'none',
    );
    foreach (_hansel_get_action_types() as $types => $details) {
      $options[$types] = $types;
    }
    $form['config']['crumb_action'] = array(
      '#type' => 'select',
      '#title' => t('Action with breadcrumbs'),
      '#options' => $options,
      '#default_value' => isset($rule->crumb_action) ? $rule->crumb_action : 'add single link',
    );
    $form['config']['action'] = array(
      '#type' => 'radios',
      '#title' => t('Action'),
      '#required' => TRUE,
      '#options' => array(
        'goto' => t('Goto rule'),
        'leave' => t('Leave'),
        'switch' => t('Switch'),
      ),
      '#default_value' => isset($rule_action) ? $rule_action : '',
    );
    $form['config']['next'] = array(
      '#type' => 'submit',
      '#value' => t('Next'),
      '#submit' => array(
        'hansel_ui_rule_form_submit',
      ),
      '#ajax' => array(
        'callback' => 'hansel_ui_rule_form_ajax',
        'wrapper' => 'hansel-config',
        'effect' => 'fade',
      ),
    );

    // Add a cancel link only if we are editting an existing rule
    if (isset($rule->name)) {
      $form['config']['buttons']['cancel'] = array(
        '#value' => l(t('Cancel'), 'admin/config/search/hansel'),
      );
    }
  }
  else {
    if (!empty($form_state['values']['crumb_action'])) {
      $action_types = _hansel_get_action_types();
      if (!empty($action_types[$form_state['values']['crumb_action']]['config form'])) {
        $arguments = !empty($rule->crumb_action_arguments) ? $rule->crumb_action_arguments : array();
        $config_form = call_user_func($action_types[$form_state['values']['crumb_action']]['config form'], $arguments);
        if (!empty($config_form)) {
          $form['config']['crumb'] = array(
            '#type' => 'fieldset',
            '#title' => t('Actions with breadcrumbs'),
          );
        }
        foreach ($config_form as $name => $element) {
          $form['config']['crumb']['crumb_action_' . $name] = $element;
        }

        // Add the list of available tokens.
        if (!empty($action_types[$form_state['values']['crumb_action']]['show token list'])) {
          $form['config']['tokens'] = _hansel_ui_rule_form_generate_token_list();
        }
      }
    }
    if ($form_state['values']['action'] == 'goto') {
      $options = _hansel_get_rules_tree();
      $form['config']['goto'] = array(
        '#type' => 'fieldset',
        '#title' => t('Goto'),
      );
      $form['config']['goto']['destination_rid'] = array(
        '#type' => 'select',
        '#title' => t('Destination'),
        '#options' => $options,
        '#required' => TRUE,
        '#default_value' => isset($rule->goto_destination) ? $rule->goto_destination : 0,
      );
    }
    if ($form_state['values']['action'] == 'switch') {
      $options = array();
      foreach (_hansel_get_switch_types() as $types => $details) {
        $options[$types] = $types;
      }
      $form['config']['switch'] = array(
        '#type' => 'fieldset',
        '#title' => t('Switch'),
      );
      $form['config']['switch']['handler'] = array(
        '#type' => 'select',
        '#title' => t('Switch on'),
        '#options' => $options,
        '#required' => TRUE,
        '#default_value' => isset($rule->switch_handler) ? $rule->switch_handler : '',
      );
    }
    if ($form_state['values']['action'] == 'leave') {
      $form['config']['leave'] = array(
        '#type' => 'fieldset',
        '#title' => t('Leave'),
      );
      $form['config']['leave']['restore'] = array(
        '#type' => 'checkbox',
        '#title' => t('Restore original breadcrumbs'),
        '#default_value' => !empty($rule->leave_restore) ? TRUE : FALSE,
      );
    }
    $form['config']['buttons']['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#submit' => array(
        'hansel_ui_rule_form_submit',
      ),
    );
    $form['config']['buttons']['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#submit' => array(
        'hansel_ui_rule_form_submit',
      ),
    );
    $form['config']['buttons']['cancel'] = array(
      '#markup' => l(t('Cancel'), 'admin/config/search/hansel'),
    );
  }
  return $form;
}