/** * Format custom fields before inserting. * * @param int $customFieldId * Custom field id. * @param array $customFormatted * Formatted array. * @param mix $value * Value of custom field. * @param string $customFieldExtend * Custom field extends. * @param int $customValueId * Custom option value id. * @param int $entityId * Entity id (contribution, membership...). * @param bool $inline * Consider inline custom groups only. * @param bool $checkPermission * If false, do not include permissioning clause. * @param bool $includeViewOnly * If true, fields marked 'View Only' are included. Required for APIv3. * * @return array|NULL * formatted custom field array */ public static function formatCustomField($customFieldId, &$customFormatted, $value, $customFieldExtend, $customValueId = NULL, $entityId = NULL, $inline = FALSE, $checkPermission = TRUE, $includeViewOnly = FALSE) { //get the custom fields for the entity //subtype and basic type $customDataSubType = NULL; if ($customFieldExtend) { // This is the case when getFieldsForImport() requires fields // of subtype and its parent.CRM-5143 // 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 = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType); if (is_array($customFieldExtend)) { $customFieldExtend = array_unique(array_values($customFieldExtend)); } } } $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend, FALSE, $inline, $customDataSubType, NULL, FALSE, FALSE, $checkPermission); if (!array_key_exists($customFieldId, $customFields)) { return NULL; } // return if field is a 'code' field if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) { return NULL; } list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId); if (!$customValueId && !$customFields[$customFieldId]['is_multiple'] && $entityId) { $query = "\nSELECT id\n FROM {$tableName}\n WHERE entity_id={$entityId}"; $customValueId = CRM_Core_DAO::singleValueQuery($query); } //fix checkbox, now check box always submits values if ($customFields[$customFieldId]['html_type'] == 'CheckBox') { if ($value) { // Note that only during merge this is not an array, and you can directly use value if (is_array($value)) { $selectedValues = array(); foreach ($value as $selId => $val) { if ($val) { $selectedValues[] = $selId; } } if (!empty($selectedValues)) { $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValues) . CRM_Core_DAO::VALUE_SEPARATOR; } else { $value = ''; } } } } if ($customFields[$customFieldId]['html_type'] == 'Multi-Select' || $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; } } else { $value = ''; } } if (($customFields[$customFieldId]['html_type'] == 'Multi-Select' || $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldId]['html_type'] == 'CheckBox') && $customFields[$customFieldId]['data_type'] == 'String' && !empty($customFields[$customFieldId]['text_length']) && !empty($value)) { // lets make sure that value is less than the length, else we'll // be losing some data, CRM-7481 if (strlen($value) >= $customFields[$customFieldId]['text_length']) { // need to do a few things here // 1. lets find a new length $newLength = $customFields[$customFieldId]['text_length']; $minLength = strlen($value); while ($newLength < $minLength) { $newLength = $newLength * 2; } // set the custom field meta data to have a length larger than value // alter the custom value table column to match this length CRM_Core_BAO_SchemaHandler::alterFieldLength($customFieldId, $tableName, $columnName, $newLength); } } $date = NULL; if ($customFields[$customFieldId]['data_type'] == 'Date') { if (!CRM_Utils_System::isNull($value)) { $format = $customFields[$customFieldId]['date_format']; $date = CRM_Utils_Date::processDate($value, NULL, FALSE, 'YmdHis', $format); } $value = $date; } if ($customFields[$customFieldId]['data_type'] == 'Float' || $customFields[$customFieldId]['data_type'] == 'Money') { if (!$value) { $value = 0; } if ($customFields[$customFieldId]['data_type'] == 'Money') { $value = CRM_Utils_Rule::cleanMoney($value); } } if (($customFields[$customFieldId]['data_type'] == 'StateProvince' || $customFields[$customFieldId]['data_type'] == 'Country') && empty($value)) { // CRM-3415 $value = 0; } $fileId = NULL; if ($customFields[$customFieldId]['data_type'] == 'File') { if (empty($value)) { return; } $config = CRM_Core_Config::singleton(); $fName = $value['name']; $mimeType = $value['type']; $filename = pathinfo($fName, PATHINFO_BASENAME); // rename this file to go into the secure directory if (!rename($fName, $config->customFileUploadDir . $filename)) { CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory')); } if ($customValueId) { $query = "\nSELECT {$columnName}\n FROM {$tableName}\n WHERE id = %1"; $params = array(1 => array($customValueId, 'Integer')); $fileId = CRM_Core_DAO::singleValueQuery($query, $params); } $fileDAO = new CRM_Core_DAO_File(); if ($fileId) { $fileDAO->id = $fileId; } $fileDAO->uri = $filename; $fileDAO->mime_type = $mimeType; $fileDAO->upload_date = date('Ymdhis'); $fileDAO->save(); $fileId = $fileDAO->id; $value = $filename; } if (!is_array($customFormatted)) { $customFormatted = array(); } if (!array_key_exists($customFieldId, $customFormatted)) { $customFormatted[$customFieldId] = array(); } $index = -1; if ($customValueId) { $index = $customValueId; } if (!array_key_exists($index, $customFormatted[$customFieldId])) { $customFormatted[$customFieldId][$index] = array(); } $customFormatted[$customFieldId][$index] = array('id' => $customValueId > 0 ? $customValueId : NULL, 'value' => $value, 'type' => $customFields[$customFieldId]['data_type'], 'custom_field_id' => $customFieldId, 'custom_group_id' => $groupID, 'table_name' => $tableName, 'column_name' => $columnName, 'file_id' => $fileId, 'is_multiple' => $customFields[$customFieldId]['is_multiple']); //we need to sort so that custom fields are created in the order of entry krsort($customFormatted[$customFieldId]); return $customFormatted; }