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