You are here

function _save_contributors in Bibliography Module 7

Same name and namespace in other branches
  1. 6 biblio.contributors.inc \_save_contributors()

Save contributors to the database.

Parameters

$authors:

$nid:

$vid:

$update:

Return value

success of database operations

3 calls to _save_contributors()
biblio_insert_contributors in includes/biblio.contributors.inc
biblio_update_6027 in ./biblio.install
biblio_update_contributors in includes/biblio.contributors.inc

File

includes/biblio.contributors.inc, line 407

Code

function _save_contributors(&$contributors, $nid, $vid, $update = FALSE) {
  $rank = 0;
  db_delete('biblio_contributor')
    ->condition(db_and()
    ->condition('nid', $nid)
    ->condition('vid', $vid))
    ->execute();

  // re-sort the authors by rank because the rank may have changed due to tabledrag on the input form.
  _biblio_contributor_sort($contributors);
  $name_parser = new HumanNameParser_Parser();
  foreach ($contributors as $key => $author) {
    if (!empty($author['name'])) {
      if (isset($author['cid']) && !empty($author['cid'])) {

        // Check to make sure the name wasn't changed
        // this should only happen via the node/add/biblio input form.
        $auth = biblio_get_contributor($author['cid']);
        if (!empty($auth) && isset($auth->name) && $auth->name != $author['name']) {

          // If the name has changed, NULL the cid so a new entry is created.
          $author['cid'] = NULL;
        }
        else {
          $contributors[$key] = array_merge($author, (array) $auth);
        }
      }

      // If we don't have a cid, lets see if we can find and exact match
      // to the name and use that cid.
      if (!isset($author['cid']) || empty($author['cid'])) {
        $auth = biblio_get_contributor_by_name($author['name']);
        if (!empty($auth) && isset($auth->cid)) {
          $author['cid'] = $auth->cid;
          $contributors[$key] = array_merge($author, (array) $auth);
        }
      }

      // If we still don't have a cid, then create a new entry in the biblio_contirbutor_data table.
      if (empty($author['cid'])) {
        try {
          $author = $name_parser
            ->parseName($author);
        } catch (Exception $e) {
          $link = l('in node ' . $nid, 'node/' . $nid);
          $message = $e
            ->getMessage() . ' ' . $link;
          drupal_set_message($message, 'error');
          watchdog('biblio', $message, array(), WATCHDOG_ERROR);
        }
        $contributors[$key] = $author;
        biblio_save_contributor($author);
      }

      // We should now have a cid, if not we are in big trouble...
      if (empty($author['cid'])) {

        // Throw error that author was not saved.
      }
      $link_array = array(
        'nid' => $nid,
        'vid' => $vid,
        'cid' => $author['cid'],
        'rank' => $rank++,
        'auth_type' => !empty($author['auth_type']) ? $author['auth_type'] : $author['auth_category'],
        'auth_category' => $author['auth_category'],
      );
      if (!drupal_write_record('biblio_contributor', $link_array)) {
        return FALSE;
      }
    }
  }

  // @todo check if it is necessary to reset aka here...
  // @code
  //  db_query("UPDATE {biblio_contributor_data} SET aka = cid WHERE aka = 0 OR aka IS NULL");
  //  db_update('biblio_contributor_data')
  //    ->fields(array('aka', )
  // @endcode
  // Successfully saved all contributors.
  return TRUE;
}