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