]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WatchPage.php
function run: @return mixed
[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
108         $args = $this->getArgs($argstr, $request);
109         if (isa($request, 'MockRequest'))
110             return '';
111         $user =& $request->_user;
112         $userid = $user->UserName();
113         $page = $args['page'];
114         if (!$user->isAuthenticated() or empty($userid)) {
115             // wrong or unauthenticated user
116             if (defined('FUSIONFORGE') and FUSIONFORGE) {
117                 // No login banner for FusionForge
118                 return HTML::div(array('class' => 'error'),
119                     HTML::p(_("You must sign in to watch pages.")));
120             }
121             return $request->_notAuthorized(WIKIAUTH_BOGO);
122         } else {
123             $pref = &$request->_prefs;
124             $messages = "";
125             if (!defined('FUSIONFORGE') or !FUSIONFORGE) {
126                 $email = $pref->get("email");
127                 if (empty($email)) {
128                     return HTML::p(
129                         array('class' => 'error'),
130                         _("ERROR: No e-mail defined! You need to do this in your "),
131                         WikiLink(_("UserPreferences")));
132                 }
133                 $emailVerified = $pref->get("emailVerified");
134                 if (empty($emailVerified)) {
135                     $messages = HTML::div(array('class' => 'mw-warning'),
136                         HTML::p("WARNING! Your email address was not verifed yet!"),
137                         HTML::p("EmailNotifications currently disabled. <TODO>"));
138                 }
139             }
140             $pagelist = $pref->get("notifyPages");
141             if (!$request->isPost()) {
142                 return $this->showNotify($request, $messages, $page, $pagelist, false);
143             } else { // POST
144                 $errmsg = '';
145                 if ($request->getArg('cancel')) {
146                     $request->redirect(WikiURL($request->getArg('pagename'),
147                         array('warningmsg' => _('WatchPage cancelled')),
148                         'absolute_url'));
149                     // noreturn
150                     return '';
151                 }
152                 if ($request->getArg('edit')) {
153                     $request->redirect(WikiURL(_("UserPreferences"), array(), 'absolute_url')); // noreturn
154                     return '';
155                 }
156                 $add = $request->getArg('add');
157                 if ($add and !$request->getArg('verify')) {
158                     return $this->showNotify($request, $messages, $page, $pagelist, true);
159                 } elseif ($add and $request->getArg('verify')) { // this is not executed so far.
160                     // add page to watchlist, verified
161                     $rp = clone($user->getPreferences());
162                     $rp->set('notifyPages', $this->addpagelist($page, $pagelist));
163                     $user->setPreferences($rp);
164                     $request->_setUser($user);
165                     $request->setArg("verify", false);
166                     $request->setArg("add", false);
167                     $errmsg .= _("E-mail notification for the current page successfully stored in your preferences.");
168                     $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
169                     return Template('userprefs', $args);
170                 }
171             }
172         }
173         return '';
174     }
175 }
176
177 // Local Variables:
178 // mode: php
179 // tab-width: 8
180 // c-basic-offset: 4
181 // c-hanging-comment-ender-p: nil
182 // indent-tabs-mode: nil
183 // End: