1<?php
2/**
3 * Page Buttons plugin
4 *
5 * @copyright (c) 2020 Cody Ernesti
6 * @license GPLv2 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
7 * @author  Cody Ernesti
8 *
9 *  Modified from: https://github.com/dregad/dokuwiki-plugin-deletepagebutton
10 *
11 *   Original license info:
12 *
13 * @copyright (c) 2020 Damien Regad
14 * @license GPLv2 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
15 * @author  Damien Regad
16 */
17
18// must be run within Dokuwiki
19if(!defined('DOKU_INC')) die();
20
21use dokuwiki\plugin\pagebuttons\DeletePageButton;
22use dokuwiki\plugin\pagebuttons\NewPageButton;
23use dokuwiki\plugin\pagebuttons\NewFolderButton;
24
25/**
26 * Class action_plugin_pagebuttons
27 *
28 * @package dokuwiki\plugin\pagebuttons
29 */
30class action_plugin_pagebuttons extends DokuWiki_Action_Plugin {
31
32    /**
33     * Register event handlers.
34     *
35     * @param Doku_Event_Handler $controller The plugin controller
36     */
37    public function register(Doku_Event_Handler $controller) {
38        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addjsinfo');
39        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addNewPageButton' );
40        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addNewFolderButton' );
41        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addDeleteButton' );
42        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'actionPage' );
43    }
44
45
46     /**
47     * Adds details to JSINFO
48     *
49     */
50    function addjsinfo($event, $params){
51        global $JSINFO;
52        global $conf;
53        $JSINFO['plugin_pagebuttons'] = array(
54            'usePrompt' => $this->getConf('usePrompt'),
55            'useslash' => $conf['useslash'],
56            'start' => $conf['start']
57        );
58    }
59
60    /**
61     * Hook for MENU_ITEMS_ASSEMBLY event.
62     *
63     * Adds 'Delete' button to DokuWiki's PageMenu.
64     *
65     * @param Doku_Event $event
66     */
67    public function addDeleteButton(Doku_Event $event) {
68        global $ID;
69
70        if (
71            $event->data['view'] !== 'page'
72            || $this->getConf('hideDelete')
73            || !$this->canDelete($ID)
74        ) {
75            return;
76        }
77
78        array_splice($event->data['items'], -1, 0, array(new DeletePageButton($this->getLang('delete_menu_item'))));
79    }
80
81    /**
82     * Hook for MENU_ITEMS_ASSEMBLY event.
83     *
84     * Adds 'New Page' button to DokuWiki's PageMenu.
85     *
86     * @param Doku_Event $event
87     */
88    public function addNewPageButton(Doku_Event $event) {
89        global $ID;
90        global $conf;
91
92        if (
93            $event->data['view'] !== 'page'
94            || $this->getConf('hideNewPage')
95            || !page_exists($ID)
96            || ($this->getConf('onlyShowNewButtonsOnStart') && !(substr_compare($ID, ":".$conf['start'], -strlen(":".$conf['start'])) === 0))
97        ) {
98            return;
99        }
100
101        array_splice($event->data['items'], -1, 0, array(new NewPageButton($this->getLang('newpage_menu_item'))));
102    }
103
104    /**
105     * Hook for MENU_ITEMS_ASSEMBLY event.
106     *
107     * Adds 'New Page' button to DokuWiki's PageMenu.
108     *
109     * @param Doku_Event $event
110     */
111    public function addNewFolderButton(Doku_Event $event) {
112        global $ID;
113        global $conf;
114
115        if (
116            $event->data['view'] !== 'page'
117            || $this->getConf('hideNewFolder')
118            || !page_exists($ID)
119            || ($this->getConf('onlyShowNewButtonsOnStart') && !(substr_compare($ID, ":".$conf['start'], -strlen(":".$conf['start'])) === 0))
120        ) {
121            return;
122        }
123
124        array_splice($event->data['items'], -1, 0, array(new NewFolderButton($this->getLang('newfolder_menu_item'))));
125    }
126
127    /**
128     * Determines whether the Delete button should be shown.
129     *
130     * @param $id
131     * @return bool
132     */
133    protected function canDelete($id) {
134        global $ACT;
135
136        return ($ACT == 'show' || empty($ACT))
137            && page_exists($id)
138            && auth_quickaclcheck($id) >= AUTH_EDIT
139            && checklock($id) === false && !@file_exists(wikiLockFN($id));
140    }
141
142    /**
143     * Hook for ACTION_ACT_PREPROCESS event.
144     *
145     * Handles the plugin's custom page deletion action: deletes the page and
146     * redirects to page view ('show' action).
147     *
148     * @param Doku_Event $event
149     */
150    public function actionPage(Doku_Event $event) {
151        global $ID, $INFO, $lang;
152
153        // Ignore other actions
154        if ($event->data != 'deletepagebutton' && $event->data != 'newfolderbutton' && $event->data != 'newpagebutton') {
155            return;
156        };
157
158        if(checkSecurityToken() && $INFO['exists']) {
159            if($event->data == 'deletepagebutton'){
160                // Save the page with empty contents to delete it
161                saveWikiText($ID, null, $lang['deleted']);
162
163                // Display confirmation message
164                msg($this->getLang('deleted_ok'), 1);
165            }
166        }
167
168        // Redirect to page view
169        $event->data = 'redirect';
170    }
171
172}
173