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        $hello = trim((string)$data['body']);
62        if (!$hello) $hello = $this->getLang('hello');
63
64        $opts = [
65            'hello' => $hello,
66            'placeholder' => $this->getLang('placeholder'),
67            'url' => DOKU_BASE . 'lib/exe/ajax.php?call=aichat',
68            'title-send' => $this->getLang('send-button'),
69            'title-restart' => $this->getLang('restart-button'),
70        ];
71        $html = '<aichat-chat ' . buildAttributes($opts) . '></aichat-chat>';
72
73        if (in_array('button', $data['params'])) {
74            $opts = [
75                'label' => $this->getLang('title'),
76                'title-close' => $this->getLang('close-button'),
77                'title-fullscreen' => $this->getLang('fullscreen-button'),
78            ];
79            if (in_array('float', $data['params'])) $opts['class'] = 'float';
80
81            $html = '<aichat-button ' . buildAttributes($opts) . '>' . $html . '</aichat-button>';
82        }
83
84        $renderer->doc .= $html;
85        return true;
86    }
87}
88