You are here

function _biblio_save_contributors in Bibliography Module 6.2

Save contributors to the database.

Parameters

$contributors:

$nid:

$vid:

$update: (optional) A logical flag indicating whether ...

Return value

success of database operations

4 calls to _biblio_save_contributors()
biblio_insert_contributors in includes/biblio.contributors.inc
biblio_update_6027 in ./biblio.install
Update ...
biblio_update_contributors in includes/biblio.contributors.inc
_biblio_move_authors in ./biblio.install
Moves author data into biblio_contributor* tables.

File

includes/biblio.contributors.inc, line 435
Functions related to contributors in Drupal biblio module.

Code

function _biblio_save_contributors(&$contributors, $nid, $vid, $update = FALSE) {
  module_load_include('php', 'biblio', 'includes/Parser');
  module_load_include('php', 'biblio', 'includes/Name');
  $rank = 0;
  db_query('DELETE FROM {biblio_contributor} WHERE nid = %d AND vid = %d', array(
    $nid,
    $vid,
  ));
  $name_parser = new HumanNameParser_Parser();
  if (is_array($contributors) && count($contributors)) {
    foreach ($contributors as $cat => $authors) {

      // Re-sort the authors by rank because the rank may have changed due to
      // javascript tabledrag on the input form.
      _biblio_contributor_sort($authors);
      foreach ($authors 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[$cat][$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'])) {
            $cid = biblio_get_cid_by_name($author['name']);
            if ($cid) {
              $author['cid'] = $cid;
              $contributors[$cat][$key]['cid'] = $cid;
            }
          }

          // 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[$cat][$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++,
            //((isset($author['rank']) && is_numeric($author['rank'])) ? $author['rank'] : $key),
            'auth_type' => $author['auth_type'],
            'auth_category' => $cat,
          );
          if (!drupal_write_record('biblio_contributor', $link_array)) {
            return FALSE;
          }
        }
      }
    }
  }
  db_query("UPDATE {biblio_contributor_data} SET aka = cid WHERE aka = 0 OR aka IS NULL");
  return TRUE;
}