| 1 |
<?php
|
| 2 |
/*
|
| 3 |
* $Id:$
|
| 4 |
* @name PayPalNode Admin
|
| 5 |
* @description Backend administration processes
|
| 6 |
* @author Mike Carter <ixis.co.uk/contact>
|
| 7 |
*
|
| 8 |
*/
|
| 9 |
|
| 10 |
/*
|
| 11 |
paypalnode_settings_form
|
| 12 |
*/
|
| 13 |
function paypalnode_settings() {
|
| 14 |
$form = array();
|
| 15 |
$types = array();
|
| 16 |
$vocabs = array();
|
| 17 |
|
| 18 |
$form['paypal'] = array('#type' => 'fieldset', '#title' => t('PayPal Account'), '#description' => t('You should ensure your PayPal account is set up as a business to accept payments in your selected currency.'));
|
| 19 |
|
| 20 |
$form['paypal']['paypalnode_email_account'] = array(
|
| 21 |
'#type' => 'textfield',
|
| 22 |
'#title' => 'PayPal Account',
|
| 23 |
'#description' => 'The email address associated with your PayPal account that will accept the payments.',
|
| 24 |
'#default_value' => variable_get('paypalnode_email_account', PAYPALNODE_EMAIL_ACCOUNT),
|
| 25 |
'#required' => TRUE
|
| 26 |
);
|
| 27 |
|
| 28 |
$form['paypal']['paypalnode_currency'] = array(
|
| 29 |
'#type' => 'select',
|
| 30 |
'#title' => t('Currency'),
|
| 31 |
'#default_value' => variable_get('paypalnode_currency', PAYPALNODE_CURRENCY),
|
| 32 |
'#options' => array('USD'=>'USD', 'GBP'=>'GBP', 'EUR'=>'EUR'),
|
| 33 |
'#description' => t('Define the currency the merchant PayPal account accepts.'),
|
| 34 |
);
|
| 35 |
|
| 36 |
$form['paypal']['paypalnode_thanks_path'] = array(
|
| 37 |
'#type' => 'textfield',
|
| 38 |
'#title' => t('Thankyou Page'),
|
| 39 |
'#description' => 'The path to a page displaying a thankyou message once the payment has been made. Because the new node will not be published instantly, it is not possible to redirect them to the new content straight away.',
|
| 40 |
'#default_value' => variable_get('paypalnode_thanks_path', PAYPALNODE_THANKS_PATH),
|
| 41 |
'#required' => TRUE
|
| 42 |
);
|
| 43 |
|
| 44 |
$form['paypal']['paypalnode_interstitial'] = array(
|
| 45 |
'#type' => 'textarea',
|
| 46 |
'#title' => t('Interstitial Message'),
|
| 47 |
'#description' => t('An optional message to display after content submission but before being redirected to PayPal. This could be used to inform the user about what is happening. You can include raw HTML here.'),
|
| 48 |
'#rows' => 5,
|
| 49 |
'#default_value' => variable_get('paypalnode_interstitial', '')
|
| 50 |
);
|
| 51 |
|
| 52 |
$form['content'] = array(
|
| 53 |
'#type' => 'fieldset',
|
| 54 |
'#title' => t('Site Content'),
|
| 55 |
'#description' => t('A single !content can be made charagable when used in combination with vocabulary terms configured in the !fee.', array('!content' => l(t('content-type'), 'admin/content/types'), '!fee' => l(t('content Fees'), 'admin/content/fees')))
|
| 56 |
);
|
| 57 |
|
| 58 |
$available_types = node_get_types();
|
| 59 |
foreach($available_types as $type) {
|
| 60 |
$types[$type->type] = $type->name;
|
| 61 |
}
|
| 62 |
asort($types);
|
| 63 |
$form['content']['paypalnode_content_type'] = array(
|
| 64 |
'#type' => 'select',
|
| 65 |
'#title' => t('Content Type'),
|
| 66 |
'#description' => t('The content type you want to charge users for publishing.'),
|
| 67 |
'#options' => $types,
|
| 68 |
'#size' => 1,
|
| 69 |
'#default_value' => variable_get('paypalnode_content_type', PAYPALNODE_CHARGE_TYPE)
|
| 70 |
);
|
| 71 |
|
| 72 |
$available_vocabs = taxonomy_get_vocabularies();
|
| 73 |
foreach($available_vocabs as $vocab) {
|
| 74 |
if(!$vocab->tags) {
|
| 75 |
$vocabs[$vocab->vid] = $vocab->name;
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
$form['content']['paypalnode_vocabularies'] = array(
|
| 80 |
'#type' => 'select',
|
| 81 |
'#title' => t('Vocabularies'),
|
| 82 |
'#description' => t('The vocabularies which contain terms that have an associated price. NOTE: Free tagging vocabularies cannot be used. You can manage costs for combinations of terms in the !fee section.', array('!fee' => l(t('Content Fees'), 'admin/content/fees'))),
|
| 83 |
'#options' => $vocabs,
|
| 84 |
'#size' => 10,
|
| 85 |
'#multiple' => TRUE,
|
| 86 |
'#default_value' => variable_get('paypalnode_vocabularies', PAYPALNODE_CHARGE_VOCABULARIES)
|
| 87 |
);
|
| 88 |
|
| 89 |
$form['expiry'] = array(
|
| 90 |
'#type' => 'fieldset',
|
| 91 |
'#title' => t('Content Expiration'),
|
| 92 |
'#description' => t('Content can be automatically un-published after a certain lifespan specified in days.')
|
| 93 |
);
|
| 94 |
|
| 95 |
$form['expiry']['paypalnode_expiry_days'] = array(
|
| 96 |
'#type' => 'textfield',
|
| 97 |
'#title' => t('Content expiration time'),
|
| 98 |
'#field_suffix' => t('day(s)'),
|
| 99 |
'#description' => t('Time specified as a number of days before the content should be unpublished. %never days means the content will never expire.', array('%never' => t('0'))),
|
| 100 |
'#default_value' => variable_get('paypalnode_expiry_days', '0'),
|
| 101 |
'#size' => 3,
|
| 102 |
);
|
| 103 |
|
| 104 |
$form['expiry']['paypalnode_expiry_notify'] = array(
|
| 105 |
'#type' => 'checkbox',
|
| 106 |
'#title' => t('Notify author'),
|
| 107 |
'#description' => t('Notify author by email about their content expiration.'),
|
| 108 |
'#default_value' => variable_get('paypalnode_expiry_notify', '0'),
|
| 109 |
);
|
| 110 |
|
| 111 |
$form['expiry']['paypalnode_expiry_email_subject'] = array(
|
| 112 |
'#type' => 'textfield',
|
| 113 |
'#title' => t('Notification email subject'),
|
| 114 |
'#description' => t(''),
|
| 115 |
'#default_value' => variable_get('paypalnode_expiry_email_subject', paypalnode_help('notifyemail/subject')),
|
| 116 |
);
|
| 117 |
|
| 118 |
$form['expiry']['paypalnode_expiry_email_body'] = array(
|
| 119 |
'#type' => 'textarea',
|
| 120 |
'#title' => t('Notification email body'),
|
| 121 |
'#rows' => 11,
|
| 122 |
'#description' => t('If the notify author option is activated (above) then an email will be sent out to the author when the content is unpublished. Available variables are: !username, !site, !site-url, !site-name, !expirydays, !title, !created.'),
|
| 123 |
'#default_value' => variable_get('paypalnode_expiry_email_body', paypalnode_help('notifyemail/body'))
|
| 124 |
);
|
| 125 |
|
| 126 |
return system_settings_form($form);
|
| 127 |
}
|
| 128 |
|
| 129 |
|
| 130 |
/*
|
| 131 |
* Token module - used to provide token replacements when writing emails.
|
| 132 |
*/
|
| 133 |
function paypalnode_token_values($type, $object = NULL) {
|
| 134 |
if ($type == 'PayPal Node') {
|
| 135 |
$tokens['expirydays'] = variable_get('paypalnode_expiry_days', 'never');
|
| 136 |
$tokens['created'] = format_date($object['created'], 'large');
|
| 137 |
$tokens['title'] = $object['title'];
|
| 138 |
$tokens['username'] = $object['name'];
|
| 139 |
return $tokens;
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
/*
|
| 145 |
* Token module - used to provide descriptive help about each available token
|
| 146 |
* when writing emails.
|
| 147 |
*/
|
| 148 |
function paypalnode_token_list($type = 'all') {
|
| 149 |
if ($type == 'PayPal Node') {
|
| 150 |
$tokens['PayPal Node']['expirydays'] = t("The expiry date for paid content.");
|
| 151 |
$tokens['PayPal Node']['created'] = t("Full date of when the content was created.");
|
| 152 |
$tokens['PayPal Node']['title'] = t("Title of the content.");
|
| 153 |
$tokens['PayPal Node']['username'] = t('Author name of the content.');
|
| 154 |
return $tokens;
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
|
| 159 |
/*
|
| 160 |
* Price Admin System
|
| 161 |
*/
|
| 162 |
function paypalnode_fee_settings_form() {
|
| 163 |
$form = paypalnode_fee_edit_form();
|
| 164 |
return $form;
|
| 165 |
}
|
| 166 |
|
| 167 |
|
| 168 |
function paypalnode_fee_edit_form($terms = '') {
|
| 169 |
$op = t('Add cost');
|
| 170 |
|
| 171 |
// Check if there are any term names passed as an argument for editing
|
| 172 |
if(arg(4)) {
|
| 173 |
$edit_form = paypalnode_fee_select(arg(4));
|
| 174 |
$op = t('Update item');
|
| 175 |
}
|
| 176 |
|
| 177 |
$form = array();
|
| 178 |
|
| 179 |
$vocabs = variable_get('paypalnode_vocabularies', PAYPALNODE_CHARGE_VOCABULARIES);
|
| 180 |
$form['vocab'] = array('#type' => 'fieldset',
|
| 181 |
'#collapsible' => FALSE);
|
| 182 |
|
| 183 |
// List the form elements in reverse order to match Drupals taxonomy form display
|
| 184 |
rsort($vocabs);
|
| 185 |
|
| 186 |
foreach($vocabs as $vid) {
|
| 187 |
$vocab = taxonomy_get_vocabulary($vid);
|
| 188 |
$form['vocab'][$vid] = array(
|
| 189 |
'#type' => 'textfield',
|
| 190 |
'#title' => $vocab->name,
|
| 191 |
'#autocomplete_path' => 'taxonomy/autocomplete/'. $vocab->vid,
|
| 192 |
'#maxsize' => 64,
|
| 193 |
'#size' => 24,
|
| 194 |
'#default_value' => is_array($edit_form['terms'][$vocab->vid]) ? current($edit_form['terms'][$vocab->vid]) : '',
|
| 195 |
'#weight' => $vocab->weight,
|
| 196 |
);
|
| 197 |
}
|
| 198 |
|
| 199 |
$form['price'] = array(
|
| 200 |
'#type' => 'textfield',
|
| 201 |
'#size' => 10,
|
| 202 |
'#maxlength' => 10,
|
| 203 |
'#default_value' => number_format($edit_form['price'], 2)
|
| 204 |
);
|
| 205 |
$form['submit'] = array(
|
| 206 |
'#type' => 'submit',
|
| 207 |
'#value' => $op,
|
| 208 |
'#weight' => 5
|
| 209 |
);
|
| 210 |
|
| 211 |
$form['#tree'] = TRUE;
|
| 212 |
|
| 213 |
return $form;
|
| 214 |
}
|
| 215 |
|
| 216 |
|
| 217 |
function paypalnode_fee_edit_form_submit($form_id, $form_values) {
|
| 218 |
paypalnode_fee_settings_form_submit($form_id, $form_values);
|
| 219 |
|
| 220 |
return 'admin/content/fees';
|
| 221 |
}
|
| 222 |
|
| 223 |
|
| 224 |
function paypalnode_fee_settings_form_validate($form_id, $form_values) {
|
| 225 |
// check the terms exist
|
| 226 |
}
|
| 227 |
|
| 228 |
|
| 229 |
/*
|
| 230 |
* Clean up the submitted cost entry and save to the database
|
| 231 |
*/
|
| 232 |
function paypalnode_fee_settings_form_submit($form_id, $form_values) {
|
| 233 |
$terms = array();
|
| 234 |
|
| 235 |
foreach($form_values['vocab'] as $term) {
|
| 236 |
$terms[] = '"'.$term.'"';
|
| 237 |
}
|
| 238 |
|
| 239 |
paypalnode_fee_insert($terms, $form_values['price']);
|
| 240 |
}
|
| 241 |
|
| 242 |
|
| 243 |
/*
|
| 244 |
Add or Update a cost entry in the database
|
| 245 |
*/
|
| 246 |
function paypalnode_fee_insert($terms, $price) {
|
| 247 |
$tids = implode(',', paypalnode_terms_to_tids($terms));
|
| 248 |
|
| 249 |
// Attempt to update an existing entry, if any
|
| 250 |
db_query("UPDATE {term_price} SET price = %f WHERE tids = '%s'", $price, $tids);
|
| 251 |
|
| 252 |
// If there was no existing entry, insert a fresh one
|
| 253 |
if(db_affected_rows($result) == 0) {
|
| 254 |
$result = db_query("INSERT INTO {term_price} VALUES ('%s', %f)", $tids, $price);
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
|
| 259 |
/*
|
| 260 |
paypalnode_fee_select
|
| 261 |
*/
|
| 262 |
function paypalnode_fee_select($tids) {
|
| 263 |
$entry = array();
|
| 264 |
|
| 265 |
$term_args = explode(',' , $tids);
|
| 266 |
if(count($term_args) > 0) {
|
| 267 |
|
| 268 |
// Convert term names in to term ids
|
| 269 |
$terms = array();
|
| 270 |
foreach($term_args as $term_name) {
|
| 271 |
$term = taxonomy_get_term_by_name($term_name);
|
| 272 |
$terms[] = $term[0]->tid;
|
| 273 |
$entry['terms'][$term[0]->vid][$term[0]->tid] = $term[0]->name;
|
| 274 |
}
|
| 275 |
|
| 276 |
// Generate a string suitable for accessing the database
|
| 277 |
$tids = implode(',', $terms);
|
| 278 |
}
|
| 279 |
|
| 280 |
// Retrieve matching cost & merge with existing data
|
| 281 |
$price = db_fetch_array(db_query("SELECT * FROM {term_price} WHERE tids = '%s'", $tids));
|
| 282 |
|
| 283 |
if(is_array($price)) {
|
| 284 |
$entry += $price;
|
| 285 |
}
|
| 286 |
|
| 287 |
return $entry;
|
| 288 |
}
|
| 289 |
|
| 290 |
|
| 291 |
/*
|
| 292 |
Delete a cost from the database
|
| 293 |
*/
|
| 294 |
function paypalnode_fee_delete($terms) {
|
| 295 |
$tids = implode(',', paypalnode_terms_to_tids($terms));
|
| 296 |
$r = db_query("DELETE FROM {term_price} WHERE tids = '%s'", $tids);
|
| 297 |
return db_affected_rows($r);
|
| 298 |
}
|
| 299 |
|
| 300 |
|
| 301 |
function paypalnode_fee_delete_form() {
|
| 302 |
$terms = arg(4);
|
| 303 |
|
| 304 |
// If no terms provided then redirect
|
| 305 |
if(!$terms) {
|
| 306 |
drupal_set_message(t('No entry was specified to delete.'), 'error');
|
| 307 |
drupal_goto('admin/content/fees');
|
| 308 |
}
|
| 309 |
|
| 310 |
$form['tids'] = array('#type' => 'value', '#value' => $terms);
|
| 311 |
return confirm_form($form,
|
| 312 |
t('Deletion of %title', array('%title' => $terms)),
|
| 313 |
'admin/content/fees',
|
| 314 |
t('Are you sure you want to delete this cost entry? This action cannot be undone.'),
|
| 315 |
t('Delete'), t('Cancel'));
|
| 316 |
}
|
| 317 |
|
| 318 |
|
| 319 |
function paypalnode_fee_delete_form_submit($form_id, $form_values) {
|
| 320 |
if($form_values['op'] == t('Delete') && $form_values['confirm'] == 1) {
|
| 321 |
$term_string = '"' . str_replace(',', '","', $form_values['tids']) . '"';
|
| 322 |
$terms = explode(',', $term_string);
|
| 323 |
|
| 324 |
if(paypalnode_fee_delete($terms)) {
|
| 325 |
drupal_set_message('Entry deleted');
|
| 326 |
} else {
|
| 327 |
drupal_set_message('Deletion failed.', 'error');
|
| 328 |
}
|
| 329 |
}
|
| 330 |
|
| 331 |
return 'admin/content/fees';
|
| 332 |
}
|
| 333 |
|
| 334 |
|
| 335 |
/*
|
| 336 |
Produce a list of all the current taxonomy combinations and prices
|
| 337 |
USed for the theme table display
|
| 338 |
*/
|
| 339 |
function paypalnode_fee_list() {
|
| 340 |
$i = 0;
|
| 341 |
$costs = array();
|
| 342 |
|
| 343 |
$prices = db_query("SELECT * FROM {term_price} WHERE price != 0 ORDER BY tids");
|
| 344 |
while($row = db_fetch_array($prices)) {
|
| 345 |
$tids = explode(',', $row['tids']);
|
| 346 |
|
| 347 |
foreach($tids as $term_id) {
|
| 348 |
$term = taxonomy_get_term($term_id);
|
| 349 |
$costs[$i]['terms'][$term->vid] = $term->name;
|
| 350 |
}
|
| 351 |
|
| 352 |
$costs[$i]['price'] = number_format($row['price'], 2);
|
| 353 |
|
| 354 |
$i++;
|
| 355 |
}
|
| 356 |
|
| 357 |
return $costs;
|
| 358 |
}
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
function theme_paypalnode_fee_settings_form($form) {
|
| 364 |
|
| 365 |
$header = array(t('Categories'), t('Cost'), array('data' => t('Operations'), 'colspan' => 2));
|
| 366 |
$rows[] = array(drupal_render($form['vocab']), drupal_render($form['price']), array('data' => drupal_render($form['submit']), colspan => 2));
|
| 367 |
|
| 368 |
foreach (paypalnode_fee_list() as $rid => $price) {
|
| 369 |
$terms = implode(', ', $price['terms']);
|
| 370 |
$rows[] = array($terms, $price['price'],
|
| 371 |
l(t('edit'), 'admin/content/fees/edit/'. $terms),
|
| 372 |
l(t('delete'), 'admin/content/fees/delete/'. $terms)
|
| 373 |
);
|
| 374 |
}
|
| 375 |
|
| 376 |
$output = drupal_render($form);
|
| 377 |
$output .= theme('table', $header, $rows, array('width' => '100%'));
|
| 378 |
|
| 379 |
return $output;
|
| 380 |
}
|
| 381 |
|
| 382 |
|
| 383 |
function theme_paypalnode_fee_edit_form($form) {
|
| 384 |
$header = array(t('Categories'), t('Cost'), array('data' => t('Operations'), 'colspan' => 2));
|
| 385 |
$rows[] = array(drupal_render($form['vocab']), drupal_render($form['price']), array('data' => drupal_render($form['submit']), colspan => 2));
|
| 386 |
$output = drupal_render($form);
|
| 387 |
$output .= theme('table', $header, $rows, array('width' => '100%'));
|
| 388 |
|
| 389 |
return $output;
|
| 390 |
}
|