Merge pull request #2362 from totten/4.4-sync-redirect
[civicrm-core.git] / CRM / Core / BAO / UFField.php
index 773a4747e966a6602d976cc7a653f1c530404440..e4baabe332852abc74cb65d56b2b47572d5de886 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -127,6 +127,9 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
    * Function to check duplicate for duplicate field in a group
    *
    * @param array $params an associative array with field and values
+   * @param $ids
+   *
+   * @return mixed
    * @ids   array $ids    array that containd ids
    *
    * @access public
@@ -147,8 +150,8 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
     return $ufField->find(TRUE);
   }
 
-  /*
-   *Does profile consists of a multi-record custom field
+  /**
+   * Does profile consists of a multi-record custom field
    */
   public static function checkMultiRecordFieldExists($gId) {
     $queryString = "SELECT f.field_name
@@ -176,7 +179,7 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
       $queryString = "
       SELECT cg.id as cgId
  FROM civicrm_custom_group cg
- INNER JOIN civicrm_custom_field cf 
+ INNER JOIN civicrm_custom_field cf
  ON cg.id = cf.custom_group_id
 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
 
@@ -193,7 +196,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * function to add the UF Field
    *
    * @param array $params (reference) array containing the values submitted by the form
-   * @param array $ids    (reference) array containing the id
+   * @param array $ids array containing the id
    *
    * @return object CRM_Core_BAO_UFField object
    *
@@ -201,7 +204,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * @static
    *
    */
-  static function add(&$params, &$ids) {
+  static function add(&$params, $ids = array()) {
     // set values for uf field properties and save
     $ufField = new CRM_Core_DAO_UFField();
     $ufField->field_type = $params['field_name'][0];
@@ -253,7 +256,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
       $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
     }
     $fieldValues = array('uf_group_id' => $params['group_id']);
-    return CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, $params['weight'], $fieldValues);
+    return CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array::value('weight', $params, 0), $fieldValues);
   }
 
   /**
@@ -279,7 +282,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   }
 
   /**
-   * Function to copy exisiting profile fields to
+   * Function to copy existing profile fields to
    * new profile from the already built profile
    *
    * @param int      $old_id  from which we need to copy
@@ -353,6 +356,8 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    *
    * @params  int $UFFieldId     uf field id
    *
+   * @param $UFFieldId
+   *
    * @return boolean   false if custom field are disabled else true
    * @static
    * @access public
@@ -377,7 +382,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
     }
   }
 
-  /*
+  /**
    * Function to find out whether given profile group using Activity
    * Profile fields with contact fields
    */
@@ -433,14 +438,15 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
     return TRUE;
   }
 
-  /* Function to find out whether given profile group uses $required 
-   * and/or $optionalprofile types
-   *  
+  /**
+   * Function to find out whether given profile group uses $required
+   * and/or $optional profile types
+   *
    * @param integer $ufGroupId  profile id
    * @param array   $required   array of types those are required
    * @param array   $optional   array of types those are optional
    *
-   * @return boolean $valid  
+   * @return boolean $valid
    * @static
    */
   static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
@@ -489,6 +495,8 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * @params boolean $check      this is to check mix profile (if true it will check if profile is
    *                             pure ie. it contains only one contact type)
    *
+   * @param $ufGroupId
+   *
    * @return  true for mix profile else false
    * @acess public
    * @static
@@ -551,9 +559,11 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   /**
    * function to get the profile type (eg: individual/organization/household)
    *
-   * @param int      $ufGroupId     uf group id
-   * @param boolean  $returnMixType this is true, then field type of  mix profile field is returned
-   * @param boolean  $onlyPure      true if only pure profiles are required
+   * @param int $ufGroupId     uf group id
+   * @param boolean $returnMixType this is true, then field type of  mix profile field is returned
+   * @param boolean $onlyPure      true if only pure profiles are required
+   *
+   * @param bool $skipComponentType
    *
    * @return  profile group_type
    * @acess public
@@ -573,10 +583,13 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   /**
    * function to get the profile type (eg: individual/organization/household)
    *
-   * @param int      $ufGroupId     uf group id
-   * @param boolean  $returnMixType this is true, then field type of  mix profile field is returned
-   * @param boolean  $onlyPure      true if only pure profiles are required
+   * @param $ufGroupType
+   * @param boolean $returnMixType this is true, then field type of  mix profile field is returned
+   * @param boolean $onlyPure      true if only pure profiles are required
+   *
+   * @param bool $skipComponentType
    *
+   * @internal param int $ufGroupId uf group id
    * @return  profile group_type
    * @acess public
    * @static
@@ -679,6 +692,8 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   /**
    * function to check for mix profiles groups (eg: individual + other contact types)
    *
+   * @param $ctype
+   *
    * @return  true for mix profile group else false
    * @acess public
    * @static
@@ -715,6 +730,8 @@ SELECT ufg.id as id
    *
    * @params int     $profileID profile id.
    *
+   * @param $profileID
+   *
    * @return boolean $result    true/false.
    */
   static function checkSearchableORInSelector($profileID) {
@@ -724,8 +741,8 @@ SELECT ufg.id as id
     }
 
     $query = "
-SELECT  id 
-  From  civicrm_uf_field 
+SELECT  id
+  From  civicrm_uf_field
  WHERE  (in_selector = 1 OR is_searchable = 1)
    AND  uf_group_id = {$profileID}";
 
@@ -743,6 +760,8 @@ SELECT  id
    *
    * @params int $profileID profile id.
    *
+   * @param $profileID
+   *
    * @return void.
    */
   function resetInSelectorANDSearchable($profileID) {
@@ -767,13 +786,17 @@ SELECT  id
    *   as this will be used to
    * transfer profile address data to billing fields
    * http://issues.civicrm.org/jira/browse/CRM-5869
+   *
    * @param string $key Field key - e.g. street_address-Primary, first_name
-   * @params array $profileAddressFields array of profile fields that relate to address fields
+   * @param array $profileAddressFields array of profile fields that relate to address fields
+   * @param array $profileFilter filter to apply to profile fields - expected usage is to only fill based on
+   * the bottom profile per CRM-13726
    */
-  static function assignAddressField($key, &$profileAddressFields) {
+  static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
     $billing_id = CRM_Core_BAO_LocationType::getBilling();
     list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
 
+    $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter, array('is_active' => 1, 'return' => 'field_name')));
     //check for valid fields ( fields that are present in billing block )
     $validBillingFields = array(
       'first_name',
@@ -786,8 +809,15 @@ SELECT  id
       'postal_code',
       'country'
     );
+    $validProfileFields = array();
+
+    foreach ($profileFields['values'] as $field) {
+      if(in_array($field['field_name'], $validBillingFields)) {
+        $validProfileFields[] = $field['field_name'];
+      }
+    }
 
-    if (!in_array($prefixName, $validBillingFields)) {
+    if (!in_array($prefixName, $validProfileFields) ) {
       return;
     }
 
@@ -824,6 +854,9 @@ SELECT  id
       'Organization' => CRM_Contact_BAO_Contact::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
     );
 
+    // include hook injected fields
+    $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook::singleton()->getFields());
+
     // add current employer for individuals
     $fields['Individual']['current_employer'] = array(
       'name' => 'organization_name',
@@ -865,8 +898,7 @@ SELECT  id
     $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
     foreach ($subTypes as $name => $val) {
       //custom fields for sub type
-      $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($name);
-
+      $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
       if (array_key_exists($val['parent'], $fields)) {
         $fields[$name] = $fields[$val['parent']] + $subTypeFields;
       }
@@ -987,6 +1019,8 @@ SELECT  id
   /**
    * Get a list of fields which can be added to profiles
    *
+   * @param bool $force
+   *
    * @return array, multidimensional; e.g. $result['field_name']['label']
    * @static
    */