Subversion Repositories PHPX

Rev

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

Rev 7 Rev 8
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
   
60
     
61
      $footnotes[$name] = new Footnote($sign, $text);
61
      $footnotes[$name] = new Footnote($name, $sign, $text);
62
    }
62
    }
63
   
63
   
64
    return $footnotes[$name]->printRef();
64
    return $footnotes[$name]->getRef();
65
  }
65
  }
66
 
66
 
67
  /**
67
  /**
68
   * Prints the list of footnotes
68
   * Prints the list of footnotes
69
   */
69
   */
70
  public function printMe()
70
  public function printMe()
71
  {
71
  {
72
    $footnotes =& $this->footnotes;
72
    $footnotes =& $this->footnotes;
73
   
73
   
-
 
74
    function cmp($a, $b)
-
 
75
    {
-
 
76
      if ($a->sign < $b->sign)
-
 
77
      {
-
 
78
        return -1;
-
 
79
      }
-
 
80
      else if ($a->sign > $b->sign)
-
 
81
      {
-
 
82
        return 1;
-
 
83
      }
-
 
84
      else
-
 
85
      {
-
 
86
        return 0;
-
 
87
      }
-
 
88
    }
-
 
89
   
-
 
90
    uasort($footnotes, 'cmp');
-
 
91
   
-
 
92
    ?><table class="footnotes">
-
 
93
    <?php
-
 
94
   
74
    foreach ($footnotes as $name => &$footnote)
95
    foreach ($footnotes as $name => &$footnote)
75
    {
96
    {
76
      /* TODO */
-
 
77
      $footnote->printMe();
97
      $footnote->printMe();
78
    }
98
    }
-
 
99
   
-
 
100
    ?></table><?php
79
  }
101
  }
80
 
102
 
81
  /**
103
  /**
82
   * Prints the list of footnotes and clears the list in memory
104
   * Prints the list of footnotes and clears the list in memory
83
   */
105
   */
84
  public function flush()
106
  public function flush()
85
  {
107
  {
86
    $this->printMe();
108
    $this->printMe();
87
    $this->clear();
109
    $this->clear();
88
  }
110
  }
89
}
111
}
90
112
91
/**
113
/**
92
 * A footnote to be used in a {@link #FootnoteList "footnote list"}
114
 * A footnote to be used in a {@link #FootnoteList "footnote list"}
93
 *
115
 *
94
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
116
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
95
 */
117
 */
96
class Footnote
118
class Footnote
97
{
119
{
98
  /**
120
  /**
-
 
121
   * The name of this footnote
-
 
122
   *
-
 
123
   * @var string
-
 
124
   */
-
 
125
  protected $name = '';
-
 
126
 
-
 
127
  /**
99
   * The sign used for referring to this footnote
128
   * The sign used for referring to this footnote
100
   *
129
   *
101
   * @var string
130
   * @var string
102
   */
131
   */
103
  protected $sign = '';
132
  protected $sign = '';
104
 
133
 
105
  /**
134
  /**
106
   * The text for this footnote
135
   * The text for this footnote
107
   *
136
   *
108
   * @var string
137
   * @var string
109
   */
138
   */
110
  protected $text = '';
139
  protected $text = '';
111
 
140
 
112
  /**
141
  /**
-
 
142
   * The number of times this footnote has been referred
-
 
143
   *
-
 
144
   * @var int
-
 
145
   */
-
 
146
  protected $references = 0;
-
 
147
 
-
 
148
  /**
113
   * Creates a footnote
149
   * Creates a footnote
114
   *
150
   *
-
 
151
   * @param string $name
-
 
152
   *   The name of this footnote
115
   * @param string $sign
153
   * @param string $sign
116
   *   The sign that should be used for referring to this footnote
154
   *   The sign that should be used for referring to this footnote
117
   * @param string $text
155
   * @param string $text
118
   *   The text for this footnote
156
   *   The text for this footnote
119
   */
157
   */
120
  public function __construct($sign, $text)
158
  public function __construct($name, $sign, $text)
121
  {
159
  {
-
 
160
    $this->name = $name;
122
    $this->sign = $sign;
161
    $this->sign = $sign;
123
    $this->text = $text;
162
    $this->text = $text;
124
  }
163
  }
-
 
164
-
 
165
  /**
-
 
166
   * Universal getter
-
 
167
   *
-
 
168
   * @param string $name
-
 
169
   *   Name of the property to be read-accessed.  Currently only 'sign'
-
 
170
   *   is supported.
-
 
171
   * @throws Exception if a non-existing property is accessed
-
 
172
   * @return mixed
-
 
173
   *   Property value
-
 
174
   */
-
 
175
  public function __get($name)
-
 
176
  {
-
 
177
    if ($name === 'sign')
-
 
178
    {
-
 
179
      return $this->sign;
-
 
180
    }
-
 
181
    else
-
 
182
    {
-
 
183
      throw new Exception('No such property ' . get_class($this) . "::\$$name");
-
 
184
    }
-
 
185
  }
125
 
186
 
126
  /**
187
  /**
127
   * Returns the reference for this footnote
188
   * Returns the reference for this footnote
128
   *
189
   *
129
   * @return string
190
   * @return string
130
   */
191
   */
131
  public function printRef()
192
  public function getRef()
132
  {
193
  {
-
 
194
    $s = $this->name;
-
 
195
   
-
 
196
    $ret = "<sup><a href='#footnote-{$s}'"
-
 
197
      . ($this->references === 0
-
 
198
        ? " name='fn-{$s}-ref' id='fn-{$s}-ref'"
-
 
199
        : '')
-
 
200
      . " class='footnote'>{$this->sign}</a></sup>";
-
 
201
-
 
202
    ++$this->references;
-
 
203
                         
133
    return "<sup>$sign</sup>";
204
    return $ret;
134
  }
205
  }
135
 
206
 
136
  /**
207
  /**
137
   * Prints this footnote in a footnote list
208
   * Prints this footnote in a footnote list
138
   */
209
   */
139
  public function printMe()
210
  public function printMe()
140
  {
211
  {
-
 
212
    $s = $this->name;
-
 
213
   
-
 
214
    echo "  <tr>
-
 
215
        <th><sup><a name='footnote-{$s}' id='footnote-{$s}' class='footnote'
-
 
216
          >{$this->sign}</a></sup><a href='#fn-{$s}-ref' name='footnote-{$s}'
-
 
217
          id='footnote-{$s}' class='backref'>&#8593;</a></th>
-
 
218
        <td>{$this->text}</td>
141
    /* TODO */
219
      </tr>
-
 
220
    ";
142
  }
221
  }
143
}
222
}
144
223
145
?>
224
?>