CRM-17646 - Add getOptions method to custom field objects
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
index 84f743e6a3e346dca4dce0bfedb586e7ccd4d2ee..e6764f96975787699db5199a6d6abf5a84189f86 100644 (file)
@@ -362,6 +362,49 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
   }
 
+  /**
+   * @param string $context
+   * @return array|bool
+   */
+  public function getOptions($context = 'get') {
+    CRM_Core_DAO::buildOptionsContext($context);
+    $options = FALSE;
+
+    if (!$this->id) {
+      return FALSE;
+    }
+    if (!$this->data_type || !$this->custom_group_id) {
+      $this->find(TRUE);
+    }
+
+    if (!empty($this->option_group_id)) {
+      $options = CRM_Core_OptionGroup::valuesByID(
+        $this->option_group_id,
+        FALSE,
+        FALSE,
+        FALSE,
+        'label',
+        !($context == 'validate' || $context == 'get')
+      );
+    }
+    else {
+      if ($this->data_type === 'StateProvince') {
+        $options = CRM_Core_Pseudoconstant::stateProvince();
+      }
+      elseif ($this->data_type === 'Country') {
+        $options = $context == 'validate' ? CRM_Core_Pseudoconstant::countryIsoCode() : CRM_Core_Pseudoconstant::country();
+      }
+      elseif ($this->data_type === 'Boolean') {
+        $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
+      }
+    }
+    CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
+    $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends');
+    $entity = in_array($entity, array('Individual', 'Household', 'Organization')) ? 'Contact' : $entity;
+    CRM_Utils_Hook::fieldOptions($entity, "custom_{$this->id}", $options, array('context' => $context));
+    return $options;
+  }
+
   /**
    * Store and return an array of all active custom fields.
    *
@@ -2194,6 +2237,7 @@ ORDER BY html_type";
    * @param int $entityID
    * @param string $customFieldExtends
    * @param bool $inline
+   * @param bool $checkPermissions
    *
    * @return array
    */
@@ -2201,7 +2245,8 @@ ORDER BY html_type";
     &$params,
     $entityID,
     $customFieldExtends,
-    $inline = FALSE
+    $inline = FALSE,
+    $checkPermissions = TRUE
   ) {
     $customData = array();
 
@@ -2224,7 +2269,8 @@ ORDER BY html_type";
           $customFieldExtends,
           $customFieldInfo[1],
           $entityID,
-          $inline
+          $inline,
+          $checkPermissions
         );
       }
     }
@@ -2232,61 +2278,8 @@ ORDER BY html_type";
   }
 
   /**
-   * Build option.
-   *
-   * @param array $field
-   * @param array $options
    *
-   * @throws Exception
    */
-  public static function buildOption($field, &$options) {
-    // Fixme - adding anything but options to the $options array is a bad idea
-    // What if an option had the key 'attributes'?
-    $options['attributes'] = array(
-      'label' => $field['label'],
-      'data_type' => $field['data_type'],
-      'html_type' => $field['html_type'],
-    );
-
-    $optionGroupID = NULL;
-    if (($field['html_type'] == 'CheckBox' ||
-      $field['html_type'] == 'Radio' ||
-      $field['html_type'] == 'Select' ||
-      $field['html_type'] == 'AdvMulti-Select' ||
-      $field['html_type'] == 'Multi-Select' ||
-      ($field['html_type'] == 'Autocomplete-Select' && $field['data_type'] != 'ContactReference')
-    )
-    ) {
-      if ($field['option_group_id']) {
-        $optionGroupID = $field['option_group_id'];
-      }
-      elseif ($field['data_type'] != 'Boolean') {
-        CRM_Core_Error::fatal();
-      }
-    }
-
-    // build the cache for custom values with options (label => value)
-    if ($optionGroupID != NULL) {
-      $query = "
-SELECT label, value
-  FROM civicrm_option_value
- WHERE option_group_id = $optionGroupID
-";
-
-      $dao = CRM_Core_DAO::executeQuery($query);
-      while ($dao->fetch()) {
-        if ($field['data_type'] == 'Int' || $field['data_type'] == 'Float') {
-          $num = round($dao->value, 2);
-          $options["$num"] = $dao->label;
-        }
-        else {
-          $options[$dao->value] = $dao->label;
-        }
-      }
-
-      CRM_Utils_Hook::customFieldOptions($field['id'], $options);
-    }
-  }
 
   /**
    * Get custom field ID.