You are here

function biblio_contributors_js in Bibliography Module 6.2

Same name and namespace in other branches
  1. 6 biblio.module \biblio_contributors_js()

Prepares a JSON response for the contributor widget.

1 string reference to 'biblio_contributors_js'
biblio_menu in ./biblio.module
Implements hook_menu().

File

./biblio.module, line 1701
Main file for Drupal module biblio.

Code

function biblio_contributors_js($tid, $auth_category, $other_fields = FALSE) {
  $delta = count($_POST['biblio_contributors'][$auth_category]);

  // Build our new form element.
  $ctypes = _biblio_get_auth_types($auth_category, $tid);
  $ctypes = db_query('SELECT * FROM {biblio_contributor_type_data}
                      WHERE auth_type IN (' . implode(',', $ctypes) . ')');
  while ($ctype = db_fetch_object($ctypes)) {
    $options[$ctype->auth_type] = $ctype->title;
  }
  $default_values = array(
    'name' => '',
    'cid' => '',
    'auth_type' => key($options),
    'rank' => $delta,
  );
  $form_element = _biblio_contributor_form($delta, $auth_category, $default_values, $options);
  drupal_alter('form', $form_element, array(), 'biblio_contributors_js');

  // Build the new form.
  $form_state = array(
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];

  // Add the new element to the stored form. Without adding the element to the
  // form, Drupal is not aware of this new elements existence and will not
  // process it. We retreive the cached form, add the element, and resave.
  $form = form_get_cache($form_build_id, $form_state);
  if ($other_fields) {
    $form['other_fields']['contributors' . $auth_category . '_wrapper']['biblio_contributors'][$auth_category][$delta] = $form_element;
  }
  else {
    $form['contributors' . $auth_category . '_wrapper']['biblio_contributors'][$auth_category][$delta] = $form_element;
  }
  form_set_cache($form_build_id, $form, $form_state);
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );

  // Rebuild the form.
  $form = form_builder('biblio_node_form', $form, $form_state);

  // Render the new output.
  if ($other_fields) {
    $contributor_form = $form['other_fields']['contributors' . $auth_category . '_wrapper']['biblio_contributors'][$auth_category];
  }
  else {
    $contributor_form = $form['contributors' . $auth_category . '_wrapper']['biblio_contributors'][$auth_category];
  }
  unset($contributor_form['#prefix'], $contributor_form['#suffix']);

  // Prevent duplicate wrappers.
  $contributor_form[$delta]['#attributes']['class'] = empty($contributor_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $contributor_form[$delta]['#attributes']['class'] . ' ahah-new-content';
  $output = theme('status_messages') . drupal_render($contributor_form);
  print drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
  exit;
}