You are here

function _biblio_contributor_widget in Bibliography Module 6.2

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

Defines a contributor widget to be used as part of biblio node form.

Parameters

object|array $node: An object or node with information about about the biblio item.

array $fld: An array of information about a field.

string $auth_category: A string representing the category of author.

string $biblio_type: A string indicating the type of biblio item.

bool $other_fieldset: (optional) A logical flag indicating

Return value

array Associative array with form element keys for this contributor widget.

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

File

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

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, return nothing for 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>',
  );

  // Define a 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++) {

    // The contributor already exists.
    if (isset($contributors[$delta])) {
      $values = $contributors[$delta];
    }
    else {
      $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;
  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,
    // In the event of no javascript action, execute this submit function.
    '#submit' => array(
      'biblio_more_contributors_submit',
    ),
    '#ahah' => array(
      'path' => $path,
      'wrapper' => $type,
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['contributors' . $auth_category . '_wrapper'] = $wrapper;
  return $form;
}