CRM-13179 a few fixes lost in gitting re-instated
authoreileen <eileen@fuzion.co.nz>
Wed, 14 Aug 2013 12:46:16 +0000 (00:46 +1200)
committereileen <eileen@fuzion.co.nz>
Sun, 18 Aug 2013 04:49:48 +0000 (16:49 +1200)
api/v3/Profile.php
templates/CRM/common/AutoComplete.js

index d2a838dce04a386dceacc5e6bb13b724c1c02e44..4393a1265788d1809835db6c35cc926213bc0fd8 100644 (file)
@@ -52,10 +52,13 @@ require_once 'api/v3/utils.php';
  * NOTE this api is not standard & since it is tested we need to honour that
  * but the correct behaviour is for it to return an id indexed array as this supports
  * multiple instances
+ *
+ * Note that if contact_id is empty an array of defaults is returned
+ *
  */
 function civicrm_api3_profile_get($params) {
   $nonStandardLegacyBehaviour = is_numeric($params['profile_id']) ?  TRUE : FALSE;
-  if(!empty($params['check_permissions']) && !1 === civicrm_api3('contact', 'getcount', array('contact_id' => 1, 'check_permissions' => 1))) {
+  if(!empty($params['check_permissions']) && !empty($params['contact_id']) && !1 === civicrm_api3('contact', 'getcount', array('contact_id' => $params['contact_id'], 'check_permissions' => 1))) {
     throw new API_Exception('permission denied');
   }
   $profiles = (array) $params['profile_id'];
@@ -116,9 +119,12 @@ function civicrm_api3_profile_get($params) {
       CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values[$profileID], TRUE);
     }
   }
-  else {
+  elseif(!empty($params['contact_id'])) {
     CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $profileFields, $values[$profileID], TRUE);
   }
+  else{
+    $values[$profileID] = array_fill_keys(array_keys($profileFields), '');
+  }
   }
   if($nonStandardLegacyBehaviour) {
     $result = civicrm_api3_create_success();
@@ -132,8 +138,7 @@ function civicrm_api3_profile_get($params) {
 
 function _civicrm_api3_profile_get_spec(&$params) {
   $params['profile_id']['api.required'] = TRUE;
-  // does this mean we use a different call to get a 'blank' profile?
-  $params['contact_id']['api.required'] = TRUE;
+  $params['contact_id']['description'] = 'If no contact is specified an array of defaults will be returned';
 }
 /**
  * Update Profile field values.
@@ -338,12 +343,28 @@ function civicrm_api3_profile_getfields($params) {
  * It gets standard credit card address fields etc
  * Note this is 'better' that the inbuilt version as it will pull in fallback values
  *  billing location -> is_billing -> primary
+ *
+ *  Note that that since the existing code for deriving a blank profile is not easily accessible our
+ *  interim solution is just to return an empty array
  */
 function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
   $addressFields = array('street_address', 'city', 'state_province_id', 'country_id');
+  $locations = civicrm_api3('address', 'getoptions', array('field' => 'location_type_id'));
+  $locationTypeID = array_search('Billing', $locations['values']);
+
+  if(empty($params['contact_id'])) {
+    $blanks =  array(
+      'billing_first_name' => '',
+      'billing_middle_name' => '',
+      'billing_last_name' => '',
+    );
+    foreach ($addressFields as $field) {
+      $blanks['billing_' . $field . '_' . $locationTypeID] = '';
+    }
+    return $blanks;
+  }
   $result = civicrm_api3('contact', 'getsingle', array(
     'id' => $params['contact_id'],
-    'api.address.getoptions' => array('field' => 'location_type_id',),
     'api.address.get.1' => array('location_type_id' => 'Billing',  'return' => $addressFields),
     // getting the is_billing required or not is an extra db call but probably cheap enough as this isn't an import api
     'api.address.get.2' => array('is_billing' => True, 'return' => $addressFields),
@@ -352,7 +373,6 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
     'return' => 'api.email.get, api.address.get, api.address.getoptions, email, first_name, last_name, middle_name,' . implode($addressFields, ','),
    )
   );
-  $locationTypeID = array_search('Billing', $result['api.address.getoptions']['values']);
 
   $values = array(
     'billing_first_name' => $result['first_name'],
@@ -385,5 +405,7 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
   else{
     $values['billing-email'. '-' . $locationTypeID] = $result['email'];
   }
+  // return both variants of email to reflect inconsistencies in form layer
+  $values['email'. '-' . $locationTypeID] = $values['billing-email'. '-' . $locationTypeID];
   return $values;
 }
index 0077542d35d0a501059c0c53637763464cb4b7ee..1439b22e2d5d02382c5e3f4bf44ba527da6c8268 100644 (file)
@@ -114,6 +114,7 @@ cj(function ($) {
       else{
         $('#crm-contact-toggle-text-'  + id_field).text(hidden_text);
         $('#' + id_field).val('');
+        $('#' + name_field).val('');
         CRM.api('profile', 'get', {'profile_id' : profileids}, {
           success: function(result) {
             $.each(result.values, function (id, values){