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