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