1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Luigi Micco <l.micco@tiscali.it>
5 * @author     Gerrit Uitslag <klapinklapin@gmail.com>
6 */
7
8/**
9 * Show book bar and pagetool button at a wiki page
10 */
11class action_plugin_bookcreator_pagetools extends DokuWiki_Action_Plugin {
12
13    /**
14     * Constructor
15     */
16    function __construct() {
17//        $this->setupLocale();    //TODO required?
18    }
19
20    /**
21     * Registers a callback function for a given event
22     *
23     * @param Doku_Event_Handler $controller
24     */
25    function register(Doku_Event_Handler $controller) {
26        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'bookbar', array());
27        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_extendJSINFO');
28        $controller->register_hook('TPL_ACTION_GET', 'BEFORE', $this, 'allowaddbutton');
29        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton');
30        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array());
31    }
32
33    /**
34     *  Prints html of bookbar (performed before the wikipage content is output)
35     *
36     * @param Doku_Event $event event object by reference
37     * @param array $param empty
38     */
39    public function bookbar(Doku_Event $event, $param) {
40        if($event->data != 'show') return; // nothing to do for us
41
42        if(!$this->isVisible($isbookbar = true)) return;
43
44        /**
45         * Display toolbar
46         */
47        $html = "<div class='bookcreator__bookbar' style='vertical-align:bottom;'>";
48
49        //add page to selection
50        $html .= '<div class="bookcreator__panel" id="bookcreator__add">
51                      <b>' . $this->getLang('toolbar') . '</b><br>
52                      <a class="bookcreator__tglPgSelection bc__bookmarkplus" href="#">
53                       '. inlineSVG(__DIR__ . '/../images/bookmark-plus.svg') . '
54                        &nbsp;' . $this->getLang('addpage') . '
55                    </a>
56                  </div>';
57
58        //remove page to selection
59        $html .= '<div class="bookcreator__panel" id="bookcreator__remove">
60                      <b>' . $this->getLang('toolbar') . '</b><br>
61                      <a class="bookcreator__tglPgSelection bc__bookmarkmin" href="#">
62                      ' . inlineSVG(__DIR__ . '/../images/bookmark-minus.svg') . '
63                      &nbsp;' . $this->getLang('removepage') . '
64                      </a>&nbsp;
65                  </div>';
66
67        //pointer to Book Manager
68        $html .= '<div class="bookcreator__panel" >
69                      <br>
70                      <a href="' . wl($this->getConf('book_page')) . '" class="bc__manager">
71                      ' . inlineSVG(__DIR__ . '/../images/notebook-edit-outline.svg') . '
72                      &nbsp;' . $this->getLang('showbook') . '(<span id="bookcreator__pages">0</span> ' . $this->getLang('pages') . ')
73                      </a>
74                  </div>';
75
76        // pointer to help
77        $html .= '<div class="bookcreator__panel" style="float:right;">
78                      <a href="' . wl($this->getConf('help_page')) . '" class="bc__bookmarkhelp">
79                      ' . inlineSVG(__DIR__ . '/../images/help-circle.svg') . '
80                      &nbsp;' . $this->getLang('help') . '
81                      </a>
82                  </div>';
83
84        $html .= '</div>';
85        echo $html;
86
87    }
88
89    /**
90     * Add additional info to $JSINFO
91     *
92     * @param Doku_Event $event
93     * @param mixed $param not defined
94     */
95    public function _extendJSINFO(Doku_Event $event, $param) {
96        global $JSINFO;
97
98        $JSINFO['bookcreator']['areToolsVisible'] = $this->isVisible();
99        $JSINFO['bookcreator']['showBookbar'] = $this->getConf('toolbar');
100    }
101
102    /**
103     * Accepts the 'addtobook' action, while using the default action link properties.
104     *
105     * @param Doku_Event $event
106     * @param $param
107     */
108    public function allowaddbutton(Doku_Event $event, $param) {
109        if($event->data['type'] != 'plugin_bookcreator_addtobook') {
110            return;
111        }
112
113        $event->preventDefault();
114    }
115
116    /**
117     * Add 'add page'-button to pagetools
118     *
119     * @param Doku_Event $event
120     * @param mixed $param not defined
121     */
122    public function addbutton(Doku_Event $event, $param) {
123        global $lang;
124
125        if($this->hasAccessToBookmanager() && $event->data['view'] == 'main') {
126            //store string in global lang array
127            $jslocal = $this->getLang('js');
128            $lang['btn_plugin_bookcreator_addtobook'] = $jslocal['btn_addtobook'] ;
129
130            $event->data['items'] =
131                array_slice($event->data['items'], 0, -1, true) +
132                array('plugin_bookcreator_addtobook' => tpl_action('plugin_bookcreator_addtobook', true, 'li', true, '<span>', '</span>')) +
133                array_slice($event->data['items'], -1, 1, true);
134        }
135    }
136
137    /**
138     * Add 'export pdf' button to page tools, new SVG based mechanism
139     *
140     * @param Doku_Event $event
141     */
142    public function addsvgbutton(Doku_Event $event) {
143        if($event->data['view'] != 'page') return;
144        array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\bookcreator\MenuItem()]);
145    }
146
147    /**
148     * Check if user should see the tools at a page
149     *
150     * @param bool $isbookbar do additional check for booktool
151     * @return bool
152     */
153    private function isVisible($isbookbar = false) {
154        global $ID;
155
156        // show the bookbar?
157        if($isbookbar && ($this->getConf('toolbar') == "never")) {
158            return false;
159        }
160
161        if(cleanID($this->getConf('book_page')) == $ID) {
162            return false;
163        }
164
165        // has read permissions to bookmanager page?
166        if(!$this->hasAccessToBookmanager()) {
167            return false;
168        }
169
170        // not skip page?
171        $exists = false; //assume that page does not exists
172        $id = $ID;
173        resolve_pageid('', $id, $exists);
174
175        $skipPagesRegexp = join("|", explode(",", preg_quote($this->getConf('skip_ids'))));
176        if(!$exists || ($this->getConf('skip_ids') !== '' && preg_match("/$skipPagesRegexp/i", $ID))) {
177            return false;
178        }
179
180        return true;
181    }
182
183    /**
184     * Check if current user could access the bookmanager page
185     *
186     * @return bool has read access
187     */
188    private function hasAccessToBookmanager() {
189        return auth_quickaclcheck(cleanID($this->getConf('book_page'))) >= AUTH_READ;
190    }
191}
192