You are here

function hansel_ui_rule_form_submit in Hansel breadcrumbs 8

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

Form submit handler.

Saves the rule to the database.

1 string reference to 'hansel_ui_rule_form_submit'
hansel_ui_rule_form in hansel_ui/hansel_ui.module
Generates the form for adding and editing rules.

File

hansel_ui/hansel_ui.module, line 442
Hansel UI module

Code

function hansel_ui_rule_form_submit($form, &$form_state) {
  switch ($form_state['clicked_button']['#id']) {
    case 'edit-save':
      $name = $form_state['values']['name'];
      $pid = $form_state['values']['pid'];
      $action = $form_state['storage']['action'];
      $crumb_action = $form_state['storage']['crumb_action'];
      $rule = new stdClass();

      // Build the update argument for drupal_write_record()
      $update = array();
      if (!empty($form['#rid'])) {
        $update[] = 'rid';
        $rule->rid = $form['#rid'];
      }
      $rule->name = $name;
      $rule->pid = $pid;
      if ($crumb_action) {
        $crumb_action_arguments = array();
        foreach ($form_state['values'] as $name => $value) {
          if (preg_match('/^crumb_action_(.+)$/', $name, $match)) {
            $crumb_action_arguments[$match[1]] = $value;
          }
        }
        $rule->crumb_action = $crumb_action;
        $rule->crumb_action_arguments = serialize($crumb_action_arguments);
      }
      else {
        $rule->crumb_action = '';
        $rule->crumb_action_arguments = serialize(array());
      }
      if (!drupal_write_record('hansel_rule', $rule, $update)) {
        drupal_set_message(t('Error writing rule to database'), 'error');
        return;
      }
      if ($update) {
        foreach (array(
          'goto',
          'leave',
          'switch',
        ) as $table_suffix) {
          $result = db_query("DELETE FROM {hansel_rule_action_" . $table_suffix . "} WHERE rid = :rid", array(
            ':rid' => $rule->rid,
          ));
          $result
            ->rowCount();

          // Make sure the query ran before inserting the new row
        }
      }
      $rule_action = new stdClass();
      $rule_action->rid = $rule->rid;
      switch ($action) {
        case 'goto':
          $rule_action->destination = $form_state['values']['destination_rid'];
          break;
        case 'leave':
          $rule_action->restore_original = $form_state['values']['restore'] ? 1 : 0;
          break;
        case 'switch':
          $rule_action->handler = $form_state['values']['handler'];
          $rule_action->arguments = serialize(array());
          break;
      }
      if (!drupal_write_record('hansel_rule_action_' . $action, $rule_action)) {
        drupal_set_message(t('Error writing rule to database'), 'error');
        return;
      }
      cache_clear_all('hansel_config', 'cache');
      drupal_set_message(t('Succesfully saved rule %name', array(
        '%name' => $rule->name,
      )));
      $form_state['redirect'] = 'admin/config/search/hansel';
      drupal_goto('admin/config/search/hansel');

      // $form_state['redirect'] does not redirect for some reason
      break;
  }
  $form_state['rebuild'] = TRUE;
}