| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: donation.module,v 1.3.2.3 2009/10/14 22:49:00 kbahey Exp $ 3 4 /** 5 * @file 6 * A simple donation module. 7 * Originally by Dries Butyaert. Maintained by Khalid Baheyeldin http://2bits.com 8 */ 9 10 define('DONATION_HIDDEN', 0); 11 define('DONATION_PUBLIC', 1); 12 13 define('DONATION_PAGER', 25); 14 15 define('DONATION_EMAIL', 'donation_email'); 16 define('DONATION_STATE', 'donation_state'); 17 define('DONATION_THANKS_TITLE', 'donation_thanks_title'); 18 define('DONATION_THANKS_DEFAULT_TITLE', t('Thank You')); 19 define('DONATION_THANKS_TEXT', 'donation_thanks_text'); 20 define('DONATION_THANKS_DEFAULT_TEXT', t('Thank you for your donation.')); 21 define('DONATION_DONORS_TEXT', 'donation_donors_text'); 22 define('DONATION_DONORS_DEFAULT_TEXT', t('<ul><li><a href="/donate">Donate now</a>.</li><li><p>This page lists all donors.</li></ul>')); 23 define ('DONATION_DONATE_MESSAGE', 'donation_donate_message'); 24 define ('DONATION_DONATE_MESSAGE_DEFAULT_TEXT', t('Please make a donation.')); 25 define ('DONATION_CURRENCY_TEXT', 'donation_currency_text'); 26 define ('DONATION_CURRENCY_DEFAULT_TEXT', t('We accept payments in these currencies.')); 27 define ('DONATION_AMOUNT_TEXT', 'donation_amount_text'); 28 define ('DONATION_AMOUNT_DEFAULT_TEXT', t('Enter the amount you wish to donate.')); 29 define ('DONATION_SUBMIT_TEXT', 'donation_submit_text'); 30 define ('DONATION_SUBMIT_DEFAULT_TEXT', t('Proceed to paypal.com for payment')); 31 define ('DONATION_MEMO_TEXT', 'donation_memo_text'); 32 define ('DONATION_MEMO_DEFAULT_TEXT', t('Website Donation')); 33 define ('DONATION_PAGE_TITLE', 'donation_page_title'); 34 define ('DONATION_DEFAULT_PAGE_TITLE', t('Donate')); 35 define ('DONATION_DEFAULT_EMAIL', t('webmaster@localhost')); 36 define ('DONATION_UPDATE_DONATIONS_THERMOMETER', 'donation_update_donations_thermometer'); 37 define ('DONATION_CONTENT_TYPE', 'donations_content_type'); 38 define ('DONATION_CONTENT_TYPE_DEFAULT', 'donation_target'); 39 define ('DONATION_TARGET_TEXT', 'donations_content_type_text'); 40 define ('DONATION_TARGET_DEFAULT_TEXT', 'Please choose the project you want to support'); 41 define ('DONATION_GENERIC_TEXT', 'donations_generic_text'); 42 define ('DONATION_GENERIC_DEFAULT_TEXT', 'Make a generic donation'); 43 44 /** 45 * Implementation of hook_perm(). 46 */ 47 function donation_perm() { 48 return array('administer donations'); 49 } 50 51 /** 52 * Implementation of hook_menu(). 53 */ 54 function donation_menu() { 55 $items = array(); 56 57 $access = array('administer site configuration'); 58 59 $items['ipn/donation'] = array( 60 'page callback' => 'donation_ipn', 61 'type' => MENU_CALLBACK, 62 'access callback' => TRUE, 63 ); 64 $items['donation/thanks'] = array( 65 'title' => variable_get(DONATION_THANKS_TITLE, DONATION_THANKS_DEFAULT_TITLE), 66 'page callback' => 'donation_thanks', 67 'type' => MENU_CALLBACK, 68 'access callback' => TRUE, 69 ); 70 $items['admin/settings/donations'] = array( 71 'title' => 'Donations', 72 'page callback' => 'drupal_get_form', 73 'page arguments' => array('donation_settings'), 74 'description' => 'Administer donations', 75 'access arguments' => $access, 76 ); 77 $items['admin/build/donations'] = array( 78 'title' => 'Donations', 79 'access arguments' => $access, 80 'page callback' => 'donation_admin', 81 'description' => 'Manages donations to your site via Paypal', 82 ); 83 $items['admin/build/donations/list'] = array( 84 'title' => 'List', 85 'access arguments' => $access, 86 'type' => MENU_DEFAULT_LOCAL_TASK, 87 'weight' => -10, 88 ); 89 $items['admin/build/donations/add'] = array( 90 'title' => 'Add donation', 91 'access arguments' => $access, 92 'page callback' => 'drupal_get_form', 93 'page arguments' => array('donation_edit', 'add'), 94 'type' => MENU_LOCAL_TASK, 95 ); 96 $items['admin/build/donations/edit'] = array( 97 'title' => 'Edit donation', 98 'access arguments' => $access, 99 'page callback' => 'drupal_get_form', 100 'page arguments' => array('donation_edit', 'edit'), 101 'type' => MENU_CALLBACK, 102 ); 103 $items['admin/build/donations/delete'] = array( 104 'title' => 'Delete donation', 105 'access arguments' => $access, 106 'page callback' => 'drupal_get_form', 107 'page arguments' => array('donation_edit', 'delete'), 108 'type' => MENU_CALLBACK, 109 ); 110 $items['admin/build/donations/import'] = array( 111 'title' => 'Import', 112 'access arguments' => $access, 113 'page callback' => 'drupal_get_form', 114 'page arguments' => array('donation_import'), 115 'type' => MENU_CALLBACK, 116 ); 117 $items['donate'] = array( 118 'title' => variable_get(DONATION_PAGE_TITLE, DONATION_DEFAULT_PAGE_TITLE), 119 'page callback' => 'drupal_get_form', 120 'page arguments' => array('donation_form_build'), 121 'type' => MENU_CALLBACK, 122 'access callback' => TRUE, 123 ); 124 $items['donate/%'] = array( 125 'title' => variable_get(DONATION_PAGE_TITLE, DONATION_DEFAULT_PAGE_TITLE), 126 'page callback' => 'drupal_get_form', 127 'page arguments' => array('donation_form_build', 1), 128 'type' => MENU_CALLBACK, 129 'access callback' => TRUE, 130 ); 131 $items['hiddendonations'] = array( 132 'title' => 'Donations', 133 'page callback' => 'donation_public_page', 134 'type' => MENU_CALLBACK, 135 'access callback' => TRUE, 136 ); 137 return $items; 138 } 139 140 /** 141 * Implementation of hook_settings(). 142 */ 143 function donation_settings() { 144 $form['general'] = array( 145 '#type' => 'fieldset', 146 '#title' => t('General Settings'), 147 '#collapsible' => TRUE, 148 '#collapsed' => FALSE, 149 ); 150 $form['general'][DONATION_EMAIL] = array( 151 '#type' => 'textfield', 152 '#title' => t('Donations email address'), 153 '#default_value' => variable_get(DONATION_EMAIL, DONATION_DEFAULT_EMAIL), 154 '#description' => t('Only donations to this email address are considered by this module.'), 155 ); 156 157 $form['general'][DONATION_STATE] = array( 158 '#type' => 'select', 159 '#title' => t('Default donation state'), 160 '#default_value' => variable_get(DONATION_STATE, DONATION_PUBLIC), 161 '#options' => array( 162 DONATION_PUBLIC => t('Public'), 163 DONATION_HIDDEN => t('Hidden') 164 ), 165 '#description' => t('Select whether donations will be public or private for this site.'), 166 ); 167 168 $form['general'][DONATION_MEMO_TEXT] = array( 169 '#type' => 'textfield', 170 '#title' => t('Text for the PayPal memo field'), 171 '#default_value' => variable_get(DONATION_MEMO_TEXT, DONATION_MEMO_DEFAULT_TEXT), 172 '#description' => t('This text will be sent to PayPal in the memo field.'), 173 '#collapsible' => TRUE, 174 '#collapsed' => FALSE, 175 ); 176 177 $form['donate_page'] = array( 178 '#type' => 'fieldset', 179 '#title' => t('Donate Page'), 180 '#collapsible' => TRUE, 181 '#collapsed' => FALSE, 182 ); 183 184 $form['donate_page'][DONATION_PAGE_TITLE] = array( 185 '#type' => 'textfield', 186 '#title' => t('Title for the donate page'), 187 '#default_value' => variable_get(DONATION_PAGE_TITLE, DONATION_DEFAULT_PAGE_TITLE), 188 '#description' => t('This text will be used as the Title for the donate page.'), 189 ); 190 191 $form['donate_page'][DONATION_DONATE_MESSAGE] = array( 192 '#type' => 'textarea', 193 '#title' => t('Introductory text for the donate page'), 194 '#default_value' => variable_get(DONATION_DONATE_MESSAGE, DONATION_DONATE_MESSAGE_DEFAULT_TEXT), 195 '#description' => t('This text will be displayed to the user after they come back from Paypal.'), 196 ); 197 198 $form['donate_page'][DONATION_CURRENCY_TEXT] = array( 199 '#type' => 'textfield', 200 '#title' => t('Description text for the currency field'), 201 '#default_value' => variable_get(DONATION_CURRENCY_TEXT, DONATION_CURRENCY_DEFAULT_TEXT), 202 '#description' => t('This text will be displayed as the description for the currency field on the donate page.'), 203 ); 204 205 $form['donate_page'][DONATION_AMOUNT_TEXT] = array( 206 '#type' => 'textfield', 207 '#title' => t('Description text for the amount field'), 208 '#default_value' => variable_get(DONATION_AMOUNT_TEXT, DONATION_AMOUNT_DEFAULT_TEXT), 209 '#description' => t('This text will be displayed as the description for the amount field on the donate page.'), 210 ); 211 212 $form['donate_page'][DONATION_SUBMIT_TEXT] = array( 213 '#type' => 'textfield', 214 '#title' => t('Text for the Submit button'), 215 '#default_value' => variable_get(DONATION_SUBMIT_TEXT, DONATION_SUBMIT_DEFAULT_TEXT), 216 '#description' => t('This text will be displayed on the Submit button of the donate page.'), 217 ); 218 219 $form['misc'] = array( 220 '#type' => 'fieldset', 221 '#title' => t('Miscellaneous'), 222 '#collapsible' => TRUE, 223 '#collapsed' => FALSE, 224 ); 225 226 $form['misc'][DONATION_DONORS_TEXT] = array( 227 '#type' => 'textarea', 228 '#title' => t('Text for the donors list page'), 229 '#default_value' => variable_get(DONATION_DONORS_TEXT, DONATION_DONORS_DEFAULT_TEXT), 230 '#description' => t('This text will be displayed at the top of the donors list page.'), 231 ); 232 233 $form['misc'][DONATION_THANKS_TITLE] = array( 234 '#type' => 'textfield', 235 '#title' => t('Title for the donations thank you page'), 236 '#default_value' => variable_get(DONATION_THANKS_TITLE, DONATION_THANKS_DEFAULT_TITLE), 237 '#description' => t('This text will be the Title of the page the user sees after they come back from Paypal.'), 238 ); 239 240 $form['misc'][DONATION_THANKS_TEXT] = array( 241 '#type' => 'textarea', 242 '#title' => t('Text for the donations thank you page'), 243 '#default_value' => variable_get(DONATION_THANKS_TEXT, DONATION_THANKS_DEFAULT_TEXT), 244 '#description' => t('This text will be displayed to the user after they come back from Paypal.'), 245 ); 246 247 $form['integration'] = array( 248 '#type' => 'fieldset', 249 '#title' => t('Integration'), 250 '#collapsible' => TRUE, 251 '#collapsed' => FALSE, 252 ); 253 254 $form['integration'][DONATION_UPDATE_DONATIONS_THERMOMETER] = array('#type' => 'checkbox', 255 '#title' => t('Update Donations Thermometer Amount'), 256 '#default_value' => variable_get(DONATION_UPDATE_DONATIONS_THERMOMETER, 0), 257 '#disabled' => !module_exists('donations_thermometer') ? TRUE : FALSE, 258 '#description' => t('When enabled the Donations Thermometer Amount is automatically updated with the total of donations. This requires the !mod module.', array('!mod' => '<a href="http://drupal.org/project/donations_thermometer">donations thermometer</a>')), 259 ); 260 261 return system_settings_form($form); 262 } 263 264 /** 265 * FAPI definition for the donation edit form. 266 * 267 * @ingroup forms 268 * @see donation_edit_submit() 269 */ 270 function donation_edit(&$form_state, $mode) { 271 $timestamp = format_date(time(), 'custom', 'Y-m-d H:i O'); 272 273 switch ($mode) { 274 case 'edit': 275 case 'delete': 276 $did = (int)arg(4); 277 if ($did) { 278 $result = db_query('SELECT * FROM {donations} WHERE did = %d', $did); 279 $donation = db_fetch_object($result); 280 $timestamp = format_date($donation->timestamp, 'custom', 'Y-m-d H:i O'); 281 $uid = $donation->uid; 282 } 283 } 284 285 $form['timestamp'] = array( 286 '#type' => 'textfield', 287 '#title' => t('Date'), 288 '#default_value' => $timestamp, 289 '#size' => 30, 290 '#maxlength' => 30, 291 '#description' => 'Format: Y-M-D H:M Z', 292 ); 293 $form['uid'] = array( 294 '#type' => 'textfield', 295 '#title' => t('User ID'), 296 '#default_value' => $uid, 297 '#description' => t('If the user is registered on this site, enter his/her the Drupal uid.'), 298 ); 299 $form['name'] = array( 300 '#type' => 'textfield', 301 '#title' => t('Name of donor'), 302 '#default_value' => $donation->name, 303 '#size' => 60, 304 '#maxlength' => 60, 305 '#description' => t('The name of the person or the company who donated money.'), 306 ); 307 $form['mail'] = array( 308 '#type' => 'textfield', 309 '#title' => t('E-mail address of donor'), 310 '#default_value' => $donation->mail, 311 '#size' => 60, 312 '#maxlength' => 60, 313 '#description' => t('The e-mail address of the person or the company who donated money.'), 314 ); 315 $form['amount'] = array( 316 '#type' => 'textfield', 317 '#title' => t('Amount'), 318 '#default_value' => $donation->amount, 319 '#size' => 12, 320 '#maxlength' => 12, 321 '#description' => t('The amount of money donated, after subtracting transfer fees.'), 322 ); 323 $form['currency'] = array( 324 '#type' => 'select', 325 '#title' => t('Currency'), 326 '#default_value' => $donation->currency, 327 '#options' => simple_paypal_get_currencies(), 328 ); 329 $form['status'] = array( 330 '#type' => 'select', 331 '#title' => t('Status'), 332 '#default_value' => $donation->status, 333 '#options' => array( 334 DONATION_PUBLIC => t('Public'), 335 DONATION_HIDDEN => t('Hidden') 336 ), 337 ); 338 339 if ($mode == 'delete') { 340 $form['delete'] = array( 341 '#type' => 'submit', 342 '#value' => t('Delete'), 343 ); 344 } 345 else { 346 $form['add'] = array( 347 '#type' => 'submit', 348 '#value' => t('Save'), 349 ); 350 } 351 352 $form['mode'] = array( 353 '#type' => 'hidden', 354 '#value' => $mode, 355 ); 356 357 $form['did'] = array( 358 '#type' => 'hidden', 359 '#value' => $did, 360 ); 361 362 return ($form); 363 } 364 365 /** 366 * Submit handler for donation edit form 367 */ 368 function donation_edit_submit($form, &$form_state) { 369 $mode = $form_state['values']['mode']; 370 371 if ($form_state['values']['delete'] == t('Delete')) { 372 $mode = 'delete'; 373 } 374 375 switch ($mode) { 376 case 'add': 377 db_query("INSERT INTO {donations} (timestamp, uid, name, mail, amount, currency, status) VALUES 378 (%d, %d, '%s', '%s', '%s', '%s', %d)", 379 strtotime($form_state['values']['timestamp']), 380 $form_state['values']['uid'], 381 $form_state['values']['name'], 382 $form_state['values']['mail'], 383 $form_state['values']['amount'], 384 $form_state['values']['currency'], 385 $form_state['values']['status']); 386 drupal_set_message(t('The donation has been added.')); 387 donation_update_donation_thermometer_amount(); 388 break; 389 390 case 'edit': 391 if ($form_state['values']['did']) { 392 db_query("UPDATE {donations} 393 SET timestamp = %d, uid = %d, name = '%s', mail = '%s', amount = %f, currency = '%s', status = %d WHERE did = %d", 394 strtotime($form_state['values']['timestamp']), 395 $form_state['values']['uid'], 396 $form_state['values']['name'], 397 $form_state['values']['mail'], 398 $form_state['values']['amount'], 399 $form_state['values']['currency'], 400 $form_state['values']['status'], 401 $form_state['values']['did']); 402 drupal_set_message(t('The donation has been updated.')); 403 donation_update_donation_thermometer_amount(); 404 } 405 break; 406 407 case 'delete': 408 if ($form_state['values']['did']) { 409 db_query("DELETE FROM {donations} WHERE did = %d", $form_state['values']['did']); 410 drupal_set_message(t('The donation has been deleted.')); 411 donation_update_donation_thermometer_amount(); 412 } 413 break; 414 } 415 416 drupal_goto('admin/build/donations'); 417 } 418 419 /** 420 * FAPI definition for the donation import form. 421 * 422 * @ingroup forms 423 * @see donate_form_edit_submit() 424 */ 425 function donation_import($form = NULL) { 426 $form = array( 427 '#value' => t('Paypal lets you export your payment history to a CSV file. Copy/paste the contents of the file (or parts thereof) in the form below to mass import your Paypal history into Drupal. Note that the import routine does not check for duplicate entries.'), 428 ); 429 $form['data'] = array( 430 '#type' => 'textarea', 431 '#title' => t('Data'), 432 '#default_value' => NULL, 433 '#cols' => 70, 434 '#rows' => 30, 435 ); 436 437 $form['import'] = array( 438 '#type' => 'submit', 439 '#value' => t('Import donations'), 440 ); 441 442 return $form; 443 } 444 445 function donation_import_submit($form, &$form_state) { 446 $lines = explode("\n", $form_state['values']['data']); 447 foreach ($lines as $line) { 448 list($date, $time, $timezone, $name, $type, $status, $currency, $gross, $fee, $net, $from_email, 449 $to_email, $tid, $cp_status, $shipping_address, $address_status, $item_title, $item_id, $sh_amount, 450 $insurance_amount, $sales_tax, $opt_1_name, $opt_1_value, $opt_2_name, $opt_2_value, $auction_site, 451 $buyer_id, $item_url, $closing_date, $escrow_id, $invoice_id, $ref_txn_id, $invoice_number, 452 $custom_number, $receipt_id, $contact_phone_number ) = explode('","', $line); 453 if ($to_email == variable_get(DONATION_EMAIL, DONATION_DEFAULT_EMAIL) && $name && $net) { 454 // We need to swap the day and month in Paypal's date format: 455 list($month, $day, $year) = explode('/', substr($date, 1), 3); 456 457 $uid = donation_resolve_uid($mail); 458 db_query("INSERT INTO {donations} (timestamp, uid, name, mail, amount, currency, status) VALUES 459 (%d, %d, '%s', '%s', '%s', '%s', %d)", 460 strtotime("$month/$day/$year $time $timezone"), // date + time + timezone 461 $uid, 462 $name, 463 $from_email, 464 (float)str_replace(',', '', $net), // net amount (gross - fee), commas stripped 465 $currency, 466 variable_get(DONATION_STATE, DONATION_PUBLIC)); 467 } 468 } 469 470 drupal_set_message(t('The donations have been imported.')); 471 donation_update_donation_thermometer_amount(); 472 drupal_goto('admin/build/donations'); 473 } 474 475 function donation_resolve_uid($mail) { 476 return (int)db_result(db_query("SELECT uid FROM {users} WHERE mail = '%s'", $mail)); 477 } 478 479 function donation_admin() { 480 $rows = array(); 481 $header = array( 482 array('data' => t('Date'), 'field' => 'timestamp', 'sort' => 'desc'), 483 array('data' => t('Name'), 'field' => 'name'), 484 array('data' => t('Amount'), 'field' => 'amount'), 485 array('data' => t('Status'), 'field' => 'status'), 486 array('data' => t('Operations'), 'colspan' => '2') 487 ); 488 489 $sql = 'SELECT d.* FROM {donations} d'. tablesort_sql($header); 490 $result = pager_query($sql, DONATION_PAGER, 0, NULL); 491 while ($donation = db_fetch_object($result)) { 492 $rows[] = array(format_date($donation->timestamp, 'small'), 493 ($donation->uid ? theme('username', $donation) : check_plain($donation->name)), 494 simple_paypal_format_amount($donation->amount, $donation->currency), 495 ($donation->status == DONATION_PUBLIC ? t('public') : t('hidden')), 496 "<a href=\"mailto:$donation->mail\">". t('mail') ."</a>", 497 l(t('edit'), "admin/build/donations/edit/$donation->did"), 498 l(t('delete'), "admin/build/donations/delete/$donation->did"), 499 ); 500 } 501 502 $output = theme('table', $header, $rows); 503 $output .= theme('pager', NULL, DONATION_PAGER, 0); 504 print theme('page', $output); 505 } 506 507 function donation_get_list($nid = NULL) { 508 $html = ''; 509 510 $rows = array(); 511 $header = array( 512 array('data' => t('Name'), 'field' => 'name'), 513 array('data' => t('Amount'), 'field' => 'amount'), 514 array('data' => t('Date'), 'field' => 'timestamp', 'sort' => 'desc'), 515 ); 516 517 if (!$nid) { 518 $sql = 'SELECT d.* FROM {donations} d WHERE d.status = %d'. tablesort_sql($header); 519 $result = pager_query($sql, DONATION_PAGER, 0, NULL, DONATION_PUBLIC); 520 } else { 521 $sql = 'SELECT d.* FROM {donations} d WHERE d.status = %d AND d.nid = %d'. tablesort_sql($header); 522 $result = pager_query($sql, DONATION_PAGER, 0, NULL, DONATION_PUBLIC, $nid); 523 } 524 525 while ($donation = db_fetch_object($result)) { 526 if ($donation->uid) { 527 $user = user_load(array('uid' => $donation->uid)); 528 $name = theme('username', $user); 529 } 530 else { 531 $name = $donation->name; 532 } 533 534 $rows[] = array( 535 $name, 536 simple_paypal_format_amount($donation->amount, $donation->currency), 537 t('%time ago', array('%time' => format_interval(time() - $donation->timestamp, 1)))); 538 } 539 540 $html .= theme('table', $header, $rows); 541 $html .= theme('pager', NULL, DONATION_PAGER, 0); 542 543 return $html; 544 } 545 546 function donation_public_page() { 547 $output = variable_get(DONATION_DONORS_TEXT, DONATION_DONORS_DEFAULT_TEXT); 548 $output .= theme('table', $header, $rows); 549 $output .= donation_get_list(); 550 print theme('page', $output); 551 } 552 553 function donation_thanks() { 554 print theme('page', variable_get(DONATION_THANKS_TEXT, DONATION_THANKS_DEFAULT_TEXT)); 555 } 556 557 function donation_ipn() { 558 // Verify that the request came from Paypal, and not from some intrusion 559 if (!simple_paypal_ipn_verify($_POST)) { 560 // curl verification failed 561 watchdog('donation', 'curl verification failed'); 562 return; 563 } 564 565 $receiver = $_POST['business']; 566 if ($receiver == '') { 567 $receiver = $_POST['receiver_email']; 568 } 569 570 if ($receiver != variable_get(DONATION_EMAIL, DONATION_DEFAULT_EMAIL)) { 571 // Payment is not for the email address configured 572 watchdog('donation', 'Donation was not considered: Payment is not for the email address configured'); 573 return; 574 } 575 576 // Format the fields 577 $name = check_plain($_POST['first_name'] .' '. $_POST['last_name'] . ($_POST['payer_business_name'] ? ' ('. $_POST['payer_business_name'] .')' : '')); 578 $amount = check_plain((float)$_POST['mc_gross'] - (float)$_POST['mc_fee']); 579 $fee = check_plain($_POST['mc_fee']); 580 $timestamp = check_plain(strtotime($_POST['payment_date'])); 581 $payer_email = check_plain($_POST['payer_email']); 582 $currency = check_plain($_POST['mc_currency']); 583 $uid = check_plain($_POST['custom']); 584 $uid = $uid ? $uid : donation_resolve_uid($mail); 585 $memo = check_plain($_POST['item_name']); 586 $nid = 0; 587 588 $pattern = '/\[PROJECT:(.*)\]/'; 589 preg_match($pattern, $memo, $matches); 590 if (count($matches) > 0) { 591 $memo = trim(str_replace($matches[0], '', $zeichenkette)); 592 $nid = $matches[1]; 593 } 594 595 // Record the donation in the database 596 db_query("INSERT INTO {donations} (timestamp, uid, name, mail, amount, fee, currency, status, donor_memo, nid) VALUES 597 (%d, %d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%d')", 598 $timestamp, 599 $uid, 600 $name, 601 $payer_email, 602 $amount, 603 $fee, 604 $currency, 605 variable_get(DONATION_STATE, DONATION_PUBLIC), 606 $memo, 607 $nid); 608 $did = db_last_insert_id('donations', 'did'); 609 $res = db_query('SELECT * FROM {donations} WHERE did=%d', $did); 610 $donation = db_fetch_object($res); 611 612 donation_update_donation_thermometer_amount(); 613 watchdog('donation', 'Donation from @name (@mail) amount of @amount @currency.', array( 614 '@name' => $name, 615 '@mail' => $payer_email, 616 '@amount' => $amount, 617 '@currency' => $currency, 618 )); 619 620 module_invoke_all('donation_received', $donation); 621 } 622 623 /** 624 * Output this function in a PHP node when you want a form which points to Paypal. 625 */ 626 function donation_form() { 627 return drupal_get_form('donation_form_build'); 628 } 629 630 function donation_form_build($form_data, $nid = null) { 631 global $user; 632 633 $memo_text = variable_get(DONATION_MEMO_TEXT, DONATION_MEMO_DEFAULT_TEXT); 634 $donation_message = t(variable_get(DONATION_DONATE_MESSAGE, DONATION_DONATE_MESSAGE_DEFAULT_TEXT)); 635 if ($nid != null) { 636 $res = db_query('SELECT nid, title FROM {node} WHERE type=\'%s\' AND nid=%d', variable_get(DONATION_CONTENT_TYPE, DONATION_CONTENT_TYPE_DEFAULT), $nid); 637 if ($node = db_fetch_object($res)) { 638 $memo_text .= ' [PROJECT:' . $node->nid . ']'; 639 $donation_message .= '<br /><br /><strong>' . t('Target') . ':</strong> ' . $node->title; 640 } else { 641 642 $nid = null; 643 } 644 } 645 646 if ($nid==null) { 647 $memo = variable_get(DONATION_MEMO_TEXT, DONATION_MEMO_DEFAULT_TEXT); 648 $targets = array($memo => variable_get(DONATION_GENERIC_TEXT, DONATION_GENERIC_DEFAULT_TEXT)); 649 $res = db_query('SELECT nid, title FROM {node} WHERE type=\'%s\'', variable_get(DONATION_CONTENT_TYPE, DONATION_CONTENT_TYPE_DEFAULT)); 650 while ($node = db_fetch_object($res)) { 651 $targets[$memo . ' [PROJECT:' . $node->nid . ']'] = $node->title; 652 } 653 } 654 655 $form['#action'] = simple_paypal_get_url(); 656 $form['pre'] = array( 657 '#value' => $donation_message); 658 $form['business'] = array( 659 '#type' => 'hidden', 660 '#name' => 'business', 661 '#value' => variable_get(DONATION_EMAIL, DONATION_DEFAULT_EMAIL)); 662 $form['cmd'] = array( 663 '#type' => 'hidden', 664 '#value' => '_xclick', 665 '#name' => 'cmd'); 666 667 if ((count($targets) > 1) && ($nid == null)){ 668 $form['item_name'] = array( 669 '#type' => 'select', 670 '#name' => 'item_name', 671 '#options' => $targets, 672 '#description' => t(variable_get(DONATION_TARGET_TEXT, DONATION_TARGET_DEFAULT_TEXT)) 673 ); 674 } else { 675 $form['item_name'] = array( 676 '#type' => 'hidden', 677 '#value' => $memo_text, 678 '#name' => 'item_name' 679 ); 680 } 681 $form['no_shipping'] = array( 682 '#type' => 'hidden', 683 '#value' => 1, 684 '#name' => 'no_shipping'); 685 $form['return'] = array( 686 '#type' => 'hidden', 687 '#value' => url('donation/thanks', array('absolute' => TRUE)), 688 '#name' => 'return'); 689 $form['currency_code'] = array( 690 '#type' => 'select', 691 '#title' => t('Currency'), 692 '#options' => simple_paypal_get_currencies(), 693 '#name' => 'currency_code', 694 '#description' => t(variable_get(DONATION_CURRENCY_TEXT, DONATION_CURRENCY_DEFAULT_TEXT)), 695 ); 696 $form['amount'] = array( 697 '#type' => 'textfield', 698 '#title' => t('Amount'), 699 '#description' => t(variable_get(DONATION_AMOUNT_TEXT, DONATION_AMOUNT_DEFAULT_TEXT)), 700 '#size' => 40, 701 '#required' => TRUE, 702 '#maxlength' => 255, 703 '#name' => 'amount', 704 '#value' => '10.00', 705 ); 706 $form['notify_url'] = array( 707 '#type' => 'hidden', 708 '#value' => url('ipn/donation', array('absolute' => TRUE)), 709 '#name' => 'notify_url', 710 ); 711 $form['custom'] = array( 712 '#type' => 'hidden', 713 '#value' => $user->uid, 714 '#name' => 'custom', 715 ); 716 $form['submit'] = array( 717 '#type' => 'submit', 718 '#value' => t(variable_get(DONATION_SUBMIT_TEXT, DONATION_SUBMIT_DEFAULT_TEXT)), 719 '#name' => 'submit', 720 ); 721 722 return $form; 723 } 724 725 /** 726 * Return the total amount of the donations 727 */ 728 729 function donation_total() { 730 $sql = 'SELECT SUM(d.Amount) FROM {donations} d WHERE d.status=1'; 731 $result = db_result(db_query($sql)); 732 $result = check_plain(sprintf('%.2f',$result)); 733 return $result; 734 } 735 736 /** 737 * If enabled update the donations_thermometer_amount 738 */ 739 740 function donation_update_donation_thermometer_amount() { 741 if (variable_get(DONATION_UPDATE_DONATIONS_THERMOMETER, 0) == 1) { 742 variable_set('donations_thermometer_amount', donation_total()); 743 } 744 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |