You are here

function biblio_form in Bibliography Module 6

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

Implementation of hook_form().

Create the form for collecting the information specific to this node type. This hook requires us to return some HTML that will be later placed inside the form.

File

./biblio.module, line 1156

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;

  /* publication type */
  $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);
  }
  if (!isset($node->nid) && !isset($form_state['storage']['biblio_type'])) {

    // only show the doi lookup and paste boxes if this is a new entry
    if ($user->uid > 0 && phpversion() > 5 && !isset($form_state['storage'])) {
      $form['biblio_doi_lookup'] = array(
        '#type' => 'fieldset',
        '#title' => t('DOI Lookup'),
        '#weight' => -20,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $pid = '';
      if (isset($user->biblio_crossref_pid) && !empty($user->biblio_crossref_pid) && variable_get('biblio_show_crossref_profile_form', '1')) {
        $pid = $user->biblio_crossref_pid;
      }
      else {
        $pid = variable_get('biblio_crossref_pid', '');
      }
      if (empty($pid)) {
        $form['biblio_doi_lookup']['doi_register'] = array(
          '#prefix' => '<p><b>',
          '#suffix' => '</b></p>',
          '#value' => t('You need to register with CrossRef (!crossrefurl) and then enter your CrossRef UserID in the "<i>CrossRef Login Information</i>" section of your account profile(!profilelink).', array(
            '!crossrefurl' => l('http://www.crossref.org/requestaccount/', 'http://www.crossref.org/requestaccount/', array(
              'attributes' => array(
                'target' => '_blank',
              ),
              'absolue' => TRUE,
            )),
            '!profilelink' => l(t('here'), 'user/' . $user->uid . '/edit'),
          )),
        );
      }
      $form['biblio_doi_lookup']['doi_data'] = array(
        '#type' => 'textfield',
        '#title' => t('DOI'),
        '#required' => FALSE,
        '#default_value' => '',
        '#description' => t('Enter a DOI name in the form: <b>10.1000/123456</b>'),
        '#disabled' => $have_pid,
        '#size' => 60,
        '#maxlength' => 255,
        '#weight' => -4,
      );
      $form['biblio_doi_lookup']['doi_submit'] = array(
        '#type' => 'submit',
        '#disabled' => $have_pid,
        '#value' => t('Populate using DOI'),
      );
    }
    if (!isset($form_state['storage'])) {
      $form['biblio_cut_paste'] = array(
        '#type' => 'fieldset',
        '#title' => t('Paste'),
        '#weight' => -20,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['biblio_cut_paste']['paste_data'] = array(
        '#type' => 'textarea',
        '#title' => t('BibTex'),
        '#required' => FALSE,
        '#default_value' => '',
        '#description' => t('Paste a BibTex entry here'),
        '#size' => 60,
        '#maxlength' => 2048,
        '#weight' => -4,
      );
      $form['biblio_cut_paste']['paste_submit'] = array(
        '#type' => 'submit',
        '#value' => t('Populate using BibTex'),
      );
    }
  }
  $form['biblio_type'] = array(
    '#type' => 'select',
    '#title' => t('Publication Type'),
    '#default_value' => $tid,
    '#options' => $options,
    '#description' => NULL,
    '#weight' => -15,
    '#attributes' => array(
      'onchange' => 'document.getElementById(\'node-form\').submit()',
    ),
    '#multiple' => FALSE,
    '#required' => TRUE,
  );
  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    \t\t\t\t\tINNER JOIN {biblio_field_type} bt ON b.fid = bt.fid\n    \t\t\t\t\tINNER JOIN {biblio_field_type_data} btd ON btd.ftdid=bt.ftdid\n    \t\t\t\t\tWHERE 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') {
          $sep = check_plain(variable_get('biblio_keyword_sep', ','));

          // is 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'])) {
            require_once drupal_get_path('module', 'biblio') . '/biblio.keywords.inc';
            $form_state['values']['biblio_keywords'] = biblio_implode_keywords($form_state['values']['biblio_keywords']);
          }
          if (isset($node->{$key}) && is_array($node->{$key})) {
            require_once drupal_get_path('module', 'biblio') . '/biblio.keywords.inc';
            $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');
        }

        //        '#options' => $options,
        if ($main_area && $fld['type'] == 'textarea') {

          /* wrap all textarea fields in collapsed field sets 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,
          );
          $key = $fld['name'] . '_field';
        }

        // embed field directly or in "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 'Other biblio fields' directly below 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 filter form in "Full Text" fieldset, because it applies to the full text only
    $form['body_field']['format'] = filter_form($node->format, 20);
  }

  //$form['format'] = filter_form($node->format, 20);
  return $form;
}