]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
Put buttons at the top so that they are always visible
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChown.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5  Copyright 2008 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 _("Chown 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         $ul = HTML::ul();
64         $count = 0;
65         foreach ($pages as $name) {
66             $page = $dbi->getPage($name);
67             if ( ($owner = $page->getOwner()) and 
68                  $newowner != $owner ) {
69                 if (!mayAccessPage('change', $name)) {
70                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
71                                                   WikiLink($name))));
72                 } else {
73                     $page->set('owner', $newowner);
74                     if ($page->get('owner') === $newowner) {
75                         $ul->pushContent(HTML::li(fmt("Chown page '%s' to '%s'.",
76                                                       WikiLink($name), WikiLink($newowner))));
77                         $count++;
78                     } else {
79                         $ul->pushContent(HTML::li(fmt("Couldn't chown page '%s' to '%s'.", 
80                                                       WikiLink($name), $newowner)));
81                     }
82                 }
83             }
84         }
85         if ($count) {
86             $dbi->touch();
87             return HTML($ul, HTML::p(fmt("%s pages have been permanently changed.",
88                                          $count)));
89         } else {
90             return HTML($ul, HTML::p(fmt("No pages changed.")));
91         }
92     }
93     
94     function run($dbi, $argstr, &$request, $basepage) {
95         if ($request->getArg('action') != 'browse')
96             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
97                 return $this->disabled("(action != 'browse')");
98         
99         $args = $this->getArgs($argstr, $request);
100         $this->_args = $args;
101         if (empty($args['user']))
102             $args['user'] = $request->_user->UserName();
103         /*if (!empty($args['exclude']))
104             $exclude = explodePageList($args['exclude']);
105         else
106         $exclude = false;*/
107         $this->preSelectS($args, $request);
108
109         $p = $request->getArg('p');
110         if (!$p) $p = $this->_list;
111         $post_args = $request->getArg('admin_chown');
112         if (!$request->isPost() and empty($post_args['user']))
113             $post_args['user'] = $args['user'];
114         $next_action = 'select';
115         $pages = array();
116         if ($p && !$request->isPost())
117             $pages = $p;
118         if ($p && $request->isPost() &&
119             !empty($post_args['chown']) && empty($post_args['cancel'])) {
120             // without individual PagePermissions:
121             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
122                 $request->_notAuthorized(WIKIAUTH_ADMIN);
123                 $this->disabled("! user->isAdmin");
124             }
125             // DONE: error message if not allowed.
126             if ($post_args['action'] == 'verify') {
127                 // Real action
128                 return $this->chownPages($dbi, $request, array_keys($p), 
129                                           $post_args['user']);
130             }
131             if ($post_args['action'] == 'select') {
132                 if (!empty($post_args['user']))
133                     $next_action = 'verify';
134                 foreach ($p as $name => $c) {
135                     $pages[$name] = 1;
136                 }
137             }
138         }
139         if ($next_action == 'select' and empty($pages)) {
140             // List all pages to select from.
141             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], 
142                                          $args['exclude']);
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'], $args['exclude'], $args);
150         $pagelist->addPageList($pages);
151
152         $header = HTML::div();
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                           $buttons,
173                           $pagelist->getContent(),
174                           HiddenInputs($request->getArgs(),
175                                         false,
176                                         array('admin_chown')),
177                           HiddenInputs(array('admin_chown[action]' => $next_action)),
178                           ENABLE_PAGEPERM
179                           ? ''
180                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
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 // Local Variables:
194 // mode: php
195 // tab-width: 8
196 // c-basic-offset: 4
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil
199 // End:
200 ?>