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
8use dokuwiki\plugin\bookcreator\MenuItem;
9
10/**
11 * Show book bar and pagetool button at a wiki page
12 */
13class action_plugin_bookcreator_pagetools extends DokuWiki_Action_Plugin {
14
15    /**
16     * Registers a callback function for a given event
17     *
18     * @param Doku_Event_Handler $controller
19     */
20    function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'bookbar');
22        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_extendJSINFO');
23        $controller->register_hook('TPL_ACTION_GET', 'BEFORE', $this, 'allowaddbutton');
24        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton');
25    }
26
27    /**
28     *  Prints html of bookbar (performed before the wikipage content is output)
29     *
30     * @param Doku_Event $event event object by reference
31     */
32    public function bookbar(Doku_Event $event) {
33        if($event->data != 'show') return; // nothing to do for us
34
35        if(!$this->isVisible(true)) return;
36
37        /**
38         * Display toolbar
39         */
40        $html = "<div class='bookcreator__bookbar' style='vertical-align:bottom;'>";
41
42        //add page to selection
43        $html .= '<div class="bookcreator__panel" id="bookcreator__add">
44                      <b>' . $this->getLang('toolbar') . '</b><br>
45                      <a class="bookcreator__tglPgSelection bc__bookmarkplus" href="#">
46                       '. inlineSVG(__DIR__ . '/../images/bookmark-plus.svg') . '
47                        &nbsp;' . $this->getLang('addpage') . '
48                    </a>
49                  </div>';
50
51        //remove page to selection
52        $html .= '<div class="bookcreator__panel" id="bookcreator__remove">
53                      <b>' . $this->getLang('toolbar') . '</b><br>
54                      <a class="bookcreator__tglPgSelection bc__bookmarkmin" href="#">
55                      ' . inlineSVG(__DIR__ . '/../images/bookmark-minus.svg') . '
56                      &nbsp;' . $this->getLang('removepage') . '
57                      </a>&nbsp;
58                  </div>';
59
60        //pointer to Book Manager
61        $html .= '<div class="bookcreator__panel" >
62                      <br>
63                      <a href="' . wl($this->getConf('book_page')) . '" class="bc__manager">
64                      ' . inlineSVG(__DIR__ . '/../images/notebook-edit-outline.svg') . '
65                      &nbsp;' . $this->getLang('showbook') . '(<span id="bookcreator__pages">0</span> ' . $this->getLang('pages') . ')
66                      </a>
67                  </div>';
68
69        // pointer to help
70        $html .= '<div class="bookcreator__panel" style="float:right;">
71                      <a href="' . wl($this->getConf('help_page')) . '" class="bc__bookmarkhelp">
72                      ' . inlineSVG(__DIR__ . '/../images/help-circle.svg') . '
73                      &nbsp;' . $this->getLang('help') . '
74                      </a>
75                  </div>';
76
77        $html .= '</div>';
78        echo $html;
79
80    }
81
82    /**
83     * Add additional info to $JSINFO
84     *
85     * @param Doku_Event $event
86     */
87    public function _extendJSINFO(Doku_Event $event) {
88        global $JSINFO;
89
90        $JSINFO['bookcreator']['areToolsVisible'] = $this->isVisible();
91        $JSINFO['bookcreator']['showBookbar'] = $this->getConf('toolbar');
92    }
93
94    /**
95     * Accepts the 'addtobook' action, while using the default action link properties.
96     *
97     * @param Doku_Event $event
98     */
99    public function allowaddbutton(Doku_Event $event) {
100        if($event->data['type'] != 'plugin_bookcreator_addtobook') {
101            return;
102        }
103
104        $event->preventDefault();
105    }
106
107    /**
108     * Add 'export pdf' button to page tools, new SVG based mechanism
109     *
110     * @param Doku_Event $event
111     */
112    public function addsvgbutton(Doku_Event $event) {
113        if($event->data['view'] != 'page') return;
114        array_splice($event->data['items'], -1, 0, [new MenuItem()]);
115    }
116
117    /**
118     * Check if user should see the tools at a page
119     *
120     * @param bool $isbookbar do additional check for booktool
121     * @return bool
122     */
123    private function isVisible($isbookbar = false) {
124        global $ID;
125
126        // show the bookbar?
127        if($isbookbar && ($this->getConf('toolbar') == "never")) {
128            return false;
129        }
130
131        if(cleanID($this->getConf('book_page')) == $ID) {
132            return false;
133        }
134
135        // has read permissions to bookmanager page?
136        if(!$this->hasAccessToBookmanager()) {
137            return false;
138        }
139
140        // not skip page?
141        $exists = page_exists($ID);
142
143        $skipPagesRegexp = join("|", explode(",", preg_quote($this->getConf('skip_ids'))));
144        if(!$exists || ($this->getConf('skip_ids') !== '' && preg_match("/$skipPagesRegexp/i", $ID))) {
145            return false;
146        }
147
148        return true;
149    }
150
151    /**
152     * Check if current user could access the bookmanager page
153     *
154     * @return bool has read access
155     */
156    private function hasAccessToBookmanager() {
157        return auth_quickaclcheck(cleanID($this->getConf('book_page'))) >= AUTH_READ;
158    }
159}
160