xref: /plugin/aichat/syntax/chat.php (revision c1e277fbba52f3cb14b82e5184993eb0611909c6)
101f06932SAndreas Gohr<?php
201f06932SAndreas Gohr
37ebc7895Ssplitbrainuse dokuwiki\Extension\SyntaxPlugin;
47ebc7895Ssplitbrain
501f06932SAndreas Gohr/**
601f06932SAndreas Gohr * DokuWiki Plugin aichat (Syntax Component)
701f06932SAndreas Gohr *
801f06932SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
901f06932SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
1001f06932SAndreas Gohr */
117ebc7895Ssplitbrainclass syntax_plugin_aichat_chat extends SyntaxPlugin
1201f06932SAndreas Gohr{
1301f06932SAndreas Gohr    /** @inheritDoc */
1401f06932SAndreas Gohr    public function getType()
1501f06932SAndreas Gohr    {
1601f06932SAndreas Gohr        return 'substition';
1701f06932SAndreas Gohr    }
1801f06932SAndreas Gohr
1901f06932SAndreas Gohr    /** @inheritDoc */
2001f06932SAndreas Gohr    public function getPType()
2101f06932SAndreas Gohr    {
2201f06932SAndreas Gohr        return 'block';
2301f06932SAndreas Gohr    }
2401f06932SAndreas Gohr
2501f06932SAndreas Gohr    /** @inheritDoc */
2601f06932SAndreas Gohr    public function getSort()
2701f06932SAndreas Gohr    {
2801f06932SAndreas Gohr        return 155;
2901f06932SAndreas Gohr    }
3001f06932SAndreas Gohr
3101f06932SAndreas Gohr    /** @inheritDoc */
3201f06932SAndreas Gohr    public function connectTo($mode)
3301f06932SAndreas Gohr    {
3401f06932SAndreas Gohr        $this->Lexer->addSpecialPattern('<aichat(?: [^>]+)*>.*?(?:<\/aichat>)', $mode, 'plugin_aichat_chat');
3501f06932SAndreas Gohr    }
3601f06932SAndreas Gohr
3701f06932SAndreas Gohr
3801f06932SAndreas Gohr    /** @inheritDoc */
3901f06932SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
4001f06932SAndreas Gohr    {
4101f06932SAndreas Gohr        $match = substr($match, 7, -9);
4201f06932SAndreas Gohr        [$params, $body] = explode('>', $match, 2);
4301f06932SAndreas Gohr        $params = explode(' ', $params);
4401f06932SAndreas Gohr
4501f06932SAndreas Gohr        return ['params' => $params, 'body' => $body];
4601f06932SAndreas Gohr    }
4701f06932SAndreas Gohr
4801f06932SAndreas Gohr    /** @inheritDoc */
4901f06932SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
5001f06932SAndreas Gohr    {
5101f06932SAndreas Gohr        if ($format !== 'xhtml') {
5201f06932SAndreas Gohr            return false;
5301f06932SAndreas Gohr        }
5401f06932SAndreas Gohr
5501f06932SAndreas Gohr        if ($this->getConf('restricted')) $renderer->nocache();
5601f06932SAndreas Gohr        $helper = plugin_load('helper', 'aichat');
5701f06932SAndreas Gohr        if (!$helper->userMayAccess()) {
5801f06932SAndreas Gohr            return true;
5901f06932SAndreas Gohr        }
6001f06932SAndreas Gohr
61*c1e277fbSAndreas Gohr        $hello = trim((string)$data['body']);
62*c1e277fbSAndreas Gohr        if (!$hello) $hello = $this->getLang('hello');
63*c1e277fbSAndreas Gohr
6401f06932SAndreas Gohr        $opts = [
65*c1e277fbSAndreas Gohr            'hello' => $hello,
6601f06932SAndreas Gohr            'placeholder' => $this->getLang('placeholder'),
6701f06932SAndreas Gohr            'url' => DOKU_BASE . 'lib/exe/ajax.php?call=aichat',
68f43684f4SAndreas Gohr            'title-send' => $this->getLang('send-button'),
69f43684f4SAndreas Gohr            'title-restart' => $this->getLang('restart-button'),
7001f06932SAndreas Gohr        ];
7101f06932SAndreas Gohr        $html = '<aichat-chat ' . buildAttributes($opts) . '></aichat-chat>';
7201f06932SAndreas Gohr
7301f06932SAndreas Gohr        if (in_array('button', $data['params'])) {
7401f06932SAndreas Gohr            $opts = [
7501f06932SAndreas Gohr                'label' => $this->getLang('title'),
76f43684f4SAndreas Gohr                'title-close' => $this->getLang('close-button'),
77f43684f4SAndreas Gohr                'title-fullscreen' => $this->getLang('fullscreen-button'),
7801f06932SAndreas Gohr            ];
7901f06932SAndreas Gohr            if (in_array('float', $data['params'])) $opts['class'] = 'float';
8001f06932SAndreas Gohr
8101f06932SAndreas Gohr            $html = '<aichat-button ' . buildAttributes($opts) . '>' . $html . '</aichat-button>';
8201f06932SAndreas Gohr        }
8301f06932SAndreas Gohr
8401f06932SAndreas Gohr        $renderer->doc .= $html;
8501f06932SAndreas Gohr        return true;
8601f06932SAndreas Gohr    }
8701f06932SAndreas Gohr}
88