Subversion Repositories PHPX

Rev

Rev 15 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 PointedEar 1
<?php
2
 
3
/**
4
 * A footnote list contains {@link #Footnote Footnotes}
5
 *
6
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
7
 */
8
class FootnoteList
9
{
10
  /**
11
   * The footnotes of this list
12
   *
13
   * @var Array
14
   */
15
  protected $footnotes;
16
 
17
  /**
18
   * Last used number sign for a footnote
19
   *
20
   * @var int
21
   */
22
  protected $lastNumberSign;
23
 
13 PointedEar 24
  protected $defaultPrefix;
25
  protected $defaultSuffix;
26
 
27
  protected $makeTooltip;
28
 
29
  public function __construct($defaultPrefix = '', $defaultSuffix = '', $makeTooltip = false)
7 PointedEar 30
  {
31
    $this->clear();
13 PointedEar 32
    $this->defaultPrefix = $defaultPrefix;
33
    $this->defaultSuffix = $defaultSuffix;
34
    $this->makeTooltip = $makeTooltip;
7 PointedEar 35
  }
36
 
37
  /**
38
   * Clears the footnote list
39
   */
40
  public function clear()
41
  {
42
    $this->footnotes = array();
43
    $this->lastNumberSign = 0;
44
  }
45
 
46
  /**
47
   * Adds a footnote to the list (unless already specified)
48
   *
49
   * @param string $name
50
   *   Name of the footnote
51
   * @param string $sign
52
   *   Sign of the footnote.  If empty, the next available number is used.
53
   * @param string $text
54
   *   Text for the footnote
55
   * @return string
56
   *   The code for printing the footnote reference.
57
   */
13 PointedEar 58
  public function add($name, $sign = '', $text = '', $tooltip = null)
7 PointedEar 59
  {
60
    $footnotes =& $this->footnotes;
61
 
62
    if (!isset($footnotes[$name]))
63
    {
64
      if (!$sign)
65
      {
66
        $sign = ++$this->lastNumberSign;
67
      }
11 PointedEar 68
      else if (is_int($sign))
69
      {
12 PointedEar 70
        $this->lastNumberSign = $sign;
11 PointedEar 71
      }
8 PointedEar 72
 
13 PointedEar 73
      $footnotes[$name] = new Footnote($name, $sign, $text,
74
        $this->defaultSuffix, $this->defaultPrefix,
75
        $this->makeTooltip ? ($tooltip !== null ? $tooltip : $text) : '');
7 PointedEar 76
    }
77
 
8 PointedEar 78
    return $footnotes[$name]->getRef();
7 PointedEar 79
  }
80
 
81
  /**
82
   * Prints the list of footnotes
83
   */
84
  public function printMe()
85
  {
86
    $footnotes =& $this->footnotes;
87
 
8 PointedEar 88
    function cmp($a, $b)
89
    {
90
      if ($a->sign < $b->sign)
91
      {
92
        return -1;
93
      }
94
      else if ($a->sign > $b->sign)
95
      {
96
        return 1;
97
      }
98
      else
99
      {
100
        return 0;
101
      }
102
    }
103
 
104
    uasort($footnotes, 'cmp');
105
 
106
    ?><table class="footnotes">
107
    <?php
108
 
7 PointedEar 109
    foreach ($footnotes as $name => &$footnote)
110
    {
111
      $footnote->printMe();
112
    }
8 PointedEar 113
 
114
    ?></table><?php
7 PointedEar 115
  }
116
 
117
  /**
118
   * Prints the list of footnotes and clears the list in memory
119
   */
120
  public function flush()
121
  {
122
    $this->printMe();
123
    $this->clear();
124
  }
125
}
126
 
127
/**
128
 * A footnote to be used in a {@link #FootnoteList "footnote list"}
129
 *
130
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
131
 */
132
class Footnote
133
{
134
  /**
8 PointedEar 135
   * The name of this footnote
136
   *
137
   * @var string
138
   */
139
  protected $name = '';
140
 
141
  /**
7 PointedEar 142
   * The sign used for referring to this footnote
143
   *
144
   * @var string
145
   */
146
  protected $sign = '';
147
 
148
  /**
149
   * The text for this footnote
150
   *
151
   * @var string
152
   */
153
  protected $text = '';
154
 
13 PointedEar 155
  protected $prefix = '';
156
  protected $suffix = '';
157
  protected $tooltip = '';
158
 
7 PointedEar 159
  /**
8 PointedEar 160
   * The number of times this footnote has been referred
161
   *
162
   * @var int
163
   */
164
  protected $references = 0;
165
 
166
  /**
7 PointedEar 167
   * Creates a footnote
168
   *
8 PointedEar 169
   * @param string $name
170
   *   The name of this footnote
7 PointedEar 171
   * @param string $sign
172
   *   The sign that should be used for referring to this footnote
173
   * @param string $text
174
   *   The text for this footnote
13 PointedEar 175
   * @param string $suffix
176
   *   The suffix for this footnote
177
   * @param string $prefix
178
   *   The prefix for this footnote
7 PointedEar 179
   */
13 PointedEar 180
  public function __construct($name, $sign, $text, $suffix = '', $prefix = '',
181
                                 $tooltip = '')
7 PointedEar 182
  {
8 PointedEar 183
    $this->name = $name;
7 PointedEar 184
    $this->sign = $sign;
185
    $this->text = $text;
13 PointedEar 186
    $this->suffix = $suffix;
187
    $this->prefix = $prefix;
188
    $this->tooltip = $tooltip;
7 PointedEar 189
  }
8 PointedEar 190
 
191
  /**
192
   * Universal getter
193
   *
194
   * @param string $name
195
   *   Name of the property to be read-accessed.  Currently only 'sign'
196
   *   is supported.
10 PointedEar 197
   * @throws InvalidArgumentException if a non-existing property is accessed
8 PointedEar 198
   * @return mixed
199
   *   Property value
200
   */
201
  public function __get($name)
202
  {
203
    if ($name === 'sign')
204
    {
205
      return $this->sign;
206
    }
207
    else
208
    {
10 PointedEar 209
      throw new InvalidArgumentException(
210
        'No such property ' . get_class($this) . "::\$$name");
8 PointedEar 211
    }
212
  }
7 PointedEar 213
 
214
  /**
215
   * Returns the reference for this footnote
216
   *
217
   * @return string
218
   */
8 PointedEar 219
  public function getRef()
7 PointedEar 220
  {
8 PointedEar 221
    $s = $this->name;
222
 
223
    $ret = "<sup><a href='#footnote-{$s}'"
224
      . ($this->references === 0
225
        ? " name='fn-{$s}-ref' id='fn-{$s}-ref'"
226
        : '')
15 PointedEar 227
      . ' class="footnote"'
13 PointedEar 228
      . ($this->tooltip
229
        ? ' title="'
230
          . preg_replace('/"/', '&quot;',
231
              trim(reduceWhitespace(strip_tags($this->tooltip))))
232
          . '"'
233
        : '')
15 PointedEar 234
      . ">{$this->prefix}{$this->sign}{$this->suffix}"
13 PointedEar 235
      . '</a></sup>';
8 PointedEar 236
 
237
    ++$this->references;
238
 
239
    return $ret;
7 PointedEar 240
  }
241
 
242
  /**
243
   * Prints this footnote in a footnote list
244
   */
245
  public function printMe()
246
  {
8 PointedEar 247
    $s = $this->name;
248
 
249
    echo "  <tr>
250
        <th><sup><a name='footnote-{$s}' id='footnote-{$s}' class='footnote'
13 PointedEar 251
          >{$this->sign}</a></sup><a
16 PointedEar 252
          href='#fn-{$s}-ref' class='backref'>&#8593;</a></th>
8 PointedEar 253
        <td>{$this->text}</td>
254
      </tr>
255
    ";
7 PointedEar 256
  }
257
}
258
 
259
?>