diff --git a/action.php b/action.php --- a/action.php +++ b/action.php @@ -1,108 +1,138 @@ */ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); class action_plugin_metaeditor extends DokuWiki_Action_Plugin { // Load the helper plugin public function action_plugin_metaeditor() { } // Register our hooks function register(&$controller) { $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown'); } function handle_ajax_call_unknown($event, $param) { if($event->data != 'plugin_metaeditor') return; $event->preventDefault(); $event->stopPropagation(); global $INPUT; //global $auth; // FIXME: Add auth check for admin user here $action = trim($INPUT->post->str('q')); $pageid = trim($INPUT->post->str('r')); $key = $INPUT->post->arr('k'); $data = array(); $json = false; switch($action) { case 'getMeta': $data = $this->getMetaForPage($pageid); $json = true; break; case 'getMetaValue': $data = $this->getMetaValueForPage($pageid, $key); break; + case 'setMetaValue': + $oldval = $key['oldval']; + $newval = $key['newval']; + $key = $key['key']; + $data = $this->setMetaValueForPage($pageid, $key, $oldval, $newval); + breka; } //$data = $_SERVER['REMOTE_USER']; if($json) { //json library of DokuWiki require_once DOKU_INC . 'inc/JSON.php'; $json = new JSON(); //set content type header('Content-Type: application/json'); echo $json->encode($data); } else { echo $data; } } + function setMetaValueForPage($pageid, $key, $oldval, $newval) + { + $cache = false; + $meta = p_read_metadata($pageid, $cache); + $m = &$meta; + foreach($key as $k) + $m = &$m[$k]; + if($m == $oldval) + { + $m = $newval; + if(p_save_metadata($pageid, $meta)) + return "Successfully saved: $newval"; + else + return "Error saving value: $newval"; + } + else + { + return "Key has changed in the meantime, expected $oldval but got $m. Nothing was saved!"; + } + + } + function parseMetaTree($meta) { $out = array(); foreach($meta as $k => $v) { $a = array(); $a['text'] = $k; if(is_array($v)) { $a['children'] = $this->parseMetaTree($v); } + else + $a['icon'] = DOKU_URL."/lib/images/page.png"; $out[] = $a; } return $out; } function getMetaForPage($pageid) { $cache = false; $meta = p_read_metadata($pageid, $cache); $out = $this->parseMetaTree($meta); return $out; } function getMetaValueForPage($pageid, $key) { $cache = false; $meta = p_read_metadata($pageid, $cache); foreach($key as $k) { $meta = $meta[$k]; } return $meta; } } diff --git a/admin/editor.php b/admin/editor.php --- a/admin/editor.php +++ b/admin/editor.php @@ -1,98 +1,82 @@ */ // must be run within Dokuwiki if (!defined('DOKU_INC')) die(); require_once(DOKU_PLUGIN.'admin.php'); class admin_plugin_metaeditor_editor extends DokuWiki_Admin_Plugin { /** * Constructor. Load helper plugin */ function admin_plugin_metaeditor_editor(){ } function getMenuSort() { return 501; } function forAdminOnly() { return true; } function getMenuText($language) { return "Simple Persistent Meta Data Editor"; } function handle() { if(!is_array($_REQUEST['d']) || !checkSecurityToken()) return; - - //p_save_metadata($id, $meta); } - /* - function recurseTree($var){ - //$out = '
';
echo '
| ';
echo ''; - echo ' | '; - //echo ' | ';
+ echo ' '; + echo ''; + echo ' | ';
}
}
// vim:ts=4:sw=4:et:enc=utf-8:
diff --git a/script.js b/script.js
--- a/script.js
+++ b/script.js
@@ -1,73 +1,99 @@
/* DOKUWIKI:include_once jstree.js */
jQuery(function()
{
var selectedPageId = null;
var selectedNode = null;
+ var selectedNodeValue = null;
+
+ jQuery('#event_save').click(function() {
+ var newVal = jQuery('#event_value').val()
+ jQuery.post(
+ DOKU_BASE + 'lib/exe/ajax.php',
+ {
+ call: 'plugin_metaeditor',
+ q: 'setMetaValue',
+ r: selectedPageId,
+ k: {
+ key : selectedNode,
+ oldval : selectedNodeValue,
+ newval : newVal
+ }
+ },
+ function(data) {
+ alert(data);
+ }
+ );
+
+ });
jQuery('#metaTree').on('changed.jstree', function (e, data) {
var i, j, r;
if(data.selected.length != 1)
return;
var node = data.instance.get_node(data.selected[0]);
if(!data.instance.is_leaf(node))
return;
r = data.instance.get_path(data.selected[0]);
selectedNode = r;
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_metaeditor',
q: 'getMetaValue',
r: selectedPageId,
k: r
},
function(data) {
- jQuery('#event_result').html(data);
+ jQuery('#event_path').html(selectedNode.join(':'));
+ jQuery('#event_value').val(data);
+ selectedNodeValue = data;
}
);
})
// create the instance
.jstree({
core : {
multiple: false
- }
+ },
+ plugins: ["wholerow"]
});
jQuery('#fileTree').on('changed.jstree', function (e, data) {
var i, j, r;
if(data.selected.length != 1)
return;
var node = data.instance.get_node(data.selected[0]);
if(!data.instance.is_leaf(node))
return;
r = node.text;
selectedPageId = r;
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_metaeditor',
q: 'getMeta',
r: r
},
function(data) {
jQuery('#metaTree').jstree(true).settings.core.data = data;
jQuery('#metaTree').jstree(true).refresh();
}
);
})
// create the instance
.jstree({
core : {
multiple: false
- }
+ },
+ plugins: ["wholerow"]
});
});