Subversion Repositories PHPX

Rev

Rev 79 | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
<?php

/**
 * A footnote list contains {@link Footnote Footnotes}
 *
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
 */

class FootnoteList
{
  /**
   * The footnotes of this list
   *
   * @var Array
   */

  protected $_footnotes;

  /**
   * Last used number sign for a footnote
   *
   * @var int
   */

  protected $_lastNumberSign;

  protected $_defaultPrefix;
  protected $_defaultSuffix;

  protected $_makeTooltip;

  /* (non-PHPdoc) @see Footnote::_html4Compat */
  protected $_html4Compat;

  public function __construct($defaultPrefix = '', $defaultSuffix = '', $makeTooltip = false, $html4Compatible = false)
  {
    $this->clear();
    $this->_defaultPrefix = $defaultPrefix;
    $this->_defaultSuffix = $defaultSuffix;
    $this->_makeTooltip = $makeTooltip;
    $this->_html4Compat = $html4Compatible;
  }

  public function __get($name)
  {
    if (property_exists($this, "_$name"))
    {
      return $this->{"_$name"};
    }

    throw new InvalidArgumentException(
      'No such property ' . get_class($this) . "::\$$name");
  }

  /**
   * Clears the footnote list
   */

  public function clear()
  {
    $this->_footnotes = array();
    $this->_lastNumberSign = 0;
  }

  /**
   * Adds a footnote to the list (unless already specified)
   *
   * @param string $name
   *   Name of the footnote
   * @param string $sign
   *   Sign of the footnote.  If empty, the next available number is used.
   * @param string $text
   *   Text for the footnote
   * @param string $tooltip
   *   Tooltip for the footnote
   * @return string
   *   The code for printing the footnote reference.
   */

  public function add($name, $sign = '', $text = '', $tooltip = null)
  {
    $footnotes =& $this->_footnotes;

    if (!isset($footnotes[$name]))
    {
      if (!$sign)
      {
        $sign = ++$this->_lastNumberSign;
      }
      else if (is_int($sign))
      {
        $this->_lastNumberSign = $sign;
      }

      $footnotes[$name] = new Footnote($name, $sign, $text,
        $this->_defaultSuffix, $this->_defaultPrefix,
        $this->_makeTooltip ? ($tooltip !== null ? $tooltip : $text) : '',
        $this->_html4Compat);
    }

    return $footnotes[$name]->getRef();
  }

  /**
   * Prints the list of footnotes
   */

  public function printMe()
  {
    $footnotes =& $this->_footnotes;

    uasort($footnotes, function ($a, $b) {
      /* Sort numbered footnotes first */
      if (is_int($a->sign) && !is_int($b->sign))
      {
        return -1;
      }

      if (is_int($b->sign) && !is_int($a->sign))
      {
        return 1;
      }

      /* Sort footnotes either numerically or alphabetically */
      /* TODO: Sort "1b" before "12a" */
      if ($a->sign < $b->sign)
      {
        return -1;
      }

      if ($a->sign > $b->sign)
      {
        return 1;
      }

      return 0;
    });

    ?><table class="footnotes">
    <?php

    foreach ($footnotes as $name => &$footnote)
    {
      $footnote->printMe();
    }

    ?></table><?php
  }

  /**
   * Prints the list of footnotes and clears the list in memory
   */

  public function flush()
  {
    $this->printMe();
    $this->clear();
  }
}

/**
 * A footnote to be used in a {@link FootnoteList "footnote list"}
 *
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
 */

class Footnote
{
  /**
   * The name of this footnote
   *
   * @var string
   */

  protected $_name = '';

  /**
   * The sign used for referring to this footnote
   *
   * @var string
   */

  protected $_sign = '';

  /**
   * The text for this footnote
   *
   * @var string
   */

  protected $_text = '';

  protected $_prefix = '';
  protected $_suffix = '';
  protected $_tooltip = '';

  /**
   * HTML 4 compatibility: If <code>true</code>, generates
   * <code>name</code> attributes.  Default: <code>false</code>.
   *
   * @var boolean
   */

  protected $_html4Compat = false;

  /**
   * The number of times this footnote has been referred
   *
   * @var int
   */

  protected $_references = 0;

  /**
   * Creates a footnote
   *
   * @param string $name
   *   The name of this footnote
   * @param string $sign
   *   The sign that should be used for referring to this footnote
   * @param string $text
   *   The text for this footnote
   * @param string $suffix
   *   The suffix for this footnote
   * @param string $prefix
   *   The prefix for this footnote
   */

  public function __construct($name, $sign, $text, $suffix = '', $prefix = '',
                                 $tooltip = '', $html4Compatible = false)
  {
    $this->_name = $name;
    $this->_sign = (is_numeric($sign) ? (int) $sign : $sign);
    $this->_text = $text;
    $this->_suffix = $suffix;
    $this->_prefix = $prefix;
    $this->_tooltip = $tooltip;
    $this->_html4Compat = $html4Compatible;
  }

  /**
   * Universal getter
   *
   * @param string $name
   *   Name of the property to be read-accessed.  Currently only 'sign'
   *   is supported.
   * @throws InvalidArgumentException if a non-existing property is accessed
   * @return mixed
   *   Property value
   */

  public function __get($name)
  {
    if (property_exists($this, "_$name"))
    {
      return $this->{"_$name"};
    }

    throw new InvalidArgumentException(
      'No such property ' . get_class($this) . "::\$$name");
  }

  /**
   * Returns the reference for this footnote
   *
   * @return string
   */

  public function getRef()
  {
    $s = $this->_name;

    $ret = "<sup><a href='#footnote-{$s}'"
      . ($this->_references === 0
        ? (($this->_html4Compat ? " name='fn-{$s}-ref'" : "") . " id='fn-{$s}-ref'")
        : '')
      . ' class="footnote"'
      . ($this->_tooltip
        ? ' title="'
          . preg_replace('/"/', '&quot;',
              trim(reduceWhitespace(strip_tags($this->_tooltip))))
          . '"'
        : '')
      . ">{$this->_prefix}{$this->_sign}{$this->_suffix}"
      . '</a></sup>';

    ++$this->_references;

    return $ret;
  }

  /**
   * Prints this footnote in a footnote list
   */

  public function printMe()
  {
    $s = $this->_name;

    echo "  <tr>
        <th><sup><a"
. ($this->_html4Compat ? " name='footnote-{$s}'" : "")
      . " id='footnote-{$s}' class='footnote'
          >{$this->_sign}</a></sup><a
          href='#fn-{$s}-ref' class='backref'>&#8593;</a></th>
        <td>{$this->_text}</td>
      </tr>
    "
;
  }
}

?>