r78728 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78727‎ | r78728 | r78729 >
Date:19:51, 21 December 2010
Author:awjrichards
Status:deferred
Tags:
Comment:
Added address handling to wmf_premiums and moved state handling in queue2civicrm to a function for easier reuse
Modified paths:
  • /civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm.module (modified) (history)
  • /civicrm/trunk/sites/all/modules/wmf_premiums/wmf_premiums.module (modified) (history)

Diff [purge]

Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm.module
@@ -262,20 +262,6 @@
263263 $contact['id'] = $contact_result['contact_id'];
264264
265265
266 - /**
267 - * When passing CiviCRM a state abbreviation, odd things can happen - like getting the right abbreviation, but the wrong state
268 - * So we'll pull back the correct state/province name based off of a user's country/state abbreviation
269 - */
270 - if ( strlen( $msg['country'] ) == 2 ) {
271 - $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.iso_code='" . addslashes( $msg['country'] ) . "' AND s.abbreviation='" . addslashes( $msg['state_province'] ) . "'";
272 - } else {
273 - $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.name='" . addslashes( $msg['country'] ) . "' AND s.abbreviation='" . addslashes( $msg['state_province'] ) . "'";
274 - }
275 - $dao = CRM_Core_DAO::executeQuery( $query );
276 - while ( $dao->fetch() ) {
277 - $state = $dao->state_name;
278 - }
279 -
280266 // Insert the location record
281267 $address = array(
282268 'contact_id' => $contact['id'],
@@ -283,7 +269,7 @@
284270 'street_address' => $msg['street_address'],
285271 'supplemental_address_1' => $msg['supplemental_address_1'],
286272 'city' => $msg['city'],
287 - 'state_province' => ( !is_null( $state )) ? $state : $msg['state_province'],
 273+ 'state_province' => queue2civicrm_get_state( $msg[ 'country' ], $msg['state_province'] ),
288274 'postal_code' => $msg['postal_code'],
289275 'country' => $msg['country'],
290276 'is_primary' => 1,
@@ -365,6 +351,27 @@
366352 }
367353
368354 /**
 355+ * Find correct state for insertion
 356+ *
 357+ * When passing CiviCRM a state abbreviation, odd things can happen - like getting the right abbreviation, but the wrong state
 358+ * So we'll pull back the correct state/province name based off of a user's country/state abbreviation
 359+ */
 360+function queue2civicrm_get_state( $country, $state ) {
 361+
 362+ if ( strlen( $country ) == 2 ) {
 363+ $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.iso_code='" . addslashes( $country ) . "' AND s.abbreviation='" . addslashes( $state ) . "'";
 364+ } else {
 365+ $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.name='" . addslashes( $country ) . "' AND s.abbreviation='" . addslashes( $state ) . "'";
 366+ }
 367+ $dao = CRM_Core_DAO::executeQuery( $query );
 368+ while ( $dao->fetch() ) {
 369+ $state = ( !is_null( $dao->state_name ) ) ? $dao->state_name : $state;
 370+ }
 371+
 372+ return $state;
 373+}
 374+
 375+/**
369376 * Make the form to insert a test message into the queue
370377 */
371378 function queue2civicrm_insertmq_form() {
Index: civicrm/trunk/sites/all/modules/wmf_premiums/wmf_premiums.module
@@ -36,20 +36,43 @@
3737 // make sure that we have premiums to deal with
3838 if ( !$contribution_info[ 'msg' ][ 'size' ] ) {
3939 watchdog( 'wmf_premiums', 'No premium selection for contribution id: %contrib_id', array('%contrib_id' => $contribution_info['contribution_id'] ), WATCHDOG_DEBUG);
40 - return;
 40+ return 0;
4141 }
4242
43 - $query = "INSERT INTO civicrm.premiums (tracking_id, contact_id, contribution_id, shirt, shirt_size, shirt_language )
44 - VALUES ( %d, %d, %d, %d, '%s', '%s' )";
 43+ // prepare the shipping address
 44+ $shipping = wmf_premiums_get_shipping_address( $contribution_info[ 'msg' ] );
4545
 46+ $query = "INSERT INTO civicrm.premiums (
 47+ tracking_id,
 48+ contact_id,
 49+ contribution_id,
 50+ shirt,
 51+ size,
 52+ premium_language,
 53+ name,
 54+ address,
 55+ supplemental_address
 56+ city,
 57+ state,
 58+ postal_code,
 59+ country
 60+ ) VALUES ( %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )";
 61+
4662 watchdog( 'wmf_premiums', 'Preparing to insert premium data with the following contribution info: %contrib_info', array( '%contrib_info' => print_r( $contribution_info, true )), WATCHDOG_DEBUG );
47 - $result = db_query( $query,
 63+ $result = db_query( $query,
4864 $contribution_info['msg']['contribution_tracking_id'],
4965 $contribution_info['contact_id'],
5066 $contribution_info['contribution_id'],
5167 1,
5268 $contribution_info['msg']['size'],
53 - $contribution_info['msg']['premium_language']);
 69+ $contribution_info['msg']['premium_language'],
 70+ $shipping[ 'name' ],
 71+ $shipping[ 'address' ],
 72+ $shipping[ 'supplemental_address' ],
 73+ $shipping[ 'city' ],
 74+ $shipping[ 'state' ],
 75+ $shipping[ 'postal_code' ],
 76+ $shipping[ 'country' ] );
5477 if ( !$result ) {
5578 watchdog( 'wmf_premiums', 'There was an error inserting with the following contrib info: %contribution_info', array( '%contribution_info' => print_r( $contribution_info, true)), WATCHDOG_ERROR );
5679 return false;
@@ -57,15 +80,49 @@
5881 $insert_id = db_last_insert_id( 'civicrm.premiums', 'id' );
5982 watchdog( 'wmf_premiums', 'Created new premiums record with id: %insert_id', array( '%insert_id' => $insert_id ), WATCHDOG_DEBUG );
6083 }
 84+
6185 return $insert_id;
6286 }
6387
 88+/**
 89+ * Determine the shipping address fields
 90+ *
 91+ * @param $trxn_info The full trxn message taken from the queue
 92+ * @return array of shipping information
 93+ */
 94+function wmf_premiums_get_shipping_address( $trxn_info ) {
 95+ $shipping_info = array();
 96+
 97+ // if we have a secondary address, use that for shipping
 98+ if ( strlen( trim( $trxn_info[ 'first_name_2' ] )) && strlen( trim( $trxn_info[ 'last_name_2' ]))) {
 99+ $shipping[ 'name' ] = $trxn_info[ 'first_name_2' ] . " " . $trxn_info[ 'last_name_2' ];
 100+ $shipping[ 'address' ] = $trxn_info[ 'street_address_2' ];
 101+ $shipping[ 'supplemental_address' ] = $trxn_info[ 'supplemental_address_2' ];
 102+ $shipping[ 'city' ] = $trxn_info[ 'city_2' ];
 103+ // account for civi-related state oddness
 104+ $shipping[ 'state' ] = queue2civicrm_get_state( $trxn_info[ 'country_2' ], $trxn_info[ 'state_province_2' ] );
 105+ $shipping[ 'postal_code' ] = $trxn_info[ 'postal_code_2' ];
 106+ $shipping[ 'country' ] = $trxn_info[ 'country_2' ];
 107+ } else {
 108+ $shipping[ 'name' ] = $trxn_info[ 'first_name' ] . " " . $trxn_info[ 'last_name' ];
 109+ $shipping[ 'address' ] = $trxn_info[ 'street_address' ];
 110+ $shipping[ 'supplemental_address' ] = $trxn_info[ 'supplemental_address_1' ];
 111+ $shipping[ 'city' ] = $trxn_info[ 'city' ];
 112+ // civi-related state oddness already handled by queue2civicrm
 113+ $shipping[ 'state' ] = $trxn_info[ 'state_province' ];
 114+ $shipping[ 'postal_code' ] = $trxn_info[ 'postal_code' ];
 115+ $shipping[ 'country' ] = $trxn_info[ 'country' ];
 116+ }
 117+
 118+ return $shipping_info;
 119+}
 120+
64121 function wmf_premiums_test_insert() {
65122 $contribution_info = array(
66123 'msg' => array(
67 - 'size' => 'large',
68 - 'contribution_tracking_id' => 31337,
69 - 'premium_language' => 'en' ),
 124+ 'size' => 'large',
 125+ 'contribution_tracking_id' => 31337,
 126+ 'premium_language' => 'en' ),
70127 'contact_id' => 1234,
71128 'contribution_id' => 4321,
72129 );

Status & tagging log