| 1 |
<?php
|
| 2 |
// $Id: buymeabeer.module,v 1.10 2009/05/04 18:40:24 yaph Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Author: Ramiro Gómez - http://www.ramiro.org
|
| 7 |
* A Drupal module that allows donations via Paypal to buy beer.
|
| 8 |
*/
|
| 9 |
|
| 10 |
define('BMAB_ANCHOR_TEXT', 'If you liked this post, buy me a beer.');
|
| 11 |
define('BMAB_LINK_TITLE_TEXT', 'Donate a beer or two via Paypal.');
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_menu().
|
| 15 |
*/
|
| 16 |
function buymeabeer_menu() {
|
| 17 |
$items = array();
|
| 18 |
$items['admin/content/buymeabeer'] = array(
|
| 19 |
'title' => 'Buy Me a Beer',
|
| 20 |
'description' => 'Enable the node types to display the buy me a beer link, set the paypal e-mail and the anchor text for the link.',
|
| 21 |
'page callback' => 'drupal_get_form',
|
| 22 |
'page arguments' => array('buymeabeer_admin_settings'),
|
| 23 |
'access arguments' => array('administer site configuration'),
|
| 24 |
'type' => MENU_NORMAL_ITEM,
|
| 25 |
'file' => 'buymeabeer.admin.inc'
|
| 26 |
);
|
| 27 |
return $items;
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_theme().
|
| 32 |
*/
|
| 33 |
function buymeabeer_theme() {
|
| 34 |
return array(
|
| 35 |
'buymeabeer_link' => array(
|
| 36 |
'arguments' => array('link' => NULL)
|
| 37 |
)
|
| 38 |
);
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_nodeapi().
|
| 43 |
*/
|
| 44 |
function buymeabeer_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 45 |
if ($op == 'view') {
|
| 46 |
//Check our node is one of the checked types
|
| 47 |
if (in_array($node->type, variable_get('buymeabeer_node_types', array()), TRUE)) {
|
| 48 |
$display_link = FALSE;
|
| 49 |
switch (variable_get('buymeabeer_teaser_full_view', 0)) {
|
| 50 |
// display in full view only
|
| 51 |
case 0:
|
| 52 |
if (!$a3) {
|
| 53 |
$display_link = TRUE;
|
| 54 |
}
|
| 55 |
break;
|
| 56 |
// display in teaser only
|
| 57 |
case 1:
|
| 58 |
if ($a3) {
|
| 59 |
$display_link = TRUE;
|
| 60 |
}
|
| 61 |
break;
|
| 62 |
// display in full view and teaser
|
| 63 |
case 2:
|
| 64 |
$display_link = TRUE;
|
| 65 |
break;
|
| 66 |
}
|
| 67 |
if ($display_link) {
|
| 68 |
$node->content['buymeabeer_ad'] = array(
|
| 69 |
'#value' => buymeabeer_paypal_link($node),
|
| 70 |
'#weight' => variable_get('buymeabeer_weight', 20)
|
| 71 |
);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* create the HTML for the donation link
|
| 79 |
*
|
| 80 |
* @return String themed donation link
|
| 81 |
*/
|
| 82 |
function buymeabeer_paypal_link($node) {
|
| 83 |
if (!(isset($node->uid) && variable_get('buymeabeer_userbeer_enable', 0) && $paypal_mail = buymeabeer_get_paypal_user_email($node->uid))) {
|
| 84 |
$paypal_mail = variable_get('buymeabeer_paypal_mail', '');
|
| 85 |
}
|
| 86 |
$anchor_text = variable_get('buymeabeer_anchor_text', t(BMAB_ANCHOR_TEXT));
|
| 87 |
$title_text = variable_get('buymeabeer_link_title_text', t(BMAB_LINK_TITLE_TEXT));
|
| 88 |
$href = "https://www.paypal.com/cgi-bin/webscr";
|
| 89 |
$query = "cmd=_xclick&business=$paypal_mail&item_name=$title_text";
|
| 90 |
$link = l(
|
| 91 |
$anchor_text,
|
| 92 |
$href,
|
| 93 |
array('query' => $query,
|
| 94 |
'html' => TRUE,
|
| 95 |
'absolute' => TRUE,
|
| 96 |
'attributes' => array('class' => 'buymeabeer-link', 'title' => $title_text)
|
| 97 |
)
|
| 98 |
);
|
| 99 |
return theme('buymeabeer_link', $link);
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Implementation of hook_link().
|
| 104 |
*/
|
| 105 |
function buymeabeer_link($type, $object = NULL, $teaser = FALSE) {
|
| 106 |
$links = array();
|
| 107 |
if (isset($object->uid) && variable_get('buymeabeer_userbeer_enable', 0)) {
|
| 108 |
$display_link = false;
|
| 109 |
if ('node' == $type && in_array($node->type, variable_get('buymeabeer_node_types', array()), TRUE)) {
|
| 110 |
$display_link = true;
|
| 111 |
}
|
| 112 |
if ('comment' == $type) {
|
| 113 |
$display_link = true;
|
| 114 |
}
|
| 115 |
if ($display_link && isset($object->buymeabeer_paypal_mail)) {
|
| 116 |
$paypal_mail = $object->buymeabeer_paypal_mail;
|
| 117 |
$title_text = variable_get('buymeabeer_link_title_text', t(BMAB_LINK_TITLE_TEXT));
|
| 118 |
$href = "https://www.paypal.com/cgi-bin/webscr";
|
| 119 |
$query = "cmd=_xclick&business=$paypal_mail&item_name=$title_text";
|
| 120 |
$links['buymeabeer_user'] = array(
|
| 121 |
'title' => t('Buy %name a beer!', array('%name' => $object->registered_name)),
|
| 122 |
'href' => $href,
|
| 123 |
'query' => $query,
|
| 124 |
'absolute' => TRUE,
|
| 125 |
'html' => TRUE,
|
| 126 |
'attributes' => array('class' => 'buymeabeer-link', 'title' => $title_text)
|
| 127 |
);
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
return $links;
|
| 132 |
}
|
| 133 |
|
| 134 |
/**
|
| 135 |
* theme function for link display
|
| 136 |
*/
|
| 137 |
function theme_buymeabeer_link($link) {
|
| 138 |
return '<div class="buymeabeer">'. $link .'</div>';
|
| 139 |
}
|
| 140 |
|
| 141 |
/**
|
| 142 |
*Implementation of hook_user().
|
| 143 |
*/
|
| 144 |
function buymeabeer_user($op, &$edit, &$account, $category = NULL) {
|
| 145 |
if (variable_get('buymeabeer_userbeer_enable', 0)) {
|
| 146 |
switch ($op) {
|
| 147 |
case 'form':
|
| 148 |
$form['buymeabeer'] = array(
|
| 149 |
'#type' => 'fieldset',
|
| 150 |
'#title' => t('Beer donation settings'),
|
| 151 |
'#collapsible' => TRUE,
|
| 152 |
'#weight' => 10
|
| 153 |
);
|
| 154 |
$form['buymeabeer']['buymeabeer_paypal_mail'] = array(
|
| 155 |
'#type' => 'textfield',
|
| 156 |
'#title' => t('Your paypal email'),
|
| 157 |
'#description' => t('Enter your paypal email address here to receive beer donations'),
|
| 158 |
'#default_value' => $account->buymeabeer_paypal_mail,
|
| 159 |
'#size' => 40,
|
| 160 |
'#maxlength' => 255,
|
| 161 |
);
|
| 162 |
return $form;
|
| 163 |
case 'submit':
|
| 164 |
$edit['data'] = serialize($edit['buymeabeer_paypal_mail']);
|
| 165 |
break;
|
| 166 |
}
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* get the paypal email address of the user with the given uid
|
| 172 |
*
|
| 173 |
* @param Integer
|
| 174 |
* Numeric Drupal user id
|
| 175 |
*
|
| 176 |
* @return String
|
| 177 |
* Paypal email address of user
|
| 178 |
*/
|
| 179 |
function buymeabeer_get_paypal_user_email($uid) {
|
| 180 |
$account = user_load(array('uid' => $uid));
|
| 181 |
if (isset($account->buymeabeer_paypal_mail)) {
|
| 182 |
return $account->buymeabeer_paypal_mail;
|
| 183 |
}
|
| 184 |
return FALSE;
|
| 185 |
}
|