Subversion Repositories PHPX

Rev

Rev 15 | Rev 25 | Go to most recent revision | 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;
 
  public function __construct($defaultPrefix = '', $defaultSuffix = '', $makeTooltip = false)
  {
    $this->clear();
    $this->defaultPrefix = $defaultPrefix;
    $this->defaultSuffix = $defaultSuffix;
    $this->makeTooltip = $makeTooltip;
  }
 
  /**
   * 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
   * @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) : '');
    }
   
    return $footnotes[$name]->getRef();
  }
 
  /**
   * Prints the list of footnotes
   */

  public function printMe()
  {
    $footnotes =& $this->footnotes;
   
    function cmp($a, $b)
    {
      if ($a->sign < $b->sign)
      {
        return -1;
      }
      else if ($a->sign > $b->sign)
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
   
    uasort($footnotes, 'cmp');
   
    ?><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 = '';
 
  /**
   * 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 = '')
  {
    $this->name = $name;
    $this->sign = $sign;
    $this->text = $text;
    $this->suffix = $suffix;
    $this->prefix = $prefix;
    $this->tooltip = $tooltip;
  }

  /**
   * 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 ($name === 'sign')
    {
      return $this->sign;
    }
    else
    {
      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
        ? " 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 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>
    "
;
  }
}

?>