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