]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WatchPage.php
Remove MockRequest
[SourceForge/phpwiki.git] / lib / plugin / WatchPage.php
1 <?php
2
3 /**
4  * Copyright (C) 2006 $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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Plugin to manage notifications e-mails per page. action=WatchPage
26  * mode = add or edit
27  * pagename = pagename to be added
28  *
29  * Prefs are stored in metadata in the current session,
30  *  within the user's home page or in a database.
31  */
32 class WikiPlugin_WatchPage
33     extends WikiPlugin
34 {
35     function getDescription()
36     {
37         return _("Manage notifications e-mails per page.");
38     }
39
40     function getDefaultArguments()
41     {
42         return array('page' => '[pagename]',
43             'mode' => 'add',
44         );
45     }
46
47     function contains($pagelist, $page)
48     {
49         if (!isset($this->_explodePageList))
50             $this->_explodePageList = explodePageList($pagelist);
51         return in_array($page, $this->_explodePageList);
52     }
53
54     // This could be expanded as in Mediawiki to a list of each page with a remove button.
55     function showWatchList($pagelist)
56     {
57         return HTML::strong(HTML::samp(empty($pagelist) ? _("<empty>") : $pagelist));
58     }
59
60     function addpagelist($page, $pagelist)
61     {
62         if (!empty($pagelist)) {
63             if ($this->contains($pagelist, $page))
64                 return "$pagelist";
65             else
66                 return "$pagelist, $page";
67         } else
68             return "$page";
69     }
70
71     function showNotify(&$request, $messages, $page, $pagelist, $verified)
72     {
73         $isNecessary = !$this->contains($pagelist, $page);
74         $form = HTML::form(array('action' => $request->getPostURL(),
75                 'method' => 'post'),
76             HiddenInputs(array('verify' => 1)),
77             HiddenInputs($request->getArgs(), false, array('verify')),
78             $messages,
79             HTML::p(_("Your current watchlist: "), $this->showWatchList($pagelist)));
80         if ($isNecessary) {
81             $form->pushContent(HTML::p(_("New watchlist: "),
82                     $this->showWatchList($this->addpagelist($page, $pagelist))),
83                 HTML::p(sprintf(_("Do you %s want to add this page ā€œ%sā€ to your WatchList?"),
84                     ($verified ? _("really") : ""), $page)),
85                 HTML::p(Button('submit:add', _("Yes")),
86                     HTML::raw('&nbsp;'),
87                     Button('submit:cancel', _("Cancel"))));
88         } else {
89             $form->pushContent(HTML::p(fmt("The page %s is already watched!", $page)),
90                 HTML::p(Button('submit:edit', _("Edit")),
91                     HTML::raw('&nbsp;'),
92                     Button('submit:cancel', _("Cancel"))));
93         }
94         $fieldset = HTML::fieldset(HTML::legend(_("Watch Page")), $form);
95         return $fieldset;
96     }
97
98     /**
99      * @param WikiDB $dbi
100      * @param string $argstr
101      * @param WikiRequest $request
102      * @param string $basepage
103      * @return mixed
104      */
105     function run($dbi, $argstr, &$request, $basepage)
106     {
107         $args = $this->getArgs($argstr, $request);
108         $user =& $request->_user;
109         $userid = $user->UserName();
110         $page = $args['page'];
111         if (!$user->isAuthenticated() or empty($userid)) {
112             // wrong or unauthenticated user
113             if (defined('FUSIONFORGE') && FUSIONFORGE) {
114                 // No login banner for FusionForge
115                 return HTML::div(array('class' => 'error'),
116                     HTML::p(_("You must sign in to watch pages.")));
117             }
118             return $request->_notAuthorized(WIKIAUTH_BOGO);
119         } else {
120             $pref = &$request->_prefs;
121             $messages = "";
122             if (!defined('FUSIONFORGE') || !FUSIONFORGE) {
123                 $email = $pref->get("email");
124                 if (empty($email)) {
125                     return HTML::p(
126                         array('class' => 'error'),
127                         _("ERROR: No e-mail defined! You need to do this in your "),
128                         WikiLink(_("UserPreferences")));
129                 }
130                 $emailVerified = $pref->get("emailVerified");
131                 if (empty($emailVerified)) {
132                     $messages = HTML::div(array('class' => 'mw-warning'),
133                         HTML::p("WARNING! Your email address was not verifed yet!"),
134                         HTML::p("EmailNotifications currently disabled. <TODO>"));
135                 }
136             }
137             $pagelist = $pref->get("notifyPages");
138             if (!$request->isPost()) {
139                 return $this->showNotify($request, $messages, $page, $pagelist, false);
140             } else { // POST
141                 $errmsg = '';
142                 if ($request->getArg('cancel')) {
143                     $request->redirect(WikiURL($request->getArg('pagename'),
144                         array('warningmsg' => _('WatchPage cancelled')),
145                         'absolute_url'));
146                     // noreturn
147                     return '';
148                 }
149                 if ($request->getArg('edit')) {
150                     $request->redirect(WikiURL(_("UserPreferences"), array(), 'absolute_url')); // noreturn
151                     return '';
152                 }
153                 $add = $request->getArg('add');
154                 if ($add and !$request->getArg('verify')) {
155                     return $this->showNotify($request, $messages, $page, $pagelist, true);
156                 } elseif ($add and $request->getArg('verify')) { // this is not executed so far.
157                     // add page to watchlist, verified
158                     $rp = clone($user->getPreferences());
159                     $rp->set('notifyPages', $this->addpagelist($page, $pagelist));
160                     $user->setPreferences($rp);
161                     $request->_setUser($user);
162                     $request->setArg("verify", false);
163                     $request->setArg("add", false);
164                     $errmsg .= _("E-mail notification for the current page successfully stored in your preferences.");
165                     $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
166                     return Template('userprefs', $args);
167                 }
168             }
169         }
170         return '';
171     }
172 }
173
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End: