1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Andreas Gohr <gohr@cosmocode.de>
5 */
6
7use dokuwiki\plugin\blogtng\entities\Comment;
8
9/**
10 * Class action_plugin_blogtng_ajax
11 */
12class action_plugin_blogtng_ajax extends DokuWiki_Action_Plugin{
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller
18     */
19    public function register(Doku_Event_Handler $controller) {
20        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'renderCommentPreview', array());
21    }
22
23    /**
24     * Callback function for event 'AJAX_CALL_UNKNOWN' to return a rendered preview of a comment
25     * (which will be shown below the comment input field)
26     *
27     * @param Doku_Event $event  event object by reference
28     * @param array      $param  empty array as passed to register_hook()
29     */
30    public function renderCommentPreview(Doku_Event $event, $param) {
31        /** @var DokuWiki_Auth_Plugin $auth */
32        global $auth, $INPUT;
33
34        if($event->data != 'blogtng__comment_preview') return;
35        $event->preventDefault();
36        $event->stopPropagation();
37
38        $comment = new Comment();
39        $comment->setText($INPUT->post->str('text'));
40        $comment->setName($INPUT->post->str('name'));
41        $comment->setMail($INPUT->post->str('mail'));
42        $comment->setWeb($INPUT->post->str('web'));
43        $comment->setCid('preview');
44        $comment->setCreated(time());
45        $comment->setStatus('visible');
46
47        if(!$comment->getName() && $INPUT->server->str('REMOTE_USER')){
48            if($auth AND $info = $auth->getUserData($INPUT->server->str('REMOTE_USER'))) {
49                $comment->setName($info['name']);
50                $comment->setMail($info['mail']);
51            }
52        }
53
54        $comment->output($INPUT->post->str('tplname'));
55    }
56}
57