You are here

function csl_name::render in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 modules/CiteProc/CSL.inc \csl_name::render()
  2. 7 modules/CiteProc/CSL.inc \csl_name::render()

Overrides csl_rendering_element::render

File

modules/CiteProc/CSL.inc, line 543

Class

csl_name

Code

function render($names, $mode = NULL) {
  $text = '';
  $authors = array();
  $count = 0;
  $auth_count = 0;
  $et_al_triggered = FALSE;
  $initialize_with = $this->{'initialize-with'};
  if (!$this->attr_init || $this->attr_init != $mode) {
    $this
      ->init_attrs($mode);
  }
  foreach ($names as $rank => $name) {
    if (empty($name['literal'])) {
      if (!isset($name['lastname'])) {
        module_load_include('inc', 'biblio', '/includes/biblio.contributors');
        $name = biblio_parse_author($name);

        // this is needed for form preview to fill in all fields
      }
      $count++;
      if (!empty($name['firstname']) && isset($initialize_with)) {
        $name['firstname'] = preg_replace("/([{$this->upper}])[{$this->lower}]+/{$this->patternModifiers}", '\\1', $name['firstname']);
        $name['firstname'] = preg_replace("/(?<=[-{$this->upper}]) +(?=[-{$this->upper}])/{$this->patternModifiers}", "", $name['firstname']);
        $name['initials'] = $name['firstname'] . $name['initials'];
      }
      if (isset($name['initials'])) {

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

        // within initials, remove any spaces *between* initials:
        $name['initials'] = preg_replace("/(?<=[-{$this->upper}]) +(?=[-{$this->upper}])/{$this->patternModifiers}", "", $name['initials']);
        if (isset($this->citeproc->style) && $this->citeproc->style->{'initialize-with-hyphen'} == 'false') {
          $name['initials'] = preg_replace("/-/", '', $name['initials']);
        }

        // within initials, add a space after a hyphen, but only if ...
        if (preg_match("/ \$/", $initialize_with)) {

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

        // then, separate initials with the specified delimiter:
        $name['initials'] = preg_replace("/([{$this->upper}])(?=[^{$this->lower}]+|\$)/{$this->patternModifiers}", "\\1{$initialize_with}", $name['initials']);

        //      $shortenInitials = (isset($options['numberOfInitialsToKeep'])) ? $options['numberOfInitialsToKeep'] : FALSE;
        //      if ($shortenInitials) $given = drupal_substr($given, 0, $shortenInitials);
        if (isset($initialize_with)) {
          $name['firstname'] = $name['initials'];

          // if ($shortenInitials) $name['firstname'] = drupal_substr($name['firstname'], 0, $shortenInitials);
        }
        elseif (!empty($name['firstname'])) {
          $name['firstname'] = $name['firstname'] . ' ' . $name['initials'];
        }
        elseif (empty($name['firstname'])) {
          $name['firstname'] = $name['initials'];
        }
      }
      $given = $this
        ->format($name['firstname'], 'given');
      if (isset($name['lastname'])) {
        if (!empty($name['prefix'])) {
          $name['lastname'] = $name['prefix'] . ' ' . $name['lastname'];
        }
        if (!empty($name['suffix'])) {
          $name['lastname'] = $name['lastname'] . ', ' . $name['suffix'];
        }
        $name['lastname'] = $this
          ->format($name['lastname'], 'family');
        if ($this->form == 'short') {
          $text = $name['lastname'];
        }
        else {
          switch ($this->{'name-as-sort-order'}) {
            case 'first':
            case 'all':
              $text = $name['lastname'] . $this->sort_separator . $given;
              break;
            default:
              $text = $given . ' ' . $name['lastname'];
          }
        }
        $name['name'] = $text;
      }
    }
    if (variable_get('biblio_author_links', 1)) {
      $text = theme('biblio_author_link', array(
        'author' => $name,
      ));
    }
    $authors[] = $this
      ->format($text);
    if (isset($this->{'et-al-min'}) && $count >= $this->{'et-al-min'}) {
      break;
    }
  }
  if (isset($this->{'et-al-min'}) && $count >= $this->{'et-al-min'} && isset($this->{'et-al-use-first'}) && $count >= $this->{'et-al-use-first'} && count($names) > $this->{'et-al-use-first'}) {
    if ($this->{'et-al-use-first'} < $this->{'et-al-min'}) {
      for ($i = $this->{'et-al-use-first'}; $i < $count; $i++) {
        unset($authors[$i]);
      }
    }
    if ($this->etal) {
      $authors[] = $this->etal
        ->render();
    }
    else {
      $authors[] = $this->citeproc
        ->get_locale('term', 'et-al');
    }
    $et_al_triggered = TRUE;
  }
  if (!empty($authors) && !$et_al_triggered) {
    $auth_count = count($authors);
    if (isset($this->and) && $auth_count > 1) {
      $authors[$auth_count - 1] = $this->and . ' ' . $authors[$auth_count - 1];

      //stick an "and" in front of the last author if "and" is defined
    }
  }
  $text = implode($this->delimiter, $authors);
  if ($this->form == 'count') {
    if (!$et_al_triggered) {
      return (int) count($authors);
    }
    else {
      return (int) (count($authors) - 1);
    }
  }

  // strip out the last delimiter if not required
  if (isset($this->and) && $auth_count > 1) {
    $last_delim = strrpos($text, $this->delimiter . $this->and);
    switch ($this->dpl) {

      //dpl == delimiter proceeds last
      case 'always':
        return $text;
        break;
      case 'never':
        return substr_replace($text, ' ', $last_delim, strlen($this->delimiter));
        break;
      case 'contextual':
      default:
        if ($auth_count < 3) {
          return substr_replace($text, ' ', $last_delim, strlen($this->delimiter));
        }
    }
  }
  return $text;
}