You are here

function _biblio_contributor_widget in Bibliography Module 6

Same name and namespace in other branches
  1. 6.2 biblio.module \_biblio_contributor_widget()

Parameters

$node:

$fld:

$auth_category:

$biblio_type:

$other_fields:

Return value

contributor fieldset form

1 call to _biblio_contributor_widget()
biblio_form in ./biblio.module
Implementation of hook_form().

File

./biblio.module, line 1410

Code

function _biblio_contributor_widget($node, $fld, $auth_category, $biblio_type, $other_fieldset = FALSE) {
  $init_count = variable_get('biblio_init_auth_count', 4);
  $fldname = $fld['name'];
  $type = str_replace('_', '-', $fldname);
  if (is_object($node)) {
    $contributors = (array) $node->biblio_contributors[$auth_category];
  }
  else {
    $contributors = $node['biblio_contributors'][$auth_category];
  }
  $contributor_count = max($init_count, count($contributors));
  $ctypes = _biblio_get_auth_types($auth_category, $biblio_type);

  // if no author types are available skip this widget
  if (!isset($ctypes)) {
    return array();
  }
  $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;
  }

  // Add a wrapper for the choices and more button.
  $wrapper = array(
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => !$fld['required'] && count($contributors) == 0,
    '#title' => check_plain($fld['title']),
    '#weight' => $fld['weight'] / 10,
    '#description' => t('Enter a single name per line using a format such as "Smith, John K" or "John K Smith" or "J.K. Smith"'),
    '#prefix' => '<div class="clear-block" id="' . $type . '-wrapper">',
    '#suffix' => '</div>',
  );

  // Container for just the contributors.
  $wrapper['biblio_contributors'][$auth_category] = array(
    '#prefix' => '<div id="' . $type . '">',
    '#suffix' => '</div>',
    '#theme' => 'biblio_contributors',
    '#id' => $fldname,
    '#hideRole' => count($options) <= 1,
  );

  // Add the current choices to the form.
  $default_values = array(
    'name' => '',
    'cid' => '',
    'auth_type' => key($options),
  );
  for ($delta = 0; $delta < $contributor_count; $delta++) {
    if (isset($contributors[$delta])) {

      // contributor already exists
      $values = $contributors[$delta];
    }
    else {

      // contributor is new
      $values = $default_values;
    }
    $values['rank'] = $delta;
    $wrapper['biblio_contributors'][$auth_category][$delta] = _biblio_contributor_form($delta, $auth_category, $values, $options, $fld['autocomplete']);
  }

  // We name our button 'contrib_more' to avoid conflicts with other modules using
  // AHAH-enabled buttons with the id 'more'.
  $path = 'biblio/js/' . $biblio_type . '/' . $auth_category . '/' . $fld['autocomplete'];
  if ($other_fieldset) {
    $path .= '/1';
  }
  $wrapper[$fldname . '_more'] = array(
    '#type' => 'submit',
    '#value' => t('More @title', array(
      '@title' => $fld['title'],
    )),
    '#description' => t("If there aren't enough boxes above, click here to add more."),
    '#weight' => 1,
    '#submit' => array(
      'biblio_more_contributors_submit',
    ),
    // If no javascript action.
    '#ahah' => array(
      'path' => $path,
      'wrapper' => $type,
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['contributors' . $auth_category . '_wrapper'] = $wrapper;
  return $form;
}