Merge pull request #5473 from aydun/CRM-16160
[civicrm-core.git] / api / v3 / Profile.php
index 75f5700761003f3d6202e34564ecf90a3c152ce3..23682854ed30d0f6eed2892390413fc1d33f0768 100644 (file)
@@ -1,10 +1,9 @@
 <?php
-
 /*
   +--------------------------------------------------------------------+
   | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2014                                |
+  | Copyright CiviCRM LLC (c) 2004-2015                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
   | GNU Affero General Public License or the licensing of CiviCRM,     |
   | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
   +--------------------------------------------------------------------+
-*/
+ */
 
 /**
- * File for the CiviCRM APIv3 activity profile functions
+ * This api exposes CiviCRM profiles.
  *
- * @package CiviCRM_APIv3
- * @subpackage API_ActivityProfile
- * @copyright CiviCRM LLC (c) 2004-2014
- * @version $Id: ActivityProfile.php 30486 2011-05-20 16:12:09Z rajan $
+ * Profiles are collections of fields used as forms, listings, search columns, etc.
  *
+ * @package CiviCRM_APIv3
  */
 
 /**
  * Retrieve Profile field values.
  *
- * @param array $params
- *   Associative array of property name/value.
- *                             pairs to get profile field values
- *
- * @throws API_Exception
- * @return array|CRM_Error
- *
  * 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 - if a  single profile is passed in we will not return a normal api result array
  * in order to avoid breaking code. (This could still be confusing :-( but we have to keep the tested behaviour working
  *
  * Note that if contact_id is empty an array of defaults is returned
+ *
+ * @param array $params
+ *   Associative array of property name/value.
+ *   pairs to get profile field values
+ *
+ * @throws API_Exception
+ * @return array
  */
 function civicrm_api3_profile_get($params) {
   $nonStandardLegacyBehaviour = is_numeric($params['profile_id']) ? TRUE : FALSE;
@@ -116,7 +113,7 @@ function civicrm_api3_profile_get($params) {
         $ufGroupBAO->setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values[$profileID], TRUE);
       }
     }
-    elseif(!empty($params['contact_id'])) {
+    elseif (!empty($params['contact_id'])) {
       $ufGroupBAO->setProfileDefaults($params['contact_id'], $profileFields, $values[$profileID], TRUE);
       foreach ($values[$profileID] as $fieldName => $field) {
         // we should return 'Primary' with & without capitalisation. it is more consistent with api to not
@@ -139,6 +136,8 @@ function civicrm_api3_profile_get($params) {
 }
 
 /**
+ * Adjust profile get function metadata.
+ *
  * @param array $params
  */
 function _civicrm_api3_profile_get_spec(&$params) {
@@ -150,6 +149,7 @@ function _civicrm_api3_profile_get_spec(&$params) {
 
 /**
  * Submit a set of fields against a profile.
+ *
  * Note choice of submit versus create is discussed CRM-13234 & related to the fact
  * 'profile' is being treated as a data-entry entity
  *
@@ -174,7 +174,7 @@ function civicrm_api3_profile_submit($params) {
 
   $contactParams = $activityParams = $missingParams = array();
 
-  $profileFields = civicrm_api3('profile', 'getfields', array('action' => 'submit', 'profile_id' => $profileID));
+  $profileFields = civicrm_api3('Profile', 'getfields', array('action' => 'submit', 'profile_id' => $profileID));
   $profileFields = $profileFields['values'];
   if ($isContactActivityProfile) {
     civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
@@ -245,54 +245,17 @@ function civicrm_api3_profile_submit($params) {
     $profileParams['api.activity.create'] = $activityParams;
   }
 
-  $groups = $tags = array();
-  if (isset($profileParams['group'])) {
-    $groups = $profileParams['group'];
-    unset($profileParams['group']);
-  }
-
-  if (isset($profileParams['tag'])) {
-    $tags = $profileParams['tag'];
-    unset($profileParams['tag']);
-  }
-
   return civicrm_api3('contact', 'create', $profileParams);
-
-  $ufGroupDetails = array();
-  $ufGroupParams = array('id' => $profileID);
-  CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
-
-  if (isset($profileFields['group'])) {
-    CRM_Contact_BAO_GroupContact::create($groups,
-      $params['contact_id'],
-      FALSE,
-      'Admin'
-    );
-  }
-
-  if (isset($profileFields['tag'])) {
-    CRM_Core_BAO_EntityTag::create($tags,
-      'civicrm_contact',
-      $params['contact_id']
-    );
-  }
-
-  if (!empty($ufGroupDetails['add_to_group_id'])) {
-    $contactIds = array($params['contact_id']);
-    CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds,
-      $ufGroupDetails['add_to_group_id']
-    );
-  }
-
-  return $result;
-
 }
 
 /**
+ * Translate field names for BAO.
+ *
  * The api standards expect field names to be lower case but the BAO uses mixed case
  * so we accept 'email-primary' but pass 'email-Primary' to the BAO
  * we could make the BAO handle email-primary but this would alter the fieldname seen by hooks
  * & we would need to consider that change
+ *
  * @param string $fieldName
  *   API field name.
  *
@@ -303,8 +266,10 @@ function _civicrm_api3_profile_translate_fieldnames_for_bao($fieldName) {
   $fieldName = str_replace('url', 'URL', $fieldName);
   return str_replace('primary', 'Primary', $fieldName);
 }
+
 /**
- * metadata for submit action
+ * Metadata for submit action.
+ *
  * @param array $params
  * @param array $apirequest
  */
@@ -329,39 +294,39 @@ function _civicrm_api3_profile_submit_spec(&$params, $apirequest) {
 }
 
 /**
+ * Update Profile field values.
+ *
  * @deprecated - calling this function directly is deprecated as 'set' is not a clear action
  * use submit
- * Update Profile field values.
  *
  * @param array $params
- *   Associative array of property name/value.
- *                             pairs to update profile field values
+ *   Array of property name/value.
+ *   pairs to update profile field values
  *
  * @return array
  *   Updated Contact/ Activity object|CRM_Error
- *
- *
  */
 function civicrm_api3_profile_set($params) {
   return civicrm_api3('profile', 'submit', $params);
 }
 
 /**
+ * Apply profile.
+ *
  * @deprecated - appears to be an internal function - should not be accessible via api
  * Provide formatted values for profile fields.
  *
  * @param array $params
- *   Associative array of property name/value.
- *                             pairs to profile field values
+ *   Array of property name/value.
+ *   pairs to profile field values
  *
  * @throws API_Exception
- * @return array|CRM_Error
+ * @return array
  *
  * @todo add example
  * @todo add test cases
  */
 function civicrm_api3_profile_apply($params) {
-
   $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
     FALSE,
     NULL,
@@ -391,6 +356,8 @@ function civicrm_api3_profile_apply($params) {
 
 
 /**
+ * Get pseudo profile 'billing'.
+ *
  * This is a function to help us 'pretend' billing is a profile & treat it like it is one.
  * It gets standard credit card address fields etc
  * Note this is 'better' that the inbuilt version as it will pull in fallback values
@@ -405,7 +372,7 @@ function civicrm_api3_profile_apply($params) {
  */
 function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
 
-  $locations = civicrm_api3('address', 'getoptions', array('field' => 'location_type_id'));
+  $locations = civicrm_api3('address', 'getoptions', array('field' => 'location_type_id', 'context' => 'validate'));
   $locationTypeID = array_search('Billing', $locations['values']);
 
   if (empty($params['contact_id'])) {
@@ -448,7 +415,7 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
       $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result['api.address.get.1']['values'][0][$fieldname]) ? $result['api.address.get.1']['values'][0][$fieldname] : '';
     }
   }
-  elseif(!empty($result['api.address.get.2']['count'])) {
+  elseif (!empty($result['api.address.get.2']['count'])) {
     foreach ($addressFields as $fieldname) {
       $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result['api.address.get.2']['values'][0][$fieldname]) ? $result['api.address.get.2']['values'][0][$fieldname] : '';
     }
@@ -462,7 +429,7 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
   if (!empty($result['api.email.get.1']['count'])) {
     $values['billing-email' . '-' . $locationTypeID] = $result['api.email.get.1']['values'][0]['email'];
   }
-  elseif(!empty($result['api.email.get.2']['count'])) {
+  elseif (!empty($result['api.email.get.2']['count'])) {
     $values['billing-email' . '-' . $locationTypeID] = $result['api.email.get.2']['values'][0]['email'];
   }
   else {
@@ -474,8 +441,9 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
 }
 
 /**
- * Here we will build  up getfields type data for all the fields in the profile. Because the integration with the
- * form layer in core is so hard-coded we are not going to attempt to re-use it
+ * Here we will build  up getfields type data for all the fields in the profile.
+ *
+ * Because the integration with the form layer in core is so hard-coded we are not going to attempt to re-use it
  * However, as this function is unit-tested & hence 'locked in' we can aspire to extract sharable
  * code out of the form-layer over time.
  *
@@ -489,14 +457,14 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
  *   0 = don't resolve, 1 = resolve non-aggressively, 2 = resolve aggressively - ie include country & state.
  * @param $is_flush
  *
- * @return array
+ * @return array|void
  */
 function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = 1, $is_flush) {
   static $profileFields = array();
   if ($is_flush) {
     $profileFields = array();
     if (empty($profileID)) {
-      return;
+      return NULL;
     }
   }
   if (isset($profileFields[$profileID])) {
@@ -645,7 +613,7 @@ function _civicrm_api3_map_profile_fields_to_entity(&$field) {
     }
     $fieldName .= '-' . $field['location_type_id'];
   }
-  elseif(array_key_exists($fieldName, $locationFields)) {
+  elseif (array_key_exists($fieldName, $locationFields)) {
     $fieldName .= '-Primary';
     $entity = 'email';
   }
@@ -684,7 +652,7 @@ function _civicrm_api3_map_profile_fields_to_entity(&$field) {
     'soft_credit_type' => 'contribution_soft',
     'group' => 'group_contact',
     'tag' => 'entity_tag',
-   );
+  );
   if (array_key_exists($fieldName, $hardCodedEntityMappings)) {
     $entity = $hardCodedEntityMappings[$fieldName];
   }