Skip to content

Commit

Permalink
* Add block integration, now you can enable donate block in each node
Browse files Browse the repository at this point in the history
* New configurations and beyond are available in donate page edit form.
  • Loading branch information
aimakun committed Mar 15, 2011
1 parent 3570c39 commit 5290634
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 27 deletions.
55 changes: 55 additions & 0 deletions paypal_donate.module
Expand Up @@ -58,6 +58,33 @@ function paypal_donate_settings() {
return system_settings_form($form);
}

function paypal_donate_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks = array();
$nodes = variable_get('paypal_donate_block', array());

This comment has been minimized.

Copy link
@aimakun

aimakun Mar 15, 2011

Author Owner

Note: I decided to use array because most of websites not need many different donate pages, so use variable table may slow down process but convince me to maintain these code.

foreach ($nodes as $nid => $isset) {
$node = node_load($nid);
$blocks[$node->nid] = array(
'info' => 'Paypal: ' . $node->title,
);
}
return $blocks;
break;

case 'view':
$nodes = variable_get('paypal_donate_block', array());
if (isset($nodes[$delta])) {
$node = node_load($delta);
drupal_add_css(drupal_get_path('module', 'paypal_donate') . '/paypal_form.css');
return array(
'subject' => $node->title,
'content' => theme('paypal_form', $node),
);
}
}
}

/**
* Returns a list of permissions that can be used for this module.
*
Expand Down Expand Up @@ -125,6 +152,7 @@ function paypal_donate_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (user_access('access content')) {
if ($op == 'alter') {
if ($node->type == 'paypal_page') {
drupal_add_css(drupal_get_path('module', 'paypal_donate') . '/paypal_form.css');
$node->body = theme('paypal_form', $node);
return NULL;
}
Expand Down Expand Up @@ -167,6 +195,18 @@ function paypal_donate_form(&$node) {
'#default_value' => $node->paypal_account,
'#weight' => 2
);

// Get array of block variable.
$blocks = variable_get('paypal_donate_block', array());
$donate_block = isset($blocks[$node->nid]) ? 1 : 0;

$form['donate_block'] = array(
'#type' => 'radios',
'#title' => t('Create block for this page'),
'#default_value' => $donate_block,
'#options' => array(1 => t('Enable'), 0 => t('Disable')),
);

return $form;
}

Expand All @@ -191,6 +231,7 @@ function paypal_donate_load($node) {
* @return void
*/
function paypal_donate_insert($node) {
paypal_donate_block_update($node);
return db_query("INSERT INTO {paypal_donate} SET nid='%s', paypal_account='%s'", $node->nid, $node->account);
}

Expand All @@ -204,6 +245,7 @@ function paypal_donate_insert($node) {
*/
function paypal_donate_update($node) {
if ($node->nid > 0) {
paypal_donate_block_update($node);
return db_query("UPDATE {paypal_donate} SET paypal_account='%s' WHERE nid='%s'", $node->account, $node->nid);
} else /* This else should never be executed */
paypal_donate_save($node);
Expand All @@ -222,3 +264,16 @@ function paypal_donate_delete($node) {
return db_query("DELETE FROM {paypal_donate} WHERE nid='%s'", $node->nid);
}
}

function paypal_donate_block_update($node) {
if ($node->donate_block) {
$blocks = variable_get('paypal_donate_block', array());
if ($node->donate_block == 1) {
$blocks[$node->nid] = TRUE;
}
else {
unset($blocks[$node->nid]);
}
variable_set('paypal_donate_block', $blocks);
}
}
2 changes: 2 additions & 0 deletions paypal_form.css
@@ -0,0 +1,2 @@
#paypal_donate_form .form-block{display:block;}
#paypal_donate_form .form-field-label{font-weight:bold;}
55 changes: 28 additions & 27 deletions paypal_form.tpl.php
Expand Up @@ -4,46 +4,48 @@
<div class="node_<?php print $node->nid?>">
<?php print $node->body ?>

<!-- <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="<?php print $node->paypal_account ?>">
<input type="hidden" name="lc" value="TH">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="2">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="paypal_donate_form">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="<?php print $node->paypal_account ?>">
<input type="hidden" name="lc" value="TH">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" id="item_name" name="item_name" value="Donation">
<?php
$currency = variable_get('paypal_donate_currency', 'USD');
if (!empty($currency)):
?>
<input type="hidden" name="currency_code" value="<?php print $currency; ?>">
<input type="hidden" name="currency_code" value="<?php print $currency; ?>">
<?php endif; ?>
<?php
$options = variable_get('paypal_donate_options', '');
if (!empty($options)):
$options = explode("\n", $options);
$options = explode("\r\n", $options);
?>
<div>
<label for="item_name"><?php print t('Donate option:'); ?></label>
<select name="item_name">
<label for="item_name" class="form-block form-field-label"><?php print t('Donate option:'); ?></label>
<?php foreach ($options as $option) {
if (empty($option)) {
print '<option value="">-- None --</option>';
}
else {
print '<option value="' . check_plain($option) . '">' . check_plain($option) . '</option>';
if (!empty($option)) {
print ' <label class="form-block"><input type="checkbox" name="items" value="' . check_plain($option) . '" />' . check_plain($option) . '</label>';
}
}
?>
</select>
</div>
<?php endif; ?>

<input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted">
<input type="image" src="https://www.paypal.com/en_GB/TH/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>


<input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted">
<input type="image" id="donate_button" src="https://www.paypal.com/en_GB/TH/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
<script type="text/javascript">
$('#paypal_donate_form').submit( function() {
var item_names = [];
$('input[name="items"]').each( function() {
if ($(this).attr('checked')) {
item_names.push($(this).val());
}
});
$('input[name="item_name"]').val(item_names.join(' + '));
});
</script>
</div>
<?php /*
<form action="http://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_donations">
Expand All @@ -60,6 +62,5 @@
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</div>
*/

0 comments on commit 5290634

Please sign in to comment.