]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WatchPage.php
Activated Revision substitution for Subversion
[SourceForge/phpwiki.git] / lib / plugin / WatchPage.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  Copyright (C) 2006 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Plugin to manage notifications emails per page. action=WatchPage
25  * mode = add or edit
26  * pagename = pagename to be added
27  *
28  * Prefs are stored in metadata in the current session, 
29  *  within the user's home page or in a database.
30  */
31 class WikiPlugin_WatchPage
32 extends WikiPlugin
33 {
34     function getName () {
35         return _("WatchPage");
36     }
37
38     function getVersion() {
39         return preg_replace("/[Revision: $]/", '',
40                             "\$Revision$");
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
73             (array('action' => $request->getPostURL(),
74                    'method' => 'post'),
75              HiddenInputs(array('verify' => 1)),
76              HiddenInputs($request->getArgs(),false,array('verify')),
77              $messages,
78              HTML::p(_("Your current watchlist: "), $this->showWatchList($pagelist)),
79              HTML::p(_("New watchlist: "), 
80                      $this->showWatchList($this->addpagelist($page, $pagelist))));
81         if ($isNecessary) {
82             $form->pushContent(HTML::p(sprintf(_("Do you %s want to add this page \"%s\" to your WatchList?"), 
83                                                ($verified ? _("really") : ""), $page)),
84                                HTML::p(Button('submit:add', _("Yes")),
85                                        HTML::Raw('&nbsp;'),
86                                        Button('submit:cancel', _("Cancel"))));
87         } else {
88             $form->pushContent(HTML::p(fmt("The page %s is already watched!", $page)),
89                                HTML::p(Button('submit:edit', _("Edit")),
90                                        HTML::Raw('&nbsp;'),
91                                        Button('submit:cancel', _("Cancel"))));
92         }
93         return $form;
94     }
95
96     function run($dbi, $argstr, &$request, $basepage) {
97         $args = $this->getArgs($argstr, $request);
98         if (isa($request,'MockRequest'))
99             return '';
100         $user =& $request->_user;
101         $userid = $user->UserName();
102         $page = $args['page'];
103         if (!$user->isAuthenticated() or empty($userid)) {
104             // wrong or unauthenticated user
105             return $request->_notAuthorized(WIKIAUTH_BOGO);
106             //return $user->PrintLoginForm ($request, $args, false, false);
107         } else {
108             $pref = &$request->_prefs;
109             $messages = "";
110             $email = $pref->get("email");
111             if (empty($email))
112                 return HTML::div(
113                         array('class' => 'errors'),
114                         _("ERROR: No email defined! You need to do this in your "), 
115                         WikiLink(_("UserPreferences")));
116                 
117             $emailVerified = $pref->get("emailVerified");
118             if (empty($emailVerified))
119                 $messages = HTML::div(array('class' => 'mw-warning'),
120                                       HTML::p("WARNING! Your email address was not verifed yet!"),
121                                       HTML::p("EmailNotifications currently disabled. <TODO>"));
122             $pagelist = $pref->get("notifyPages");
123             if (! $request->isPost() ) {
124                 return $this->showNotify($request, $messages, $page, $pagelist, false);
125             } else { // POST
126                 $errmsg = '';
127                 if ($request->getArg('cancel')) {
128                     $request->redirect(WikiURL($request->getArg('pagename'), 
129                                                false, 'absolute_url')); // noreturn
130                     return;
131                 }
132                 if ($request->getArg('edit')) {
133                     $request->redirect(WikiURL(_("UserPreferences"), 
134                                                false, 'absolute_url')); // noreturn
135                     return;
136                 }
137                 $add = $request->getArg('add');
138                 if ($add and !$request->getArg('verify')) {
139                     return $this->showNotify($request, $messages, $page, $pagelist, true); 
140                 }
141                 elseif ($add and $request->getArg('verify')) { // this is not executed so far.
142                     // add page to watchlist, verified
143                     $pref = &$request->_prefs;
144                     $pref->set('notifyPages', $this->addpagelist($page, $pagelist));
145                     $user->setPreferences($pref);
146                     $request->_setUser($user);
147                     $request->setArg("verify",false);
148                     $request->setArg("add",false);
149                     $alert = new Alert(
150                      _("Message"),
151                      _("E-Mail Notification for the current page successfully stored in your preferences."));
152                     $alert->show();
153                     return;
154                 }
155             }
156         }
157     }
158 };
159
160 // $Log: not supported by cvs2svn $
161 // Revision 1.1  2006/12/22 01:28:23  rurban
162 // new plugin similar to mediawiki
163 //
164
165 // For emacs users
166 // Local Variables:
167 // mode: php
168 // tab-width: 8
169 // c-basic-offset: 4
170 // c-hanging-comment-ender-p: nil
171 // indent-tabs-mode: nil
172 // End:
173 ?>