Add civicrm_post hook to regenerate member buttons.
authorDavid Thompson <davet@gnu.org>
Mon, 17 Nov 2014 19:26:14 +0000 (14:26 -0500)
committerDavid Thompson <davet@gnu.org>
Mon, 17 Nov 2014 19:26:14 +0000 (14:26 -0500)
* 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
memberdashboard.php
settings/MemberDashboard.setting.php
templates/CRM/Admin/Form/Setting/MemberDashboard.tpl

index 5b0bda48e3cd71226307f0d8be02f5b85b93fcda..a2b32f3de8037a2db27df074076f57574aff9aaa 100644 (file)
@@ -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');
index f06aabc2d5b32621b02f130aa6ec0d3f088b96e7..f84f76fe5e9682cf29428d0b7c1cc0afa9b93cd6 100644 (file)
@@ -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();
+  }
+}
index 0af6b2204f6c52e55736f6d4026c05fd22385a39..445656688ba4baa4e6fc0e74059057c768457452 100644 (file)
@@ -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
+);
index 245e05fa4bc46bcc6cbce7de2c38189af8897bb3..3484fec9b66581cd929935cb126154a9e5d435a4 100644 (file)
           {$form.memberdashboard_info_profile_id.html}
         </td>
       </tr>
+      <tr>
+        <td class="label">
+          {$form.memberdashboard_button_api_url.label}
+        </td>
+        <td>
+          {$form.memberdashboard_button_api_url.html}
+        </td>
+      </tr>
+      <tr>
+        <td class="label">
+          {$form.memberdashboard_button_api_user.label}
+        </td>
+        <td>
+          {$form.memberdashboard_button_api_user.html}
+        </td>
+      </tr>
+      <tr>
+        <td class="label">
+          {$form.memberdashboard_button_api_password.label}
+        </td>
+        <td>
+          {$form.memberdashboard_button_api_password.html}
+        </td>
+      </tr>
     </table>
   </fieldset>