From 225be3f496bd4fc8e46d199489439725358e94e5 Mon Sep 17 00:00:00 2001 From: rurban Date: Tue, 15 May 2007 16:32:25 +0000 Subject: [PATCH] Recursive array editing: support global_data git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@5678 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/plugin/EditMetaData.php | 155 ++++++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 60 deletions(-) diff --git a/lib/plugin/EditMetaData.php b/lib/plugin/EditMetaData.php index a1542ee84..50b857258 100644 --- a/lib/plugin/EditMetaData.php +++ b/lib/plugin/EditMetaData.php @@ -1,7 +1,7 @@ '[pagename]' ); } - function run($dbi, $argstr, &$request, $basepage) { $this->_args = $this->getArgs($argstr, $request); extract($this->_args); if (!$page) return ''; - $hidden_pagemeta = array ('_cached_html'); - $readonly_pagemeta = array ('hits'); + $this->hidden_pagemeta = array ('_cached_html'); + $this->readonly_pagemeta = array ('hits', 'passwd'); $dbi = $request->getDbh(); $p = $dbi->getPage($page); $pagemeta = $p->getMetaData(); - + $this->chunk_split = false; + // Look at arguments to see if submit was entered. If so, // process this request before displaying. // - if ($request->isPost() and $request->_user->isAdmin() and $request->getArg('metaedit')) { + if ($request->isPost() + and $request->_user->isAdmin() + and $request->getArg('metaedit')) + { $metafield = trim($request->getArg('metafield')); $metavalue = trim($request->getArg('metavalue')); - if (!in_array($metafield, $readonly_pagemeta)) { + $meta = $request->getArg('meta'); + $changed = 0; + // meta[__global[_upgrade][name]] => 1030.13 + foreach ($meta as $key => $val) { + if ($val != $pagemeta[$key] + and !in_array($key, $this->readonly_pagemeta)) + { + $changed++; + $p->set($key, $val); + } + } + if ($metafield and !in_array($metafield, $this->readonly_pagemeta)) { + // __global[_upgrade][name] => 1030.13 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) { - list(,$array_field, $array_key) = $matches; + list(, $array_field, $array_key) = $matches; $array_value = $pagemeta[$array_field]; $array_value[$array_key] = $metavalue; - $p->set($array_field, $array_value); - } else { + if ($pagemeta[$array_field] != $array_value) { + $changed++; + $p->set($array_field, $array_value); + } + } elseif ($pagemeta[$metafield] != $metavalue) { + $changed++; $p->set($metafield, $metavalue); } } - $dbi->touch(); - $url = $request->getURLtoSelf(false, - array('metaedit','metafield','metavalue')); - $request->redirect($url); - // The rest of the output will not be seen due to the - // redirect. - + if ($changed) { + $dbi->touch(); + $url = $request->getURLtoSelf(false, + array('meta','metaedit','metafield','metavalue')); + $request->redirect($url); + // The rest of the output will not be seen due to the + // redirect. + return; + } } // Now we show the meta data and provide entry box for new data. - $html = HTML(); - $html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:", - $page))); - $dl = HTML::dl(); - foreach ($pagemeta as $key => $val) { - if (is_string($val) and (substr($val,0,2) == 'a:')) { - $dl->pushContent(HTML::dt("\n$key => $val\n", - $dl1 = HTML::dl())); - foreach (unserialize($val) as $akey => $aval) { - $dl1->pushContent(HTML::dt(HTML::strong("$key" . '[' - . $akey - . "] => $aval\n")) - ); - } - $dl->pushContent($dl1); - } elseif (is_array($val)) { - $dl->pushContent(HTML::dt("\n$key:\n", $dl1 = HTML::dl())); - foreach ($val as $akey => $aval) { - $dl1->pushContent(HTML::dt(HTML::strong("$key" . '[' - . $akey - . "] => $aval\n")) - ); - } - $dl->pushContent($dl1); - } elseif (in_array($key,$hidden_pagemeta)) { - ; - } elseif (in_array($key,$readonly_pagemeta)) { - $dl->pushContent(HTML::dt(array('style' => 'background: #dddddd'), - "$key => $val\n")); - } else { - $dl->pushContent(HTML::dt(HTML::strong("$key => $val\n"))); - } + //$html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:", + // $page))); + //$dl = $this->_display_values('', $pagemeta); + //$html->pushContent($dl); + if (!$pagemeta) { + // FIXME: invalid HTML + $html->pushContent(HTML::p(fmt("No metadata for %s", $page))); + $table = HTML(); + } + else { + $table = HTML::table(array('border' => 1, + 'cellpadding' => 2, + 'cellspacing' => 0)); + $this->_fixupData($pagemeta); + $table->pushContent($this->_showhash("MetaData('$page')", $pagemeta)); } - $html->pushContent($dl); if ($request->_user->isAdmin()) { $action = $request->getPostURL(); @@ -150,6 +151,9 @@ extends WikiPlugin 'method' => 'post', 'accept-charset' => $GLOBALS['charset']), $hiddenfield, + // edit existing fields + $table, + // add new ones $instructions, HTML::br(), $keyfield, ' => ', $valfield, HTML::raw(' '), $button @@ -161,9 +165,40 @@ extends WikiPlugin } return $html; } + + function _showvalue ($key, $val, $prefix='') { + if (is_array($val) or is_object($val)) return $val; + if (in_array($key, $this->hidden_pagemeta)) return ''; + if ($prefix) { + $fullkey = $prefix . '[' . $key . ']'; + if (substr($fullkey,0,1) == '[') { + $meta = "meta".$fullkey; + $fullkey = preg_replace("/\]\[/", "[", substr($fullkey, 1), 1); + } else { + $meta = preg_replace("/^([^\[]+)\[/", "meta[$1][", $fullkey, 1); + } + } else { + $fullkey = $key; + $meta = "meta[".$key."]"; + } + //$meta = "meta[".$fullkey."]"; + $arr = array('name' => $meta, 'value' => $val); + if (strlen($val) > 20) + $arr['size'] = strlen($val); + if (in_array($key, $this->readonly_pagemeta)) { + $arr['readonly'] = 'readonly'; + return HTML::input($arr); + } else { + return HTML(HTML::em($fullkey), HTML::br(), + HTML::input($arr)); + } + } }; // $Log: not supported by cvs2svn $ +// Revision 1.12 2007/01/04 16:46:31 rurban +// Make the header a h3 +// // Revision 1.11 2004/06/01 16:48:11 rurban // dbi->touch // security fix to allow post admin only. -- 2.45.0