]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
Remove useless semicolon
[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 MichaelVanDam, to test out some ideas about
37  * PagePermissions and PageTypes.
38  *
39  * Rewritten for recursive array support by ReiniUrban.
40  */
41
42 require_once 'lib/plugin/_BackendInfo.php';
43
44 class WikiPlugin_EditMetaData
45     extends WikiPlugin__BackendInfo
46 {
47     function getName()
48     {
49         return _("EditMetaData");
50     }
51
52     function getDescription()
53     {
54         return sprintf(_("Edit metadata for %s"), '[pagename]');
55     }
56
57     function getDefaultArguments()
58     {
59         return array('page' => '[pagename]'
60         );
61     }
62
63     function run($dbi, $argstr, &$request, $basepage)
64     {
65         $this->_args = $this->getArgs($argstr, $request);
66         extract($this->_args);
67         if (!$page)
68             return '';
69
70         $this->hidden_pagemeta = array('_cached_html');
71         $this->readonly_pagemeta = array('hits', 'passwd');
72         $dbi = $request->getDbh();
73         $p = $dbi->getPage($page);
74         $pagemeta = $p->getMetaData();
75         $this->chunk_split = false;
76
77         // Look at arguments to see if submit was entered. If so,
78         // process this request before displaying.
79         //
80         if ($request->isPost()
81             and $request->_user->isAdmin()
82                 and $request->getArg('metaedit')
83         ) {
84             $metafield = trim($request->getArg('metafield'));
85             $metavalue = trim($request->getArg('metavalue'));
86             $meta = $request->getArg('meta');
87             $changed = 0;
88             // meta[__global[_upgrade][name]] => 1030.13
89             foreach ($meta as $key => $val) {
90                 if ($val != $pagemeta[$key]
91                     and !in_array($key, $this->readonly_pagemeta)
92                 ) {
93                     $changed++;
94                     $p->set($key, $val);
95                 }
96             }
97             if ($metafield and !in_array($metafield, $this->readonly_pagemeta)) {
98                 // __global[_upgrade][name] => 1030.13
99                 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
100                     list(, $array_field, $array_key) = $matches;
101                     $array_value = $pagemeta[$array_field];
102                     $array_value[$array_key] = $metavalue;
103                     if ($pagemeta[$array_field] != $array_value) {
104                         $changed++;
105                         $p->set($array_field, $array_value);
106                     }
107                 } elseif ($pagemeta[$metafield] != $metavalue) {
108                     $changed++;
109                     $p->set($metafield, $metavalue);
110                 }
111             }
112             if ($changed) {
113                 $dbi->touch();
114                 $url = $request->getURLtoSelf(false,
115                     array('meta', 'metaedit', 'metafield', 'metavalue'));
116                 $request->redirect($url);
117                 // The rest of the output will not be seen due to the
118                 // redirect.
119                 return '';
120             }
121         }
122
123         // Now we show the meta data and provide entry box for new data.
124         $html = HTML();
125         //$html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:",
126         //                                $page)));
127         //$dl = $this->_display_values('', $pagemeta);
128         //$html->pushContent($dl);
129         if (!$pagemeta) {
130             // FIXME: invalid HTML
131             $html->pushContent(HTML::p(fmt("No metadata for %s", $page)));
132             $table = HTML();
133         } else {
134             $table = HTML::table(array('border' => 1,
135                 'cellpadding' => 2,
136                 'cellspacing' => 0));
137             $this->_fixupData($pagemeta);
138             $table->pushContent($this->_showhash("MetaData('$page')", $pagemeta));
139         }
140
141         if ($request->_user->isAdmin()) {
142             $action = $request->getPostURL();
143             $hiddenfield = HiddenInputs($request->getArgs());
144             $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving the value-box empty.");
145             $keyfield = HTML::input(array('name' => 'metafield'), '');
146             $valfield = HTML::input(array('name' => 'metavalue'), '');
147             $button = Button('submit:metaedit', _("Submit"), false);
148             $form = HTML::form(array('action' => $action,
149                     'method' => 'post',
150                     'accept-charset' => $GLOBALS['charset']),
151                 $hiddenfield,
152                 // edit existing fields
153                 $table,
154                 // add new ones
155                 $instructions, HTML::br(),
156                 $keyfield, ' => ', $valfield,
157                 HTML::raw('&nbsp;'), $button
158             );
159
160             $html->pushContent($form);
161         } else {
162             $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
163         }
164         return $html;
165     }
166
167     function _showvalue($key, $val, $prefix = '')
168     {
169         if (is_array($val) or is_object($val)) return $val;
170         if (in_array($key, $this->hidden_pagemeta)) return '';
171         if ($prefix) {
172             $fullkey = $prefix . '[' . $key . ']';
173             if (substr($fullkey, 0, 1) == '[') {
174                 $meta = "meta" . $fullkey;
175                 $fullkey = preg_replace("/\]\[/", "[", substr($fullkey, 1), 1);
176             } else {
177                 $meta = preg_replace("/^([^\[]+)\[/", "meta[$1][", $fullkey, 1);
178             }
179         } else {
180             $fullkey = $key;
181             $meta = "meta[" . $key . "]";
182         }
183         //$meta = "meta[".$fullkey."]";
184         $arr = array('name' => $meta, 'value' => $val);
185         if (strlen($val) > 20)
186             $arr['size'] = strlen($val);
187         if (in_array($key, $this->readonly_pagemeta)) {
188             $arr['readonly'] = 'readonly';
189             return HTML::input($arr);
190         } else {
191             return HTML(HTML::em($fullkey), HTML::br(),
192                 HTML::input($arr));
193         }
194     }
195 }
196
197
198
199 // Local Variables:
200 // mode: php
201 // tab-width: 8
202 // c-basic-offset: 4
203 // c-hanging-comment-ender-p: nil
204 // indent-tabs-mode: nil
205 // End: