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