| 1 | # Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it |
|---|
| 4 | # Copyright (C) 2001-2004 Peter Thoeny, peter@thoeny.com |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or |
|---|
| 7 | # modify it under the terms of the GNU General Public License |
|---|
| 8 | # as published by the Free Software Foundation; either version 2 |
|---|
| 9 | # of the License, or (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details, published at |
|---|
| 15 | # http://www.gnu.org/copyleft/gpl.html |
|---|
| 16 | # |
|---|
| 17 | # $Revision$ |
|---|
| 18 | # |
|---|
| 19 | # ========================= |
|---|
| 20 | # |
|---|
| 21 | # This is an empty TWiki plugin. Use it as a template |
|---|
| 22 | # for your own plugins; see %SYSTEMWEB%.Plugins for details. |
|---|
| 23 | # |
|---|
| 24 | # Each plugin is a package that may contain these functions: VERSION: |
|---|
| 25 | # |
|---|
| 26 | # earlyInitPlugin ( ) 1.020 |
|---|
| 27 | # initPlugin ( $topic, $web, $user, $installWeb ) 1.000 |
|---|
| 28 | # initializeUserHandler ( $loginName, $url, $pathInfo ) 1.010 |
|---|
| 29 | # registrationHandler ( $web, $wikiName, $loginName ) 1.010 |
|---|
| 30 | # beforeCommonTagsHandler ( $text, $topic, $web ) 1.024 |
|---|
| 31 | # commonTagsHandler ( $text, $topic, $web ) 1.000 |
|---|
| 32 | # afterCommonTagsHandler ( $text, $topic, $web ) 1.024 |
|---|
| 33 | # startRenderingHandler ( $text, $web ) 1.000 |
|---|
| 34 | # outsidePREHandler ( $text ) 1.000 |
|---|
| 35 | # insidePREHandler ( $text ) 1.000 |
|---|
| 36 | # endRenderingHandler ( $text ) 1.000 |
|---|
| 37 | # beforeEditHandler ( $text, $topic, $web ) 1.010 |
|---|
| 38 | # afterEditHandler ( $text, $topic, $web ) 1.010 |
|---|
| 39 | # beforeSaveHandler ( $text, $topic, $web ) 1.010 |
|---|
| 40 | # afterSaveHandler ( $text, $topic, $web, $errors ) 1.020 |
|---|
| 41 | # writeHeaderHandler ( $query ) 1.010 Use only in one Plugin |
|---|
| 42 | # redirectCgiQueryHandler ( $query, $url ) 1.010 Use only in one Plugin |
|---|
| 43 | # getSessionValueHandler ( $key ) 1.010 Use only in one Plugin |
|---|
| 44 | # setSessionValueHandler ( $key, $value ) 1.010 Use only in one Plugin |
|---|
| 45 | # |
|---|
| 46 | # initPlugin is required, all other are optional. |
|---|
| 47 | # For increased performance, all handlers except initPlugin are |
|---|
| 48 | # disabled. To enable a handler remove the leading DISABLE_ from |
|---|
| 49 | # the function name. Remove disabled handlers you do not need. |
|---|
| 50 | # |
|---|
| 51 | # NOTE: To interact with TWiki use the official TWiki functions |
|---|
| 52 | # in the TWiki::Func module. Do not reference any functions or |
|---|
| 53 | # variables elsewhere in TWiki!! |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | # ========================= |
|---|
| 57 | package TWiki::Plugins::FundraisingPlugin; # change the package name and $pluginName!!! |
|---|
| 58 | |
|---|
| 59 | # ========================= |
|---|
| 60 | use vars qw( |
|---|
| 61 | $web $topic $user $installWeb $VERSION $pluginName |
|---|
| 62 | $debug $twikiWebname |
|---|
| 63 | $fundraisingEndDate $fundraisingGoal $fundraisingPledged $fundraisingDonateTopic |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | use Time::Local; |
|---|
| 67 | |
|---|
| 68 | $VERSION = '0.2.1'; |
|---|
| 69 | $pluginName = 'FundraisingPlugin'; |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | # ========================= |
|---|
| 73 | sub initPlugin |
|---|
| 74 | { |
|---|
| 75 | ( $topic, $web, $user, $installWeb ) = @_; |
|---|
| 76 | |
|---|
| 77 | # check for Plugins.pm versions |
|---|
| 78 | if( $TWiki::Plugins::VERSION < 1.021 ) { |
|---|
| 79 | TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" ); |
|---|
| 80 | return 0; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | # Get plugin debug flag |
|---|
| 84 | $debug = TWiki::Func::getPreferencesFlag( "\U$pluginName\E_DEBUG" ); |
|---|
| 85 | |
|---|
| 86 | # Get plugin preferences |
|---|
| 87 | $fundraisingEndDate = TWiki::Func::getPreferencesValue( "\U$pluginName\E_ENDDATE" ) || ""; |
|---|
| 88 | $fundraisingGoal = TWiki::Func::getPreferencesValue( "\U$pluginName\E_GOAL" ) || "0"; |
|---|
| 89 | $fundraisingPledged = TWiki::Func::getPreferencesValue( "\U$pluginName\E_PLEDGED" ) || "0"; |
|---|
| 90 | $fundraisingPreMsg = TWiki::Func::getPreferencesValue( "\U$pluginName\E_PREMSG" ) || TWiki::Func::getWikiToolName() . " needs money"; |
|---|
| 91 | $fundraisingToGoMsg = TWiki::Func::getPreferencesValue( "\U$pluginName\E_TOGOMSG" ) || '$TOGO to go'; |
|---|
| 92 | $fundraisingPledgedMsg = TWiki::Func::getPreferencesValue( "\U$pluginName\E_PLEDGEDMSG" ) || "$fundraisingPledged"; |
|---|
| 93 | $fundraisingPostMsg = TWiki::Func::getPreferencesValue( "\U$pluginName\E_POSTMSG" ) || "before $fundraisingEndDate."; |
|---|
| 94 | |
|---|
| 95 | # Parse configuration variables that need parsing, do any calculus needed |
|---|
| 96 | if ( "$fundraisingEndDate" =~ /^(\d+)-(\d+)-(\d+)$/ ) { |
|---|
| 97 | $theYear = $1; |
|---|
| 98 | $theMonth = $2; |
|---|
| 99 | $theDay = $3; |
|---|
| 100 | } |
|---|
| 101 | $fundraisingToGo = $fundraisingGoal - $fundraisingPledged; |
|---|
| 102 | $fundraisingToGoMsg =~ s/TOGO/$fundraisingToGo/; |
|---|
| 103 | |
|---|
| 104 | # Check configuration variables consistency and plugin activation |
|---|
| 105 | $fundraisingActivated = 1 unless |
|---|
| 106 | ( |
|---|
| 107 | (timelocal(0, 0, 0, $theDay, $theMonth, $theYear) < time()) or |
|---|
| 108 | ($fundraisingToGo <= 0) |
|---|
| 109 | ); |
|---|
| 110 | |
|---|
| 111 | # Plugin correctly initialized? |
|---|
| 112 | if ($debug) { |
|---|
| 113 | if ( $fundraisingActivated == 1 ) { |
|---|
| 114 | TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ); |
|---|
| 115 | } else { |
|---|
| 116 | TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) will display nothing" ); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | return 1; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | # ========================= |
|---|
| 123 | sub handleFundraisingMessage |
|---|
| 124 | { |
|---|
| 125 | my $message = ""; |
|---|
| 126 | |
|---|
| 127 | if ($fundraisingActivated) { |
|---|
| 128 | |
|---|
| 129 | my $pledgedPercentage = $fundraisingPledged * 100 / $fundraisingGoal; |
|---|
| 130 | my $toGoPercentage = 100 - $pledgedPercentage; |
|---|
| 131 | |
|---|
| 132 | $message = qq{ |
|---|
| 133 | <div class="fundraisingBanner" align="center" style="margin: 1em; font-size: 100%;"> |
|---|
| 134 | <div style="text-align: center"> |
|---|
| 135 | <div style="width: 100%; margin: 0 auto;"> |
|---|
| 136 | <div style="font-size: 90%"> |
|---|
| 137 | $fundraisingPreMsg |
|---|
| 138 | </div> |
|---|
| 139 | <div style="width: 100%; border: 1px solid gray; background-color: #ff5050; margin: .2em auto .2em"> |
|---|
| 140 | <div style="width: $pledgedPercentage%; float: left; background-color: #90ee90;"> |
|---|
| 141 | <p style="margin: 0;"> |
|---|
| 142 |   <small>$fundraisingPledgedMsg</small> |
|---|
| 143 | </p> |
|---|
| 144 | </div> |
|---|
| 145 | <div style="width: $toGoPercentage%; float: right; margin-left: -1px"> |
|---|
| 146 | <p style="margin: 0;"> |
|---|
| 147 | <strong><small>$fundraisingToGoMsg</small></strong> |
|---|
| 148 | </p> |
|---|
| 149 | </div> |
|---|
| 150 | <div style="clear:both"> |
|---|
| 151 | </div> |
|---|
| 152 | </div> |
|---|
| 153 | <div style="font-size: 90%"> |
|---|
| 154 | $fundraisingPostMsg |
|---|
| 155 | </div> |
|---|
| 156 | </div> |
|---|
| 157 | </div> |
|---|
| 158 | </div> |
|---|
| 159 | }; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | return $message; |
|---|
| 163 | |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | # ========================= |
|---|
| 167 | sub commonTagsHandler |
|---|
| 168 | { |
|---|
| 169 | ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead |
|---|
| 170 | |
|---|
| 171 | TWiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $_[2].$_[1] )" ) if $debug; |
|---|
| 172 | |
|---|
| 173 | # This is the place to define customized tags and variables |
|---|
| 174 | # Called by TWiki::handleCommonTags, after %INCLUDE:"..."% |
|---|
| 175 | |
|---|
| 176 | # do custom extension rule, like for example: |
|---|
| 177 | # $_[0] =~ s/%XYZ%/&handleXyz()/ge; |
|---|
| 178 | # $_[0] =~ s/%XYZ{(.*?)}%/&handleXyz($1)/ge; |
|---|
| 179 | $_[0] =~ s/%FUNDRAISINGMESSAGE%/&handleFundraisingMessage()/ge; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | # ========================= |
|---|
| 183 | |
|---|
| 184 | 1; |
|---|