Skip to content

Commit

Permalink
Item11574: Finish modernization of plugin
Browse files Browse the repository at this point in the history
Popup function now works.
Validation now working.
  • Loading branch information
gac410 committed Apr 13, 2017
1 parent 822707d commit 31fc8bd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 95 deletions.
22 changes: 22 additions & 0 deletions data/System/NotificationPlugin.txt
@@ -1,3 +1,4 @@
%META:TOPICINFO{author="JoeUser" comment="pending" date="1492043321" format="1.1" version="2"}%
---+!! <nop>%TOPIC%
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
Expand Down Expand Up @@ -33,6 +34,27 @@ Default values for all these attributes are "on". You can set it "off" to hide b
* =%<nop>NTF{}%= - displays all four buttons
* =%<nop>NTF{win="off" wn="off"}%= - displays only buttons for topic immediate notification and regular topic notification

---++++ NTF Popup
%STARTSECTION{"notification"}%
<div id='changenotify' title="%MAKETEXT{"Change Notification"}%" class="jqUIDialog { modal:true, resizable:false, draggable:false, width:400, autoCenter:true, cache:false}">
<form method="post" action="%SCRIPTURLPATH{"rest"}%/NotificationPlugin/changenotify">
<input type="hidden" name="popup" value="1" />
<input type="hidden" name="topic" value="%URLPARAM{"notifyweb"}%.%URLPARAM{"notifytopic"}%" />
---++ <nop>%URLPARAM{"notifyweb"}%.%URLPARAM{"notifytopic"}%
---+++ Immediate Notifications
<input type="checkbox" name="TIN" value="ON" %URLPARAM{"TIN"}% >changes to the Topic<br/>
<input type="checkbox" name="WIN" value="ON" %URLPARAM{"WIN"}% >changes to the Web<br/>
---+++ Normal Notifications
<input type="checkbox" name="TN" value="ON" %URLPARAM{"TN"}% >changes to the Topic<br/>
<input type="checkbox" name="WN" value="ON" %URLPARAM{"WN"}% >changes to the web<br/>
<input type="hidden" value="Update" name="action"></form>
<a class="jqUIDialogButton jqUIDialogSubmit jqUIDialogClose {icon:'ui-icon-circle-check'}" >%MAKETEXT{"Change"}%</a>
<a id='cancelLink' class="jqUIDialogButton jqUIDialogClose {icon:'ui-icon-cancel'}">%MAKETEXT{"Cancel"}%</a>
</div>


%ENDSECTION{"notification"}%

---++ Description of <nop>NotifyList topics

There are two types of notifications - immediate and regular.
Expand Down
153 changes: 58 additions & 95 deletions lib/Foswiki/Plugins/NotificationPlugin.pm
Expand Up @@ -8,6 +8,7 @@ our $VERSION = '1.30';
our $RELEASE = '24 Feb 2017';
our $SHORTDESCRIPTION =
'Send fine grained notifications of topics you are interested in';
our $NO_PREFS_IN_TOPIC = 1;

my @sections = (
"(Topic) immediate notifications",
Expand Down Expand Up @@ -40,7 +41,7 @@ sub initPlugin {
Foswiki::Func::registerRESTHandler(
'changenotify', \&restHandler,
authenticate => 1, # Set to 0 if handler should be useable by WikiGuest
validate => 0, # Set to 0 to disable StrikeOne CSRF protection
validate => 1, # Set to 0 to disable StrikeOne CSRF protection
http_allow => 'POST', # Set to 'GET,POST' to allow use HTTP GET and POST
description => 'Modify NotificationPlugin subscriptions'
);
Expand Down Expand Up @@ -78,7 +79,7 @@ sub beforeSaveHandler {
push( @notifyUsers, getUsersToNotify( $_[2], $_[1], 1 ) );
push( @notifyUsers, getUsersToNotify( $_[2], $_[1], 2 ) );

Foswiki::Func::writeDebug("COUNT = $#notifyUsers");
Foswiki::Func::writeDebug("COUNT = $#notifyUsers") if $debug;
my $subject = "Topic $_[2].$_[1] has been changed by $wikiUser.";
my $body =
"Topic "
Expand All @@ -98,7 +99,7 @@ sub getUsers {
foreach my $name (@topics) {
next unless $name =~ /^(.*)NotifyList$/;

Foswiki::Func::writeDebug("NAME = $1");
Foswiki::Func::writeDebug("NAME = $1") if $debug;
$result[ ++$#result ] = $1 if ( $1 ne "" );
}

Expand Down Expand Up @@ -192,9 +193,9 @@ sub getNotificationsOfUser {
sub notifyUsers {
my ( $notifyUsers, $subject, $body ) = @_;

Foswiki::Func::writeDebug("NT = $notifyUsers");
Foswiki::Func::writeDebug( "NT = " . join( ',', $notifyUsers ) ) if $debug;
foreach my $tmp ( @{$notifyUsers} ) {
Foswiki::Func::writeDebug("MAIL SENT TO $tmp ...");
Foswiki::Func::writeDebug("MAIL SENT TO $tmp ...") if $debug;

my $to = getUserEmail($tmp);
my $email = <<"HERE";
Expand All @@ -215,7 +216,7 @@ $body
--------------2D594AE113AD25493C2C7246--
HERE

Foswiki::Func::writeDebug("Sending mail to $tmp ...");
Foswiki::Func::writeDebug("Sending mail to $tmp ...") if $debug;
my $error = Foswiki::Func::sendEmail($email);
if ($error) {
Foswiki::Func::writeDebug("ERROR WHILE SENDING MAIL - $error");
Expand All @@ -237,11 +238,10 @@ sub addItemToNotifyList {
my $who = shift;
my $what = shift;
my $section = shift;
my $meta = shift || "";
my $text = shift || "";
my $meta = shift;
my $text = shift;

#Foswiki::Func::writeDebug( "NTF:addItemToNotifyList: adding '$what' to section $sections[$section]" );
( $meta, $text ) = checkUserNotifyList($who) if ( $text eq "" );
return ( $meta, $text )
if ( isItemInSection( $who, $what, $section, $text ) );
my @items =
Expand Down Expand Up @@ -275,11 +275,10 @@ sub removeItemFromNotifyList {
my $who = shift;
my $what = shift;
my $section = shift;
my $meta = shift || "";
my $text = shift || "";
my $meta = shift;
my $text = shift;

#Foswiki::Func::writeDebug( "NTF:removeItemFromNotifyList: removing '$what' from section $sections[$section]" );
( $meta, $text ) = checkUserNotifyList($who) if ( $text eq "" );
return ( $meta, $text )
if ( !isItemInSection( $who, $what, $section, $text ) );
my @items =
Expand Down Expand Up @@ -315,42 +314,38 @@ sub checkUserNotifyList {

#Foswiki::Func::writeDebug( "NTF:checkUserNotifyList: WHO = $who" );
if (
!Foswiki::Func::topicExists(
Foswiki::Func::topicExists(
$Foswiki::cfg{UsersWebName},
$who . "NotifyList"
)
)
{
( $tmpMeta, $tmpText ) =
Foswiki::Func::readTopic( $Foswiki::cfg{UsersWebName},
"NotificationPluginListTemplate" );
$tmpMeta->put( "TOPICPARENT", { "name" => $who } );
saveUserNotifyList( $who, $tmpMeta, $tmpText );
$who . "NotifyList" );
}
else {

unless ($tmpText) {
( $tmpMeta, $tmpText ) =
Foswiki::Func::readTopic( $Foswiki::cfg{UsersWebName},
$who . "NotifyList" );
"NotificationPluginListTemplate" );
$tmpMeta->put( "TOPICPARENT", { "name" => $who } );
saveUserNotifyList( $who, $tmpMeta, $tmpText );
}

return ( $tmpMeta, $tmpText );
}

sub saveUserNotifyList {
my ( $who, $meta, $text ) = @_;

#Foswiki::Func::writeDebug( "NTF:saveUserNotifyList: Saving Main.".$who."NotifyList topic..." );
#Foswiki::Func::writeDebug( "NTF:saveUserNotifyList: Saving Main.".$who."NotifyList topic...".$text );

my $topicObject = Foswiki::Func::saveTopic(
$Foswiki::cfg{UsersWebName},
$who . "NotifyList",
$meta, $text
);
if ( !$topicObject ) {

#my $url =
# Foswiki::Func::getOopsUrl( $web, $topic, "oopssaveerr", 'save failed' );
#Foswiki::Func::redirectCgiQuery( $query, $url );
}
}

sub isItemInSection {
Expand Down Expand Up @@ -393,25 +388,32 @@ sub showNotifyButtons {
$tnOn = "off"
if ( !isItemInSection( $curWikiName, "$web.$topic", 3 ) );
$wnOn = "off" if ( !isItemInSection( $curWikiName, "$web", 4 ) );
$text .=
"<input onClick='javascript:window.open(\""
. Foswiki::Func::getScriptUrl( $web, $topic, "changenotify" )
. "?popup=on\");' type='button' value='Popup'>&nbsp;"
if ( $popup eq "on" );

if ( $popup eq 'on' ) {
my $tinC = ( $tinOn eq 'on' ) ? 'checked' : '';
my $winC = ( $winOn eq 'on' ) ? 'checked' : '';
my $tnC = ( $tnOn eq 'on' ) ? 'checked' : '';
my $wnC = ( $wnOn eq 'on' ) ? 'checked' : '';
$text .= <<HERE;
%JQREQUIRE{"ui::dialog"}%
<a href="%SCRIPTURLPATH{view}%/%SYSTEMWEB%/NotificationPlugin?skin=text;section=notification;TIN=$tinC;WIN=$winC;WN=$wnC;TN=$tnC;notifyweb=%WEB%;notifytopic=%TOPIC%" alt="Update..." title="Update" class="foswikiButton jqUIDialogLink {cache:false}">Update notifications</a>
HERE
}

$text .= <<"HERE" if $tin eq 'on';
<form id="TIN" action="$restURL" method="post">
<input type="hidden" name="what" value="TIN" />
<input type="hidden" name="topic" value="$web.$topic" />
<input type="hidden" name="action" value="$tmp{$tinOn}" />
<input href="#" type="button" onclick="document.getElementById('TIN').submit();" value="TIN $tinOn" title="Topic immediate notifications! Click to set it $tmp{$tinOn}!" />
<input type="submit" class="foswikiButton" value="TIN $tinOn" title="Topic immediate notifications! Click to set it $tmp{$tinOn}!" />
</form>
HERE
$text .= <<"HERE" if $win eq 'on';
<form id="WIN" action="$restURL" method="post">
<input type="hidden" name="what" value="WIN" />
<input type="hidden" name="topic" value="$web.$topic" />
<input type="hidden" name="action" value="$tmp{$winOn}" />
<input href="#" type="button" onclick="document.getElementById('WIN').submit();" value="WIN $winOn" title="Topic immediate notifications! Click to set it $tmp{$winOn}!" />
<input type="submit" class="foswikiButton" value="WIN $winOn" title="Topic immediate notifications! Click to set it $tmp{$winOn}!" />
</form>
HERE

Expand All @@ -420,7 +422,7 @@ HERE
<input type="hidden" name="what" value="TN" />
<input type="hidden" name="topic" value="$web.$topic" />
<input type="hidden" name="action" value="$tmp{$tnOn}" />
<input href="#" type="button" onclick="document.getElementById('TN').submit();" value="TN $tnOn" title="Topic immediate notifications! Click to set it $tmp{$tnOn}!" />
<input type="submit" class="foswikiButton" value="TN $tnOn" title="Topic immediate notifications! Click to set it $tmp{$tnOn}!" />
</form>
HERE

Expand All @@ -429,7 +431,7 @@ HERE
<input type="hidden" name="what" value="WN" />
<input type="hidden" name="topic" value="$web.$topic" />
<input type="hidden" name="action" value="$tmp{$wnOn}" />
<input href="#" type="button" onclick="document.getElementById('WN').submit();" value="WN $wnOn" title="Topic immediate notifications! Click to set it $tmp{$wnOn}!" />
<input type="submit" class="foswikiButton" value="WN $wnOn" title="Topic immediate notifications! Click to set it $tmp{$wnOn}!" />
</form>
HERE

Expand All @@ -444,106 +446,67 @@ sub restHandler {
my $web = $session->{webName};
my $topic = $session->{topicName};

#
# # Use return to have foswiki manage the output
# return "This is an example of a REST invocation\n\n";
#
# # To completely control the output from the handler:
# $response->headers() - output headers, which must be utf-8 encoded
# $response->body() - output binary data that should not be encoded.
# $response->print() - output unicode text.
# # Note that print() and body() may not be combined. Use one or the other.

my $query = Foswiki::Func::getCgiQuery();
my $wikiName = Foswiki::Func::getWikiName();
my $scriptUrlPath = Foswiki::Func::getScriptUrlPath('view');
my $scriptUrlPath = Foswiki::Func::getScriptUrlPath( $web, $topic, 'view' );

my $action = $query->param("action");
my $what = $query->param("what");

if ( $wikiName ne "WikiGuest" ) {
my ( $meta, $text ) = checkUserNotifyList($wikiName);

if ($action) {
if ($what) {
modify_notification( $web, $topic, $what, $action );
( $meta, $text ) =
modify_notification( $meta, $text, $web, $topic, $what,
$action );
}
else {
# loop thru all possible checkboxes
for (qw(TIN WIN TN WN)) {
modify_notification( $web,
$topic, $_, scalar $query->param($_) );
( $meta, $text ) =
modify_notification( $meta, $text, $web, $topic, $_,
scalar $query->param($_) );
}
}
}
Foswiki::Plugins::NotificationPlugin::saveUserNotifyList( $wikiName,
$meta, $text );

# All work is done; redirect if needed
unless ( scalar $query->param("popup") ) {
Foswiki::Func::writeDebug("URL = $scriptUrlPath/$web/$topic");
Foswiki::Func::redirectCgiQuery( $query,
$scriptUrlPath . "/$web/$topic" );
}
Foswiki::Func::writeDebug("URL = $scriptUrlPath") if $debug;
Foswiki::Func::redirectCgiQuery( $query, $scriptUrlPath );
}

# Fallthru: do something if no Javascript
return draw_checkboxes( $scriptUrlPath, $topic, $web );
}

sub modify_notification {
my ( $webName, $topic, $what, $action ) = @_;
my ( $meta, $text, $webName, $topic, $what, $action ) = @_;
$action ||= '';

Foswiki::Func::writeDebug("ModifyNotiication entered - ($what), ($action)");
Foswiki::Func::writeDebug("ModifyNotiication entered - ($what), ($action)")
if $debug;

my %tmp = ( "TIN" => 0, "WIN" => 1, "TN" => 3, "WN" => 4 );

my $str = "$webName.$topic";
$str = "$webName" if ( $tmp{$what} == 1 || $tmp{$what} == 4 );
Foswiki::Func::writeDebug("WHAT = $what");
Foswiki::Func::writeDebug("STR = $str");
my ( $meta, $text ) = ( "", "" );
Foswiki::Func::writeDebug("WHAT = $what") if $debug;
Foswiki::Func::writeDebug("STR = $str") if $debug;
my $wikiName = Foswiki::Func::getWikiName;

if ( $action eq "ON" ) {
( $meta, $text ) =
Foswiki::Plugins::NotificationPlugin::addItemToNotifyList( $wikiName,
$str, $tmp{$what} );
$str, $tmp{$what}, $meta, $text );
}
else {
( $meta, $text ) =
Foswiki::Plugins::NotificationPlugin::removeItemFromNotifyList(
$wikiName, $str, $tmp{$what} );
$wikiName, $str, $tmp{$what}, $meta, $text );
}
Foswiki::Plugins::NotificationPlugin::saveUserNotifyList( $wikiName, $meta,
$text );
}

sub draw_checkboxes {
my ( $scriptUrlPath, $topic, $webName ) = @_;

my $wikiName = Foswiki::Func::getWikiName();

my $tinOn =
Foswiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
"$webName.$topic", 0 );
my $winOn =
Foswiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
"$webName", 1 );
my $tnOn = Foswiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
"$webName.$topic", 3 );
my $wnOn = Foswiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
"$webName", 4 );
my $action = $scriptUrlPath . "/changenotify/" . $webName . "/" . $topic;
my $html =
qq!<form onSubmit="setTimeout('window.close()',2000)" method="post" action="$action">
<input type="hidden" name="popup" value="1" />
<input type="checkbox" name="TIN" value="ON">Immediate Notification of changes to <b>$topic</b><br>
<input type="checkbox" name="WIN" value="ON">Immediate Notification of changes to <b>$webName</b><br>
<input type="checkbox" name="TN" value="ON" >Normal Notification of changes to <b>$topic</b><br>
<input type="checkbox" name="WN" value="ON" >Normal Notification of changes to <b>$webName</b><br>
<input type="submit" value="Update" name="action"></form>!;
$html =~ s/(name="TIN")/$1 checked="checked"/ if $tinOn;
$html =~ s/(name="WIN")/$1 checked="checked"/ if $winOn;
$html =~ s/(name="TN")/$1 checked="checked"/ if $tnOn;
$html =~ s/(name="WN")/$1 checked="checked"/ if $wnOn;
return $html;
return ( $meta, $text );
}

1;

0 comments on commit 31fc8bd

Please sign in to comment.