From fdb5b92e5bf2d51ed124d4693dddadde75025e96 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 17 Nov 2014 14:26:14 -0500 Subject: [PATCH] Add civicrm_post hook to regenerate member buttons. * CRM/Admin/Form/Setting/MemberDashboard.php (CRM_Admin_Form_Setting_MemberDashboard) [buildQuickForm]: Add inputs for API settings. [postProcess]: Update API settings. * memberdashboard.php (memberdashboard_civicrm_post): New function. * settings/MemberDashboard.setting.php: Add url, user, and password settings. * templates/CRM/Admin/Form/Setting/MemberDashboard.tpl: Render new setting form inputs. --- CRM/Admin/Form/Setting/MemberDashboard.php | 17 +++++-- memberdashboard.php | 51 +++++++++++++++++++ settings/MemberDashboard.setting.php | 35 ++++++++++++- .../Admin/Form/Setting/MemberDashboard.tpl | 24 +++++++++ 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/CRM/Admin/Form/Setting/MemberDashboard.php b/CRM/Admin/Form/Setting/MemberDashboard.php index 5b0bda4..a2b32f3 100644 --- a/CRM/Admin/Form/Setting/MemberDashboard.php +++ b/CRM/Admin/Form/Setting/MemberDashboard.php @@ -45,6 +45,12 @@ class CRM_Admin_Form_Setting_MemberDashboard extends CRM_Admin_Form_Setting { public function buildQuickForm() { $this->add('select', 'memberdashboard_info_profile_id', 'Member Info Profile', $this->fetchProfiles()); + $this->add('text', 'memberdashboard_button_api_url', + 'Member Button API URL'); + $this->add('text', 'memberdashboard_button_api_user', + 'Member Button API User'); + $this->add('text', 'memberdashboard_button_api_password', + 'Member Button API Password'); $this->applyFilter('__ALL__', 'trim'); $this->addButtons(array( array( @@ -57,11 +63,16 @@ class CRM_Admin_Form_Setting_MemberDashboard extends CRM_Admin_Form_Setting { public function postProcess() { $params = $this->exportValues(); - $key = 'memberdashboard_info_profile_id'; - $value = $params[$key]; + $profileKey = 'memberdashboard_info_profile_id'; + $urlKey = 'memberdashboard_button_api_url'; + $userKey = 'memberdashboard_button_api_user'; + $passwordKey = 'memberdashboard_button_api_password'; civicrm_api3('setting', 'create', array( - $key => $value + $profileKey => $params[$profileKey], + $urlKey => $params[$urlKey], + $userKey => $params[$userKey], + $passwordKey => $params[$passwordKey] )); CRM_Core_Session::setStatus(ts('Settings saved.'), '', 'success'); diff --git a/memberdashboard.php b/memberdashboard.php index f06aabc..f84f76f 100644 --- a/memberdashboard.php +++ b/memberdashboard.php @@ -109,3 +109,54 @@ function memberdashboard_civicrm_caseTypes(&$caseTypes) { function memberdashboard_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { _memberdashboard_civix_civicrm_alterSettingsFolders($metaDataFolders); } + +/** + * Implementation of hook_civicrm_post + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post + */ +function memberdashboard_civicrm_post($op, $objectName, $objectId, &$objectRef) { + // When a membership is modified, make a RPC to regenerate the + // member button for the contact. + $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 + )); + $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(); + } +} diff --git a/settings/MemberDashboard.setting.php b/settings/MemberDashboard.setting.php index 0af6b22..4456566 100644 --- a/settings/MemberDashboard.setting.php +++ b/settings/MemberDashboard.setting.php @@ -31,5 +31,38 @@ return array( 'is_contact' => 0, 'description' => 'Info Profile ID', 'help_text' => 'Profile ID for member-editable contact information' + ), + 'memberdashboard_button_api_url' => array( + 'group_name' => 'Member Dashboard Preferences', + 'group' => 'memberdashboard', + 'name' => 'memberdashboard_button_api_url', + 'type' => 'String', + 'default' => '', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'Member button API URL', + 'help_text' => 'The location of the FSF member button generator API' + ), + 'memberdashboard_button_api_user' => array( + 'group_name' => 'Member Dashboard Preferences', + 'group' => 'memberdashboard', + 'name' => 'memberdashboard_button_api_user', + 'type' => 'String', + 'default' => '', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'User name for logging into the member button API', + 'help_text' => '' + ), + 'memberdashboard_button_api_password' => array( + 'group_name' => 'Member Dashboard Preferences', + 'group' => 'memberdashboard', + 'name' => 'memberdashboard_button_api_password', + 'type' => 'String', + 'default' => '', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'Password for the member button API', + 'help_text' => 'Super secret password for logging into the member button API' ) -); \ No newline at end of file +); diff --git a/templates/CRM/Admin/Form/Setting/MemberDashboard.tpl b/templates/CRM/Admin/Form/Setting/MemberDashboard.tpl index 245e05f..3484fec 100644 --- a/templates/CRM/Admin/Form/Setting/MemberDashboard.tpl +++ b/templates/CRM/Admin/Form/Setting/MemberDashboard.tpl @@ -37,6 +37,30 @@ {$form.memberdashboard_info_profile_id.html} + + + {$form.memberdashboard_button_api_url.label} + + + {$form.memberdashboard_button_api_url.html} + + + + + {$form.memberdashboard_button_api_user.label} + + + {$form.memberdashboard_button_api_user.html} + + + + + {$form.memberdashboard_button_api_password.label} + + + {$form.memberdashboard_button_api_password.html} + + -- 2.25.1