Subversion Repositories PHPX

Rev

Rev 7 | Rev 10 | 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;
 
  public function __construct()
  {
    $this->clear();
  }
 
  /**
   * 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 = '')
  {
    $footnotes =& $this->footnotes;
   
    if (!isset($footnotes[$name]))
    {
      if (!$sign)
      {
        $sign = ++$this->lastNumberSign;
      }
     
      $footnotes[$name] = new Footnote($name, $sign, $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 = '';
 
  /**
   * 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
   */

  public function __construct($name, $sign, $text)
  {
    $this->name = $name;
    $this->sign = $sign;
    $this->text = $text;
  }

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

  public function __get($name)
  {
    if ($name === 'sign')
    {
      return $this->sign;
    }
    else
    {
      throw new Exception('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->sign}</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' name='footnote-{$s}'
          id='footnote-{$s}' class='backref'>&#8593;</a></th>
        <td>{$this->text}</td>
      </tr>
    "
;
  }
}

?>