You are here

public function csl_name::render in Bibliography Module 7

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

Overrides csl_rendering_element::render

File

modules/CiteProc/CSL.inc, line 721
CiteProc-PHP.

Class

csl_name

Code

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

        // This is needed for form preview to fill in all fields.
        $name = biblio_parse_author($name);
      }
      $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 ...
        // ... the delimiter that separates initials ends with a space.
        if (preg_match("/ \$/", $initialize_with)) {
          $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' && $rank == 0:
            case 'all':
              $text = $name['lastname'] . $this->sort_separator . $given;
              break;
            default:
              $text = $given . ' ' . $name['lastname'];
          }
        }
        $text = $this
          ->format($text);
        $name['name'] = $text;
        if (strstr($text, 'div') || strstr($text, 'span')) {
          $options = array(
            'html' => TRUE,
          );
        }
        else {
          $options = array(
            'html' => FALSE,
          );
        }
      }
    }
    if (variable_get('biblio_author_links', 1)) {
      $text = theme('biblio_author_link', array(
        'author' => $name,
        'options' => $options,
      ));
    }
    $authors[] = $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) {
      $etal = $this->etal
        ->render();
    }
    else {
      $etal = $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) {

      // Stick an "and" in front of the last author if "and" is defined.
      $authors[$auth_count - 1] = $this->and . ' ' . $authors[$auth_count - 1];
    }
  }
  $text = implode($this->delimiter, $authors);
  if (!empty($authors) && $et_al_triggered) {
    switch ($this->{'delimiter-precedes-et-al'}) {
      case 'never':
        $text = $text . " {$etal}";
        break;
      case 'always':
        $text = $text . "{$this->delimiter}{$etal}";
        break;
      default:
        $text = count($authors) == 1 ? $text . " {$etal}" : $text . "{$this->delimiter}{$etal}";
    }
  }
  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);

    // Dpl == delimiter proceeds last.
    switch ($this->dpl) {
      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;
}