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