From: David Thompson Date: Mon, 17 Nov 2014 21:16:18 +0000 (-0500) Subject: Wrap hook in a try/catch block. X-Git-Tag: v0.1~7 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8abf106888aae2a4223b0ee4ae9c49d55c02411d;p=org.fsf.memberdashboard.git Wrap hook in a try/catch block. * memberdashboard.php (memberdashboard_civicrm_post): Wrap DB and API calls in a try/catch. --- diff --git a/memberdashboard.php b/memberdashboard.php index e650b1b..a8890f0 100644 --- a/memberdashboard.php +++ b/memberdashboard.php @@ -121,45 +121,49 @@ function memberdashboard_civicrm_post($op, $objectName, $objectId, &$objectRef) $triggerOps = array('create', 'edit', 'delete'); if($objectName == 'Membership' && in_array($op, $triggerOps)) { - // TODO: Extract all of this to a class for clarity and - // organization's sake. - - // Get the oldest join date for the contact's memberships. - $contactId = $objectRef->contact_id; - $dao = CRM_Core_DAO::executeQuery( - 'SELECT join_date FROM civicrm_membership WHERE contact_id=%1 ORDER BY join_date ASC LIMIT 1', - array( 1 => array($contactId, 'Integer') ) - ); - - if($dao->fetch()) { - // Make the API call. - $joinDate = $dao->join_date; - $apiUrl = civicrm_api3('setting', 'getvalue', array( - 'name' => 'memberdashboard_button_api_url', - 'group' => MEMBERDASHBOARD_SETTINGS_GROUP - )); - $user = civicrm_api3('setting', 'getvalue', array( - 'name' => 'memberdashboard_button_api_user', - 'group' => MEMBERDASHBOARD_SETTINGS_GROUP - )); - $password = civicrm_api3('setting', 'getvalue', array( - 'name' => 'memberdashboard_button_api_password', - 'group' => MEMBERDASHBOARD_SETTINGS_GROUP - )); - $params = http_build_query(array( - 'contact_id' => $contactId, - 'date' => $joinDate - )); - - if(!empty($apiUrl) && !empty($user) && !empty($password)) { - $curl = curl_init(); - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($curl, CURLOPT_USERPWD, "$user:$password"); - curl_setopt($curl, CURLOPT_URL, "$apiUrl?$params"); - curl_exec($curl); + try { + // TODO: Extract all of this to a class for clarity and + // organization's sake. + + // Get the oldest join date for the contact's memberships. + $contactId = $objectRef->contact_id; + $dao = CRM_Core_DAO::executeQuery( + 'SELECT join_date FROM civicrm_membership WHERE contact_id=%1 ORDER BY join_date ASC LIMIT 1', + array( 1 => array($contactId, 'Integer') ) + ); + + if($dao->fetch()) { + // Make the API call. + $joinDate = $dao->join_date; + $apiUrl = civicrm_api3('setting', 'getvalue', array( + 'name' => 'memberdashboard_button_api_url', + 'group' => MEMBERDASHBOARD_SETTINGS_GROUP + )); + $user = civicrm_api3('setting', 'getvalue', array( + 'name' => 'memberdashboard_button_api_user', + 'group' => MEMBERDASHBOARD_SETTINGS_GROUP + )); + $password = civicrm_api3('setting', 'getvalue', array( + 'name' => 'memberdashboard_button_api_password', + 'group' => MEMBERDASHBOARD_SETTINGS_GROUP + )); + $params = http_build_query(array( + 'contact_id' => $contactId, + 'date' => $joinDate + )); + + if(!empty($apiUrl) && !empty($user) && !empty($password)) { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($curl, CURLOPT_USERPWD, "$user:$password"); + curl_setopt($curl, CURLOPT_URL, "$apiUrl?$params"); + curl_exec($curl); + } } - } - $dao->free(); + $dao->free(); + } catch(Exception $e) { + // Ignore it. Not the end of the world if this fails. + } } }