Add buildForm hook to fix state select box when editing info profile.
[org.fsf.memberdashboard.git] / memberdashboard.php
index 881a4f773064d1f08b47270d4ba8017b0e5b8c4b..8ceed2d4ebdf78c81decfdaf1064f1daadd1530b 100644 (file)
@@ -2,6 +2,9 @@
 
 require_once 'memberdashboard.civix.php';
 
+define('MEMBERDASHBOARD_SETTINGS_GROUP', 'Member Dashboard Preferences');
+define('MEMBERDASHBOARD_MAX_EMAIL_ALIASES', 5);
+
 /**
  * Implementation of hook_civicrm_config
  *
@@ -106,3 +109,68 @@ 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)) {
+    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
+        ));
+
+        if(!empty($apiUrl) && !empty($user) && !empty($password)) {
+          $url = "$apiUrl?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, $url);
+          curl_exec($curl);
+        }
+      }
+
+      $dao->free();
+    } catch(Exception $e) {
+      // Ignore it.  Not the end of the world if this fails.
+    }
+  }
+}
+
+function memberdashboard_civicrm_buildForm($formName, &$form) {
+  // Hack to fix state select box in 4.4.x
+  if($formName == 'CRM_Profile_Form_Edit') {
+    $contactId = CRM_Core_Session::singleton()->get('userID');
+    $contact = civicrm_api3('contact', 'getsingle', array( 'id' => $contactId ));
+    $defaults['state_province-1'] = $contact['state_province_id'];
+    $form->setDefaults($defaults);
+  }
+}