Project

General

Profile

Download (3.25 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Class to manage footnotes.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 */
15

    
16
/**
17
 * Manages footnotes.
18
 */
19
class FootnoteManager {
20
  private static $fnstore = array();
21
  private static $nextFootnoteKey = 1;
22

    
23
  /**
24
   * Private constructor.
25
   */
26
  private function __construct() {}
27

    
28
  /**
29
   * Get a list of footnotes.
30
   *
31
   * @param string $footnoteListKey
32
   *   A string as key to the list of footnotes.
33
   *
34
   * @return array
35
   *   An array of footnotes objects.
36
   */
37
  public static function getFootnoteList($footnoteListKey) {
38
    return array_key_exists($footnoteListKey, self::$fnstore) ? self::$fnstore[$footnoteListKey] : NULL;
39
  }
40

    
41
  /**
42
   * Remove a list of footnotes.
43
   *
44
   * @param string $footnoteListKey
45
   *   A string as key to the list of footnotes.
46
   *
47
   * @return void
48
   */
49
  public static function removeFootnoteList($footnoteListKey) {
50
    if (array_key_exists($footnoteListKey, self::$fnstore)) {
51
      unset(self::$fnstore[$footnoteListKey]);
52
    }
53
  }
54

    
55
  /**
56
   * Render a footnote list
57
   *
58
   * @param array $footnoteListKey
59
   * @param string $separator
60
   *
61
   * @return string
62
   *   The rendered footnotelist.
63
   */
64
  public static function renderFootnoteList($footnoteListKey, $separator = ', ') {
65
    $out = '';
66
    if (array_key_exists($footnoteListKey, self::$fnstore)) {
67
      foreach (self::$fnstore[$footnoteListKey] as $fn) {
68
        $out .= $fn->doRender() . $separator;
69
      }
70
      $out = substr($out, 0, strlen($out) - strlen($separator));
71
    }
72
    return $out;
73
  }
74

    
75
  /**
76
   * Add a new footnote.
77
   *
78
   * @param $footnoteListKey
79
   * @param $object
80
   * @param $theme
81
   * @param $themeArguments
82
   *
83
   * @return unknown_type
84
   */
85
  public static function addNewFootnote($footnoteListKey, $object = NULL, $theme = NULL, $themeArguments = array()) {
86
    if (!$object) {
87
      return FALSE;
88
    }
89
    if (!array_key_exists($footnoteListKey, self::$fnstore)) {
90
      self::$fnstore[$footnoteListKey] = array();
91
    }
92

    
93
    $fnKey = NULL;
94
    if (!($fnKey = self::footnoteExists($footnoteListKey, $object))) {
95
      $fnKey = self::$nextFootnoteKey++;
96
      $fn = new Footnote($fnKey, $object, $theme, $themeArguments);
97
      self::$fnstore[$footnoteListKey][$fnKey] = $fn;
98
    }
99

    
100
    return new FootnoteKey($fnKey, $footnoteListKey);
101
  }
102

    
103
  /**
104
   * Check if a footnote exists.
105
   *
106
   * @param $footnoteListKey
107
   * @param $object
108
   *
109
   * @return unknown_type
110
   */
111
  private static function footnoteExists($footnoteListKey, $object) {
112
    foreach (self::$fnstore[$footnoteListKey] as $key => $fn) {
113
      /*
114
      When using the comparison operator (==), object variables are compared
115
      in a simple manner, namely:
116
      Two object instances are equal if they have the same attributes and
117
      values, and are instances of the same class.
118
      */
119
      if ($object == $fn->object) {
120
        return $key;
121
      }
122
    }
123
    return FALSE;
124
  }
125

    
126
  /**
127
   * Stop users from cloning.
128
   */
129
  public function __clone() {
130
    trigger_error('Cloning instances of the singleton class FootNoteManager is prohibited', E_USER_ERROR);
131
  }
132
}
(3-3/4)