/** * Function to take in an array of entityID, custom_XXX => value * and set the value in the appropriate table. Should also be able * to set the value to null. Follows api parameter/return conventions * * @array $params * * @return array * @static */ static function setValues(&$params) { require_once 'CRM/Utils/Type.php'; if (!isset($params['entityID']) || CRM_Utils_Type::escape($params['entityID'], 'Integer', false) === null) { return CRM_Core_Error::createAPIError(ts('entityID needs to be set and of type Integer')); } // first collect all the id/value pairs. The format is: // custom_X => value or custom_X_VALUEID => value (for multiple values), VALUEID == -1, -2 etc for new insertions $values = array(); $fieldValues = array(); require_once 'CRM/Core/BAO/CustomField.php'; foreach ($params as $n => $v) { if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($n, true)) { $fieldID = (int) $customFieldInfo[0]; if (CRM_Utils_Type::escape($fieldID, 'Integer', false) === null) { return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1', array(1 => $fieldID))); } if (!array_key_exists($fieldID, $fieldValues)) { $fieldValues[$fieldID] = array(); } $id = -1; if ($customFieldInfo[1]) { $id = (int) $customFieldInfo[1]; } $fieldValues[$fieldID][] = array('value' => $v, 'id' => $id); } } $fieldIDList = implode(',', array_keys($fieldValues)); // format it so that we can just use create $sql = "\nSELECT cg.table_name as table_name ,\n cg.id as cg_id ,\n cg.is_multiple as is_multiple,\n cf.column_name as column_name,\n cf.id as cf_id ,\n cf.data_type as data_type \nFROM civicrm_custom_group cg,\n civicrm_custom_field cf\nWHERE cf.custom_group_id = cg.id\nAND cf.id IN ( {$fieldIDList} )\n"; $dao = CRM_Core_DAO::executeQuery($sql); $cvParams = array(); while ($dao->fetch()) { // ensure that value is of the right data type $dataType = $dao->data_type == 'Date' ? 'Timestamp' : $dao->data_type; foreach ($fieldValues[$dao->cf_id] as $fieldValue) { if (CRM_Utils_Type::escape($fieldValue['value'], $dataType, false) === null) { return CRM_Core_Error::createAPIError(ts('value: %1 is not of the right field data type: %2', array(1 => $fieldValue['value'], 2 => $dao->data_type))); } $cvParam = array('entity_id' => $params['entityID'], 'value' => $fieldValue['value'], 'type' => $dataType, 'custom_field_id' => $dao->cf_id, 'custom_group_id' => $dao->cg_id, 'table_name' => $dao->table_name, 'column_name' => $dao->column_name, 'is_multiple' => $dao->is_multiple); if (!array_key_exists($dao->table_name, $cvParams)) { $cvParams[$dao->table_name] = array(); } if (!array_key_exists($fieldValue['id'], $cvParams[$dao->table_name])) { $cvParams[$dao->table_name][$fieldValue['id']] = array(); } if ($fieldValue['id'] > 0) { $cvParam['id'] = $fieldValue['id']; } $cvParams[$dao->table_name][$fieldValue['id']][] = $cvParam; } } if (!empty($cvParams)) { self::create($cvParams); return CRM_Core_Error::createAPISuccess(); } return CRM_Core_Error::createAPIError(ts('Unknown error')); }
/** * * @param <type> $result * @return <type> */ function civicrm_create_success($result = 1) { return CRM_Core_Error::createAPISuccess($result); }