1<?php
2/**
3 * DokuWiki Plugin copypage (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  orangain <orangain@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_copypage extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22        $controller->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'get_template');
23        // since 2017-09-01
24        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'add_menu_item', array());
25        // DEPRECATED since 2017-09-01
26        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'add_tool_button');
27    }
28
29    /**
30     * Handler to load page template.
31     *
32     * @param Doku_Event $event  event object by reference
33     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
34     *                           handler was registered]
35     * @return void
36     */
37    public function get_template(Doku_Event &$event, $param) {
38        if (strlen($_REQUEST['copyfrom']) > 0) {
39            $template_id = $_REQUEST['copyfrom'];
40            if (auth_quickaclcheck($template_id) >= AUTH_READ) {
41                $tpl = io_readFile(wikiFN($template_id));
42                if ($this->getConf('replaceid')) {
43                    $id = $event->data['id'];
44                    $tpl = str_replace($template_id, $id, $tpl);
45                }
46                $event->data['tpl'] = $tpl;
47                $event->preventDefault();
48            }
49        }
50    }
51
52    /**
53     * (DEPRECATED since 2017-09-01) Handler to add page tools.
54     *
55     * @param Doku_Event $event  event object by reference
56     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
57     *                           handler was registered]
58     * @return void
59     */
60    public function add_tool_button(Doku_Event &$event, $param) {
61        $event->data['items']['copypage'] = '<li>' .
62            '<a href="#" class="action copypage copypageplugin__copy" rel="nofollow">' .
63            '<span>' .
64            $this->getLang('copypage') .
65            '</span>' .
66            '</a>' .
67            '</li>';
68    }
69
70    /**
71     * Handler to add menu item (since 2017-09-01).
72     *
73     * @param Doku_Event $event  event object
74     * @return void
75     */
76    public function add_menu_item(Doku_Event $event) {
77        if ($event->data['view'] != 'page') {
78            return;
79        }
80        array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\copypage\MenuItem()]);
81    }
82
83}
84