1<?php
2/**
3 * MoaiEditor plugin for DokuWiki
4 */
5# Constants
6const MOAIED_FAKE_MESSAGES = false;     # Create fake messages (info, error, success, notify) to test template compatibilty
7
8
9use dokuwiki\Extension\Plugin;
10
11if(!defined('DOKU_INC')) die();
12
13class action_plugin_moaieditor extends DokuWiki_Action_Plugin {
14
15    // Register hook function
16    public function register(Doku_Event_Handler $controller) {
17        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'moaieditor_main');
18    }
19    // Main function
20    public function moaieditor_main (Doku_Event &$event, $param) {
21        global $ACT, $INFO, $conf, $lang;
22
23        // Exit if not in edit mode
24        if (!in_array($ACT, ['edit'])) {
25            return;
26        }
27        // Pass some data to Javascript
28        $jsinfo = array(
29            // Configuration options
30            'hide_editor_by_default'    => $this->getConf('hide_editor_by_default'),
31            'editor_width_side_by_side' => $this->getConf('editor_width_side_by_side'),
32            'editor_width_top_bottom'   => $this->getConf('editor_width_top_bottom'),
33            'editor_width_single_pane'  => $this->getConf('editor_width_single_pane'),
34            // Docinfo
35            'docinfo'                   => $this->getDocinfo(),
36            // Editor intro message
37            'intromsg'                  => $this->getEditorIntroMessage(),
38        );
39        $event->data['script'][] = array(
40            'type' => 'text/javascript',
41            '_data' => 'JSINFO.plugin_moaieditor = ' . json_encode($jsinfo),
42        );
43        // Simulate template messages (for testing template compatibility)
44        if( isset($_GET['fakemsg']) || MOAIED_FAKE_MESSAGES)
45            $this->simulate_template_messages();
46    }
47    // ───────────────────────────────────────────
48    public function getEditorIntroMessage () {
49        # The editor intro message is rendered here: inc/Ui/Editor.php -> show()
50        # In editor mode, it can be of two types: 'editrev' or 'edit'
51        #      /inc/lang/en/edit.txt       "**You've loaded an old revision of the document!** If you save it, you will create a new version with this data."
52        #      /inc/lang/en/editrev.txt    "Edit the page and hit ''Save''. See [[wiki:syntax]] for Wiki syntax. Please edit the page only if you can **improve** it. If you want to test some things, learn to make your first steps on the [[playground:playground|playground]]."
53        global $REV;
54        global $INFO;
55        $wr = $INFO['writable'] && !$INFO['locked'];
56        if (!$wr)
57            return null;
58        $type = ($REV) ? 'editrev' : 'edit';   // intro locale text (edit, rditrev, or read)
59        $html = p_locale_xhtml($type);
60        return ['type'=>$type, 'html'=>$html];
61    }
62    // ───────────────────────────────────────────
63    // Get document info (mostly copied from 'tpl_pageinfo' but we want to format the data differently)
64    public function getDocinfo () {
65        global $conf, $lang, $INFO, $ID;
66
67        // Document info will not exist when creating a new page
68        if (!$INFO['exists'])
69            return null;
70
71        // Document path on disk
72        $fn = $INFO['filepath'];
73        if (!$conf['fullpath']) {
74            if ($INFO['rev'])
75                $fn = str_replace($conf['olddir'] . '/', '', $fn);
76            else
77                $fn = str_replace($conf['datadir'] . '/', '', $fn);
78        }
79        $fn = utf8_decodeFN($fn);
80
81        // Last modification time
82        $dateLocal = dformat($INFO['lastmod']);
83        $dateIso = date(DATE_ISO8601, $INFO['lastmod']);
84
85        // Edited by
86        if ($INFO['editor'])
87            $by = $lang['by'].' <bdi><b>'.editorinfo($INFO['editor']).'</b></bdi>';
88        else
89            $by = '('.$lang['external_edit'].')';
90
91        // Return it
92        return [
93            'labelPath' => 'Document path on disk',
94            'labelMod'  => $lang['lastmod'],
95            'path'      => "<bdi><i>data/pages/</i><b>$fn</b></bdi>",
96            'time'      => "<time datetime='$dateIso'><b>$dateLocal</b></time>",
97            'by'        => $by
98        ];
99    }
100    // ───────────────────────────────────────────
101    public function simulate_template_messages () {
102        global $MSG;
103        $MSG[] = ['msg'=>'Error message (-1)',  'lvl'=>'error',   'allow'=>0, 'line'=>'', 'file'=>''];
104        $MSG[] = ['msg'=>'Info message (0)',    'lvl'=>'info',    'allow'=>0, 'line'=>'', 'file'=>''];
105        $MSG[] = ['msg'=>'Success message (1)', 'lvl'=>'success', 'allow'=>0, 'line'=>'', 'file'=>''];
106        $MSG[] = ['msg'=>'Notify message (2)',  'lvl'=>'notify',  'allow'=>0, 'line'=>'', 'file'=>''];
107    }
108
109} // End class
110