Index: civicrm/trunk/sites/all/modules/queue2civicrm/recurring/recurring.module |
— | — | @@ -206,6 +206,8 @@ |
207 | 207 | * @return int |
208 | 208 | */ |
209 | 209 | function recurring_import_subscr_payment( $msg ) { |
| 210 | + global $queue2civicrm_dbs; |
| 211 | + |
210 | 212 | /** |
211 | 213 | * if the subscr_id is not set, we can't process it due to an error in the message. |
212 | 214 | * |
— | — | @@ -247,6 +249,7 @@ |
248 | 250 | * for this contribution. |
249 | 251 | */ |
250 | 252 | if ( $msg[ 'contribution_tracking_update' ] ) { |
| 253 | + |
251 | 254 | // Map the tracking record to the CiviCRM contribution |
252 | 255 | _queue2civicrm_update_contribution_tracking( $msg, $contribution ); |
253 | 256 | |
— | — | @@ -261,8 +264,10 @@ |
262 | 265 | } |
263 | 266 | |
264 | 267 | // update subscription record with next payment date |
265 | | - $query = "UPDATE {civicrm.civicrm_contribution_recur} SET next_sched_contribution='%s'"; |
| 268 | + $queue2civicrm_dbs->use_civicrm(); |
| 269 | + $query = "UPDATE {civicrm_contribution_recur} SET next_sched_contribution='%s'"; |
266 | 270 | db_query( $query, date('Y-m-d H:i:s', strtotime( "+" . $msg[ 'frequency_interval' ] . " " . $msg[ 'frequency_unit' ], $msg[ 'payment_date' ] ))); |
| 271 | + $queue2civicrm_dbs->use_default(); |
267 | 272 | |
268 | 273 | // construct an array of useful info to invocations of queue2civicrm_import |
269 | 274 | $contribution_info = array( |
— | — | @@ -324,6 +329,7 @@ |
325 | 330 | * @return int |
326 | 331 | */ |
327 | 332 | function recurring_import_subscr_signup( $msg ) { |
| 333 | + global $queue2civicrm_dbs; |
328 | 334 | // ensure there is not already a record of this account - if so, mark the message as succesfuly processed |
329 | 335 | if ( $recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) { |
330 | 336 | watchdog( 'recurring', 'Subscription account already exists: %msg', array( '%msg' => print_r( $msg, true )), WATCHDOG_NOTICE ); |
— | — | @@ -340,7 +346,7 @@ |
341 | 347 | //$tag = _queue2civicrm_tag_insert( $contact ); |
342 | 348 | |
343 | 349 | // prepare query for inserting data to civicrm_contribution_recur |
344 | | - $query = "INSERT INTO {civicrm.civicrm_contribution_recur} ( |
| 350 | + $query = "INSERT INTO {civicrm_contribution_recur} ( |
345 | 351 | contact_id, |
346 | 352 | amount, |
347 | 353 | frequency_unit, |
— | — | @@ -352,25 +358,27 @@ |
353 | 359 | next_sched_contribution ) |
354 | 360 | VALUES ( %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s' )"; |
355 | 361 | |
356 | | - $result = db_query( $query, |
357 | | - $contact[ 'contact_id' ], |
358 | | - $msg[ 'amount' ], |
359 | | - $msg[ 'frequency_unit' ], |
360 | | - $msg[ 'frequency_interval' ], |
361 | | - $msg[ 'installments' ], |
362 | | - date( 'Y-m-d H:i:s', $msg[ 'start_date' ] ), |
363 | | - date( 'Y-m-d H:i:s', $msg[ 'create_date' ] ), |
364 | | - $msg[ 'subscr_id' ], |
365 | | - date( 'Y-m-d H:i:s', strtotime( "+" . $msg[ 'frequency_interval' ] . " " . $msg[ 'frequency_unit' ], $msg[ 'start_date' ] )) |
366 | | - ); |
| 362 | + $queue2civicrm_dbs->use_civicrm(); |
| 363 | + $result = db_query( $query, |
| 364 | + $contact[ 'contact_id' ], |
| 365 | + $msg[ 'amount' ], |
| 366 | + $msg[ 'frequency_unit' ], |
| 367 | + $msg[ 'frequency_interval' ], |
| 368 | + $msg[ 'installments' ], |
| 369 | + date( 'Y-m-d H:i:s', $msg[ 'start_date' ] ), |
| 370 | + date( 'Y-m-d H:i:s', $msg[ 'create_date' ] ), |
| 371 | + $msg[ 'subscr_id' ], |
| 372 | + date( 'Y-m-d H:i:s', strtotime( "+" . $msg[ 'frequency_interval' ] . " " . $msg[ 'frequency_unit' ], $msg[ 'start_date' ] )) |
| 373 | + ); |
| 374 | + $queue2civicrm_dbs->use_default(); |
367 | 375 | |
368 | | - if ( !$result ) { |
369 | | - watchdog( 'recurring', 'Failed inserting subscriber signup for subscriber id: %subscr_id', array( '%subscr_id' => print_r( $msg['subscr_id'], true )), WATCHDOG_NOTICE ); |
370 | | - return 0; |
371 | | - } else { |
372 | | - watchdog( 'recurring', 'Succesfully inserted subscription signup for subscriber id: %subscr_id ', array( '%subscr_id' => print_r( $msg[ 'subscr_id' ], true )), WATCHDOG_NOTICE ); |
373 | | - return 1; |
374 | | - } |
| 376 | + if ( !$result ) { |
| 377 | + watchdog( 'recurring', 'Failed inserting subscriber signup for subscriber id: %subscr_id', array( '%subscr_id' => print_r( $msg['subscr_id'], true )), WATCHDOG_NOTICE ); |
| 378 | + return 0; |
| 379 | + } else { |
| 380 | + watchdog( 'recurring', 'Succesfully inserted subscription signup for subscriber id: %subscr_id ', array( '%subscr_id' => print_r( $msg[ 'subscr_id' ], true )), WATCHDOG_NOTICE ); |
| 381 | + return 1; |
| 382 | + } |
375 | 383 | } |
376 | 384 | |
377 | 385 | /** |
— | — | @@ -380,18 +388,23 @@ |
381 | 389 | * @return int |
382 | 390 | */ |
383 | 391 | function recurring_import_subscr_cancel( $msg ) { |
| 392 | + global $queue2civicrm_dbs; |
384 | 393 | // ensure we have a record of the subscription |
385 | 394 | if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) { |
386 | 395 | watchdog( 'recurring', 'Subscription account does not exist: %msg', array( '%msg' => print_r( $msg, true )), WATCHDOG_NOTICE ); |
387 | 396 | return 2; |
388 | 397 | } |
389 | 398 | |
390 | | - $query = "UPDATE {civicrm.civicrm_contribution_recur} SET cancel_date='%s', end_date='%s' WHERE trxn_id='%s'"; |
| 399 | + $query = "UPDATE {civicrm_contribution_recur} SET cancel_date='%s', end_date='%s' WHERE trxn_id='%s'"; |
| 400 | + |
| 401 | + $queue2civicrm_dbs->use_civicrm(); |
391 | 402 | $result = db_query( |
392 | 403 | $query, |
393 | 404 | date( 'Y-m-d H:i:s', $msg[ 'cancel_date' ] ), |
394 | 405 | date( 'Y-m-d H:i:s', $msg[ 'cancel_date' ] ), |
395 | 406 | $msg[ 'subscr_id' ] ); |
| 407 | + $queue2civicrm_dbs->use_default(); |
| 408 | + |
396 | 409 | if ( !$result ) { |
397 | 410 | watchdog( 'recurring', 'There was a problem updating the subscription for cancelation for subscriber id: %subscr_id', array( '%subscr_id' => print_r( $msg[ 'subscr_id' ], true )), WATCHDOG_NOTICE ); |
398 | 411 | return 0; |
— | — | @@ -408,14 +421,20 @@ |
409 | 422 | * @return int |
410 | 423 | */ |
411 | 424 | function recurring_import_subscr_eot( $msg ) { |
| 425 | + global $queue2civicrm_dbs; |
| 426 | + |
412 | 427 | // ensure we have a record of the subscription |
413 | 428 | if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) { |
414 | 429 | watchdog( 'recurring', 'Subscription account does not exist: %msg', array( '%msg' => print_r( $msg, true )), WATCHDOG_NOTICE ); |
415 | 430 | return 2; |
416 | 431 | } |
417 | 432 | |
418 | | - $query = "UPDATE {civicrm.civicrm_contribution_recur} SET end_date='%s', next_sched_contribution=NULL WHERE trxn_id='%s'"; |
| 433 | + $query = "UPDATE {civicrm_contribution_recur} SET end_date='%s', next_sched_contribution=NULL WHERE trxn_id='%s'"; |
| 434 | + |
| 435 | + $queue2civicrm_dbs->use_civicrm(); |
419 | 436 | $result = db_query( $query, date( 'Y-m-d H:i:s' ), $msg[ 'subscr_id' ] ); |
| 437 | + $queue2civicrm_dbs->use_default(); |
| 438 | + |
420 | 439 | if ( !$result ) { |
421 | 440 | watchdog( 'recurring', 'There was a problem updating the subscription for EOT for subscription id: %subscr_id', array( '%subscr_id' => print_r( $msg[ 'subscr_id' ], true )), WATCHDOG_NOTICE ); |
422 | 441 | return 0; |
— | — | @@ -435,6 +454,8 @@ |
436 | 455 | * @return int |
437 | 456 | */ |
438 | 457 | function recurring_import_subscr_modify( $msg ) { |
| 458 | + global $queue2civicrm_dbs; |
| 459 | + |
439 | 460 | // ensure we have a record of the subscription |
440 | 461 | if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) { |
441 | 462 | watchdog( 'recurring', 'Subscription account does not exist for subscription id: %subscr_id', array( '%subscr_id' => print_r( $msg, true )), WATCHDOG_NOTICE ); |
— | — | @@ -442,7 +463,7 @@ |
443 | 464 | } |
444 | 465 | |
445 | 466 | // prepare query for updating data to civicrm_contribution_recur |
446 | | - $query = "UPDATE {civicrm.civicrm_contribution_recur} SET |
| 467 | + $query = "UPDATE {civicrm_contribution_recur} SET |
447 | 468 | SET |
448 | 469 | amount=%d, |
449 | 470 | frequency_unit='%s', |
— | — | @@ -452,31 +473,33 @@ |
453 | 474 | WHERE |
454 | 475 | trxn_id='%s'"; |
455 | 476 | |
456 | | - $result = db_query( $query, |
457 | | - $msg[ 'amount' ], |
458 | | - $msg[ 'frequency_unit' ], |
459 | | - $msg[ 'frequency_interval' ], |
460 | | - date( 'Y-m-d H:i:s', $msg[ 'modified_date' ] ), |
461 | | - date( 'Y-m-d H:i:s', strtotime( "+" . $msg[ 'frequency_interval' ] . " " . $msg[ 'frequency_unit' ], $msg[ 'start_date' ] )), |
462 | | - $msg[ 'subscr_id' ] |
463 | | - ); |
| 477 | + $queue2civicrm_dbs->use_civicrm(); |
| 478 | + $result = db_query( $query, |
| 479 | + $msg[ 'amount' ], |
| 480 | + $msg[ 'frequency_unit' ], |
| 481 | + $msg[ 'frequency_interval' ], |
| 482 | + date( 'Y-m-d H:i:s', $msg[ 'modified_date' ] ), |
| 483 | + date( 'Y-m-d H:i:s', strtotime( "+" . $msg[ 'frequency_interval' ] . " " . $msg[ 'frequency_unit' ], $msg[ 'start_date' ] )), |
| 484 | + $msg[ 'subscr_id' ] |
| 485 | + ); |
| 486 | + $queue2civicrm_dbs->use_default(); |
464 | 487 | |
465 | | - if ( !$result ) { |
466 | | - watchdog( 'recurring', 'There was a problem updating the subscription record for subscription id ', print_r( $msg['subscr_id'], true), WATCHDOG_NOTICE ); |
467 | | - return 0; |
468 | | - } |
| 488 | + if ( !$result ) { |
| 489 | + watchdog( 'recurring', 'There was a problem updating the subscription record for subscription id ', print_r( $msg['subscr_id'], true), WATCHDOG_NOTICE ); |
| 490 | + return 0; |
| 491 | + } |
469 | 492 | |
470 | | - // update the contact |
471 | | - $contact = _queue2civicrm_contact_update( $msg, $recur_record->contact_id ); |
| 493 | + // update the contact |
| 494 | + $contact = _queue2civicrm_contact_update( $msg, $recur_record->contact_id ); |
472 | 495 | |
473 | | - // Insert the location record |
474 | | - $location = _queue2civicrm_location_update( $msg, $contact ); |
| 496 | + // Insert the location record |
| 497 | + $location = _queue2civicrm_location_update( $msg, $contact ); |
475 | 498 | |
476 | | - // Tag contact for review |
477 | | - $tag = _queue2civicrm_tag_insert( $contact ); |
| 499 | + // Tag contact for review |
| 500 | + $tag = _queue2civicrm_tag_insert( $contact ); |
478 | 501 | |
479 | | - watchdog( 'recurring', 'Subscription succesfully modified for subscription id: %subscr_id', array( '%subscr_id' => print_r( $msg['subscr_id'], true )), WATCHDOG_NOTICE ); |
480 | | - return 1; |
| 502 | + watchdog( 'recurring', 'Subscription succesfully modified for subscription id: %subscr_id', array( '%subscr_id' => print_r( $msg['subscr_id'], true )), WATCHDOG_NOTICE ); |
| 503 | + return 1; |
481 | 504 | } |
482 | 505 | |
483 | 506 | /** |
— | — | @@ -485,14 +508,20 @@ |
486 | 509 | * @return unknown_type |
487 | 510 | */ |
488 | 511 | function recurring_import_subscr_failed( $msg ) { |
| 512 | + global $queue2civicrm_dbs; |
| 513 | + |
489 | 514 | // ensure we have a record of the subscription |
490 | 515 | if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) { |
491 | 516 | watchdog( 'recurring', 'Subscription account does not exist for subscription: %subscription', array( "%subscription" => print_r( $msg, true )), WATCHDOG_NOTICE ); |
492 | 517 | return 2; |
493 | 518 | } |
494 | 519 | |
495 | | - $query = "UPDATE {civicrm.civicrm_contribution_recur} SET failure_count=%d, failure_retry_date='%s' WHERE trxn_id='%s'"; |
| 520 | + $query = "UPDATE {civicrm_contribution_recur} SET failure_count=%d, failure_retry_date='%s' WHERE trxn_id='%s'"; |
| 521 | + |
| 522 | + $queue2civicrm_dbs->use_civicrm(); |
496 | 523 | $result = db_query( $query, $msg[ 'failure_count' ], date( 'Y-m-d H:i:s', $msg[ 'failure_retry_date' ] ), $msg[ 'subscr_id' ] ); |
| 524 | + $queue2civicrm_dbs->use_default(); |
| 525 | + |
497 | 526 | if ( !$result ) { |
498 | 527 | watchdog( 'recurring', 'There was a problem updating the subscription for failed payment for subscriber id: %subscr_id ', array( '%subscr_id' => print_r( $msg[ 'subscr_id' ], true )), WATCHDOG_NOTICE ); |
499 | 528 | return 0; |
— | — | @@ -508,8 +537,13 @@ |
509 | 538 | * @return object |
510 | 539 | */ |
511 | 540 | function recurring_get_recur_record( $subscr_id ) { |
512 | | - $query = "SELECT * FROM civicrm.civicrm_contribution_recur WHERE trxn_id = '%s'"; |
| 541 | + global $queue2civicrm_dbs; |
| 542 | + $query = "SELECT * FROM civicrm_contribution_recur WHERE trxn_id = '%s'"; |
| 543 | + |
| 544 | + $queue2civicrm_dbs->use_civicrm(); |
513 | 545 | $recur_record = db_fetch_object( db_query( $query, $subscr_id ) ); |
| 546 | + $queue2civicrm_dbs->use_default(); |
| 547 | + |
514 | 548 | watchdog( 'recurring', 'Recurring record: %recur_record', array( '%recur_record' => print_r( $recur_record, true )), WATCHDOG_DEBUG ); |
515 | 549 | return $recur_record; |
516 | 550 | } |
— | — | @@ -550,6 +584,7 @@ |
551 | 585 | * @return array |
552 | 586 | */ |
553 | 587 | function recurring_normalize_paypal_msg( $msg ) { |
| 588 | + global $queue2civicrm_dbs; |
554 | 589 | $msg_normalized = array(); |
555 | 590 | |
556 | 591 | $timestamp = ( !isset( $msg[ 'payment_date' ] ) || !strlen( $msg[ 'payment_date' ] )) ? strtotime( "now" ) : strtotime( $msg[ 'payment_date' ] ); |
Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_common.inc |
— | — | @@ -3,6 +3,9 @@ |
4 | 4 | * Common functions for queue2civicrm modules |
5 | 5 | */ |
6 | 6 | |
| 7 | +// include the database switching class |
| 8 | +require_once( drupal_get_path( 'module', 'queue2civicrm' ) . '/queue2civicrm_db_switcher.inc' ); |
| 9 | + |
7 | 10 | /** |
8 | 11 | * Connect using the Stomp library. |
9 | 12 | */ |
Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_db_switcher.inc |
— | — | @@ -0,0 +1,47 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Class to facilitate swithing of db access between CiviCRM and Drupal |
| 5 | + */ |
| 6 | + |
| 7 | +$queue2civicrm_dbs = new queue2civicrm_db_switcher(); |
| 8 | + |
| 9 | +class queue2civicrm_db_switcher { |
| 10 | + |
| 11 | + public function __construct() { |
| 12 | + global $db_url; |
| 13 | + |
| 14 | + /** |
| 15 | + * Dynamically configure the CiviCRM database |
| 16 | + * |
| 17 | + * Borrowed from old civicrm_api module |
| 18 | + */ |
| 19 | + if (!is_array($db_url) || !array_key_exists('civicrm', $db_url)) { |
| 20 | + require_once conf_path() . '/civicrm.settings.php'; |
| 21 | + |
| 22 | + $dsn = str_replace( '?new_link=true', '', CIVICRM_DSN ); |
| 23 | + $dsn = str_replace( 'mysql:', 'mysqli:', CIVICRM_DSN ); |
| 24 | + |
| 25 | + if (is_array($db_url)) { |
| 26 | + $db_url[ 'civicrm' ] = $dsn; |
| 27 | + } |
| 28 | + else { |
| 29 | + $db_url = array( |
| 30 | + 'default' => $db_url, |
| 31 | + 'civicrm' => $dsn, |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public function use_civicrm() { |
| 38 | + return db_set_active( 'civicrm' ); |
| 39 | + } |
| 40 | + |
| 41 | + public function use_default() { |
| 42 | + return db_set_active(); |
| 43 | + } |
| 44 | + |
| 45 | + public function __destruct() { |
| 46 | + $this->use_default(); |
| 47 | + } |
| 48 | +} |
\ No newline at end of file |