Subversion Repositories PHPX

Rev

Rev 11 | Rev 13 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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