r80589 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80588‎ | r80589 | r80590 >
Date:21:09, 19 January 2011
Author:awjrichards
Status:deferred
Tags:
Comment:
Followup r79965; Added class to facilitate db switching and dynamically connecting to civicrm db; Updated queue2civicrm and recurring module code to use new db switcher
Modified paths:
  • /civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_common.inc (modified) (history)
  • /civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_db_switcher.inc (added) (history)
  • /civicrm/trunk/sites/all/modules/queue2civicrm/recurring/recurring.module (modified) (history)

Diff [purge]

Index: civicrm/trunk/sites/all/modules/queue2civicrm/recurring/recurring.module
@@ -206,6 +206,8 @@
207207 * @return int
208208 */
209209 function recurring_import_subscr_payment( $msg ) {
 210+ global $queue2civicrm_dbs;
 211+
210212 /**
211213 * if the subscr_id is not set, we can't process it due to an error in the message.
212214 *
@@ -247,6 +249,7 @@
248250 * for this contribution.
249251 */
250252 if ( $msg[ 'contribution_tracking_update' ] ) {
 253+
251254 // Map the tracking record to the CiviCRM contribution
252255 _queue2civicrm_update_contribution_tracking( $msg, $contribution );
253256
@@ -261,8 +264,10 @@
262265 }
263266
264267 // 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'";
266270 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();
267272
268273 // construct an array of useful info to invocations of queue2civicrm_import
269274 $contribution_info = array(
@@ -324,6 +329,7 @@
325330 * @return int
326331 */
327332 function recurring_import_subscr_signup( $msg ) {
 333+ global $queue2civicrm_dbs;
328334 // ensure there is not already a record of this account - if so, mark the message as succesfuly processed
329335 if ( $recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) {
330336 watchdog( 'recurring', 'Subscription account already exists: %msg', array( '%msg' => print_r( $msg, true )), WATCHDOG_NOTICE );
@@ -340,7 +346,7 @@
341347 //$tag = _queue2civicrm_tag_insert( $contact );
342348
343349 // prepare query for inserting data to civicrm_contribution_recur
344 - $query = "INSERT INTO {civicrm.civicrm_contribution_recur} (
 350+ $query = "INSERT INTO {civicrm_contribution_recur} (
345351 contact_id,
346352 amount,
347353 frequency_unit,
@@ -352,25 +358,27 @@
353359 next_sched_contribution )
354360 VALUES ( %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s' )";
355361
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();
367375
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+ }
375383 }
376384
377385 /**
@@ -380,18 +388,23 @@
381389 * @return int
382390 */
383391 function recurring_import_subscr_cancel( $msg ) {
 392+ global $queue2civicrm_dbs;
384393 // ensure we have a record of the subscription
385394 if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) {
386395 watchdog( 'recurring', 'Subscription account does not exist: %msg', array( '%msg' => print_r( $msg, true )), WATCHDOG_NOTICE );
387396 return 2;
388397 }
389398
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();
391402 $result = db_query(
392403 $query,
393404 date( 'Y-m-d H:i:s', $msg[ 'cancel_date' ] ),
394405 date( 'Y-m-d H:i:s', $msg[ 'cancel_date' ] ),
395406 $msg[ 'subscr_id' ] );
 407+ $queue2civicrm_dbs->use_default();
 408+
396409 if ( !$result ) {
397410 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 );
398411 return 0;
@@ -408,14 +421,20 @@
409422 * @return int
410423 */
411424 function recurring_import_subscr_eot( $msg ) {
 425+ global $queue2civicrm_dbs;
 426+
412427 // ensure we have a record of the subscription
413428 if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) {
414429 watchdog( 'recurring', 'Subscription account does not exist: %msg', array( '%msg' => print_r( $msg, true )), WATCHDOG_NOTICE );
415430 return 2;
416431 }
417432
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();
419436 $result = db_query( $query, date( 'Y-m-d H:i:s' ), $msg[ 'subscr_id' ] );
 437+ $queue2civicrm_dbs->use_default();
 438+
420439 if ( !$result ) {
421440 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 );
422441 return 0;
@@ -435,6 +454,8 @@
436455 * @return int
437456 */
438457 function recurring_import_subscr_modify( $msg ) {
 458+ global $queue2civicrm_dbs;
 459+
439460 // ensure we have a record of the subscription
440461 if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) {
441462 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 @@
443464 }
444465
445466 // prepare query for updating data to civicrm_contribution_recur
446 - $query = "UPDATE {civicrm.civicrm_contribution_recur} SET
 467+ $query = "UPDATE {civicrm_contribution_recur} SET
447468 SET
448469 amount=%d,
449470 frequency_unit='%s',
@@ -452,31 +473,33 @@
453474 WHERE
454475 trxn_id='%s'";
455476
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();
464487
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+ }
469492
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 );
472495
473 - // Insert the location record
474 - $location = _queue2civicrm_location_update( $msg, $contact );
 496+ // Insert the location record
 497+ $location = _queue2civicrm_location_update( $msg, $contact );
475498
476 - // Tag contact for review
477 - $tag = _queue2civicrm_tag_insert( $contact );
 499+ // Tag contact for review
 500+ $tag = _queue2civicrm_tag_insert( $contact );
478501
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;
481504 }
482505
483506 /**
@@ -485,14 +508,20 @@
486509 * @return unknown_type
487510 */
488511 function recurring_import_subscr_failed( $msg ) {
 512+ global $queue2civicrm_dbs;
 513+
489514 // ensure we have a record of the subscription
490515 if ( !$recur_record = recurring_get_recur_record( $msg[ 'subscr_id' ] ) ) {
491516 watchdog( 'recurring', 'Subscription account does not exist for subscription: %subscription', array( "%subscription" => print_r( $msg, true )), WATCHDOG_NOTICE );
492517 return 2;
493518 }
494519
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();
496523 $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+
497526 if ( !$result ) {
498527 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 );
499528 return 0;
@@ -508,8 +537,13 @@
509538 * @return object
510539 */
511540 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();
513545 $recur_record = db_fetch_object( db_query( $query, $subscr_id ) );
 546+ $queue2civicrm_dbs->use_default();
 547+
514548 watchdog( 'recurring', 'Recurring record: %recur_record', array( '%recur_record' => print_r( $recur_record, true )), WATCHDOG_DEBUG );
515549 return $recur_record;
516550 }
@@ -550,6 +584,7 @@
551585 * @return array
552586 */
553587 function recurring_normalize_paypal_msg( $msg ) {
 588+ global $queue2civicrm_dbs;
554589 $msg_normalized = array();
555590
556591 $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 @@
44 * Common functions for queue2civicrm modules
55 */
66
 7+// include the database switching class
 8+require_once( drupal_get_path( 'module', 'queue2civicrm' ) . '/queue2civicrm_db_switcher.inc' );
 9+
710 /**
811 * Connect using the Stomp library.
912 */
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

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r79965Adding new submodule to queue2civicrm to handle processing of 'subscription' ...awjrichards22:21, 10 January 2011

Status & tagging log