forked from eileenmcnaughton/civicrm_entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm_entity_controller.inc
364 lines (320 loc) · 12.4 KB
/
civicrm_entity_controller.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* Entity Controller for CiviCRM entities
*/
class CivicrmEntityController extends EntityAPIController {
public function __construct($entityType) {
parent::__construct($entityType);
}
/**
* Implements DrupalEntityControllerInterface::load().
*
* @param array $ids
* @param array $conditions
*
* @return array
*/
public function load($ids = array(), $conditions = array()) {
$entities = array();
if (!civicrm_initialize(TRUE)) {
return;
}
// Not sure about revisioning out at this stage - I don't know if
// it could have any later use. Revisions are not statically
// cached, and require a different query to other conditions, so
// separate the revision id into its own variable.
if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
$revision_id = $conditions[$this->revisionKey];
unset($conditions[$this->revisionKey]);
}
else {
$revision_id = FALSE;
}
/*
* this seems 'harmless' - but not necessarily relevant?
* ie. deals with caching on the drupal side
*/
// Create a new variable which is either a prepared version of the $ids
// array for later comparison with the entity cache, or FALSE if no $ids
// were passed. The $ids array is reduced as items are loaded from cache,
// and we need to know if it's empty for this reason to avoid querying the
// database when all requested entities are loaded from cache.
if(!empty($ids)) {
foreach ($ids as $index => $id) {
if (is_array($id)) {
$ids[$index] = !empty($id[0]) ? $id[0] : NULL;
}
}
}
$passed_ids = !empty($ids) && !is_null(reset($ids)) ? array_flip((array) $ids) : FALSE;
// Try to load entities from the static cache, if the entity type supports
// static caching.
if ($this->cache && !$revision_id) {
$entities += $this->cacheGet($ids, $conditions);
// If any entities were loaded, remove them from the ids still to load.
if ($passed_ids) {
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
}
/*
* OK - here is where we will actually 'Do' something that is Civi-Specific
* In drupal land $ids = FALSE would load all - let's only do specific
*/
// Load any remaining entities from the database. This is the case if $ids
// is set to FALSE (so we load all entities), if there are any ids left to
// load, if loading a revision, or if $conditions was passed without $ids.
if ($ids === FALSE || $ids || $revision_id || ($conditions && !$passed_ids)) {
// Build the query.
try {
$queried_entities = array();
if ($conditions) {
$queried_entities += $this->loadEntities($conditions);
}
if (!empty($ids)) {
foreach ($ids as $id) {
// we can't rely on civicrm api accepting the 'IN' => array(1,5,6) for all entities
$queried_entities += $this->loadEntities(array('id' => $id));
}
}
} catch (Exception $e) {
watchdog('civicrm_entity', 'Failed to load ' . $this->entityInfo['description'], $conditions);
}
}
// Pass all entities loaded from the database through $this->attachLoad(),
// which attaches fields (if supported by the entity type) and calls the
// entity type specific load callback, for example hook_node_load().
if (!empty($queried_entities)) {
$this->attachLoad($queried_entities, $revision_id);
$entities += $queried_entities;
}
if ($this->cache) {
// Add entities to the cache if we are not loading a revision.
if (!empty($queried_entities) && !$revision_id) {
$this->cacheSet($queried_entities);
}
}
// Ensure that the returned array is ordered the same as the original
// $ids array if this was passed in and remove any invalid ids.
if ($passed_ids) {
// Remove any invalid ids from the array.
$passed_ids = array_intersect_key($passed_ids, $entities);
foreach ($entities as $entity) {
$passed_ids[$entity->{$this->idKey}] = $entity;
}
$entities = $passed_ids;
}
return $entities;
}
/**
* Implements EntityAPIControllerInterface.
*
* @param $entity
* @param DatabaseTransaction $transaction
* Optionally a DatabaseTransaction object to use. Allows
* overrides to pass in their transaction object.
* @return object
* @throws Exception
*/
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;
}
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
if (!civicrm_initialize()) {
throw new Exception('civicrm inaccessible');
}
$entities = entity_load($this->entityType, $ids);
if(count($entities)) {
foreach ($ids as $id) {
$params['version'] = 3;
$params['id'] = $id;
try {
$entity = $entities[$id];
$this->invoke('predelete', $entity);
$result = civicrm_api(substr($this->entityType, 8), 'delete', $params);
if (!civicrm_error($result)) {
field_attach_delete($this->entityType, $entity);
// $this->invoke('delete', $entity);
return;
}
throw new Exception($result['error_message']);
} catch (Exception $e) {
watchdog_exception($this->entityType, $e);
throw $e;
}
} //end foreach id
}
}
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$entity->content = $content;
$build = parent::buildContent($entity, $view_mode, $langcode, $content);
$langcode = isset($langcode) ? $langcode : $GLOBALS['language_content']->language;
// Add in fields.
if (!empty($this->entityInfo['fieldable'])) {
// Perform the preparation tasks if they have not been performed yet.
// An internal flag prevents the operation from running twice.
$key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
field_attach_prepare_view($this->entityType, array($key => $entity), $view_mode);
$entity->content = field_attach_view($this->entityType, $entity, $view_mode, $langcode);
}
$temp_build = $entity->content;
unset($entity->content);
foreach ($temp_build as $key => $value) {
$build[$key] = $value;
}
if (module_exists('ds')) {
$layout = ds_get_layout($this->entityType, $this->entityType, $view_mode);
if (!empty($layout['css']) && empty($layout['settings']['layout_disable_css'])) {
// Add css file.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$build['#attached']['css'][] = $layout['path'] . '/' . $layout['panels']['css'];
}
else {
$build['#attached']['css'][] = $layout['path'] . '/' . $layout['layout'] . '.css';
}
}
}
return $build;
}
/**
* Load entities to an array.
*
* @param array $condition
*
* @return mixed
* @throws \CiviCRM_API3_Exception
*/
protected function loadEntities($condition) {
$entities = array();
$fields = civicrm_api3($this->entityInfo['description'], 'getfields', array('action' => 'create'));
if (!empty($fields['values'])) {
$return_fields['return'] = array_keys($fields['values']);
}
// some fields are still not retrieved, for example the financial_type_id for Contribution get even though they are returned in the getfields list
// and if you don't use any return conditions, they come through, but in that case custom fields don't come through
// typical inconsistencies in the CiviCRm api that play havoc for automation
// we really would prefer to use the 'create' action in the getfields list, because you get more....
// but we need get to return all the field values for all the fields that are available for writing in the 'create' action
// so we make 2 api calls, one with the 'return' property, and one without
// get what we get with a API call with no 'return' property
$civicrm_entities = civicrm_api3($this->entityInfo['description'], 'get', $condition);
// get what we get by the 'return' property in the API call
if(!empty($return_fields['return'])) {
$condition['return'] = $return_fields['return'];
$civicrm_entities2 = civicrm_api3($this->entityInfo['description'], 'get', $condition);
}
// merge the two together
$new_civicrm_entities = array();
foreach ($civicrm_entities['values'] as $id => $entity) {
if(!empty($civicrm_entities2['values'][$id])) {
$new_civicrm_entities[$id] = array_merge($entity, $civicrm_entities2['values'][$id]);
}
else {
$new_civicrm_entities[$id] = $entity;
}
// unset anything not in the return array (if you wanted to) but why?
/*foreach($new_civicrm_entities[$id] as $key => $value){
if(!in_array($key, $return_fields['return'])) {
unset($new_civicrm_entities[$id][$key]);
}
}*/
}
// create the Drupal Entities
if (count($new_civicrm_entities)) {
foreach ($new_civicrm_entities as $id => $civicrm_entity) {
// @TODO improve this casting.
$entities[$id] = new CivicrmEntity($civicrm_entity, $this->entityType);
if ($this->entityType == 'civicrm_contribution') {
$entities[$id]->source = $entities[$id]->contribution_source;
}
}
return $entities;
}
return $entities;
}
}