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

Diff of /contributions/modules/donation/donation.module

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

revision 1.3.2.2, Sat Jul 18 21:58:21 2009 UTC revision 1.3.2.3, Wed Oct 14 22:49:00 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: donation.module,v 1.3.2.1 2009/07/18 21:55:21 kbahey Exp $  // $Id: donation.module,v 1.3.2.2 2009/07/18 21:58:21 kbahey Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 34  define ('DONATION_PAGE_TITLE', Line 34  define ('DONATION_PAGE_TITLE',
34  define ('DONATION_DEFAULT_PAGE_TITLE',    t('Donate'));  define ('DONATION_DEFAULT_PAGE_TITLE',    t('Donate'));
35  define ('DONATION_DEFAULT_EMAIL',         t('webmaster@localhost'));  define ('DONATION_DEFAULT_EMAIL',         t('webmaster@localhost'));
36  define ('DONATION_UPDATE_DONATIONS_THERMOMETER', 'donation_update_donations_thermometer');  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().   * Implementation of hook_perm().
# Line 115  function donation_menu() { Line 121  function donation_menu() {
121        'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
122        'access callback' => TRUE,        '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(      $items['hiddendonations'] = array(
132        'title' => 'Donations',        'title' => 'Donations',
133        'page callback' => 'donation_public_page',        'page callback' => 'donation_public_page',
# Line 491  function donation_admin() { Line 504  function donation_admin() {
504    print theme('page', $output);    print theme('page', $output);
505  }  }
506    
507  function donation_public_page() {  function donation_get_list($nid = NULL) {
508      $html = '';
509    
510    $rows = array();    $rows = array();
511    $header = array(    $header = array(
512      array('data' => t('Name'),   'field' => 'name'),      array('data' => t('Name'),   'field' => 'name'),
513      array('data' => t('Amount'), 'field' => 'amount'),      array('data' => t('Amount'), 'field' => 'amount'),
514      array('data' => t('Date'),   'field' => 'timestamp', 'sort' => 'desc'),      array('data' => t('Date'),   'field' => 'timestamp', 'sort' => 'desc'),
515      );      );
516    
517    $sql = 'SELECT d.* FROM {donations} d WHERE d.status = %d'. tablesort_sql($header);    if (!$nid) {
518    $result = pager_query($sql, DONATION_PAGER, 0, NULL, DONATION_PUBLIC);            $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)) {    while ($donation = db_fetch_object($result)) {
526      if ($donation->uid) {      if ($donation->uid) {
# Line 516  function donation_public_page() { Line 536  function donation_public_page() {
536        simple_paypal_format_amount($donation->amount, $donation->currency),        simple_paypal_format_amount($donation->amount, $donation->currency),
537        t('%time ago', array('%time' => format_interval(time() - $donation->timestamp, 1))));        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);    $output = variable_get(DONATION_DONORS_TEXT, DONATION_DONORS_DEFAULT_TEXT);
548    $output .= theme('table', $header, $rows);    $output .= theme('table', $header, $rows);
549    $output .= theme('pager', NULL, DONATION_PAGER, 0);    $output .= donation_get_list();
550    print theme('page', $output);    print theme('page', $output);
551  }  }
552    
# Line 531  function donation_ipn() { Line 558  function donation_ipn() {
558    // Verify that the request came from Paypal, and not from some intrusion    // Verify that the request came from Paypal, and not from some intrusion
559    if (!simple_paypal_ipn_verify($_POST)) {    if (!simple_paypal_ipn_verify($_POST)) {
560      // curl verification failed      // curl verification failed
561            watchdog('donation', 'curl verification failed');
562      return;      return;
563    }    }
564    
565    if ($_POST['business'] != variable_get(DONATION_EMAIL, DONATION_DEFAULT_EMAIL)) {    $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      // Payment is not for the email address configured
572      return;      watchdog('donation', 'Donation was not considered: Payment is not for the email address configured');
573            return;
574    }    }
575    
576    // Format the fields    // Format the fields
577    $name = check_plain($_POST['first_name'] .' '. $_POST['last_name'] . ($_POST['payer_business_name'] ? ' ('. $_POST['payer_business_name'] .')' : ''));    $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']);    $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']));    $timestamp   = check_plain(strtotime($_POST['payment_date']));
581    $payer_email = check_plain($_POST['payer_email']);    $payer_email = check_plain($_POST['payer_email']);
582    $currency    = check_plain($_POST['mc_currency']);    $currency    = check_plain($_POST['mc_currency']);
583    $uid         = check_plain($_POST['custom']);    $uid         = check_plain($_POST['custom']);
584    $uid         = $uid ? $uid : donation_resolve_uid($mail);    $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    // Record the donation in the database
596    db_query("INSERT INTO {donations} (timestamp, uid, name, mail, amount, currency, status) VALUES    db_query("INSERT INTO {donations} (timestamp, uid, name, mail, amount, fee, currency, status, donor_memo, nid) VALUES
597      (%d, %d, '%s', '%s', '%s', '%s', %d)",      (%d, %d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%d')",
598      $timestamp,      $timestamp,
599      $uid,      $uid,
600      $name,      $name,
601      $payer_email,      $payer_email,
602      $amount,      $amount,
603              $fee,
604      $currency,      $currency,
605      variable_get(DONATION_STATE, DONATION_PUBLIC));      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();    donation_update_donation_thermometer_amount();
613    watchdog('donation', 'Donation from @name (@mail) amount of @amount @currency.', array(    watchdog('donation', 'Donation from @name (@mail) amount of @amount @currency.', array(
614      '@name'     => $name,      '@name'     => $name,
# Line 564  function donation_ipn() { Line 616  function donation_ipn() {
616      '@amount'   => $amount,      '@amount'   => $amount,
617      '@currency' => $currency,      '@currency' => $currency,
618      ));      ));
619    
620      module_invoke_all('donation_received', $donation);
621  }  }
622    
623  /**  /**
# Line 573  function donation_form() { Line 627  function donation_form() {
627    return drupal_get_form('donation_form_build');    return drupal_get_form('donation_form_build');
628  }  }
629    
630  function donation_form_build() {  function donation_form_build($form_data, $nid = null) {
631    global $user;    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();    $form['#action'] = simple_paypal_get_url();
656    $form['pre'] = array(    $form['pre'] = array(
657      '#value' => variable_get(DONATION_DONATE_MESSAGE, DONATION_DONATE_MESSAGE_DEFAULT_TEXT));      '#value' => $donation_message);
658    $form['business'] = array(    $form['business'] = array(
659      '#type' => 'hidden',      '#type' => 'hidden',
660      '#name' => 'business',      '#name' => 'business',
# Line 587  function donation_form_build() { Line 663  function donation_form_build() {
663      '#type' => 'hidden',      '#type' => 'hidden',
664      '#value' => '_xclick',      '#value' => '_xclick',
665      '#name' => 'cmd');      '#name' => 'cmd');
666    $form['item_name'] = array(  
667      '#type' => 'hidden',    if ((count($targets) > 1) && ($nid == null)){
668      '#value' => variable_get(DONATION_MEMO_TEXT, DONATION_MEMO_DEFAULT_TEXT),            $form['item_name'] = array(
669      '#name' => 'item_name');                  '#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(    $form['no_shipping'] = array(
682      '#type' => 'hidden',      '#type' => 'hidden',
683      '#value' => 1,      '#value' => 1,
# Line 604  function donation_form_build() { Line 691  function donation_form_build() {
691      '#title' => t('Currency'),      '#title' => t('Currency'),
692      '#options' => simple_paypal_get_currencies(),      '#options' => simple_paypal_get_currencies(),
693      '#name' => 'currency_code',      '#name' => 'currency_code',
694      '#description' => variable_get(DONATION_CURRENCY_TEXT, DONATION_CURRENCY_DEFAULT_TEXT),      '#description' => t(variable_get(DONATION_CURRENCY_TEXT, DONATION_CURRENCY_DEFAULT_TEXT)),
695    );    );
696    $form['amount'] = array(    $form['amount'] = array(
697      '#type' => 'textfield',      '#type' => 'textfield',
698      '#title' => t('Amount'),      '#title' => t('Amount'),
699      '#description' => variable_get(DONATION_AMOUNT_TEXT, DONATION_AMOUNT_DEFAULT_TEXT),      '#description' => t(variable_get(DONATION_AMOUNT_TEXT, DONATION_AMOUNT_DEFAULT_TEXT)),
700      '#size' => 40,      '#size' => 40,
701      '#required' => TRUE,      '#required' => TRUE,
702      '#maxlength' => 255,      '#maxlength' => 255,
703      '#name' => 'amount',      '#name' => 'amount',
704            '#value' => '10.00',
705    );    );
706    $form['notify_url'] = array(    $form['notify_url'] = array(
707      '#type' => 'hidden',      '#type' => 'hidden',
# Line 627  function donation_form_build() { Line 715  function donation_form_build() {
715    );    );
716    $form['submit'] = array(    $form['submit'] = array(
717      '#type' => 'submit',      '#type' => 'submit',
718      '#value' => variable_get(DONATION_SUBMIT_TEXT, DONATION_SUBMIT_DEFAULT_TEXT),      '#value' => t(variable_get(DONATION_SUBMIT_TEXT, DONATION_SUBMIT_DEFAULT_TEXT)),
719      '#name' => 'submit',      '#name' => 'submit',
720    );    );
721    

Legend:
Removed from v.1.3.2.2  
changed lines
  Added in v.1.3.2.3

  ViewVC Help
Powered by ViewVC 1.1.2