You are here

function biblio_form_validate in Bibliography Module 6

Same name and namespace in other branches
  1. 6.2 biblio.module \biblio_form_validate()
  2. 7.2 biblio.module \biblio_form_validate()

Implementation of hook_validate().

Errors should be signaled with form_set_error().

1 string reference to 'biblio_form_validate'
biblio_form in ./biblio.module
Implementation of hook_form().

File

./biblio.module, line 1585

Code

function biblio_form_validate($form, &$form_state) {
  $op = isset($form['#post']['op']) ? $form['#post']['op'] : ($form['#post']['biblio_type'] > 0 ? t('Save') : '');
  switch ($op) {
    case t('Populate using DOI'):
      module_load_include('inc', 'biblio', 'biblio.import.export');
      if ($op == t('Populate using DOI') && ($doi_start = strpos($form_state['values']['doi_data'], '10.')) !== FALSE) {
        $doi = substr($form_state['values']['doi_data'], $doi_start);
        $node_data = biblio_crossref_xml_import($doi);
        if (isset($node_data['error'])) {
          unset($form_state['values']['biblio_type']);
          unset($form_state['post']['biblio_type']);
          form_set_error('doi_data', $node_data['error']);
          return;
        }
      }
      else {
        form_set_error('doi_data', t('This does not appear to be a valid DOI name, it should start with "10." '));
        return;
      }
      break;
    case t('Populate using BibTex'):
      module_load_include('inc', 'biblio', 'biblio.import.export');
      if (strlen($form_state['values']['paste_data'])) {
        $node_data = biblio_bibtex_import($form_state['values']['paste_data'], array(), FALSE, NULL, FALSE, TRUE);
      }
      else {
        form_set_error('bibtex_data', t('There does not appear to be any bibtex data to work with '));
        return;
      }
      break;
    case t('Save'):
      if ($form_state['storage']['biblio_type'] == $form_state['values']['biblio_type'] || !empty($form['#node']->biblio_type) && $form['#node']->biblio_type == $form_state['values']['biblio_type']) {
        unset($form_state['storage']);
      }
      else {

        // Clear validation errors that other modules might have set.
        // This assumes that Biblio has a greater weight than those
        // other modules.
        form_set_error(NULL, '', TRUE);
        drupal_get_messages('error');
        $form_state['storage']['biblio_type'] = $form_state['values']['biblio_type'];
        $form_state['submitted'] = TRUE;
        $form_state['rebuild'] = TRUE;
        return;
      }
  }
  if (!empty($node_data)) {
    $form_state['values'] = array_merge($form_state['values'], $node_data[0]);
    $form_state['storage']['biblio_type'] = $node_data[0]['biblio_type'];
    return;
  }
  if (isset($form_state['values']['biblio_keywords'])) {
    require_once drupal_get_path('module', 'biblio') . '/biblio.keywords.inc';
    if (!is_array($form_state['values']['biblio_keywords'])) {
      $form_state['values']['biblio_keywords'] = biblio_explode_keywords($form_state['values']['biblio_keywords']);
    }
    foreach ($form_state['values']['biblio_keywords'] as $keyword) {
      if (strlen($keyword) > 255) {
        form_set_error('biblio_keywords', t('No single keyword can be greater than 255 characters in length, the word: @kw exceeds this length', array(
          '@kw' => $keyword,
        )));
      }
    }
  }
}