public function CivicrmEntityController::save in CiviCRM Entity 7.2
Same name and namespace in other branches
- 7 civicrm_entity_controller.inc \CivicrmEntityController::save()
Implements EntityAPIControllerInterface.
Parameters
$entity:
DatabaseTransaction $transaction: Optionally a DatabaseTransaction object to use. Allows overrides to pass in their transaction object.
Return value
object
Throws
Exception
Overrides EntityAPIController::save
File
- ./
civicrm_entity_controller.inc, line 130
Class
- CivicrmEntityController
- Entity Controller for CiviCRM entities
Code
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (!civicrm_initialize()) {
throw new Exception('civicrm inaccessible');
}
try {
$entity->is_new = !empty($entity->is_new) || empty($entity->{$this->idKey});
// @TODO should we call this hook when drupal saves (as opposed
// to Civi?) ditto insert, update.
$this
->invoke('presave', $entity);
// temporary hack for activity saving issues
// https://www.drupal.org/node/2736153
if ($this->entityType == 'civicrm_activity' && isset($entity->campaign_id) && $entity->campaign_id == 0) {
$entity->campaign_id = NULL;
}
$params = get_object_vars($entity);
$params_to_unset = array(
'is_new',
'rdf_mapping',
'original',
'submit',
'form_build_id',
'form_token',
'form_id',
'op',
'changed',
);
foreach ($params as $key => $value) {
// unset some drupal form stuff
if (in_array($key, $params_to_unset)) {
unset($params[$key]);
}
// unset the drupal fields
if (strpos($key, 'field_') === 0) {
unset($params[$key]);
}
if (substr($key, 0, 7) === 'custom_' && empty($entity->original->{$key}) && empty($value)) {
unset($params[$key]);
}
// handle differences between 'get' and 'create' api for contact reference custom fields
if (substr($key, 0, 7) === 'custom_' && substr($key, -3) === '_id') {
$customkey = substr($key, 0, strlen($key) - 3);
if (!is_numeric($params[$customkey]) && is_numeric($params[$key])) {
$params[$customkey] = $params[$key];
}
unset($params[$key]);
}
}
// special handling for relationships, empty case_id throws api exception
if ($this->entityType == 'civicrm_relationship') {
if (isset($params['case_id']) && ($params['case_id'] == '' || is_null($params['case_id']))) {
unset($params['case_id']);
}
}
$params['version'] = 3;
$params['sequential'] = 1;
if ($entity->is_new) {
if (isset($params['id'])) {
unset($params['id']);
}
$result = civicrm_api(substr($this->entityType, 8), 'create', $params);
}
else {
$result = civicrm_api(substr($this->entityType, 8), 'create', $params);
}
if (isset($entity->is_new)) {
$is_new = $entity->is_new;
unset($entity->is_new);
}
unset($entity->is_new_revision);
unset($entity->original);
if (!civicrm_error($result)) {
if ($is_new) {
$entity->id = $result['values'][0]['id'];
$this
->invoke('insert', $entity);
}
else {
$this
->invoke('update', $entity);
}
foreach ($result['values'][0] as $key => $value) {
$entity->key = $value;
}
return new CivicrmEntity(array_keys($result['values']), $this->entityType);
}
throw new Exception($result['error_message']);
} catch (Exception $e) {
watchdog_exception($this->entityType, $e);
throw $e;
}
}