1<?php
2
3/**
4 * Plugin RefNotes: Scope
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10////////////////////////////////////////////////////////////////////////////////////////////////////
11class refnotes_scope_limits {
12    public $start;
13    public $end;
14
15    /**
16     * Constructor
17     */
18    public function __construct($start, $end = -1000) {
19        $this->start = $start;
20        $this->end = $end;
21    }
22}
23
24////////////////////////////////////////////////////////////////////////////////////////////////////
25class refnotes_scope_mock {
26
27    /**
28     *
29     */
30    public function getLimits() {
31        return new refnotes_scope_limits(-1, -1);
32    }
33
34    /**
35     *
36     */
37    public function isOpen() {
38        return false;
39    }
40
41    /**
42     *
43     */
44    public function getRenderer() {
45        return new refnotes_renderer_mock();
46    }
47
48    /**
49     *
50     */
51    public function getReferenceId() {
52        return 0;
53    }
54}
55
56////////////////////////////////////////////////////////////////////////////////////////////////////
57class refnotes_scope {
58
59    private $namespace;
60    private $id;
61    private $limits;
62    private $note;
63    private $notes;
64    private $references;
65
66    /**
67     * Constructor
68     */
69    public function __construct($namespace, $id, $start = -1, $end = -1000) {
70        $this->namespace = $namespace;
71        $this->id = $id;
72        $this->limits = new refnotes_scope_limits($start, $end);
73        $this->note = array();
74        $this->notes = 0;
75        $this->references = 0;
76    }
77
78    /**
79     *
80     */
81    public function getName() {
82        return $this->namespace->getName() . $this->id;
83    }
84
85    /**
86     *
87     */
88    public function getLimits() {
89        return $this->limits;
90    }
91
92    /**
93     *
94     */
95    public function isOpen() {
96        return $this->limits->end == -1000;
97    }
98
99    /**
100     *
101     */
102    public function getRenderer() {
103        return $this->namespace->getRenderer();
104    }
105
106    /**
107     *
108     */
109    public function getNoteId() {
110        return ++$this->notes;
111    }
112
113    /**
114     *
115     */
116    public function getReferenceId() {
117        return ++$this->references;
118    }
119
120    /**
121     *
122     */
123    public function addNote($note) {
124        $this->note[] = $note;
125    }
126
127    /**
128     *
129     */
130    public function rewriteReferences($limit) {
131        $block = new refnotes_note_block_iterator($this->note, $limit);
132
133        foreach ($block as $note) {
134            $note->rewriteReferences();
135        }
136    }
137
138    /**
139     *
140     */
141    public function renderNotes($mode, $limit) {
142        $minReferenceId = array();
143
144        foreach ($this->note as $note) {
145            $minReferenceId[] = $note->getMinReferenceId();
146        }
147
148        array_multisort($minReferenceId, $this->note);
149
150        $block = new refnotes_note_block_iterator($this->note, $limit);
151        $doc = '';
152
153        foreach ($block as $note) {
154            $doc .= $note->render($mode);
155        }
156
157        if ($mode == 'xhtml' && $doc != '') {
158            $open = $this->getRenderer()->renderNotesSeparator() . '<div class="notes">' . DOKU_LF;
159            $close = '</div>' . DOKU_LF;
160            $doc = $open . $doc . $close;
161        }
162
163        return $doc;
164    }
165
166    /**
167     * Finds a note given it's name or id
168     */
169    public function findNote($namespaceName, $noteName) {
170        $result = NULL;
171
172        if ($noteName != '') {
173            if (is_int($noteName)) {
174                $getter = 'getId';
175            }
176            else {
177                $getter = 'getName';
178            }
179
180            foreach ($this->note as $note) {
181                if (($note->getNamespaceName() == $namespaceName) && ($note->$getter() == $noteName)) {
182                    $result = $note;
183                    break;
184                }
185            }
186        }
187
188        return $result;
189    }
190}
191