]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
dbi->touch
[SourceForge/phpwiki.git] / lib / plugin / EditMetaData.php
1 <?php // -*-php-*-
2 rcs_id('$Id: EditMetaData.php,v 1.11 2004-06-01 16:48:11 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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  * Array support added by ReiniUrban.
40  */
41 class WikiPlugin_EditMetaData
42 extends WikiPlugin
43 {
44     function getName () {
45         return _("EditMetaData");
46     }
47
48     function getDescription () {
49         return sprintf(_("Edit metadata for %s"), '[pagename]');
50     }
51
52     function getVersion() {
53         return preg_replace("/[Revision: $]/", '',
54                             "\$Revision: 1.11 $");
55     }
56
57     // Arguments:
58     //
59     //  page - page whose metadata is editted
60
61
62     function getDefaultArguments() {
63         return array('page'       => '[pagename]'
64                     );
65     }
66
67
68     function run($dbi, $argstr, &$request, $basepage) {
69         $this->_args = $this->getArgs($argstr, $request);
70         extract($this->_args);
71         if (!$page)
72             return '';
73
74         $hidden_pagemeta = array ('_cached_html');
75         $readonly_pagemeta = array ('hits');
76         $dbi = $request->getDbh();
77         $p = $dbi->getPage($page);
78         $pagemeta = $p->getMetaData();
79
80         // Look at arguments to see if submit was entered. If so,
81         // process this request before displaying.
82         //
83         if ($request->isPost() and $request->_user->isAdmin() and $request->getArg('metaedit')) {
84             $metafield = trim($request->getArg('metafield'));
85             $metavalue = trim($request->getArg('metavalue'));
86             if (!in_array($metafield, $readonly_pagemeta)) {
87                 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
88                     list(,$array_field, $array_key) = $matches;
89                     $array_value = $pagemeta[$array_field];
90                     $array_value[$array_key] = $metavalue;
91                     $p->set($array_field, $array_value);
92                 } else {
93                     $p->set($metafield, $metavalue);
94                 }
95             }
96             $dbi->touch();
97             $url = $request->getURLtoSelf(false, 
98                                           array('metaedit','metafield','metavalue'));
99             $request->redirect($url);
100             // The rest of the output will not be seen due to the
101             // redirect.
102
103         }
104
105         // Now we show the meta data and provide entry box for new data.
106
107         $html = HTML();
108
109         $html->pushContent(fmt("Existing page-level metadata for %s:",
110                                $page));
111         $dl = HTML::dl();
112         foreach ($pagemeta as $key => $val) {
113             if (is_string($val) and (substr($val,0,2) == 'a:')) {
114                 $dl->pushContent(HTML::dt("\n$key => $val\n",
115                                           $dl1 = HTML::dl()));
116                 foreach (unserialize($val) as $akey => $aval) {
117                     $dl1->pushContent(HTML::dt(HTML::strong("$key" . '['
118                                                             . $akey
119                                                             . "] => $aval\n"))
120                                       );
121                 }
122                 $dl->pushContent($dl1);
123             } elseif (is_array($val)) {
124                 $dl->pushContent(HTML::dt("\n$key:\n", $dl1 = HTML::dl()));
125                 foreach ($val as $akey => $aval) {
126                     $dl1->pushContent(HTML::dt(HTML::strong("$key" . '['
127                                                             . $akey
128                                                             . "] => $aval\n"))
129                                       );
130                 }
131                 $dl->pushContent($dl1);
132             } elseif (in_array($key,$hidden_pagemeta)) {
133                 ;
134             } elseif (in_array($key,$readonly_pagemeta)) {
135                 $dl->pushContent(HTML::dt(array('style' => 'background: #dddddd'),
136                                           "$key => $val\n"));
137             } else {
138                 $dl->pushContent(HTML::dt(HTML::strong("$key => $val\n")));
139             }
140         }
141         $html->pushContent($dl);
142
143         if ($request->_user->isAdmin()) {
144             $action = $request->getPostURL();
145             $hiddenfield = HiddenInputs($request->getArgs());
146             $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving the value-box empty.");
147             $keyfield = HTML::input(array('name' => 'metafield'), '');
148             $valfield = HTML::input(array('name' => 'metavalue'), '');
149             $button = Button('submit:metaedit', _("Submit"), false);
150             $form = HTML::form(array('action' => $action,
151                                      'method' => 'post',
152                                      'accept-charset' => $GLOBALS['charset']),
153                                $hiddenfield,
154                                $instructions, HTML::br(),
155                                $keyfield, ' => ', $valfield,
156                                HTML::raw('&nbsp;'), $button
157                                );
158
159             $html->pushContent(HTML::br(), $form);
160         } else {
161             $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
162         }
163         return $html;
164     }
165 };
166
167 // $Log: not supported by cvs2svn $
168 // Revision 1.10  2004/04/18 01:11:52  rurban
169 // more numeric pagename fixes.
170 // fixed action=upload with merge conflict warnings.
171 // charset changed from constant to global (dynamic utf-8 switching)
172 //
173 // Revision 1.9  2004/02/17 12:11:36  rurban
174 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
175 //
176 // Revision 1.8  2003/11/27 17:05:41  carstenklapp
177 // Update: Omit page cache object ('_cached_html') from metadata display.
178 //
179 // Revision 1.7  2003/11/22 17:41:42  carstenklapp
180 // Minor internal change: Removed redundant call to gettext within
181 // fmt(). (locale make: EditMetaData.php:113: warning: keyword nested in
182 // keyword arg)
183 //
184 // Revision 1.6  2003/02/26 01:56:52  dairiki
185 // Tuning/fixing of POST action URLs and hidden inputs.
186 //
187 // Revision 1.5  2003/02/21 04:17:13  dairiki
188 // Delete now irrelevant comment.
189 //
190 // Revision 1.4  2003/01/18 21:41:01  carstenklapp
191 // Code cleanup:
192 // Reformatting & tabs to spaces;
193 // Added copyleft, getVersion, getDescription, rcs_id.
194 //
195
196 // For emacs users
197 // Local Variables:
198 // mode: php
199 // tab-width: 8
200 // c-basic-offset: 4
201 // c-hanging-comment-ender-p: nil
202 // indent-tabs-mode: nil
203 // End:
204 ?>