]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
Use CSS
[SourceForge/phpwiki.git] / lib / plugin / EditMetaData.php
1 <?php
2
3 /**
4  * Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * Plugin EditMetaData
25  *
26  * This plugin shows the current page-level metadata and gives an
27  * entry box for adding a new field or changing an existing one. (A
28  * field can be deleted by specifying a blank value.) Certain fields,
29  * such as 'hits' cannot be changed.
30  *
31  * If there is a reason to do so, I will add support for revision-
32  * level metadata as well.
33  *
34  * Access by restricted to ADMIN_USER
35  *
36  * Written by Michael Van Dam, to test out some ideas about
37  * PagePermissions and PageTypes.
38  *
39  * Rewritten for recursive array support by Reini Urban.
40  */
41
42 require_once 'lib/plugin/DebugBackendInfo.php';
43
44 class WikiPlugin_EditMetaData
45     extends WikiPlugin_DebugBackendInfo
46 {
47     function getDescription()
48     {
49         return sprintf(_("Edit metadata for %s."), '[pagename]');
50     }
51
52     function getDefaultArguments()
53     {
54         return array('page' => '[pagename]');
55     }
56
57     function run($dbi, $argstr, &$request, $basepage)
58     {
59         $this->_args = $this->getArgs($argstr, $request);
60         extract($this->_args);
61         if (!$page)
62             return '';
63
64         $this->hidden_pagemeta = array('_cached_html');
65         $this->readonly_pagemeta = array('hits', 'passwd');
66         $dbi = $request->getDbh();
67         $p = $dbi->getPage($page);
68         $pagemeta = $p->getMetaData();
69         $this->chunk_split = false;
70
71         // Look at arguments to see if submit was entered. If so,
72         // process this request before displaying.
73         //
74         if ($request->isPost()
75             and $request->_user->isAdmin()
76                 and $request->getArg('metaedit')
77         ) {
78             $metafield = trim($request->getArg('metafield'));
79             $metavalue = trim($request->getArg('metavalue'));
80             $meta = $request->getArg('meta');
81             $changed = 0;
82             // meta[__global[_upgrade][name]] => 1030.13
83             foreach ($meta as $key => $val) {
84                 if ($val != $pagemeta[$key]
85                     and !in_array($key, $this->readonly_pagemeta)
86                 ) {
87                     $changed++;
88                     $p->set($key, $val);
89                 }
90             }
91             if ($metafield and !in_array($metafield, $this->readonly_pagemeta)) {
92                 // __global[_upgrade][name] => 1030.13
93                 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
94                     list(, $array_field, $array_key) = $matches;
95                     $array_value = $pagemeta[$array_field];
96                     $array_value[$array_key] = $metavalue;
97                     if ($pagemeta[$array_field] != $array_value) {
98                         $changed++;
99                         $p->set($array_field, $array_value);
100                     }
101                 } elseif ($pagemeta[$metafield] != $metavalue) {
102                     $changed++;
103                     $p->set($metafield, $metavalue);
104                 }
105             }
106             if ($changed) {
107                 $dbi->touch();
108                 $url = $request->getURLtoSelf(false,
109                     array('meta', 'metaedit', 'metafield', 'metavalue'));
110                 $request->redirect($url);
111                 // The rest of the output will not be seen due to the
112                 // redirect.
113                 return '';
114             }
115         }
116
117         // Now we show the meta data and provide entry box for new data.
118         $html = HTML();
119         //$html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:",
120         //                                $page)));
121         //$dl = $this->_display_values('', $pagemeta);
122         //$html->pushContent($dl);
123         if (!$pagemeta) {
124             // FIXME: invalid HTML
125             $html->pushContent(HTML::p(fmt("No metadata for %s", $page)));
126             $table = HTML();
127         } else {
128             $table = HTML::table(array('class' => 'bordered'));
129             $this->_fixupData($pagemeta);
130             $table->pushContent($this->_showhash("MetaData('$page')", $pagemeta));
131         }
132
133         if ($request->_user->isAdmin()) {
134             $action = $request->getPostURL();
135             $hiddenfield = HiddenInputs($request->getArgs());
136             $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving the value-box empty.");
137             $keyfield = HTML::input(array('name' => 'metafield'), '');
138             $valfield = HTML::input(array('name' => 'metavalue'), '');
139             $button = Button('submit:metaedit', _("Submit"), false);
140             $form = HTML::form(array('action' => $action,
141                     'method' => 'post',
142                     'accept-charset' => 'UTF-8'),
143                 $hiddenfield,
144                 // edit existing fields
145                 $table,
146                 // add new ones
147                 $instructions, HTML::br(),
148                 $keyfield, ' => ', $valfield,
149                 HTML::raw('&nbsp;'), $button
150             );
151
152             $html->pushContent($form);
153         } else {
154             $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
155         }
156         return $html;
157     }
158
159     protected function _showvalue($key, $val, $prefix = '')
160     {
161         if (is_array($val) or is_object($val)) return $val;
162         if (in_array($key, $this->hidden_pagemeta)) return '';
163         if ($prefix) {
164             $fullkey = $prefix . '[' . $key . ']';
165             if (substr($fullkey, 0, 1) == '[') {
166                 $meta = "meta" . $fullkey;
167                 $fullkey = preg_replace("/\]\[/", "[", substr($fullkey, 1), 1);
168             } else {
169                 $meta = preg_replace("/^([^\[]+)\[/", "meta[$1][", $fullkey, 1);
170             }
171         } else {
172             $fullkey = $key;
173             $meta = "meta[" . $key . "]";
174         }
175         //$meta = "meta[".$fullkey."]";
176         $arr = array('name' => $meta, 'value' => $val);
177         if (strlen($val) > 20)
178             $arr['size'] = strlen($val);
179         if (in_array($key, $this->readonly_pagemeta)) {
180             $arr['readonly'] = 'readonly';
181             return HTML::input($arr);
182         } else {
183             return HTML(HTML::em($fullkey), HTML::br(),
184                 HTML::input($arr));
185         }
186     }
187 }
188
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End: