/[drupal]/contributions/modules/paypal_tipjar/paypal_tipjar.module
ViewVC logotype

Contents of /contributions/modules/paypal_tipjar/paypal_tipjar.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Fri May 27 14:39:25 2005 UTC (4 years, 6 months ago) by njivy
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +6 -6 lines
File MIME type: text/x-php
Issue #12969: Updated comments to reflect IPN interaction; fixed hexidecimal conversion
1 <?
2 // $Id: paypal_tipjar.module,v 1.6 2004/11/24 19:27:46 njivy Exp $
3
4 /*
5 * This module sets up a tip jar with PayPal so that visitors may
6 * financially contribute to whatever fund the administrator designates.
7 * Donations are logged and verified with PayPal's Instant Payment
8 * Notification (IPN), so a running total can be calculated.
9 *
10 * The PayPal acount must be a Business or Premier account to
11 * receive money.
12 *
13 * This module relies on paypal_framework.module to receive and
14 * validate IPN transactions with PayPal.com.
15 */
16
17 /*
18 * Drupal hooks used:
19 * help, perm, menu, settings, block
20 */
21
22 /**
23 * hook_help() implementation
24 */
25 function paypal_tipjar_help($section = 'admin/help#paypal_tipjar') {
26 $output = '';
27
28 switch ($section) {
29
30 case 'admin/modules#description':
31 $output = t('Provides a PayPal tip jar (donations) block. Requires paypal_framework.module.');
32 break;
33 case 'admin/modules/paypal_tipjar':
34 case 'admin/settings/paypal_tipjar':
35 $output = t('This module sets up a tip jar with PayPal so that visitors may donate money to whatever fund the administrator designates. Go to the blocks administration page to enable the donation block. The paypal_framework.module is required in order to receive information from PayPal.');
36 break;
37
38 }
39
40 return $output;
41 }
42
43 /**
44 * hook_perm() implementation
45 */
46 function paypal_tipjar_perm() {
47 return array('access tip jar', 'administer tip jar');
48 }
49
50 /**
51 * hook_menu() implementation
52 */
53 function paypal_tipjar_menu($may_cache) {
54 $items = array();
55
56 $access = user_access('administer tip jar');
57
58 if ($may_cache) {
59 // Tabs:
60 $items[] = array('path' => 'admin/settings/paypal_tipjar/configure', 'title' => t('configure'),
61 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
62 $items[] = array('path' => 'admin/settings/paypal_tipjar/update', 'title' => t('update'),
63 'callback' => 'paypal_tipjar_cron', 'callback arguments' => 'update',
64 'access' => $access, 'type' => MENU_LOCAL_TASK);
65 }
66
67 return $items;
68 }
69
70 /**
71 * hook_settings() implementation
72 */
73 function paypal_tipjar_settings() {
74 $tmp_output .= form_textfield(t('PayPal email address'), 'paypal_tipjar_account', variable_get('paypal_tipjar_account', ''), 64, 128, t('This is the PayPal account that receives money for the tip jar.'));
75 $tmp_output .= form_textfield(t('PayPal item ID'), 'paypal_tipjar_item_ID', variable_get('paypal_tipjar_item_ID', ''), 64, 128, t('This is the PayPal item ID for this donation. Other donations, products, and services may have other IDs.'));
76 $tmp_output .= form_textfield(t('Minimum hours between updates'), 'paypal_tipjar_cron_period', variable_get('paypal_tipjar_cron_period', 4), 3, 3, t('Specify the minimum number of hours between updates of the total amount of money in the tip jar.'));
77 $tmp_output .= form_textarea(t('PayPal button code'), 'paypal_tipjar_button_code', variable_get('paypal_tipjar_button_code', ''), 64, 10, t('This is the code that PayPal generates or some similar code that links to PayPal. Other images and text can be entered here as well.'));
78 $output = $tmp_output;
79
80 if (function_exists('imagepng')) {
81 $tmp_output = form_checkbox(t('Show graph of donations'), 'paypal_show_graphic', 1, variable_get('paypal_show_graphic', 1), t('Enable a graphic showing the amount of donations compared to the target donation level.'));
82 $tmp_output .= form_textfield(t('Path to image'), 'paypal_tipjar_output_file', variable_get('paypal_tipjar_output_file', ''), 64, 128, t('This is where the rendered image will be saved. Please supply the complete path to a writeable location. The image will be a PNG. If you want it to appear in the block, add an &lt;image /> tag in the PayPal button code textarea.'));
83 $tmp_output .= form_textfield(t('Ignore donations older than this number of days'), 'paypal_tipjar_ignore_older_than', variable_get('paypal_tipjar_ignore_older_than', 30), 5, 32, t('Donations older than this will not be counted toward the target level. Enter -1 to count all donations.'));
84 $tmp_output .= form_textfield(t('Target donation level'), 'paypal_tipjar_target_donation_level', variable_get('paypal_tipjar_target_donation_level', 10), 5, 32, t('This is how much money you would like to receive total. Only donations within the specified timeframe are counted.'));
85 $tmp_output .= form_textfield(t('Graph foreground color'), 'paypal_tipjar_foreground', variable_get('paypal_tipjar_foreground', '#EFEFEF'), 7, 32, t('Please supply a valid hexidecimal HTML color value.'));
86 $tmp_output .= form_textfield(t('Graph background color'), 'paypal_tipjar_background', variable_get('paypal_tipjar_background', '#FFFFFF'), 7, 32, t('Please supply a valid hexidecimal HTML color value.'));
87 $tmp_output .= form_textfield(t('Graph line color'), 'paypal_tipjar_line', variable_get('paypal_tipjar_line', '#000000'), 7, 32, t('Please supply a valid hexidecimal HTML color value.'));
88 $output .= form_group(t('Graph settings'), $tmp_output);
89 }
90
91 return $output;
92 }
93
94 /**
95 * hook_block() implementation
96 */
97 function paypal_tipjar_block($op = 'list', $delta = 0) {
98 if ($op == 'list') {
99 $blocks[0]['info'] = t('Tip jar');
100
101 return $blocks;
102 }
103 else {
104 $blocks = array();
105
106 switch ($delta) {
107 case '0':
108 if (user_access('access tip jar')) {
109 $output .= t('Donations in the last %a days: $%b', array('%a'=>variable_get('paypal_tipjar_ignore_older_than', 30), '%b'=>round(variable_get('paypal_tipjar_current_level', 0), 2))).'<br />';
110 $blocks['subject'] = t('Tip jar');
111 $blocks['content'] = $output.variable_get('paypal_tipjar_button_code', '');
112 }
113 break;
114 }
115
116 return $blocks;
117 }
118 }
119
120 /**
121 * hook_cron() implementation
122 */
123 function paypal_tipjar_cron($arg = NULL) {
124 //
125 // Update the graphic
126 //
127 $now = time();
128
129 if ((variable_get('paypal_tipjar_cron_last', 0) - $now) >= (variable_get('paypal_tipjar_cron_period', 4)*60*60) || $arg == 'update') {
130 $target_level = variable_get('paypal_tipjar_target_donation_level', 10);
131 $ignore_older_than = variable_get('paypal_tipjar_ignore_older_than', 30) * 60 * 60 * 24; // multiplied by seconds per day
132 $output_file = variable_get('paypal_tipjar_output_file', '');
133
134 if ($ignore_older_than > 0) {
135 $sql_time = ' AND ' . time() . " - UNIX_TIMESTAMP(transaction_date) <= " . $ignore_older_than;
136 }
137
138 //
139 // Get the combined results of Completed, Refunded, and Reversed transactions
140 // that are within the specified timeframe and for the specified item.
141 //
142 $result = db_query('SELECT SUM(mc_gross) AS gross, SUM(mc_fee) AS fee FROM {paypal_log}
143 WHERE item_number="'. variable_get('paypal_tipjar_item_ID', '') .'"
144 AND receiver_email="'. variable_get('paypal_tipjar_account', '') . '"
145 '.$sql_time);
146 $t = db_fetch_object($result);
147 variable_set('paypal_tipjar_current_level', round(($t->gross - $t->fee), 2));
148 $t->sum = round(($t->gross - $t->fee), 2);
149
150 //
151 // Make the graph
152 //
153 if (function_exists('imagepng') && variable_get('paypal_tipjar_show_graphic', 1)) {
154 $foreground_color = _paypal_tipjar_convert_color(variable_get('paypal_tipjar_foreground', '#00FF00'));
155 $background_color = _paypal_tipjar_convert_color(variable_get('paypal_tipjar_background', '#FFFFFF'));
156 $line_color = _paypal_tipjar_convert_color(variable_get('paypal_tipjar_line', '#000000'));
157
158 $image->width = 50;
159 $image->height = 100;
160 $image->img = imagecreate($image->width, $image->height);
161 $image->foreground = imagecolorallocate($image->img, hexdec($foreground_color[1]), hexdec($foreground_color[2]), hexdec($foreground_color[3]));
162 $image->background = imagecolorallocate($image->img, hexdec($background_color[1]), hexdec($background_color[2]), hexdec($background_color[3]));
163 $image->line = imagecolorallocate($image->img, hexdec($line_color[1]), hexdec($line_color[2]), hexdec($line_color[3]));
164
165 $current_value = round($t->sum/$target_level*(.75*$image->height));
166 $current_value < 1 && $t->sum > 0 && $current_value = 1;
167 $current_value > $image->height && $current_value = $image->height;
168 drupal_set_message(t('Sum is %a, rounded to %b. Graph Height is %c. The target height is %d. Time limit is %e',
169 array('%a'=>$t->sum, '%b'=>'n/a', '%c'=>$current_value, '%d'=>(.75*$image->height), '%e'=>($ignore_older_than/(3600*24)).' days.')));
170
171 imagefilledrectangle($image->img, 0, 0, $image->width, $image->height, $image->background);
172 imagefilledrectangle($image->img, round(.33*$image->width), $image->height - $current_value-1,
173 round(.67*$image->width), $image->height, $image->foreground);
174
175 imageline($image->img, 0, $image->height-1, $image->width, $image->height-1, $image->line);
176 imageline($image->img, round(.25*$image->width), $image->height - round(.75*$image->height)-1,
177 round(.75*$image->width), $image->height - round(.75*$image->height)-1, $image->line);
178
179 imagestring($image->img, 1, 0, $image->height - round(.75*$image->height) - 9, '$'.$target_level, $image->line);
180
181 imagepng($image->img, $output_file);
182 imagedestroy($image->img);
183
184 $error = t('The tip jar graphic was updated.');
185 }
186
187 if ($arg == 'update') {
188 //
189 // Result message
190 //
191 $error && drupal_set_message($error);
192 drupal_set_message(t('The tip jar value was updated.'));
193
194 //
195 // Redirect
196 //
197 drupal_goto('admin/settings/paypal_tipjar');
198 }
199 else {
200 //
201 // Reset the cron variable
202 //
203 !$arg && variable_set('paypal_tipjar_cron_last', time());
204 }
205 }
206 }
207
208 /*
209 * Private functions:
210 * convert_color
211 */
212
213 /**
214 * Format a color for use in imageallocatecolor()
215 *
216 * @param $color #000000
217 * @return $color An array of RGB hex values: e.g. array('0x00', '0x00', '0x00')
218 */
219 function _paypal_tipjar_convert_color($color) {
220 $output[1] = '0x'.$color{1}.$color{2};
221 $output[2] = '0x'.$color{3}.$color{4};
222 $output[3] = '0x'.$color{5}.$color{6};
223
224 return $output;
225 }
226 ?>

  ViewVC Help
Powered by ViewVC 1.1.2