1<?php
2
3/**
4 * Plugin RefNotes: Configuration interface
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10////////////////////////////////////////////////////////////////////////////////////////////////////
11class admin_plugin_refnotes extends DokuWiki_Admin_Plugin {
12    use refnotes_localization_plugin;
13
14    private $locale;
15
16    /**
17     * Constructor
18     */
19    public function __construct() {
20        refnotes_localization::initialize($this);
21
22        $this->locale = refnotes_localization::getInstance();
23    }
24
25    /**
26     * Handle user request
27     */
28    public function handle() {
29        /* All handling is done using AJAX */
30    }
31
32    /**
33     * Output appropriate html
34     */
35    public function html() {
36        print($this->locale_xhtml('intro'));
37
38        print('<!-- refnotes -->');
39
40        $this->printLanguageStrings();
41
42        print('<div id="refnotes-config"><div id="config__manager">');
43        print('<noscript><div class="error">' . $this->locale->getLang('noscript') . '</div></noscript>');
44        print('<div id="server-status" class="info" style="display: none;">&nbsp;</div>');
45        print('<form action="" method="post">');
46
47        $this->printGeneral();
48        $this->printNamespaces();
49        $this->printNotes();
50
51        print($this->getButton('save'));
52
53        print('</form></div></div>');
54        print('<!-- /refnotes -->');
55    }
56
57    /**
58     * Built-in JS localization stores all language strings in the common script (produced by js.php).
59     * The strings used by administration plugin seem to be unnecessary in that script. Instead we print
60     * them as part of the page and then load them into the LANG array on the client side.
61     */
62    private function printLanguageStrings() {
63        $lang = $this->locale->getByPrefix('js');
64
65        print('<div id="refnotes-lang" style="display: none;">');
66
67        foreach ($lang as $key => $value) {
68            print($key . ' : ' . $value . ':eos:');
69        }
70
71        print('</div>');
72    }
73
74    /**
75     *
76     */
77    private function printGeneral() {
78        $section = new refnotes_config_general();
79        $section->printHtml();
80    }
81
82    /**
83     *
84     */
85    private function printNamespaces() {
86        $section = new refnotes_config_namespaces();
87        $section->printHtml();
88    }
89
90    /**
91     *
92     */
93    private function printNotes() {
94        $section = new refnotes_config_notes();
95        $section->printHtml();
96    }
97
98    /**
99     *
100     */
101    private function getButton($action) {
102        $html = '<input type="button" class="button"';
103        $id = $action . '-config';
104        $html .= ' id="' . $id . '"';
105        $html .= ' name="' . $id . '"';
106        $html .= ' value="' . $this->locale->getLang('btn_' . $action) . '"';
107        $html .= ' />';
108
109        return $html;
110    }
111}
112
113////////////////////////////////////////////////////////////////////////////////////////////////////
114class refnotes_config_section {
115
116    protected $id;
117    protected $title;
118
119    /**
120     * Constructor
121     */
122    public function __construct($id) {
123        $this->id = $id;
124        $this->title = 'sec_' . $id;
125    }
126
127    /**
128     *
129     */
130    public function printHtml() {
131        $this->open();
132        $this->printFields();
133        $this->close();
134    }
135
136    /**
137     *
138     */
139    protected function open() {
140        $title = refnotes_localization::getInstance()->getLang($this->title);
141
142        print('<fieldset id="' . $this->id . '">');
143        print('<legend>' . $title . '</legend>');
144        print('<table class="inline" cols="3">');
145    }
146
147    /**
148     *
149     */
150    protected function close() {
151        print('</table>');
152        print('</fieldset>');
153    }
154
155    /**
156     *
157     */
158    protected function printFields() {
159        $field = $this->getFields();
160        foreach ($field as $f) {
161            $this->printFieldRow($f);
162        }
163    }
164
165    /**
166     *
167     */
168    protected function getFields() {
169        $fieldData = $this->getFieldDefinitions();
170        $field = array();
171
172        foreach ($fieldData as $id => $fd) {
173            $class = 'refnotes_config_' . $fd['class'];
174            $field[] = new $class($id, $fd);
175        }
176
177        return $field;
178    }
179
180    /**
181     *
182     */
183    protected function printFieldRow($field, $startRow = true) {
184        if ($startRow) {
185            print('<tr>');
186        }
187
188        if (get_class($field) != 'refnotes_config_textarea') {
189            $settingName = $field->getSettingName();
190
191            if ($settingName != '') {
192                print('<td class="label">');
193                print($settingName);
194            }
195            else {
196                print('<td class="lean-label">');
197            }
198
199            print($field->getLabel());
200            print('</td><td class="value">');
201        }
202        else {
203            print('<td class="value" colspan="2">');
204        }
205
206        print($field->getControl());
207        print('</td>');
208
209        print('</tr>');
210    }
211}
212
213////////////////////////////////////////////////////////////////////////////////////////////////////
214class refnotes_config_list_section extends refnotes_config_section {
215
216    private $listRows;
217
218    /**
219     * Constructor
220     */
221    public function __construct($id, $listRows) {
222        parent::__construct($id);
223
224        $this->listRows = $listRows;
225    }
226
227    /**
228     *
229     */
230    protected function close() {
231        print('</table>');
232
233        $this->printListControls();
234
235        print('</fieldset>');
236    }
237
238    /**
239     *
240     */
241    private function printListControls() {
242        print('<div class="list-controls">');
243
244        print($this->getEdit());
245        print($this->getButton('add'));
246        print($this->getButton('rename'));
247        print($this->getButton('delete'));
248
249        print('</div>');
250    }
251
252    /**
253     *
254     */
255    private function getEdit() {
256        $html = '<input type="text" class="edit"';
257        $id = 'name-' . $this->id;
258        $html .= ' id="' . $id . '"';
259        $html .= ' name="' . $id . '"';
260        $html .= ' value=""';
261        $html .= ' />';
262
263        return $html;
264    }
265
266    /**
267     *
268     */
269    private function getButton($action) {
270        $label = refnotes_localization::getInstance()->getLang('btn_' . $action);
271
272        $id = $action . '-' . $this->id;
273        $html = '<input type="button" class="button"';
274        $html .= ' id="' . $id . '"';
275        $html .= ' name="' . $id . '"';
276        $html .= ' value="' . $label . '"';
277        $html .= ' />';
278
279        return $html;
280    }
281
282    /**
283     *
284     */
285    protected function printFields() {
286        $field = $this->getFields();
287        $fields = count($field);
288
289        print('<tr>');
290        print('<td class="list" rowspan="' . $fields . '">');
291        print('<select class="list" id="select-' . $this->id . '" size="' . $this->listRows . '"></select>');
292        print('</td>');
293
294        $this->printFieldRow($field[0], false);
295
296        for ($f = 1; $f < $fields; $f++) {
297            $this->printFieldRow($field[$f]);
298        }
299    }
300}
301
302////////////////////////////////////////////////////////////////////////////////////////////////////
303class refnotes_config_general extends refnotes_config_section {
304
305    /**
306     * Constructor
307     */
308    public function __construct() {
309        parent::__construct('general');
310    }
311
312    /**
313     *
314     */
315    protected function getFieldDefinitions() {
316        static $field = array(
317            'replace-footnotes' => array(
318                'class' => 'checkbox',
319                'lean' => true
320            ),
321            'reference-db-enable' => array(
322                'class' => 'checkbox',
323                'lean' => true
324            ),
325            'reference-db-namespace' => array(
326                'class' => 'edit',
327                'lean' => true
328            )
329        );
330
331        return $field;
332    }
333}
334
335////////////////////////////////////////////////////////////////////////////////////////////////////
336class refnotes_config_namespaces extends refnotes_config_list_section {
337
338    /**
339     * Constructor
340     */
341    public function __construct() {
342        parent::__construct('namespaces', 48);
343    }
344
345    /**
346     *
347     */
348    protected function getFieldDefinitions() {
349        static $field = array(
350            'refnote-id' => array(
351                'class' => 'select',
352                'option' => array('numeric', 'latin-lower', 'latin-upper', 'roman-lower', 'roman-upper', 'stars', 'note-name', 'inherit')
353            ),
354            'reference-base' => array(
355                'class' => 'select',
356                'option' => array('super', 'normal-text', 'inherit')
357            ),
358            'reference-font-weight' => array(
359                'class' => 'select',
360                'option' => array('normal', 'bold', 'inherit')
361            ),
362            'reference-font-style' => array(
363                'class' => 'select',
364                'option' => array('normal', 'italic', 'inherit')
365            ),
366            'reference-format' => array(
367                'class' => 'select',
368                'option' => array('right-parent', 'parents', 'right-bracket', 'brackets', 'none', 'inherit')
369            ),
370            'reference-group' => array(
371                'class' => 'select',
372                'option' => array('group-none', 'group-comma', 'group-semicolon', 'inherit')
373            ),
374            'reference-render' => array(
375                'class' => 'select',
376                'option' => array('basic', 'harvard', 'inherit')
377            ),
378            'multi-ref-id' => array(
379                'class' => 'select',
380                'option' => array('ref-counter', 'note-counter', 'inherit')
381            ),
382            'note-preview' => array(
383                'class' => 'select',
384                'option' => array('popup', 'tooltip', 'none', 'inherit')
385            ),
386            'notes-separator' => array(
387                'class' => 'edit_inherit'
388            ),
389            'note-text-align' => array(
390                'class' => 'select',
391                'option' => array('justify', 'left', 'inherit')
392            ),
393            'note-font-size' => array(
394                'class' => 'select',
395                'option' => array('normal', 'reduced', 'small', 'inherit')
396            ),
397            'note-render' => array(
398                'class' => 'select',
399                'option' => array('basic', 'harvard', 'inherit')
400            ),
401            'note-id-base' => array(
402                'class' => 'select',
403                'option' => array('super', 'normal-text', 'inherit')
404            ),
405            'note-id-font-weight' => array(
406                'class' => 'select',
407                'option' => array('normal', 'bold', 'inherit')
408            ),
409            'note-id-font-style' => array(
410                'class' => 'select',
411                'option' => array('normal', 'italic', 'inherit')
412            ),
413            'note-id-format' => array(
414                'class' => 'select',
415                'option' => array('right-parent', 'parents', 'right-bracket', 'brackets', 'dot', 'none', 'inherit')
416            ),
417            'back-ref-caret' => array(
418                'class' => 'select',
419                'option' => array('prefix', 'merge', 'none', 'inherit')
420            ),
421            'back-ref-base' => array(
422                'class' => 'select',
423                'option' => array('super', 'normal-text', 'inherit')
424            ),
425            'back-ref-font-weight' => array(
426                'class' => 'select',
427                'option' => array('normal', 'bold', 'inherit')
428            ),
429            'back-ref-font-style' => array(
430                'class' => 'select',
431                'option' => array('normal', 'italic', 'inherit')
432            ),
433            'back-ref-format' => array(
434                'class' => 'select',
435                'option' => array('note-id', 'latin', 'numeric', 'caret', 'arrow', 'none', 'inherit')
436            ),
437            'back-ref-separator' => array(
438                'class' => 'select',
439                'option' => array('comma', 'none', 'inherit')
440            ),
441            'scoping' => array(
442                'class' => 'select',
443                'option' => array('reset', 'single')
444            )
445        );
446
447        return $field;
448    }
449}
450
451////////////////////////////////////////////////////////////////////////////////////////////////////
452class refnotes_config_notes extends refnotes_config_list_section {
453
454    /**
455     * Constructor
456     */
457    public function __construct() {
458        parent::__construct('notes', 14);
459    }
460
461    /**
462     *
463     */
464    protected function getFieldDefinitions() {
465        static $field = array(
466            'note-text' => array(
467                'class' => 'textarea',
468                'rows' => '4',
469                'lean' => true
470            ),
471            'inline' => array(
472                'class' => 'checkbox',
473                'lean' => true
474            ),
475            'use-reference-base' => array(
476                'class' => 'checkbox',
477                'lean' => true
478            ),
479            'use-reference-font-weight' => array(
480                'class' => 'checkbox',
481                'lean' => true
482            ),
483            'use-reference-font-style' => array(
484                'class' => 'checkbox',
485                'lean' => true
486            ),
487            'use-reference-format' => array(
488                'class' => 'checkbox',
489                'lean' => true
490            )
491        );
492
493        return $field;
494    }
495}
496
497////////////////////////////////////////////////////////////////////////////////////////////////////
498class refnotes_config_field {
499
500    protected $id;
501    protected $settingName;
502    protected $label;
503
504    /**
505     * Constructor
506     */
507    public function __construct($id, $data) {
508        $this->id = 'field-' . $id;
509        $this->label = 'lbl_' . $id;
510
511        if (array_key_exists('lean', $data) && $data['lean']) {
512            $this->settingName = '';
513        }
514        else {
515            $this->settingName = $id;
516        }
517    }
518
519    /**
520     *
521     */
522    public function getSettingName() {
523        $html = '';
524
525        if ($this->settingName != '') {
526            $html = '<span class="outkey">' . $this->settingName . '</span>';
527        }
528
529        return $html;
530    }
531
532    /**
533     *
534     */
535    public function getLabel() {
536        $label = refnotes_localization::getInstance()->getLang($this->label);
537
538        return '<label for="' . $this->id . '">' . $label . '</label>';
539    }
540}
541
542////////////////////////////////////////////////////////////////////////////////////////////////////
543class refnotes_config_checkbox extends refnotes_config_field {
544
545    /**
546     * Constructor
547     */
548    public function __construct($id, $data) {
549        parent::__construct($id, $data);
550    }
551
552    /**
553     *
554     */
555    public function getControl() {
556        $html = '<div class="input">';
557        $html .= '<input type="checkbox" class="checkbox"';
558        $html .= ' id="' . $this->id . '"';
559        $html .= ' name="' . $this->id . '" value="1"';
560        $html .= '/></div>';
561
562        return $html;
563    }
564}
565
566////////////////////////////////////////////////////////////////////////////////////////////////////
567class refnotes_config_select extends refnotes_config_field {
568
569    private $option;
570
571    /**
572     * Constructor
573     */
574    public function __construct($id, $data) {
575        parent::__construct($id, $data);
576
577        $this->option = $data['option'];
578    }
579
580    /**
581     *
582     */
583    public function getControl() {
584        $locale = refnotes_localization::getInstance();
585
586        $html = '<div class="input">';
587
588        $html .= '<select class="edit"';
589        $html .= ' id="' . $this->id . '"';
590        $html .= ' name="' . $this->id . '">' . DOKU_LF;
591
592        foreach ($this->option as $option) {
593            $html .= '<option value="' . $option . '">' . $locale->getLang('opt_' . $option) . '</option>' . DOKU_LF;
594        }
595
596        $html .= '</select>';
597        $html .= '</div>';
598
599        return $html;
600    }
601}
602
603////////////////////////////////////////////////////////////////////////////////////////////////////
604class refnotes_config_edit extends refnotes_config_field {
605
606    /**
607     * Constructor
608     */
609    public function __construct($id, $data) {
610        parent::__construct($id, $data);
611    }
612
613    /**
614     *
615     */
616    public function getControl() {
617        $html = '<div class="input">';
618
619        $html .= '<input type="text" class="edit"';
620        $html .= ' id="' . $this->id . '"';
621        $html .= ' name="' . $this->id . '" />' . DOKU_LF;
622
623        $html .= '</div>';
624
625        return $html;
626    }
627}
628
629////////////////////////////////////////////////////////////////////////////////////////////////////
630class refnotes_config_edit_inherit extends refnotes_config_field {
631
632    /**
633     * Constructor
634     */
635    public function __construct($id, $data) {
636        parent::__construct($id, $data);
637    }
638
639    /**
640     *
641     */
642    public function getControl() {
643        $buttonLabel = refnotes_localization::getInstance()->getLang('opt_inherit');
644
645        $html = '<div class="input">';
646
647        $html .= '<input type="text" class="edit"';
648        $html .= ' id="' . $this->id . '"';
649        $html .= ' name="' . $this->id . '" />' . DOKU_LF;
650
651        $html .= '<input type="button" class="button"';
652        $html .= ' id="' . $this->id . '-inherit"';
653        $html .= ' name="' . $this->id . '-inherit"';
654        $html .= ' value="' . $buttonLabel . '"';
655        $html .= ' />';
656
657        $html .= '</div>';
658
659        return $html;
660    }
661}
662
663////////////////////////////////////////////////////////////////////////////////////////////////////
664class refnotes_config_textarea extends refnotes_config_field {
665
666    private $rows;
667
668    /**
669     * Constructor
670     */
671    public function __construct($id, $data) {
672        parent::__construct($id, $data);
673
674        $this->rows = $data['rows'];
675    }
676
677    /**
678     *
679     */
680    public function getControl() {
681        $html = '<div class="input">';
682        $html .= '<textarea class="edit"';
683        $html .= ' id="' . $this->id . '"';
684        $html .= ' name="' . $this->id . '"';
685        $html .= ' cols="40" rows="' . $this->rows . '">';
686        $html .= '</textarea></div>';
687
688        return $html;
689    }
690}
691