Project

General

Profile

Download (3.51 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 in multiple list. Each of these footnote lists is
18
 * identified by a footnoteListKey.
19
 *
20
 * The $footnoteListKey for the curtent page part that should be stored in the
21
 * the RenderHints class by calling @see RenderHints::setFootnoteListKey($footnoteListKey)
22
 */
23
class FootnoteManager {
24
  private static $fnstore = array();
25
  private static $nextFootnoteKey = 1;
26

    
27
  /**
28
   * Private constructor.
29
   */
30
  private function __construct() {}
31

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

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

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

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

    
97
    $fnKey = NULL;
98
    if (!($fnKey = self::footnoteExists($footnoteListKey, $object))) {
99
      $fnKey = self::$nextFootnoteKey++;
100
      $fn = new Footnote($fnKey, $object, $theme, $themeArguments);
101
      self::$fnstore[$footnoteListKey][$fnKey] = $fn;
102
    }
103

    
104
    return new FootnoteKey($fnKey, $footnoteListKey);
105
  }
106

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

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