1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Pierre Spring <pierre.spring@liip.ch>
5 */
6
7// must be run within Dokuwiki
8if (!defined('DOKU_INC')) die();
9
10class helper_plugin_npd extends DokuWiki_Plugin
11{
12
13    function getInfo()
14    {
15        return confToHash(dirname(__FILE__).'/plugin.info.txt');
16    }
17
18    function getMethods()
19    {
20        $result = array();
21        $result[] = array(
22            'name'   => 'html_new_page_button',
23            'desc'   => 'include a html button',
24            'params' => array(),
25            'return' => array()
26        );
27        return $result;
28    }
29
30    /**
31     * Create the HTML for the new page button
32     *
33     * If $return is set to FALSE, will directly echo the HTML
34     *
35     * @param bool $return
36     * @return string
37     */
38    public function html_new_page_button($return = false)
39    {
40        $ret = '';
41
42        global $conf;
43        global $ID;
44
45        // Don't show the button if the user doesn't have edit permissions
46        if(auth_quickaclcheck($ID) < AUTH_EDIT) {
47            return '';
48        }
49
50        $label = $this->getLang('btn_create_new_page');
51        if (!$label) {
52            // Translation not found
53            $label = 'Create New Page';
54        }
55        $tip = htmlspecialchars($label);
56
57        // Filter id (without urlencoding)
58        $id = idfilter($ID, false);
59
60        // Make nice URLs even for buttons
61        if($conf['userewrite'] == 2){
62            $script = DOKU_BASE.DOKU_SCRIPT.'/'.$id . "?";
63        }elseif($conf['userewrite']){
64            $script = DOKU_BASE.$id."?";
65        }else{
66            $script = DOKU_BASE.DOKU_SCRIPT . "?";
67            $params['id'] = $id;
68        }
69        $params['idx'] = ":" . getNS($ID);
70        $params['npd'] = 1;
71
72        $url = $script;
73
74        if(is_array($params)){
75            reset($params);
76            while (list($key, $val) = each($params)) {
77                $url .= $key.'=';
78                $url .= htmlspecialchars($val.'&');
79            }
80        }
81
82        $link_type = $this->getConf('link_type');
83
84        switch ($link_type) {
85	        case 'link':
86	            $ret .= '<a rel="nofollow" href="'.$url.'" style="display:none;" id="npd__create_button" class="action npd">'.$label.'</a>';
87	            break;
88	        case 'button':
89	        default:
90	            $ret .= '<form class="button" action="'.$url.'" id="npd__create_form"><div class="no">';
91	            $ret .= '<input id="npd__create_button" type="submit" value="'.htmlspecialchars($label).'" class="button" ';
92	            $ret .= 'title="'.$tip.'" ';
93	            // the button will be enabled by js, as it does not
94	            // make any sense in a browser without js ;)
95	            $ret .= 'style="display: none;" ';
96	            $ret .= '/>';
97	            $ret .= '</div>';
98	            $ret .= '</form>';
99        }
100
101        if(!$return) echo $ret;
102        return $ret;
103    }
104}
105
106