]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
Better user interface and feedback messages
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChown.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5  Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * Usage:   <?plugin WikiAdminChown s||=* ?> or called via WikiAdminSelect
26  * @author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  * Requires PHP 4.2 so far.
30  */
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
33
34 class WikiPlugin_WikiAdminChown
35 extends WikiPlugin_WikiAdminSelect
36 {
37     function getName() {
38         return _("WikiAdminChown");
39     }
40
41     function getDescription() {
42         return _("Change owner of selected pages.");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array_merge 
52             (
53              PageList::supportedArgs(),
54              array(
55                    's'          => false,
56                    'user'       => false,
57                    /* Columns to include in listing */
58                    'info'     => 'pagename,owner,mtime',
59                    ));
60     }
61
62     function chownPages(&$dbi, &$request, $pages, $newowner) {
63         $result = HTML::div();
64         $ul = HTML::ul();
65         $count = 0;
66         foreach ($pages as $name) {
67             $page = $dbi->getPage($name);
68             $current = $page->getCurrentRevision();
69             if ( ($owner = $page->getOwner()) and 
70                  $newowner != $owner ) {
71                 if (!mayAccessPage('change', $name)) {
72                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
73                                                   WikiLink($name))));
74                 } else {
75                     $version = $current->getVersion();
76                     $meta = $current->_data;
77                     $text = $current->getPackedContent();
78                     $meta['summary'] = "Change page owner from '".$owner."' to '".$newowner."'";
79                     $meta['is_minor_edit'] = 0;
80                     $meta['author'] =  $request->_user->UserName();
81                     unset($meta['mtime']); // force new date
82                     $page->set('owner', $newowner);
83                     $page->save($text, $version + 1, $meta);
84                     if ($page->get('owner') === $newowner) {
85                         $ul->pushContent(HTML::li(fmt("Change owner of page '%s' to '%s'.",
86                                                       WikiLink($name), WikiLink($newowner))));
87                         $count++;
88                     } else {
89                         $ul->pushContent(HTML::li(fmt("Could not change owner of page '%s' to '%s'.", 
90                                                       WikiLink($name), $newowner)));
91                     }
92                 }
93             }
94         }
95         if ($count) {
96             $dbi->touch();
97             $result->setAttr('class', 'feedback');
98             if ($count == 1) {
99                 $result->pushContent(HTML::p("One page has been permanently changed:"));
100             } else {
101                 $result->pushContent(HTML::p(fmt("%s pages have been permanently changed:", $count)));
102             }
103             $result->pushContent($ul);
104             return $result;
105         } else {
106             $result->setAttr('class', 'error');
107             $result->pushContent(HTML::p("No pages changed."));
108             return $result;
109         }
110     }
111     
112     function run($dbi, $argstr, &$request, $basepage) {
113         if ($request->getArg('action') != 'browse')
114             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
115                 return $this->disabled("(action != 'browse')");
116         
117         $args = $this->getArgs($argstr, $request);
118         $this->_args = $args;
119         if (empty($args['user']))
120             $args['user'] = $request->_user->UserName();
121         /*if (!empty($args['exclude']))
122             $exclude = explodePageList($args['exclude']);
123         else
124         $exclude = false;*/
125         $this->preSelectS($args, $request);
126
127         $p = $request->getArg('p');
128         if (!$p) $p = $this->_list;
129         $post_args = $request->getArg('admin_chown');
130         if (!$request->isPost() and empty($post_args['user']))
131             $post_args['user'] = $args['user'];
132         $next_action = 'select';
133         $pages = array();
134         if ($p && !$request->isPost())
135             $pages = $p;
136         if ($p && $request->isPost() &&
137             !empty($post_args['chown']) && empty($post_args['cancel'])) {
138             // without individual PagePermissions:
139             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
140                 $request->_notAuthorized(WIKIAUTH_ADMIN);
141                 $this->disabled("! user->isAdmin");
142             }
143             // DONE: error message if not allowed.
144             if ($post_args['action'] == 'verify') {
145                 // Real action
146                 return $this->chownPages($dbi, $request, array_keys($p), 
147                                           trim($post_args['user']));
148             }
149             if ($post_args['action'] == 'select') {
150                 if (!empty($post_args['user']))
151                     $next_action = 'verify';
152                 foreach ($p as $name => $c) {
153                     $pages[$name] = 1;
154                 }
155             }
156         }
157         if ($next_action == 'select' and empty($pages)) {
158             // List all pages to select from.
159             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], 
160                                          $args['exclude']);
161         }
162         /* // let the user decide which info
163          if ($next_action == 'verify') {
164             $args['info'] = "checkbox,pagename,owner,mtime";
165         }
166         */
167         if ($next_action == 'select') {
168             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
169         } else {
170             $pagelist = new PageList_Unselectable($args['info'], $args['exclude'], $args);
171         }
172         $pagelist->addPageList($pages);
173
174         $header = HTML::fieldset();
175         if ($next_action == 'verify') {
176             $button_label = _("Yes");
177             $header->pushContent(
178               HTML::p(HTML::strong(
179                 _("Are you sure you want to permanently change the owner of the selected pages?"))));
180             $header = $this->chownForm($header, $post_args);
181         }
182         else {
183             $button_label = _("Change owner of selected pages");
184             $header->pushContent(HTML::legend(_("Select the pages to change the owner")));
185             $header = $this->chownForm($header, $post_args);
186         }
187
188         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
189                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
190         $header->pushContent($buttons);
191
192         return HTML::form(array('action' => $request->getPostURL(),
193                                 'method' => 'post'),
194                           $header,
195                           $pagelist->getContent(),
196                           HiddenInputs($request->getArgs(),
197                                         false,
198                                         array('admin_chown')),
199                           HiddenInputs(array('admin_chown[action]' => $next_action)),
200                           ENABLE_PAGEPERM
201                           ? ''
202                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
203     }
204
205     function chownForm(&$header, $post_args) {
206         $header->pushContent(_("Change owner")." ");
207         $header->pushContent(' '._("to").': ');
208         $header->pushContent(HTML::input(array('name' => 'admin_chown[user]',
209                                                'value' => $post_args['user'],
210                                                'size' => 40)));
211         return $header;
212     }
213 }
214
215 // Local Variables:
216 // mode: php
217 // tab-width: 8
218 // c-basic-offset: 4
219 // c-hanging-comment-ender-p: nil
220 // indent-tabs-mode: nil
221 // End:
222 ?>