function wf_crm_save_custom in Webform CiviCRM Integration 7.3
Save custom data for an entity
Parameters
$entity: Array of values
$entity_id: Numeric id of entity
$entity_type: Type of crm entity, e.g. "Contact"
$known: Is this a known record (as opposed to a contact matched via dedupe rules)? We only allow saving blank fields for known contacts.
1 call to wf_crm_save_custom()
- wf_crm_process_submission in ./
webform_civicrm_forms.inc - Webform submission handler Create/update CiviCRM contacts and related data Called by presave, insert and update webform hooks
File
- ./
webform_civicrm_utils.inc, line 428 - Webform CiviCRM module's common utility functions.
Code
function wf_crm_save_custom($entity, $entity_id, $entity_type, $known = TRUE) {
$existing = wf_crm_get_custom($entity_id, $entity_type, FALSE);
$params = array(
'entityID' => $entity_id,
);
foreach ($entity as $table => $values) {
if (substr($table, 0, 2) == 'cg' && is_array($values)) {
if (empty($existing[$table])) {
$existing[$table] = array();
}
$insert = 0;
foreach ($values as $custom) {
if ($id = each($existing[$table])) {
$suf = $id['key'];
}
else {
$suf = --$insert;
}
foreach ($custom as $k => $v) {
// Only save if this is not blank or data already exists and record is known
if ($v !== '' || $suf > 0 && $known) {
$params[$k . '_' . $suf] = $v;
}
}
}
}
}
if (count($params) > 1) {
$result = CRM_Core_BAO_CustomValueTable::setValues($params);
// Prevent wholesale failure by saving each param individually if there was an error while trying to save them all at once
if (!empty($result['is_error'])) {
$bt = debug_backtrace();
array_shift($params);
foreach ($params as $k => $v) {
$single_param = array(
'entityID' => $entity_id,
$k => $v,
);
$result = CRM_Core_BAO_CustomValueTable::setValues($single_param);
if (!empty($result['is_error'])) {
$file = explode('/', $bt[0]['file']);
watchdog('webform_civicrm', 'The CiviCRM "CustomValueTable::setValues" function returned the error: "%msg" when called by line !line of !file with the following parameters: "!params"', array(
'%msg' => $result['error_message'],
'!line' => $bt[0]['line'],
'!file' => array_pop($file),
'!params' => print_r($single_param, TRUE),
), WATCHDOG_ERROR);
}
}
}
}
}