You are here

class csl_name in Bibliography Module 7

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

Hierarchy

Expanded class hierarchy of csl_name

1 string reference to 'csl_name'
csl_names::render in modules/CiteProc/CSL.inc

File

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

View source
class csl_name extends csl_format {
  private $name_parts = array();
  private $attr_init = FALSE;

  /**
   *
   */
  public function __construct($dom_node, $citeproc = NULL) {
    $tags = $dom_node
      ->getElementsByTagName('name-part');
    if ($tags) {
      foreach ($tags as $tag) {
        $name_part = $tag
          ->getAttribute('name');
        $tag
          ->removeAttribute('name');
        for ($i = 0; $i < $tag->attributes->length; $i++) {
          $value = $tag->attributes
            ->item($i)->value;
          $name = str_replace(' ', '_', $tag->attributes
            ->item($i)->name);
          $this->name_parts[$name_part][$name] = $value;
        }
      }
    }
    parent::__construct($dom_node, $citeproc);
  }

  /**
   *
   */
  public function init_formatting() {
    $this->no_op = array();
    $this->format = array();
    $this->base = $this
      ->get_attributes();
    $this->format['base'] = '';
    $this->format['family'] = '';
    $this->format['given'] = '';
    $this->no_op['base'] = TRUE;
    $this->no_op['family'] = TRUE;
    $this->no_op['given'] = TRUE;
    if (isset($this->prefix)) {
      $this->no_op['base'] = FALSE;
    }
    if (isset($this->suffix)) {
      $this->no_op['base'] = FALSE;
    }
    $this
      ->init_format($this->base);
    if (!empty($this->name_parts)) {
      foreach ($this->name_parts as $name => $formatting) {
        $this
          ->init_format($formatting, $name);
      }
    }
  }

  /**
   *
   */
  public function init_attrs($mode) {

    // $and = $this->get_attributes('and');.
    if (isset($this->citeproc)) {
      $style_attrs = $this->citeproc->style
        ->get_hier_attributes();
      $mode_attrs = $this->citeproc->{$mode}
        ->get_hier_attributes();
      $this->attributes = array_merge($style_attrs, $mode_attrs, $this->attributes);
    }
    if (isset($this->and)) {
      if ($this->and == 'text') {
        $this->and = $this->citeproc
          ->get_locale('term', 'and');
      }
      elseif ($this->and == 'symbol') {
        $this->and = '&';
      }
    }
    if (!isset($this->delimiter)) {
      $this->delimiter = $this->{'name-delimiter'};
    }
    if (!isset($this->alnum)) {
      list($this->alnum, $this->alpha, $this->cntrl, $this->dash, $this->digit, $this->graph, $this->lower, $this->print, $this->punct, $this->space, $this->upper, $this->word, $this->patternModifiers) = $this
        ->get_regex_patterns();
    }
    $this->dpl = $this->{'delimiter-precedes-last'};
    $this->sort_separator = isset($this->{'sort-separator'}) ? $this->{'sort-separator'} : ', ';
    $this->delimiter = isset($this->{'name-delimiter'}) ? $this->{'name-delimiter'} : (isset($this->delimiter) ? $this->delimiter : ', ');
    $this->form = isset($this->{'name-form'}) ? $this->{'name-form'} : (isset($this->form) ? $this->form : 'long');
    $this->attr_init = $mode;
  }

  /**
   *
   */
  public function init_format($attribs, $part = 'base') {
    if (!isset($this->{$part})) {
      $this->{$part} = array();
    }
    if (isset($attribs['quotes']) && strtolower($attribs['quotes']) == 'true') {
      $this->{$part}['open-quote'] = $this->citeproc
        ->get_locale('term', 'open-quote');
      $this->{$part}['close-quote'] = $this->citeproc
        ->get_locale('term', 'close-quote');
      $this->{$part}['open-inner-quote'] = $this->citeproc
        ->get_locale('term', 'open-inner-quote');
      $this->{$part}['close-inner-quote'] = $this->citeproc
        ->get_locale('term', 'close-inner-quote');
      $this->no_op[$part] = FALSE;
    }
    if (isset($attribs['prefix'])) {
      $this->{$part}['prefix'] = $attribs['prefix'];
    }
    if (isset($attribs['suffix'])) {
      $this->{$part}['suffix'] = $attribs['suffix'];
    }
    $this->format[$part] .= isset($attribs['font-style']) ? 'font-style: ' . $attribs['font-style'] . ';' : '';
    $this->format[$part] .= isset($attribs['font-family']) ? 'font-family: ' . $attribs['font-family'] . ';' : '';
    $this->format[$part] .= isset($attribs['font-weight']) ? 'font-weight: ' . $attribs['font-weight'] . ';' : '';
    $this->format[$part] .= isset($attribs['font-variant']) ? 'font-variant: ' . $attribs['font-variant'] . ';' : '';
    $this->format[$part] .= isset($attribs['text-decoration']) ? 'text-decoration: ' . $attribs['text-decoration'] . ';' : '';
    $this->format[$part] .= isset($attribs['vertical-align']) ? 'vertical-align: ' . $attribs['vertical-align'] . ';' : '';
    if (isset($attribs['text-case'])) {
      $this->no_op[$part] = FALSE;
      $this->{$part}['text-case'] = $attribs['text-case'];
    }
    if (!empty($this->format[$part])) {
      $this->no_op[$part] = FALSE;
    }
  }

  /**
   *
   */
  public function format($text, $part = 'base') {
    if (empty($text) || $this->no_op[$part]) {
      return $text;
    }
    if (isset($this->{$part}['text-case'])) {
      switch ($this->{$part}['text-case']) {
        case 'uppercase':
          $text = drupal_strtoupper($text);
          break;
        case 'lowercase':
          $text = drupal_strtolower($text);
          break;
        case 'capitalize-all':
          $text = mb_convert_case($text, MB_CASE_TITLE);
          break;
        case 'capitalize-first':
          $text = drupal_ucfirst($text);
          break;
      }
    }
    $open_quote = isset($this->{$part}['open-quote']) ? $this->{$part}['open-quote'] : '';
    $close_quote = isset($this->{$part}['close-quote']) ? $this->{$part}['close-quote'] : '';
    $prefix = isset($this->{$part}['prefix']) ? $this->{$part}['prefix'] : '';
    $suffix = isset($this->{$part}['suffix']) ? $this->{$part}['suffix'] : '';
    if ($text[strlen($text) - 1] == $suffix) {
      unset($suffix);
    }
    if (!empty($this->format[$part])) {
      $text = '<span style="' . $this->format[$part] . '">' . $text . '</span>';
    }
    return $prefix . $open_quote . $text . $close_quote . $suffix;
  }

  /**
   *
   */
  public function author_link($author) {
    $base = variable_get('biblio_base', 'biblio');
    $options = array();
    if (isset($_GET['sort'])) {
      $options['query']['sort'] = $_GET['sort'];
    }
    if (isset($_GET['order'])) {
      $options['query']['order'] = $_GET['order'];
    }
    $html = l(trim($author['name']), "{$base}/author/" . $author['cid'], $options);
    return $html;
  }

  /**
   *
   */
  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;
  }

  /**
   *
   */
  public function get_regex_patterns() {

    // Checks if PCRE is compiled with UTF-8 and Unicode support.
    if (!@preg_match('/\\pL/u', 'a')) {

      // Probably a broken PCRE library.
      return $this
        ->get_latin1_regex();
    }
    else {

      // Unicode safe filter for the value.
      return $this
        ->get_utf8_regex();
    }
  }

  /**
   *
   */
  public function get_latin1_regex() {
    $alnum = "[:alnum:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";

    // Matches ISO-8859-1 letters:
    $alpha = "[:alpha:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";

    // Matches ISO-8859-1 control characters:
    $cntrl = "[:cntrl:]";

    // Matches ISO-8859-1 dashes & hyphens:
    $dash = "-–";

    // Matches ISO-8859-1 digits:
    $digit = "[\\d]";

    // Matches ISO-8859-1 printing characters (excluding space):
    $graph = "[:graph:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";

    // Matches ISO-8859-1 lower case letters:
    $lower = "[:lower:]äåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";

    // Matches ISO-8859-1 printing characters (including space):
    $print = "[:print:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";

    // Matches ISO-8859-1 punctuation:
    $punct = "[:punct:]";

    // Matches ISO-8859-1 whitespace (separating characters with no visual representation):
    $space = "[\\s]";

    // Matches ISO-8859-1 upper case letters:
    $upper = "[:upper:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆ";

    // Matches ISO-8859-1 "word" characters:
    $word = "_[:alnum:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";

    // Defines the PCRE pattern modifier(s) to be used in conjunction with the above variables:
    // More info: <http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php>
    $patternModifiers = "";
    return array(
      $alnum,
      $alpha,
      $cntrl,
      $dash,
      $digit,
      $graph,
      $lower,
      $print,
      $punct,
      $space,
      $upper,
      $word,
      $patternModifiers,
    );
  }

  /**
   *
   */
  public function get_utf8_regex() {

    // Matches Unicode letters & digits:
    // Unicode-aware equivalent of "[:alnum:]".
    $alnum = "\\p{Ll}\\p{Lu}\\p{Lt}\\p{Lo}\\p{Nd}";

    // Matches Unicode letters:
    // Unicode-aware equivalent of "[:alpha:]".
    $alpha = "\\p{Ll}\\p{Lu}\\p{Lt}\\p{Lo}";

    // Matches Unicode control codes & characters not in other categories:
    // Unicode-aware equivalent of "[:cntrl:]".
    $cntrl = "\\p{C}";

    // Matches Unicode dashes & hyphens:
    $dash = "\\p{Pd}";

    // Matches Unicode digits:
    // Unicode-aware equivalent of "[:digit:]".
    $digit = "\\p{Nd}";

    // Matches Unicode printing characters (excluding space):
    // Unicode-aware equivalent of "[:graph:]".
    $graph = "^\\p{C}\t\n\f\r\\p{Z}";

    // Matches Unicode lower case letters:
    // Unicode-aware equivalent of "[:lower:]".
    $lower = "\\p{Ll}\\p{M}";

    // Matches Unicode printing characters (including space):
    // same as "^\p{C}", Unicode-aware equivalent of "[:print:]".
    $print = "\\P{C}";

    // Matches Unicode punctuation (printing characters excluding letters & digits):
    // Unicode-aware equivalent of "[:punct:]".
    $punct = "\\p{P}";

    // Matches Unicode whitespace (separating characters with no visual representation):
    // Unicode-aware equivalent of "[:space:]".
    $space = "\t\n\f\r\\p{Z}";

    // Matches Unicode upper case letters:
    // Unicode-aware equivalent of "[:upper:]".
    $upper = "\\p{Lu}\\p{Lt}";

    // Matches Unicode "word" characters:
    // Unicode-aware equivalent of "[:word:]" (or "[:alnum:]" plus "_")
    $word = "_\\p{Ll}\\p{Lu}\\p{Lt}\\p{Lo}\\p{Nd}";

    // Defines the PCRE pattern modifier(s) to be used in conjunction with the above variables:
    // More info: <http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php>
    // the "u" (PCRE_UTF8) pattern modifier causes PHP/PCRE to treat pattern strings as UTF-8.
    $patternModifiers = "u";
    return array(
      $alnum,
      $alpha,
      $cntrl,
      $dash,
      $digit,
      $graph,
      $lower,
      $print,
      $punct,
      $space,
      $upper,
      $word,
      $patternModifiers,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
csl_collection::$elements protected property
csl_collection::add_element public function
csl_element::$attributes protected property
csl_element::$citeproc protected property
csl_element::get_attributes public function
csl_element::get_hier_attributes public function
csl_element::init public function 5
csl_element::name public function
csl_element::set_attributes public function
csl_element::__get public function
csl_element::__isset public function
csl_element::__set public function
csl_element::__unset public function
csl_format::$format protected property 1
csl_format::$no_op protected property
csl_name::$attr_init private property
csl_name::$name_parts private property
csl_name::author_link public function
csl_name::format public function Overrides csl_format::format
csl_name::get_latin1_regex public function
csl_name::get_regex_patterns public function
csl_name::get_utf8_regex public function
csl_name::init_attrs public function
csl_name::init_format public function
csl_name::init_formatting public function Overrides csl_format::init_formatting
csl_name::render public function Overrides csl_rendering_element::render
csl_name::__construct public function Overrides csl_format::__construct