1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Andreas Gohr <gohr@cosmocode.de>
5 */
6
7/**
8 * Class action_plugin_blogtng_new
9 */
10class action_plugin_blogtng_new extends DokuWiki_Action_Plugin{
11
12    /** @var helper_plugin_blogtng_comments */
13    protected $commenthelper = null;
14
15    /**
16     * Constructor
17     */
18    public function __construct() {
19        $this->commenthelper = plugin_load('helper', 'blogtng_comments');
20    }
21
22    /**
23     * Registers a callback function for a given event
24     *
25     * @param Doku_Event_Handler $controller
26     */
27    public function register(Doku_Event_Handler $controller) {
28        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleNewBlogFormData', array());
29    }
30
31    /**
32     * Handles input from the newform and redirects to the edit mode
33     *
34     * @author Andreas Gohr <gohr@cosmocode.de>
35     * @author Gina Haeussge <osd@foosel.net>
36     *
37     * @param Doku_Event $event  event object by reference
38     * @param array      $param  empty array as passed to register_hook()
39     * @return bool
40     */
41    public function handleNewBlogFormData(Doku_Event $event, $param) {
42        global $TEXT, $INPUT;
43        global $ID;
44
45        if($event->data != 'btngnew') return true;
46
47        /** @var helper_plugin_blogtng_tools $tools */
48        $tools = plugin_load('helper', 'blogtng_tools');
49        if(!$INPUT->str('new-title')){
50            msg($this->getLang('err_notitle'),-1);
51            $event->data = 'show';
52            return true;
53        }
54
55        $newId = $tools->mkpostid($INPUT->str('new-format'), $INPUT->str('new-title'));
56         if ($ID != $newId) {
57             // first submission is 'post', next is 'get'.
58            $urlparams = [
59                'do' => 'btngnew',
60                'post-blog' => $INPUT->post->str('post-blog'),
61                'post-tags' => $INPUT->post->str('post-tags'),
62                'post-commentstatus' => $INPUT->post->str('post-commentstatus'),
63                'new-format' => $INPUT->post->str('new-format'),
64                'new-title' => $INPUT->post->str('new-title')
65            ];
66            send_redirect(wl($newId,$urlparams,true,'&'));
67            return false; //never reached
68        } else {
69            $TEXT = $this->prepareTemplateNewEntry($newId, $INPUT->str('new-title'));
70            $event->data = 'preview';
71            return false;
72        }
73    }
74
75    /**
76     * Loads the template for a new blog post and does some text replacements
77     *
78     * @author Gina Haeussge <osd@foosel.net>
79     *
80     * @param $id
81     * @param $title
82     * @return string
83     */
84    private function prepareTemplateNewEntry($id, $title) {
85        $tpl = pageTemplate($id);
86        if(!$tpl) $tpl = io_readFile(DOKU_PLUGIN . 'blogtng/tpl/newentry.txt');
87
88        $replace = array(
89            '@TITLE@' => $title,
90        );
91        return str_replace(array_keys($replace), array_values($replace), $tpl);
92    }
93}
94