xref: /plugin/addnewpage/syntax.php (revision 5ad1ba2306c9b9680ba6a1af8f483fe88b305f2d)
1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'syntax.php');
5
6class syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin {
7    function getInfo(){
8        return array(
9            'author' => 'iDo',
10            'email'  => 'ido@idotech.info',
11            'date'   => '20/12/2006',
12            'name'   => 'addnewpage',
13            'desc'   => 'This add a "new page form" in your page. \\ Syntax : {{NEWPAGE[>namespace]}}  where [>namespace] is optional.',
14            'url'    => 'http://wiki.splitbrain.org/plugin:addnewpage',
15        );
16    }
17
18
19    function getType(){
20        return 'substition';
21    }
22
23    function getSort(){
24        return 199;
25    }
26
27    function connectTo($mode) {
28      $this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage');
29    }
30
31    function handle($match, $state, $pos, &$handler){
32      $ns = substr($match, 10, -2);  // strip markup
33      return array($ns); // use an array here
34	}
35
36
37	function render($mode, &$renderer, $data) {
38    	global $lang;
39		$renderer->info['cache'] = false;
40		$data = $data[0]; // get data back from the array
41
42		if ($mode == 'xhtml') {
43			$cmb=$this->_makecombo($data);
44			if ($cmb==$this->getLang('nooption')) {
45				$renderer->doc .=(!$this->getConf('addpage_hideACL'))?$cmb:'';
46				return true;
47			}
48
49		    $renderer->doc .= '<div class="addnewpage_form" id="addnewpage_form" align="left">';
50		    $renderer->doc .= '<form name="editform" id="editform" method="post" action="" accept-charset="'.$lang['encoding'].'" onsubmit="setName();return true;">';
51		    $renderer->doc .= $cmb;
52		    $renderer->doc .= '<input class="edit" type="text" name="title" id="addnewpage_title" size="20" maxlength="255" tabindex="2" />';
53			$renderer->doc .= '<input type="hidden" name="do" id="do" value="edit" />';
54		    $renderer->doc .= '<input class="button" type="submit" value="'.((@$this->getLang('okbutton'))?$this->getLang('okbutton'):'ok').'" tabindex="3" />';
55		    $renderer->doc .= '</form>';
56		    $renderer->doc .= '</div>';
57
58			return true;
59		}
60		return false;
61	}
62	/**
63	* Parse namespace request
64	*
65	* @author  Samuele Tognini <samuele@cli.di.unipi.it>
66	*/
67	function _parse_ns ($ns) {
68		global $ID;
69		$ns=preg_replace("/^\.(:|$)/",dirname(str_replace(':','/',$ID))."$1",$ns);
70		$ns=str_replace("/",":",$ns);
71		$ns = cleanID($ns);
72		return $ns;
73	}
74	function _makecombo($data) {
75		global $ID;
76
77		$hide=$this->getConf('addpage_hide');
78
79		if (($data != "") && ($hide))
80			return '<input type="hidden" name="np_cat" id="np_cat" value="'. $this->_parse_ns($data) .'"/>';
81
82		$ns=explode(':',$ID);
83		array_pop($ns);
84		$ns=implode(':',$ns);
85
86		$r=$this->_getnslist("");
87
88
89
90		$ret='<select class="edit" id="np_cat" name="np_cat"  tabindex="1">';
91
92		$someopt=false;
93
94		if ($this->getConf('addpage_showroot')) {
95
96			$root_disabled=(auth_quickaclcheck($data.":") < AUTH_CREATE) ?true:false;
97
98
99			if ($data=='') {
100				if (!$root_disabled) {
101					$ret.='<option '.(($ns=='')?'selected="true"':'').' value="">'.((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top').'</option>';
102					$someopt=true;
103					}
104			} else {
105				if (!$root_disabled) {
106					$ret.='<option '.(($ns==$data)?'selected="true"':'').' value="'.$data.'">'.$data.'</option>';
107					$someopt=true;
108				}
109
110			}
111		}
112		foreach ($r as $k => $v) {
113			if ($data != '')
114				if (strpos(":".$v,":".$data.":")===false) continue;
115
116			if(auth_quickaclcheck($v.":") < AUTH_CREATE)continue;
117			$vv=explode(':',$v);
118			$vv=str_repeat('&nbsp;&nbsp;',substr_count($v, ':')).$vv[count($vv)-1];
119			$ret.='<option '.(($ns==$v)?'selected="true"':'').' value="'.$v.'">'.$vv.'</option>';
120			$someopt=true;
121		}
122		$ret.='</select>';
123		if (!$someopt) $ret = $this->getLang('nooption');
124
125		return $ret;
126	}
127	function _getnslist ($tns='') {
128		require_once(DOKU_INC.'inc/search.php');
129		global $conf;
130
131		if ($tns=='')
132			$tns = $conf['datadir'];
133
134		if (!is_dir($tns))
135			$tns  = str_replace(':','/',$tns);
136
137		$data = array();
138
139		$exclude=$this->getConf('addpage_exclude');
140
141		if ($exclude=="")
142			$exclude=array();
143		else
144			$exclude=@explode(';',strtolower($exclude));
145
146		search($data,$tns,'search_index',array('ns' => ''));
147
148		$data2 = array();
149		foreach($data as $k => $v) {
150			if ($v['type']=='d') {
151				if (!in_array(strtolower($v['id']),$exclude)) {
152					array_push($data2,$v['id']);
153					$r=$this->_getnslist($tns.'/'.$v['id']);
154					foreach ($r as $vv) {
155						if (!in_array(strtolower($vv),$exclude))
156							array_push($data2,$v['id'].':'.$vv);
157					}
158				}
159			}
160		}
161		return $data2;
162	}
163}
164?>
165