You are here

function theme_biblio_format_authors in Bibliography Module 7.2

Same name and namespace in other branches
  1. 5 biblio.module \theme_biblio_format_authors()
  2. 6.2 includes/biblio_theme.inc \theme_biblio_format_authors()
  3. 6 biblio_theme.inc \theme_biblio_format_authors()
  4. 7 includes/biblio_theme.inc \theme_biblio_format_authors()
9 theme calls to theme_biblio_format_authors()
biblio_format_authors in includes/biblio.theme.inc
biblio_style_ama in styles/biblio_style_ama.inc
biblio_style_apa in styles/biblio_style_apa.inc
Apply a bibliographic style to the node
biblio_style_chicago in styles/biblio_style_chicago.inc
Apply a bibliographic style to the node
biblio_style_classic in styles/biblio_style_classic.inc
Apply a bibliographic style to the node

... See full list

File

includes/biblio.theme.inc, line 445

Code

function theme_biblio_format_authors($variables) {
  $contributions = $variables['contributions'];
  $options = $variables['options'];
  if (empty($contributions)) {
    return;
  }
  list($alnum, $alpha, $cntrl, $dash, $digit, $graph, $lower, $print, $punct, $space, $upper, $word, $patternModifiers) = _biblio_get_regex_patterns();
  $base = variable_get('biblio_base', 'biblio');
  $author_links = variable_get('biblio_author_links', 1);
  $contributionCount = count($contributions);

  // check how many authors we have to deal with
  $output = "";

  // this variable will hold the final author string
  $includeStringAfterFirstAuthor = FALSE;
  if (empty($options['numberOfAuthorsTriggeringEtAl'])) {
    $options['numberOfAuthorsTriggeringEtAl'] = $authorCount;
  }
  if (empty($options['includeNumberOfAuthors'])) {
    $options['includeNumberOfAuthors'] = $authorCount;
  }

  // Temporary until contribution ranks are implemented. @todo: add rank field
  static $rank = 0;
  foreach ($contributions as $cnid => $contribution) {

    // Contribution entity wrapper
    $cn_wrap = biblio_wrapper($contribution, 'biblio_contribution');

    // Contributor entity wrapper
    $cr_wrap = biblio_wrapper($contribution->contributor, 'biblio_contributor');
    $rank++;
    if (!isset($contribution->lastname) && empty($contribution->literal)) {
      module_load_include('inc', 'biblio', '/includes/biblio.contributors');
      $category = $cn_wrap->biblio_contributor_category
        ->value();
      $author = biblio_parse_author($contribution->contributor, (string) $category);

      // this is needed for form preview to fill in all fields
    }
    if (empty($author->literal)) {
      if (!empty($author->firstname)) {
        if ($options['shortenGivenNames']) {

          // if we're supposed to abbreviate given names
          // within initials, reduce all full first names (-> defined by a starting uppercase character, followed by one ore more lowercase characters)
          // to initials, i.e., only retain their first character
          $author->firstname = preg_replace("/([{$upper}])[{$lower}]+/{$patternModifiers}", '\\1', $author->firstname);

          //$author->firstname = drupal_substr($author->firstname, 0, 1);

          // the next line caused extra betweenInitialsDelim to appear in some circumstances

          //$author->firstname = preg_replace("/($space|$dash)?/$patternModifier", $options['betweenInitialsDelim'], $author->firstname);
        }
      }
      if (isset($author->initials)) {

        // within initials, remove any dots:
        $author->initials = preg_replace("/([{$upper}])\\.+/{$patternModifiers}", "\\1", $author->initials);

        //$author->initials = str_replace('.', '',  $author->initials);

        // within initials, remove any spaces *between* initials:
        $author->initials = preg_replace("/(?<=[-{$upper}]) +(?=[-{$upper}])/{$patternModifiers}", "", $author->initials);

        //$author->initials = str_replace(' ', '',  $author->initials);

        // within initials, add a space after a hyphen, but only if ...
        if (preg_match('/ $/', $options['betweenInitialsDelim'])) {

          // ... the delimiter that separates initials ends with a space
          $author->initials = preg_replace("/-(?=[{$upper}])/{$patternModifiers}", "- ", $author->initials);
        }

        // then, separate initials with the specified delimiter:
        $delim = $options['betweenInitialsDelim'];
        $author->initials = preg_replace("/([{$upper}])(?=[^{$lower}]+|\$)/{$patternModifiers}", "\\1{$delim}", $author->initials);
        $shortenInitials = isset($options['numberOfInitialsToKeep']) ? $options['numberOfInitialsToKeep'] : FALSE;
        if ($shortenInitials) {
          $author->initials = drupal_substr($author->initials, 0, $shortenInitials);
        }
        if ($options['shortenGivenNames'] && !empty($author->firstname)) {
          $author->firstname = $author->firstname . $options['betweenInitialsDelim'] . $author->initials;
          if ($shortenInitials) {
            $author->firstname = drupal_substr($author->firstname, 0, $shortenInitials);
          }
        }
        elseif (!empty($author->firstname)) {
          $author->firstname = $author->firstname . ' ' . $author->initials;
        }
        elseif (empty($author->firstname)) {
          $author->firstname = $author->initials;
        }
      }

      // if there is a surname prefix like "van", "von" etc, stick it back before the last name.
      if (!empty($author->prefix)) {
        $author->lastname = $author->prefix . ' ' . $author->lastname;
      }
      if (!empty($author->suffix)) {
        $author->lastname = $author->lastname . ', ' . $author->suffix;
      }
      if (!empty($author->firstname)) {
        if ($cnid == 0) {

          // -> first author
          if ($options['initialsBeforeAuthorFirstAuthor']) {
            $author->name = $author->firstname . $options['AuthorsInitialsDelimFirstAuthor'] . $author->lastname;
          }
          else {
            $author->name = $author->lastname . $options['AuthorsInitialsDelimFirstAuthor'] . $author->firstname;
          }
        }
        else {

          // $cnid > 0 // -> all authors except the first one
          if ($options['initialsBeforeAuthorStandard']) {
            $author->name = $author->firstname . $options['AuthorsInitialsDelimStandard'] . $author->lastname;
          }
          else {
            $author->name = $author->lastname . $options['AuthorsInitialsDelimStandard'] . $author->firstname;
          }
        }
      }
      else {
        $author->name = $author->lastname;
      }
    }
    if ($author_links) {
      $author->name = theme('biblio_author_link', array(
        'author' => $author,
      ));
    }
    else {
      $author->name = check_plain($author->name);
    }

    // append this author to the final author string:
    if ($rank == 0 or $rank + 1 < $contributionCount) {

      // -> first author, or (for multiple authors) all authors except the last one
      if ($rank == 0) {

        // -> first author
        $output .= $author->name;
      }
      else {

        // -> for multiple authors, all authors except the first or the last one
        $output .= $options['BetweenAuthorsDelimStandard'] . $author->name;
      }

      // we'll append the string in '$customStringAfterFirstAuthors' to the number of authors given in '$includeNumberOfAuthors' if the total number of authors is greater than the number given in '$numberOfAuthorsTriggeringEtAl':
      if ($rank + 1 == $options['includeNumberOfAuthors'] and $contributionCount > $options['numberOfAuthorsTriggeringEtAl']) {
        if (preg_match("/__NUMBER_OF_AUTHORS__/", $options['customStringAfterFirstAuthors'])) {
          $customStringAfterFirstAuthors = preg_replace("/__NUMBER_OF_AUTHORS__/", $contributionCount - $options['includeNumberOfAuthors'], $options['customStringAfterFirstAuthors']);

          // resolve placeholder
        }
        $includeStringAfterFirstAuthor = TRUE;
        break;
      }
    }
    elseif ($contributionCount > 1 and $rank + 1 == $contributionCount) {

      // -> last author (if multiple authors)
      $output .= $options['BetweenAuthorsDelimLastAuthor'] . $author->name;
    }
  }

  // do some final clean up:

  //if ($options['encodeHTML'])

  //$output = encodeHTML($output); // HTML encode higher ASCII characters within the newly arranged author contents
  if ($includeStringAfterFirstAuthor) {
    $output .= $options['customStringAfterFirstAuthors'];

    // the custom string won't get HTML encoded so that it's possible to include HTML tags (such as '<i>') within the string
  }
  $output = preg_replace("/  +/", " ", $output);

  // remove double spaces (which occur e.g., when both, $betweenInitialsDelim & $newAuthorsInitialsDelim..., end with a space)
  $output = preg_replace("/ +([,.;:?!()]|\$)/", "\\1", $output);

  // remove excess spaces before [,.;:?!()] and from the end of the author string
  return $output;
}