You are here

class csl_names in Bibliography Module 7

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

Hierarchy

Expanded class hierarchy of csl_names

File

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

View source
class csl_names extends csl_format {
  private $substitutes;

  /**
   *
   */
  public function init_formatting() {
    $this->span_class = 'biblio-authors';
    parent::init_formatting();
  }

  /**
   *
   */
  public function init($dom_node, $citeproc) {
    $etal = '';
    $tag = $dom_node
      ->getElementsByTagName('substitute')
      ->item(0);
    if ($tag) {
      $this->substitutes = csl_factory::create($tag, $citeproc);
      $dom_node
        ->removeChild($tag);
    }
    $tag = $dom_node
      ->getElementsByTagName('et-al')
      ->item(0);
    if ($tag) {
      $etal = csl_factory::create($tag, $citeproc);
      $dom_node
        ->removeChild($tag);
    }
    $var = $dom_node
      ->getAttribute('variable');
    foreach ($dom_node->childNodes as $node) {
      if ($node->nodeType == 1) {
        $element = csl_factory::create($node, $citeproc);
        if ($element instanceof csl_label) {
          $element->variable = $var;
        }
        if ($element instanceof csl_name && $etal) {
          $element->etal = $etal;
        }
        $this
          ->add_element($element);
      }
    }
  }

  /**
   *
   */
  public function render($data, $mode = NULL) {
    $matches = array();
    $variable_parts = array();
    if (!isset($this->delimiter)) {
      $style_delimiter = $this->citeproc->style->{'names-delimiter'};
      $mode_delimiter = $this->citeproc->{$mode}->{'names-delimiter'};
      $this->delimiter = isset($mode_delimiter) ? $mode_delimiter : (isset($style_delimiter) ? $style_delimiter : '');
    }
    $variables = explode(' ', $this->variable);
    foreach ($variables as $var) {
      if (!$var || in_array($var, $this->citeproc->quash)) {
        continue;
      }
      list($contributor, $category) = explode(':', $var);
      if (isset($data->{$contributor}) && !empty($data->{$contributor}) && $this
        ->_get_category($data->{$contributor}, $category)) {
        $matches[] = $var;
      }
    }

    // We don't have any primary suspects, so lets check the substitutes...
    if (empty($matches)) {
      if (isset($this->substitutes)) {
        foreach ($this->substitutes->elements as $element) {

          // Test to see if any of the other names variables has content.
          if ($element instanceof csl_names) {
            $sub_variables = explode(' ', $element->variable);
            foreach ($sub_variables as $var) {
              list($contributor, $category) = explode(':', $var);
              if (isset($data->{$contributor}) && !empty($data->{$contributor}) && $this
                ->_get_category($data->{$contributor}, $category)) {
                $matches[] = $var;
                $this->citeproc->quash[] = $var;
              }
            }
          }
          else {
            $text = $element
              ->render($data, $mode);
            $this->citeproc->quash[] = isset($element->variable) ? $element->variable : $element->var;
            if (!empty($text)) {
              $variable_parts[] = $text;
            }
          }
          if (!empty($matches)) {
            break;
          }
        }
      }
    }
    foreach ($matches as $var) {
      if (in_array($var, $this->citeproc->quash) && in_array($var, $variables)) {
        continue;
      }
      $text = '';
      list($contributor, $category) = explode(':', $var);
      if (!empty($contributor) && ($authors = $this
        ->_get_category($data->{$contributor}, $category))) {
        foreach ($this->elements as $element) {
          if (is_a($element, 'csl_label')) {
            $element->in_names = TRUE;
            $element->plural = count($authors) > 1 ? 'multiple' : 'single';
            $element->variable = $this
              ->_get_csl_name_variable($category);
            $text .= $element
              ->render($authors, $mode);
          }
          elseif (is_a($element, 'csl_name')) {
            $text .= $element
              ->render($authors, $mode);
          }
        }
      }
      if (!empty($text)) {
        $variable_parts[] = $text;
      }
    }
    if (!empty($variable_parts)) {
      $text = implode($this->delimiter, $variable_parts);
      return $this
        ->format($text);
    }
    return;
  }

  /**
   *
   */
  private function _get_category($contributors, $category) {
    $authors = array();
    foreach ($contributors as $author) {
      if ($author['auth_category'] == $category) {
        $authors[] = $author;
      }
    }
    return count($authors) ? $authors : FALSE;
  }

  /**
   *
   */
  private function _get_csl_name_variable($category) {
    switch ($category) {
      case 1:
        return 'author';
        break;
      case 2:
        return 'editor';
        break;
      case 3:
        return 'translator';
        break;
      default:
    }
  }

}

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::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_format::format public function Overrides csl_collection::format 1
csl_format::__construct public function Overrides csl_element::__construct 2
csl_names::$substitutes private property
csl_names::init public function Overrides csl_element::init
csl_names::init_formatting public function Overrides csl_format::init_formatting
csl_names::render public function Overrides csl_rendering_element::render
csl_names::_get_category private function
csl_names::_get_csl_name_variable private function