1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin aichat (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Andreas Gohr <gohr@cosmocode.de>
10 */
11class syntax_plugin_aichat_chat extends SyntaxPlugin
12{
13    /** @inheritDoc */
14    public function getType()
15    {
16        return 'substition';
17    }
18
19    /** @inheritDoc */
20    public function getPType()
21    {
22        return 'block';
23    }
24
25    /** @inheritDoc */
26    public function getSort()
27    {
28        return 155;
29    }
30
31    /** @inheritDoc */
32    public function connectTo($mode)
33    {
34        $this->Lexer->addSpecialPattern('<aichat(?: [^>]+)*>.*?(?:<\/aichat>)', $mode, 'plugin_aichat_chat');
35    }
36
37
38    /** @inheritDoc */
39    public function handle($match, $state, $pos, Doku_Handler $handler)
40    {
41        $match = substr($match, 7, -9);
42        [$params, $body] = explode('>', $match, 2);
43        $params = explode(' ', $params);
44
45        return ['params' => $params, 'body' => $body];
46    }
47
48    /** @inheritDoc */
49    public function render($format, Doku_Renderer $renderer, $data)
50    {
51        if ($format !== 'xhtml') {
52            return false;
53        }
54
55        if ($this->getConf('restricted')) $renderer->nocache();
56        $helper = plugin_load('helper', 'aichat');
57        if (!$helper->userMayAccess()) {
58            return true;
59        }
60
61        $opts = [
62            'hello' => trim((string) $data['body']),
63            'placeholder' => $this->getLang('placeholder'),
64            'url' => DOKU_BASE . 'lib/exe/ajax.php?call=aichat',
65            'title-send' => $this->getLang('send-button'),
66            'title-restart' => $this->getLang('restart-button'),
67        ];
68        $html = '<aichat-chat ' . buildAttributes($opts) . '></aichat-chat>';
69
70        if (in_array('button', $data['params'])) {
71            $opts = [
72                'label' => $this->getLang('title'),
73                'title-close' => $this->getLang('close-button'),
74                'title-fullscreen' => $this->getLang('fullscreen-button'),
75            ];
76            if (in_array('float', $data['params'])) $opts['class'] = 'float';
77
78            $html = '<aichat-button ' . buildAttributes($opts) . '>' . $html . '</aichat-button>';
79        }
80
81        $renderer->doc .= $html;
82        return true;
83    }
84}
85