Merge remote-tracking branch 'upstream/4.5' into 4.5-4.6-2015-03-16-17-24-33
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
index 17b461639c7fdea7010d5f95ba51f45573ff316e..432bd3d27d0b3fa44d093e1e08be63da0bc59565 100644 (file)
@@ -509,10 +509,13 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
                             $cfTable.option_group_id,
                             $cfTable.date_format,
                             $cfTable.time_format,
-                            $cgTable.is_multiple
+                            $cgTable.is_multiple,
+                            og.name as option_group_name
                      FROM $cfTable
                      INNER JOIN $cgTable
-                     ON $cfTable.custom_group_id = $cgTable.id
+                       ON $cfTable.custom_group_id = $cgTable.id
+                     LEFT JOIN civicrm_option_group og
+                       ON $cfTable.option_group_id = og.id
                      WHERE ( 1 ) ";
 
         if (!$showAll) {
@@ -577,6 +580,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
           $fields[$dao->id]['date_format'] = $dao->date_format;
           $fields[$dao->id]['time_format'] = $dao->time_format;
           $fields[$dao->id]['is_required'] = $dao->is_required;
+          self::getOptionsForField($fields[$dao->id], $dao->option_group_name);
         }
 
         CRM_Core_BAO_Cache::setItem($fields,
@@ -1313,7 +1317,15 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
         break;
 
       case 'File':
-        if ($value) {
+        // In the context of displaying a profile, show file/image
+        if ($contactID && $value) {
+          $url = self::getFileURL($contactID, $fieldID, $value);
+          if ($url) {
+            $display = $url['file_url'];
+          }
+        }
+        // In other contexts show a paperclip icon
+        elseif ($value) {
           $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
           $display = $icons[$value];
         }
@@ -1583,16 +1595,13 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     //subtype and basic type
     $customDataSubType = NULL;
     if (is_array($customFieldExtend)) {
-      $customFieldExtend = $customFieldExtend[0];
-    }
-
-    if (in_array($customFieldExtend,
-      CRM_Contact_BAO_ContactType::subTypes()
-    )) {
       // This is the case when getFieldsForImport() requires fields
       // of subtype and its parent.CRM-5143
-      $customDataSubType = $customFieldExtend;
-      $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType);
+      // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
+      $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType::subTypes(), (array) $customFieldExtend);
+      if (!empty($customDataSubType) && is_array($customDataSubType)) {
+        $customFieldExtend = array_unique(array_values(CRM_Contact_BAO_ContactType::getBasicType($customDataSubType)));
+      }
     }
 
     $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend,
@@ -2358,6 +2367,10 @@ WHERE      ( f.label = %1 OR f.name = %1 )
 
   /**
    * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
+   *
+   * @param array $ids
+   *
+   * @return array
    */
   public static function getNameFromID($ids) {
     if (is_array($ids)) {
@@ -2528,4 +2541,37 @@ WHERE cf.id = %1 AND cg.is_multiple = 1";
     return ($field['html_type'] == 'CheckBox' || strpos($field['html_type'], 'Multi') !== FALSE);
   }
 
+  /**
+   * @param array $field
+   * @param string|null $optionGroupName
+   */
+  private static function getOptionsForField(&$field, $optionGroupName) {
+    if ($optionGroupName) {
+      $field['pseudoconstant'] = array(
+        'optionGroupName' => $optionGroupName,
+        'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
+      );
+    }
+    elseif ($field['data_type'] == 'Boolean') {
+      $field['pseudoconstant'] = array(
+        'callback' => 'CRM_Core_SelectValues::boolean',
+      );
+    }
+    elseif ($field['data_type'] == 'Country') {
+      $field['pseudoconstant'] = array(
+        'table' => 'civicrm_country',
+        'keyColumn' => 'id',
+        'labelColumn' => 'name',
+        'nameColumn' => 'iso_code',
+      );
+    }
+    elseif ($field['data_type'] == 'StateProvince') {
+      $field['pseudoconstant'] = array(
+        'table' => 'civicrm_state_province',
+        'keyColumn' => 'id',
+        'labelColumn' => 'name',
+      );
+    }
+  }
+
 }