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