-- CRM-9288 - Website type improvements: allow multiple websites in Profiles, respect...
[civicrm-core.git] / CRM / Core / BAO / UFField.php
index fb779f7add740783f8e033796890ef8d1beb50c7..5baa73671e399e9a492f0813042e0e83704b22ff 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -140,10 +140,15 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
     $ufField->uf_group_id = CRM_Utils_Array::value('uf_group', $ids);
     $ufField->field_type = $params['field_name'][0];
     $ufField->field_name = $params['field_name'][1];
-    $ufField->location_type_id = (CRM_Utils_Array::value(2, $params['field_name'])) ? $params['field_name'][2] : 'NULL';
+    if ($params['field_name'][1] == 'url') {
+      $ufField->website_type_id = CRM_Utils_Array::value(2, $params['field_name'], NULL);
+    } 
+    else {
+      $ufField->location_type_id = (CRM_Utils_Array::value(2, $params['field_name'])) ? $params['field_name'][2] : 'NULL';
+    }
     $ufField->phone_type_id = CRM_Utils_Array::value(3, $params['field_name']);
 
-    if (CRM_Utils_Array::value('uf_field', $ids)) {
+    if (!empty($ids['uf_field'])) {
       $ufField->whereAdd("id <> " . CRM_Utils_Array::value('uf_field', $ids));
     }
 
@@ -211,7 +216,14 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
     $ufField->field_name = $params['field_name'][1];
 
     //should not set location type id for Primary
-    $locationTypeId = CRM_Utils_Array::value(2, $params['field_name']);
+    $locationTypeId = NULL;
+    if ($params['field_name'][1] == 'url') {
+      $ufField->website_type_id = CRM_Utils_Array::value(2, $params['field_name']);
+    } 
+    else {
+      $locationTypeId = CRM_Utils_Array::value(2, $params['field_name']);
+      $ufField->website_type_id = NULL;
+    }
     if ($locationTypeId) {
       $ufField->location_type_id = $locationTypeId;
     }
@@ -252,7 +264,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
     // fix for CRM-316
     $oldWeight = NULL;
 
-    if (CRM_Utils_Array::value('field_id', $params)) {
+    if (!empty($params['field_id'])) {
       $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
     }
     $fieldValues = array('uf_group_id' => $params['group_id']);
@@ -788,13 +800,15 @@ SELECT  id
    * http://issues.civicrm.org/jira/browse/CRM-5869
    *
    * @param string $key Field key - e.g. street_address-Primary, first_name
-   * @param $profileAddressFields
-   * @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',
@@ -807,8 +821,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;
     }
 
@@ -959,6 +980,24 @@ SELECT  id
       }
     }
 
+    if (CRM_Core_Permission::access('CiviCase')) {
+      $caseFields = CRM_Case_BAO_Query::getFields(TRUE);
+      $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField::getFieldsForImport('Case'));
+      if ($caseFields) {
+        // Remove fields not supported by profiles
+        CRM_Utils_Array::remove($caseFields,
+          'case_id',
+          'case_type',
+          'case_start_date',
+          'case_end_date',
+          'case_role',
+          'case_status',
+          'case_deleted'
+        );
+      }
+      $fields['Case'] = $caseFields;
+    }
+
     $activityFields = CRM_Activity_BAO_Activity::getProfileFields();
     if ($activityFields) {
       // campaign related fields.