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