summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/refnotes/scope.php
blob: b95563518c3c9e87a0cb5f21b81306cbca4c6136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

/**
 * Plugin RefNotes: Scope
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
 */

////////////////////////////////////////////////////////////////////////////////////////////////////
class refnotes_scope_limits {
    public $start;
    public $end;

    /**
     * Constructor
     */
    public function __construct($start, $end = -1000) {
        $this->start = $start;
        $this->end = $end;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
class refnotes_scope_mock {

    /**
     *
     */
    public function getLimits() {
        return new refnotes_scope_limits(-1, -1);
    }

    /**
     *
     */
    public function isOpen() {
        return false;
    }

    /**
     *
     */
    public function getRenderer() {
        return new refnotes_renderer_mock();
    }

    /**
     *
     */
    public function getReferenceId() {
        return 0;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
class refnotes_scope {

    private $namespace;
    private $id;
    private $limits;
    private $note;
    private $notes;
    private $references;

    /**
     * Constructor
     */
    public function __construct($namespace, $id, $start = -1, $end = -1000) {
        $this->namespace = $namespace;
        $this->id = $id;
        $this->limits = new refnotes_scope_limits($start, $end);
        $this->note = array();
        $this->notes = 0;
        $this->references = 0;
    }

    /**
     *
     */
    public function getName() {
        return $this->namespace->getName() . $this->id;
    }

    /**
     *
     */
    public function getLimits() {
        return $this->limits;
    }

    /**
     *
     */
    public function isOpen() {
        return $this->limits->end == -1000;
    }

    /**
     *
     */
    public function getRenderer() {
        return $this->namespace->getRenderer();
    }

    /**
     *
     */
    public function getNoteId() {
        return ++$this->notes;
    }

    /**
     *
     */
    public function getReferenceId() {
        return ++$this->references;
    }

    /**
     *
     */
    public function addNote($note) {
        $this->note[] = $note;
    }

    /**
     *
     */
    public function rewriteReferences($limit) {
        $block = new refnotes_note_block_iterator($this->note, $limit);

        foreach ($block as $note) {
            $note->rewriteReferences();
        }
    }

    /**
     *
     */
    public function renderNotes($mode, $limit) {
        $minReferenceId = array();

        foreach ($this->note as $note) {
            $minReferenceId[] = $note->getMinReferenceId();
        }

        array_multisort($minReferenceId, $this->note);

        $block = new refnotes_note_block_iterator($this->note, $limit);
        $doc = '';

        foreach ($block as $note) {
            $doc .= $note->render($mode);
        }

        if ($mode == 'xhtml' && $doc != '') {
            $open = $this->getRenderer()->renderNotesSeparator() . '<div class="notes">' . DOKU_LF;
            $close = '</div>' . DOKU_LF;
            $doc = $open . $doc . $close;
        }

        return $doc;
    }

    /**
     * Finds a note given it's name or id
     */
    public function findNote($namespaceName, $noteName) {
        $result = NULL;

        if ($noteName != '') {
            if (is_int($noteName)) {
                $getter = 'getId';
            }
            else {
                $getter = 'getName';
            }

            foreach ($this->note as $note) {
                if (($note->getNamespaceName() == $namespaceName) && ($note->$getter() == $noteName)) {
                    $result = $note;
                    break;
                }
            }
        }

        return $result;
    }
}