]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
Code cleanup:
[SourceForge/phpwiki.git] / lib / plugin / EditMetaData.php
1 <?php // -*-php-*-
2 rcs_id('$Id: EditMetaData.php,v 1.4 2003-01-18 21:41:01 carstenklapp 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.4 $");
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) {
69         $this->_args = $this->getArgs($argstr, $request);
70         extract($this->_args);
71         if (!$page)
72             return '';
73
74         $hidden_pagemeta = array ();
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         // Fixme: The redirect will only work if the output is
84         // buffered.
85
86         if ($request->getArg('metaedit')) {
87
88             $metafield = trim($request->getArg('metafield'));
89             $metavalue = trim($request->getArg('metavalue'));
90
91             if (!in_array($metafield, $readonly_pagemeta)) {
92                 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
93                     list(,$array_field, $array_key) = $matches;
94                     $array_value = $pagemeta[$array_field];
95                     $array_value[$array_key] = $metavalue;
96                     $p->set($array_field, $array_value);
97                 } else {
98                     $p->set($metafield, $metavalue);
99                 }
100             }
101
102             $url = $request->getURLtoSelf('', array('metaedit', 'metafield',
103                                                     'metavalue'));
104             $request->redirect($url);
105
106             // The rest of the output will not be seen due to the
107             // redirect.
108
109         }
110
111         // Now we show the meta data and provide entry box for new data.
112
113         $html = HTML();
114
115         $html->pushContent(fmt(_("Existing page-level metadata for %s:"),
116                                $page));
117         $dl = HTML::dl();
118
119         foreach ($pagemeta as $key => $val) {
120             if (is_string($val) and (substr($val,0,2) == 'a:')) {
121                 $dl->pushContent(HTML::dt("\n$key => $val\n",
122                                           $dl1 = HTML::dl()));
123                 foreach (unserialize($val) as $akey => $aval) {
124                     $dl1->pushContent(HTML::dt(HTML::strong("$key" . '['
125                                                             . $akey
126                                                             . "] => $aval\n"))
127                                       );
128                 }
129                 $dl->pushContent($dl1);
130             } elseif (is_array($val)) {
131                 $dl->pushContent(HTML::dt("\n$key:\n", $dl1 = HTML::dl()));
132                 foreach ($val as $akey => $aval) {
133                     $dl1->pushContent(HTML::dt(HTML::strong("$key" . '['
134                                                             . $akey
135                                                             . "] => $aval\n"))
136                                       );
137                 }
138                 $dl->pushContent($dl1);
139             } elseif (in_array($key,$hidden_pagemeta)) {
140                 ;
141             } elseif (in_array($key,$readonly_pagemeta)) {
142                 $dl->pushContent(HTML::dt(array('style' => 'background: #dddddd'),
143                                           "$key => $val\n"));
144             } else {
145                 $dl->pushContent(HTML::dt(HTML::strong("$key => $val\n")));
146             }
147         }
148         $html->pushContent($dl);
149
150         if ($request->_user->isAdmin()) {
151             $action = $request->getURLtoSelf();
152             $hiddenfield = HiddenInputs($request->getArgs());
153             $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving value-box empty.");
154             $keyfield = HTML::input(array('name' => 'metafield'), '');
155             $valfield = HTML::input(array('name' => 'metavalue'), '');
156             $button = Button('submit:metaedit', _("Submit"), false);
157             $form = HTML::form(array('action' => $action,
158                                      'method' => 'post'),
159                                $hiddenfield,
160                                $instructions, HTML::br(),
161                                $keyfield, ' => ', $valfield,
162                                HTML::raw('&nbsp;'), $button
163                                );
164
165             $html->pushContent(HTML::br(), $form);
166         } else {
167             $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
168         }
169         return $html;
170     }
171 };
172
173 // $Log: not supported by cvs2svn $
174
175 // For emacs users
176 // Local Variables:
177 // mode: php
178 // tab-width: 8
179 // c-basic-offset: 4
180 // c-hanging-comment-ender-p: nil
181 // indent-tabs-mode: nil
182 // End:
183 ?>