*/
public static $_importFields = NULL;
+ /**
+ * @var array
+ */
+ public static $displayInfoCache = array();
+
/**
* Build and retrieve the list of data types and descriptions.
*
return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
}
+ /**
+ * @param string $context
+ * @return array|bool
+ */
+ public function getOptions($context = NULL) {
+ CRM_Core_DAO::buildOptionsContext($context);
+
+ 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')
+ );
+ }
+ elseif ($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();
+ }
+ else {
+ return false;
+ }
+ 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.
*
}
/**
- * Find the display value for this field.
+ * @param string|int|array|null $value
+ * @param CRM_Core_BAO_CustomField|int $field
+ * @param $contactId
*
- * @param mixed $value
- * The custom field value.
- * @param int $id
- * The custom field id.
- * @param array $options
- * The assoc array of option name/value pairs.
- *
- * @param int $contactID
- * @param int $fieldID
- *
- * @return string
- * the display value
+ * @return array|int|null|string
*/
- public static function getDisplayValue($value, $id, &$options, $contactID = NULL, $fieldID = NULL) {
- $option = &$options[$id];
- $attributes = &$option['attributes'];
- $html_type = $attributes['html_type'];
- $data_type = $attributes['data_type'];
-
- return self::getDisplayValueCommon($value,
- $option,
- $html_type,
- $data_type,
- $contactID,
- $fieldID
- );
+ public static function displayValue($value, $field, $contactId = NULL) {
+ $fieldId = is_object($field) ? $field->id : $field;
+
+ if (!isset(self::$displayInfoCache[$fieldId])) {
+ if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
+ $field = new CRM_Core_BAO_CustomField();
+ $field->id = $fieldId;
+ }
+ $options = $field->getOptions();
+ self::$displayInfoCache[$fieldId] = array(
+ 'id' => $fieldId,
+ 'html_type' => $field->html_type,
+ 'data_type' => $field->data_type,
+ 'options' => $options,
+ );
+ }
+ return self::formatDisplayValue($value, self::$displayInfoCache[$fieldId], $contactId);
}
/**
- * Get display value.
+ * Legacy display value formatter.
+ *
+ * @deprecated
*
* @param string $value
* @param array $option
*/
public static function getDisplayValueCommon(
$value,
- &$option,
+ $option,
$html_type,
$data_type,
$contactID = NULL,
$fieldID = NULL
) {
- $display = $value;
if ($fieldID &&
(($html_type == 'Radio' && $data_type != 'Boolean') ||
CRM_Utils_Hook::customFieldOptions($fieldID, $option);
}
- switch ($html_type) {
- case 'Radio':
- if ($data_type == 'Boolean') {
- $options = array('No', 'Yes');
- }
- else {
- $options = $option;
- }
- if (is_array($value)) {
- $display = NULL;
- foreach ($value as $data) {
- $display .= $display ? ', ' . $options[$data] : $options[$data];
- }
- }
- else {
- $display = CRM_Utils_Array::value($value, $options);
- }
- break;
+ if ($data_type == 'Boolean') {
+ $option = CRM_Core_SelectValues::boolean();
+ }
- case 'Autocomplete-Select':
- if ($data_type == 'ContactReference' &&
- $value
- ) {
- $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name');
- }
- elseif (is_array($value)) {
- $display = NULL;
- foreach ($value as $data) {
- $display .= $display ? ', ' . $option[$data] : $option[$data];
- }
- }
- else {
- $display = CRM_Utils_Array::value($value, $option);
- }
- break;
+ if ($data_type == 'Country') {
+ $option = CRM_Core_PseudoConstant::country(FALSE, FALSE);
+ }
+
+ if ($data_type == 'StateProvince') {
+ $option = CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE);
+ }
+
+ $field = array(
+ 'id' => $fieldID,
+ 'html_type' => $html_type,
+ 'data_type' => $data_type,
+ 'options' => $option,
+ );
+
+ return self::formatDisplayValue($value, $field, $contactID);
+ }
- case 'Select':
- if (is_array($value)) {
- $display = NULL;
- foreach ($value as $data) {
- $display .= $display ? ', ' . $option[$data] : $option[$data];
- }
- }
- else {
- $display = CRM_Utils_Array::value($value, $option);
- }
- break;
+ /**
+ * Lower-level logic for rendering a custom field value
+ *
+ * @param string|array $value
+ * @param array $field
+ * @param int|null $contactID
+ *
+ * @return string|array|int|null
+ */
+ private static function formatDisplayValue($value, $field, $contactID = NULL) {
+ $display = $value;
+
+ if (self::isSerialized($field) && !is_array($value)) {
+ $value = (array) CRM_Utils_Array::explodePadded($value);
+ }
+ // CRM-12989 fix
+ if ($field['html_type'] == 'CheckBox') {
+ CRM_Utils_Array::formatArrayKeys($value);
+ }
+
+ switch ($field['html_type']) {
+
+ case 'Select':
+ case 'Autocomplete-Select':
+ case 'Radio':
+ case 'Select Country':
+ case 'Select State/Province':
case 'CheckBox':
case 'AdvMulti-Select':
case 'Multi-Select':
- if (is_array($value)) {
- $checkedData = $value;
+ case 'Multi-Select State/Province':
+ case 'Multi-Select Country':
+ if ($field['data_type'] == 'ContactReference' && $value) {
+ $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name');
}
- else {
- $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR,
- substr($value, 1, -1)
- );
- if ($html_type == 'CheckBox') {
- $newData = array();
- foreach ($checkedData as $v) {
- $v = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, '', $v);
- $newData[] = $v;
- }
- $checkedData = $newData;
+ elseif (is_array($value)) {
+ $v = array();
+ foreach ($value as $key => $val) {
+ $v[] = CRM_Utils_Array::value($val, $field['options']);
}
- }
-
- $v = array();
- foreach ($checkedData as $key => $val) {
- $v[] = CRM_Utils_Array::value($val, $option);
- }
- if (!empty($v)) {
$display = implode(', ', $v);
}
+ else {
+ $display = CRM_Utils_Array::value($value, $field['options']);
+ }
break;
case 'Select Date':
}
else {
// remove time element display if time is not set
- if (empty($option['attributes']['time_format'])) {
+ if (empty($field['options']['attributes']['time_format'])) {
$value = substr($value, 0, 10);
}
$display = CRM_Utils_Date::customFormat($value);
}
break;
- case 'Select State/Province':
- case 'Multi-Select State/Province':
- case 'Select Country':
- case 'Multi-Select Country':
- if (strstr($html_type, 'State/Province')) {
- $option = CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE);
- }
- else {
- $option = CRM_Core_PseudoConstant::country(FALSE, FALSE);
- }
- // process multi-select state/country field values
- if (!is_array($value)) {
- $value = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
- }
-
- $display = NULL;
- foreach ($value as $data) {
- $display .= ($display && !empty($option[$data])) ? ', ' . $option[$data] : $option[$data];
- }
- break;
-
case 'File':
// In the context of displaying a profile, show file/image
if ($contactID && $value) {
- $url = self::getFileURL($contactID, $fieldID, $value);
+ $url = self::getFileURL($contactID, $field['id'], $value);
if ($url) {
$display = $url['file_url'];
}
case 'Link':
case 'Text':
- if (empty($value)) {
- $display = '';
- }
- else {
- $display = is_array($value) ? implode(', ', $value) : $value;
- }
+ $display = is_array($value) ? implode(', ', $value) : $value;
}
return $display ? $display : $value;
}
$customFields[$customFieldId]['html_type'] == 'AdvMulti-Select'
) {
if ($value) {
- // Note that only during merge this is not an array,
- // and you can directly use value, CRM-4385
- if (is_array($value)) {
- $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
- array_values($value)
- ) . CRM_Core_DAO::VALUE_SEPARATOR;
- }
+ $value = CRM_Utils_Array::implodePadded($value);
}
else {
$value = '';
* @param int $entityID
* @param string $customFieldExtends
* @param bool $inline
+ * @param bool $checkPermissions
*
* @return array
*/
&$params,
$entityID,
$customFieldExtends,
- $inline = FALSE
+ $inline = FALSE,
+ $checkPermissions = TRUE
) {
$customData = array();
$customFieldExtends,
$customFieldInfo[1],
$entityID,
- $inline
+ $inline,
+ $checkPermissions
);
}
}
}
/**
- * 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.
}
/**
- * Get options for field.
+ * Set pseudoconstant properties for field metadata.
*
* @param array $field
* @param string|null $optionGroupName