You are here

function biblio_form in Bibliography Module 6.2

Same name and namespace in other branches
  1. 5 biblio.module \biblio_form()
  2. 6 biblio.module \biblio_form()
  3. 7 biblio.module \biblio_form()
  4. 7.2 biblio.module \biblio_form()

Implements hook_form().

This function creates the form for collecting the information specific to the biblio node type. This hook requires us to return a $form array that later will be incorporated into a complete form.

File

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

Code

function biblio_form($node, $form_state) {
  global $user;
  $fields = array();
  $tid = isset($form_state['storage']['biblio_type']) ? $form_state['storage']['biblio_type'] : (isset($node->biblio_type) ? $node->biblio_type : '');
  $show_fields = !empty($tid);
  $form['#validate'][] = 'biblio_form_validate';
  $form['#cache'] = TRUE;

  // Create a select box for the publication type of this biblio node
  $param['options'] = array(
    "enctype" => "multipart/form-data",
  );
  $result = db_query('SELECT t.* FROM {biblio_types} as t WHERE tid > -2 AND visible = 1');
  while ($option = db_fetch_object($result)) {
    $options[$option->tid] = _biblio_localize_type($option->tid, $option->name);
  }
  asort($options);
  $type_options[-1] = t('Select Type...');
  $type_options += $options;
  $form['biblio_type'] = array(
    '#type' => 'select',
    '#title' => t('Publication Type'),
    '#default_value' => !empty($tid) ? $tid : -1,
    '#options' => $type_options,
    '#description' => NULL,
    '#weight' => -15,
    '#attributes' => array(
      'onchange' => 'document.getElementById(\'node-form\').submit()',
    ),
    '#multiple' => FALSE,
    '#required' => TRUE,
  );

  // If the biblio type is defined, add various fields to the form
  if ($show_fields) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#required' => TRUE,
      '#default_value' => trim($form_state['values']['title'] ? $form_state['values']['title'] : $node->title),
      '#size' => 60,
      '#maxlength' => 255,
      '#weight' => -4,
    );

    // Build the field array used to make the form
    $result = db_query("SELECT * FROM {biblio_fields} b\n              INNER JOIN {biblio_field_type} bt ON b.fid = bt.fid\n              INNER JOIN {biblio_field_type_data} btd ON btd.ftdid=bt.ftdid\n              WHERE bt.tid=%d ORDER BY bt.weight ASC", $tid);
    while ($row = db_fetch_array($result)) {
      $fields[$row['name']] = $row;
    }
    _biblio_localize_fields($fields);
    if (!variable_get('biblio_hide_other_fields', 0)) {
      $form['other_fields'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('Other Biblio Fields'),
        '#description' => '',
        '#weight' => 0,
      );
    }
    $max_visible_weight = -5;
    foreach ($fields as $key => $fld) {
      $options = '';
      $main_area = $fld['common'] || $fld['visible'];
      if ($fld['type'] == 'contrib_widget') {
        $auth_category = $fld['fid'];
        if ($main_area) {
          if (isset($form_state['values'])) {
            $form += _biblio_contributor_widget($form_state['values'], $fld, $auth_category, $tid);
          }
          else {
            $form += _biblio_contributor_widget($node, $fld, $auth_category, $tid);
          }
        }
        else {
          if (isset($form_state['values']) && !variable_get('biblio_hide_other_fields', 0)) {
            $form['other_fields'] += _biblio_contributor_widget($form_state['values'], $fld, $auth_category, $tid, TRUE);
          }
          elseif (!variable_get('biblio_hide_other_fields', 0)) {
            $form['other_fields'] += _biblio_contributor_widget($node, $fld, $auth_category, $tid, TRUE);
          }
        }
      }
      else {
        if ($key == 'biblio_keywords') {
          module_load_include('inc', 'biblio', 'includes/biblio.keywords');
          $sep = check_plain(variable_get('biblio_keyword_sep', ','));

          // If the kewords are in array form, then implode them into a string.
          if (isset($form_state['values']['biblio_keywords']) && is_array($form_state['values']['biblio_keywords'])) {
            $form_state['values']['biblio_keywords'] = biblio_implode_keywords($form_state['values']['biblio_keywords']);
          }
          if (isset($node->{$key}) && is_array($node->{$key})) {
            $node->{$key} = biblio_implode_keywords($node->{$key});
          }
          if (empty($fld['hint'])) {
            $fld['hint'] = t('Separate keywords using the " @sep " character', array(
              '@sep' => $sep,
            ));
          }
        }
        $field_widget = array(
          '#default_value' => $form_state['values'][$key] ? $form_state['values'][$key] : $node->{$key},
          '#type' => $fld['type'],
          '#title' => check_plain($fld['title']),
          '#size' => $fld['size'],
          '#required' => $fld['required'],
          '#maxlength' => $fld['maxsize'],
          '#weight' => $fld['weight'] / 10,
          '#autocomplete_path' => $fld['autocomplete'] ? 'biblio/autocomplete/' . $fld['name'] : '',
          '#description' => check_plain($fld['hint']),
        );
        if ($key == 'biblio_refereed') {
          $field_widget['#options'] = array(
            '' => t('None'),
            'Refereed' => t('Refereed'),
            'Non-Refereed' => t('Non-Refereed'),
            'Does Not Apply' => t('Does Not Apply'),
            'Unknown' => t('Unknown'),
          );
          $field_widget['#description'] = t('If you are not sure, set this to Unknown or Does Not Apply');
        }
        if ($fld['type'] == 'textarea') {

          // Wrap all textarea fields in collapsed field sets in order to save
          // space on the page.
          $field_widget = array(
            '#type' => 'fieldset',
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#title' => check_plain($fld['title']),
            '#description' => '',
            '#weight' => $fld['weight'] / 10,
            $key => $field_widget,
            'format' => filter_form($node->biblio_formats[$key], 20, array(
              'biblio_formats',
              $key,
            )),
          );
          $key = $fld['name'] . '_field';
        }

        // Embed field directly into the form or into "Other Fields" fieldset.
        if ($main_area) {
          $form[$key] = $field_widget;
          $max_visible_weight = max($max_visible_weight, $field_widget['#weight']);
        }
        elseif (!variable_get('biblio_hide_other_fields', 0)) {
          $form['other_fields'][$key] = $field_widget;
        }
      }
    }

    // Place the 'Other biblio fields' directly below the visible fields.
    $form['other_fields']['#weight'] = $max_visible_weight + 0.1;
    $form['body_field'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Full Text'),
      '#description' => '',
      '#weight' => $max_visible_weight + 0.2,
    );
    $form['body_field']['body'] = array(
      '#type' => 'textarea',
      '#title' => t('Full Text'),
      '#default_value' => $node->body,
      '#rows' => 10,
      '#required' => FALSE,
      '#description' => t('You may enter a full text or HTML version of the publication here.'),
      '#weight' => 19,
    );

    // Embed the filter form into the "Full Text" fieldset because it applies
    // only to the full text element.
    $form['body_field']['format'] = filter_form($node->format, 20);
  }
  return $form;
}