Subversion Repositories PHPX

Rev

Rev 79 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 79 Rev 80
Line 1... Line 1...
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
{
Line 11... Line 11...
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
  /* (non-PHPdoc) @see Footnote::_html4Compat */
29
  /* (non-PHPdoc) @see Footnote::_html4Compat */
30
  protected $_html4Compat;
30
  protected $_html4Compat;
31
 
31
32
  public function __construct($defaultPrefix = '', $defaultSuffix = '', $makeTooltip = false, $html4Compatible = false)
32
  public function __construct($defaultPrefix = '', $defaultSuffix = '', $makeTooltip = false, $html4Compatible = false)
33
  {
33
  {
34
    $this->clear();
34
    $this->clear();
35
    $this->_defaultPrefix = $defaultPrefix;
35
    $this->_defaultPrefix = $defaultPrefix;
36
    $this->_defaultSuffix = $defaultSuffix;
36
    $this->_defaultSuffix = $defaultSuffix;
37
    $this->_makeTooltip = $makeTooltip;
37
    $this->_makeTooltip = $makeTooltip;
38
    $this->_html4Compat = $html4Compatible;
38
    $this->_html4Compat = $html4Compatible;
39
  }
39
  }
40
 
40
41
  public function __get($name)
41
  public function __get($name)
42
  {
42
  {
43
    if (property_exists($this, "_$name"))
43
    if (property_exists($this, "_$name"))
44
    {
44
    {
45
      return $this->{"_$name"};
45
      return $this->{"_$name"};
46
    }
46
    }
47
47
48
    throw new InvalidArgumentException(
48
    throw new InvalidArgumentException(
49
      'No such property ' . get_class($this) . "::\$$name");
49
      'No such property ' . get_class($this) . "::\$$name");
50
  }
50
  }
51
 
51
52
  /**
52
  /**
53
   * Clears the footnote list
53
   * Clears the footnote list
54
   */
54
   */
55
  public function clear()
55
  public function clear()
56
  {
56
  {
57
    $this->_footnotes = array();
57
    $this->_footnotes = array();
58
    $this->_lastNumberSign = 0;
58
    $this->_lastNumberSign = 0;
59
  }
59
  }
60
 
60
61
  /**
61
  /**
62
   * Adds a footnote to the list (unless already specified)
62
   * Adds a footnote to the list (unless already specified)
63
   *
63
   *
64
   * @param string $name
64
   * @param string $name
65
   *   Name of the footnote
65
   *   Name of the footnote
Line 73... Line 73...
73
   *   The code for printing the footnote reference.
73
   *   The code for printing the footnote reference.
74
   */
74
   */
75
  public function add($name, $sign = '', $text = '', $tooltip = null)
75
  public function add($name, $sign = '', $text = '', $tooltip = null)
76
  {
76
  {
77
    $footnotes =& $this->_footnotes;
77
    $footnotes =& $this->_footnotes;
78
   
78
79
    if (!isset($footnotes[$name]))
79
    if (!isset($footnotes[$name]))
80
    {
80
    {
81
      if (!$sign)
81
      if (!$sign)
82
      {
82
      {
83
        $sign = ++$this->_lastNumberSign;
83
        $sign = ++$this->_lastNumberSign;
84
      }
84
      }
85
      else if (is_int($sign))
85
      else if (is_int($sign))
86
      {
86
      {
87
        $this->_lastNumberSign = $sign;
87
        $this->_lastNumberSign = $sign;
88
      }
88
      }
89
     
89
90
      $footnotes[$name] = new Footnote($name, $sign, $text,
90
      $footnotes[$name] = new Footnote($name, $sign, $text,
91
        $this->_defaultSuffix, $this->_defaultPrefix,
91
        $this->_defaultSuffix, $this->_defaultPrefix,
92
        $this->_makeTooltip ? ($tooltip !== null ? $tooltip : $text) : '',
92
        $this->_makeTooltip ? ($tooltip !== null ? $tooltip : $text) : '',
93
        $this->_html4Compat);
93
        $this->_html4Compat);
94
    }
94
    }
95
   
95
96
    return $footnotes[$name]->getRef();
96
    return $footnotes[$name]->getRef();
97
  }
97
  }
98
 
98
99
  /**
99
  /**
100
   * Prints the list of footnotes
100
   * Prints the list of footnotes
101
   */
101
   */
102
  public function printMe()
102
  public function printMe()
103
  {
103
  {
104
    $footnotes =& $this->_footnotes;
104
    $footnotes =& $this->_footnotes;
105
   
105
106
    uasort($footnotes, function ($a, $b) {
106
    uasort($footnotes, function ($a, $b) {
107
      /* Sort numbered footnotes first */
107
      /* Sort numbered footnotes first */
108
      if (is_int($a->sign) && !is_int($b->sign))
108
      if (is_int($a->sign) && !is_int($b->sign))
109
      {
109
      {
110
        return -1;
110
        return -1;
Line 112... Line 112...
112
112
113
      if (is_int($b->sign) && !is_int($a->sign))
113
      if (is_int($b->sign) && !is_int($a->sign))
114
      {
114
      {
115
        return 1;
115
        return 1;
116
      }
116
      }
117
     
117
118
      /* Sort footnotes either numerically or alphabetically */
118
      /* Sort footnotes either numerically or alphabetically */
119
      /* TODO: Sort "1b" before "12a" */
119
      /* TODO: Sort "1b" before "12a" */
120
      if ($a->sign < $b->sign)
120
      if ($a->sign < $b->sign)
121
      {
121
      {
122
        return -1;
122
        return -1;
123
      }
123
      }
124
     
124
125
      if ($a->sign > $b->sign)
125
      if ($a->sign > $b->sign)
126
      {
126
      {
127
        return 1;
127
        return 1;
128
      }
128
      }
129
     
129
130
      return 0;
130
      return 0;
131
    });
131
    });
132
   
132
133
    ?><table class="footnotes">
133
    ?><table class="footnotes">
134
    <?php
134
    <?php
135
   
135
136
    foreach ($footnotes as $name => &$footnote)
136
    foreach ($footnotes as $name => &$footnote)
137
    {
137
    {
138
      $footnote->printMe();
138
      $footnote->printMe();
139
    }
139
    }
140
   
140
141
    ?></table><?php
141
    ?></table><?php
142
  }
142
  }
143
 
143
144
  /**
144
  /**
145
   * Prints the list of footnotes and clears the list in memory
145
   * Prints the list of footnotes and clears the list in memory
146
   */
146
   */
147
  public function flush()
147
  public function flush()
148
  {
148
  {
Line 150... Line 150...
150
    $this->clear();
150
    $this->clear();
151
  }
151
  }
152
}
152
}
153
153
154
/**
154
/**
155
 * A footnote to be used in a {@link #FootnoteList "footnote list"}
155
 * A footnote to be used in a {@link FootnoteList "footnote list"}
156
 *
156
 *
157
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
157
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
158
 */
158
 */
159
class Footnote
159
class Footnote
160
{
160
{
Line 162... Line 162...
162
   * The name of this footnote
162
   * The name of this footnote
163
   *
163
   *
164
   * @var string
164
   * @var string
165
   */
165
   */
166
  protected $_name = '';
166
  protected $_name = '';
167
 
167
168
  /**
168
  /**
169
   * The sign used for referring to this footnote
169
   * The sign used for referring to this footnote
170
   *
170
   *
171
   * @var string
171
   * @var string
172
   */
172
   */
173
  protected $_sign = '';
173
  protected $_sign = '';
174
 
174
175
  /**
175
  /**
176
   * The text for this footnote
176
   * The text for this footnote
177
   *
177
   *
178
   * @var string
178
   * @var string
179
   */
179
   */
180
  protected $_text = '';
180
  protected $_text = '';
181
 
181
182
  protected $_prefix = '';
182
  protected $_prefix = '';
183
  protected $_suffix = '';
183
  protected $_suffix = '';
184
  protected $_tooltip = '';
184
  protected $_tooltip = '';
185
185
186
  /**
186
  /**
Line 188... Line 188...
188
   * <code>name</code> attributes.  Default: <code>false</code>.
188
   * <code>name</code> attributes.  Default: <code>false</code>.
189
   *
189
   *
190
   * @var boolean
190
   * @var boolean
191
   */
191
   */
192
  protected $_html4Compat = false;
192
  protected $_html4Compat = false;
193
 
193
194
  /**
194
  /**
195
   * The number of times this footnote has been referred
195
   * The number of times this footnote has been referred
196
   *
196
   *
197
   * @var int
197
   * @var int
198
   */
198
   */
199
  protected $_references = 0;
199
  protected $_references = 0;
200
 
200
201
  /**
201
  /**
202
   * Creates a footnote
202
   * Creates a footnote
203
   *
203
   *
204
   * @param string $name
204
   * @param string $name
205
   *   The name of this footnote
205
   *   The name of this footnote
Line 242... Line 242...
242
    }
242
    }
243
243
244
    throw new InvalidArgumentException(
244
    throw new InvalidArgumentException(
245
      'No such property ' . get_class($this) . "::\$$name");
245
      'No such property ' . get_class($this) . "::\$$name");
246
  }
246
  }
247
 
247
248
  /**
248
  /**
249
   * Returns the reference for this footnote
249
   * Returns the reference for this footnote
250
   *
250
   *
251
   * @return string
251
   * @return string
252
   */
252
   */
Line 267... Line 267...
267
        : '')
267
        : '')
268
      . ">{$this->_prefix}{$this->_sign}{$this->_suffix}"
268
      . ">{$this->_prefix}{$this->_sign}{$this->_suffix}"
269
      . '</a></sup>';
269
      . '</a></sup>';
270
270
271
    ++$this->_references;
271
    ++$this->_references;
272
                         
272
273
    return $ret;
273
    return $ret;
274
  }
274
  }
275
 
275
276
  /**
276
  /**
277
   * Prints this footnote in a footnote list
277
   * Prints this footnote in a footnote list
278
   */
278
   */
279
  public function printMe()
279
  public function printMe()
280
  {
280
  {
281
    $s = $this->_name;
281
    $s = $this->_name;
282
   
282
283
    echo "  <tr>
283
    echo "  <tr>
284
        <th><sup><a" . ($this->_html4Compat ? " name='footnote-{$s}'" : "")
284
        <th><sup><a" . ($this->_html4Compat ? " name='footnote-{$s}'" : "")
285
      . " id='footnote-{$s}' class='footnote'
285
      . " id='footnote-{$s}' class='footnote'
286
          >{$this->_sign}</a></sup><a
286
          >{$this->_sign}</a></sup><a
287
          href='#fn-{$s}-ref' class='backref'>&#8593;</a></th>
287
          href='#fn-{$s}-ref' class='backref'>&#8593;</a></th>