Hi,
i extended the plugin bureaucracy with an automatic generated pagename based on a defined template.
I implemented the functionality so that it works but there are still some issues (see below).
New
autoinc.php in
lib/plugins/bureaucracy/fields/
<?php
/* Automatic incremented ID based on a prefix and a @@ as a pattern for
* an automatic increased number.
*
* e.g.: neu.txt
* <form>
* Action template template:bug: bugs :
* Thanks "For submitting the bug"
* Textbox "Description" "/^[^\/:]+$/"
* autoid "BUG-ID" "=BUG-@@" "/^[^\/:]+$/" @
* Submit "Send"
* </form>
*
* with template: start.txt
* ====== @@BUG-ID@@ ======
* @@Description@@
*
* The autoiid field BUG-ID contains a template BUG-@@.
* 'BUG-' is used as prefix for all new pages.
* '@@' is replaced with an incremental number based on existing namespace/directory names.
* e.g. BUG-1, BUG-2, BUG-15 esxist then a new page gets BUG-16 as pagename.
*
*/
class syntax_plugin_bureaucracy_field_autoid extends syntax_plugin_bureaucracy_field {
function render($params, $form) {
$this->_handlePreload();
$form->addHidden($params['name'], $this->getParam('value') . '');
return;
}
function handle_post($value) {
if (is_null($value))
return true;
// Save pattern
$this->opt['autoidpattern'] = $value;
// Set value first to ectract the prefix
$result = $this->setVal($value);
// Extract prefix and automatic incremented pagename
$this->getParam('pagename');
return $result;
}
function getParam($name) {
if (!isset($this->opt[$name]) || $name === 'value' && $this->hidden) {
return null;
}
if ($name === 'pagename') {
// Pattern e.g. BUG-@@
$value = $this->getParam('value');
if (is_null($value)) {
return null;
}
// Extract prefix 'BUG-'
$prefix = strstr($this->opt['autoidpattern'], "@@", true);
// Get all directories starting with the prefix in the current namespace
global $ID;
global $conf;
$ns = cleanID(getNS($ID));
$dir = utf8_encodeFN(str_replace(':', '/', $ns));
$data = array();
$opts2=array();
$opts2['listdirs'] = true;
$opts2['listfiles'] = false;
$opts2['depth'] = 0;
$opts2['dirmatch'] = "\/" . strtolower($prefix);
search($data, $conf['datadir'] . '/' . $dir, 'search_universal', $opts2, '', 0);
// Get highest ID number of found directories
$maxid = 1;
foreach($data as $i){
$current = substr($i['id'], strlen($prefix));
if (!is_null($current) && is_numeric($current))
$maxid = max(array($maxid, $current));
}
// Construct pagename
$value = $prefix . ($maxid + 1);
// Set value to be replaced in template
$this->setVal($value);
if (is_null($value)) {
return null;
}
global $conf;
if($conf['useslash']) $value = str_replace('/',' ',$value);
return str_replace(':',' ',$value);
}
return $this->opt[$name];
}
}
I have some open questions:
1. Is the above code usable and ok? I have no experience with dokuwiki and only limited knowledge with php.
2. How do i disable a resending of the form data on a refresh?
Currently every refresh creates a new page.
Redirecting is not possible with header(), because the headers were already sent and a javascript version is dangerous because javascript must be enabled/allowed.