]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
simplified admin action shortcuts
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChown.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminChown.php,v 1.3 2004-06-08 10:05:11 rurban Exp $');
3 /*
4  Copyright 2004 $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  * Usage:   <?plugin WikiAdminChown s||=* ?> or called via WikiAdminSelect
25  * @author:  Reini Urban <rurban@x-ray.at>
26  *
27  * KNOWN ISSUES:
28  * Requires PHP 4.2 so far.
29  */
30 require_once('lib/PageList.php');
31 require_once('lib/plugin/WikiAdminSelect.php');
32
33 class WikiPlugin_WikiAdminChown
34 extends WikiPlugin_WikiAdminSelect
35 {
36     function getName() {
37         return _("WikiAdminChown");
38     }
39
40     function getDescription() {
41         return _("Chown selected pages.");
42     }
43
44     function getVersion() {
45         return preg_replace("/[Revision: $]/", '',
46                             "\$Revision: 1.3 $");
47     }
48
49     function getDefaultArguments() {
50         return array(
51                      's'        => false,
52                      'user'     => false,
53                      /* Pages to exclude in listing */
54                      'exclude'  => '',
55                      /* Columns to include in listing */
56                      'info'     => 'pagename,owner,mtime',
57                      /* How to sort */
58                      'sortby'   => 'pagename',
59                      'limit'    => 0,
60                      );
61     }
62
63     function chownPages(&$dbi, &$request, $pages, $newowner) {
64         $ul = HTML::ul();
65         $count = 0;
66         foreach ($pages as $name) {
67             $page = $dbi->getPage($name);
68             if ( ($owner = $page->getOwner()) and 
69                  $newowner != $owner ) {
70                 if (!mayAccessPage('change', $name)) {
71                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
72                                                   WikiLink($name))));
73                 } else {
74                     $page->set('owner', $newowner);
75                     if ($page->get('owner') === $newowner) {
76                         $ul->pushContent(HTML::li(fmt("Chown page '%s' to '%s'.",
77                                                       WikiLink($name), WikiLink($newowner))));
78                         $count++;
79                     } else {
80                         $ul->pushContent(HTML::li(fmt("Couldn't chown page '%s' to '%s'.", 
81                                                       WikiLink($name), $newowner)));
82                     }
83                 }
84             }
85         }
86         if ($count) {
87             $dbi->touch();
88             return HTML($ul, HTML::p(fmt("%s pages have been permanently changed.",
89                                          $count)));
90         } else {
91             return HTML($ul, HTML::p(fmt("No pages changed.")));
92         }
93     }
94     
95     function run($dbi, $argstr, &$request, $basepage) {
96         if ($request->getArg('action') != 'browse')
97             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
98                 return $this->disabled("(action != 'browse')");
99         
100         $args = $this->getArgs($argstr, $request);
101         $this->_args = $args;
102         if (empty($args['user']))
103             $args['user'] = $request->_user->UserName();
104         if (!empty($args['exclude']))
105             $exclude = explodePageList($args['exclude']);
106         else
107             $exclude = false;
108         $this->preSelectS(&$args, &$request);
109
110         $p = $request->getArg('p');
111         if (!$p) $p = $this->_list;
112         $post_args = $request->getArg('admin_chown');
113         if (!$request->isPost() and empty($post_args['user']))
114             $post_args['user'] = $args['user'];
115         $next_action = 'select';
116         $pages = array();
117         if ($p && !$request->isPost())
118             $pages = $p;
119         if ($p && $request->isPost() &&
120             !empty($post_args['chown']) && empty($post_args['cancel'])) {
121             // without individual PagePermissions:
122             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
123                 $request->_notAuthorized(WIKIAUTH_ADMIN);
124                 $this->disabled("! user->isAdmin");
125             }
126             // DONE: error message if not allowed.
127             if ($post_args['action'] == 'verify') {
128                 // Real action
129                 return $this->chownPages($dbi, $request, array_keys($p), 
130                                           $post_args['user']);
131             }
132             if ($post_args['action'] == 'select') {
133                 if (!empty($post_args['user']))
134                     $next_action = 'verify';
135                 foreach ($p as $name => $c) {
136                     $pages[$name] = 1;
137                 }
138             }
139         }
140         if ($next_action == 'select' and empty($pages)) {
141             // List all pages to select from.
142             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
143         }
144         /* // let the user decide which info
145          if ($next_action == 'verify') {
146             $args['info'] = "checkbox,pagename,owner,mtime";
147         }
148         */
149         $pagelist = new PageList_Selectable($args['info'], $exclude);
150         $pagelist->addPageList($pages);
151
152         $header = HTML::p();
153         if ($next_action == 'verify') {
154             $button_label = _("Yes");
155             $header->pushContent(
156               HTML::p(HTML::strong(
157                 _("Are you sure you want to permanently chown the selected files?"))));
158             $header = $this->chownForm($header, $post_args);
159         }
160         else {
161             $button_label = _("Chown selected pages");
162             $header->pushContent(HTML::p(_("Select the pages to change the owner:")));
163             $header = $this->chownForm($header, $post_args);
164         }
165
166         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
167                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
168
169         return HTML::form(array('action' => $request->getPostURL(),
170                                 'method' => 'post'),
171                           $header,
172                           $pagelist->getContent(),
173                           HiddenInputs($request->getArgs(),
174                                         false,
175                                         array('admin_chown')),
176                           HiddenInputs(array('admin_chown[action]' => $next_action)),
177                           ENABLE_PAGEPERM
178                           ? ''
179                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
180                           $buttons);
181     }
182
183     function chownForm(&$header, $post_args) {
184         $header->pushContent(_("Chown")." ");
185         $header->pushContent(' '._("to").': ');
186         $header->pushContent(HTML::input(array('name' => 'admin_chown[user]',
187                                                'value' => $post_args['user'])));
188         $header->pushContent(HTML::p());
189         return $header;
190     }
191 }
192
193 // $Log: not supported by cvs2svn $
194 // Revision 1.2  2004/06/07 18:59:42  rurban
195 // added Chown link to Owner in statusbar
196 //
197 // Revision 1.1  2004/06/07 17:58:58  rurban
198 // new chown plugin
199 //
200 //
201
202 // Local Variables:
203 // mode: php
204 // tab-width: 8
205 // c-basic-offset: 4
206 // c-hanging-comment-ender-p: nil
207 // indent-tabs-mode: nil
208 // End:
209 ?>