]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WatchPage.php
No alert for Gforge
[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 getVersion() {
40         return preg_replace("/[Revision: $]/", '',
41                             "\$Revision$");
42     }
43
44     function getDefaultArguments() {
45         return array('page' => '[pagename]',
46                      'mode'   => 'add',
47                      );
48     }
49
50     function contains($pagelist, $page) {
51         if (!isset($this->_explodePageList))
52             $this->_explodePageList = explodePageList($pagelist);
53         return in_array($page, $this->_explodePageList);
54     }
55
56     // This could be expanded as in Mediawiki to a list of each page with a remove button.
57     function showWatchList($pagelist) {
58         return HTML::strong(HTML::tt(empty($pagelist) ? _("<empty>") : $pagelist));
59     }
60
61     function addpagelist($page, $pagelist) {
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         $isNecessary = ! $this->contains($pagelist, $page);
73         $form = HTML::form
74             (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              HTML::p(_("New watchlist: "), 
81                      $this->showWatchList($this->addpagelist($page, $pagelist))));
82         if ($isNecessary) {
83             $form->pushContent(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         return $form;
95     }
96
97     function run($dbi, $argstr, &$request, $basepage) {
98         global $WikiTheme;
99
100         $args = $this->getArgs($argstr, $request);
101         if (isa($request,'MockRequest'))
102             return '';
103         $user =& $request->_user;
104         $userid = $user->UserName();
105         $page = $args['page'];
106         if (!$user->isAuthenticated() or empty($userid)) {
107             // wrong or unauthenticated user
108             return $request->_notAuthorized(WIKIAUTH_BOGO);
109             //return $user->PrintLoginForm ($request, $args, false, false);
110         } else {
111             $pref = &$request->_prefs;
112             $messages = "";
113             $email = $pref->get("email");
114             if (empty($email))
115                 return HTML::div(
116                         array('class' => 'errors'),
117                         _("ERROR: No email defined! You need to do this in your "), 
118                         WikiLink(_("UserPreferences")));
119                 
120             $emailVerified = $pref->get("emailVerified");
121             if (empty($emailVerified))
122                 $messages = HTML::div(array('class' => 'mw-warning'),
123                                       HTML::p("WARNING! Your email address was not verifed yet!"),
124                                       HTML::p("EmailNotifications currently disabled. <TODO>"));
125             $pagelist = $pref->get("notifyPages");
126             if (! $request->isPost() ) {
127                 return $this->showNotify($request, $messages, $page, $pagelist, false);
128             } else { // POST
129                 $errmsg = '';
130                 if ($request->getArg('cancel')) {
131                     $request->redirect(WikiURL($request->getArg('pagename'), 
132                                                false, 'absolute_url')); // noreturn
133                     return;
134                 }
135                 if ($request->getArg('edit')) {
136                     $request->redirect(WikiURL(_("UserPreferences"), 
137                                                false, 'absolute_url')); // noreturn
138                     return;
139                 }
140                 $add = $request->getArg('add');
141                 if ($add and !$request->getArg('verify')) {
142                     return $this->showNotify($request, $messages, $page, $pagelist, true); 
143                 }
144                 elseif ($add and $request->getArg('verify')) { // this is not executed so far.
145                     // add page to watchlist, verified
146                     $pref = &$request->_prefs;
147                     $pref->set('notifyPages', $this->addpagelist($page, $pagelist));
148                     $user->setPreferences($pref);
149                     $request->_setUser($user);
150                     $request->setArg("verify",false);
151                     $request->setArg("add",false);
152                     // No alert for Gforge
153                     if (isa($WikiTheme, 'WikiTheme_gforge')) {
154                         return;
155                     }
156                     $alert = new Alert(
157                      _("Message"),
158                      _("E-Mail Notification for the current page successfully stored in your preferences."));
159                     $alert->show();
160                     return;
161                 }
162             }
163         }
164     }
165 };
166
167 // For emacs users
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:
175 ?>