forked from eileenmcnaughton/civicrm_entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcivicrm_entity_default_views_controller.inc
293 lines (274 loc) · 10.7 KB
/
civicrm_entity_default_views_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
<?php
/**
* @TODO Document this class.
*/
class CiviCRMEntityDefaultViewsController extends EntityDefaultViewsController {
/**
* Defines the result for hook_views_data().
*/
public function views_data() {
$data = array();
$this->relationships = array();
if (!empty($this->info['base table'])) {
$table = $this->info['base table'];
// Define the base group of this table. Fields that don't
// have a group defined will go into this field by default.
$data[$table]['table']['group'] = drupal_ucfirst($this->info['label']);
$data[$table]['table']['entity type'] = $this->type;
// If the plural label isn't available, use the regular label.
$label = isset($this->info['plural label']) ? $this->info['plural label'] : $this->info['label'];
$data[$table]['table']['base'] = array(
'field' => $this->info['entity keys']['id'],
'title' => drupal_ucfirst($label),
'help' => isset($this->info['description']) ? $this->info['description'] : '',
);
$data[$table]['table']['entity type'] = $this->type;
$data[$table] += $this->schema_fields();
// Add in any reverse-relationships which have been determined.
$data += $this->relationships;
}
return $data;
}
/**
* Find views fields using schema & entity property information.
*/
protected function schema_fields() {
// We are not using the 'normal' schema function here due to the
// problems discussed in the readme.
if (empty($this->info['base table'])) {
return array();
}
$schema = civicrm_entity_get_schema($this->info['base table']);
$properties = entity_get_property_info($this->type) + array('properties' => array());
$data = array();
// We will take out the reliance on other schema related checks
// here, ideally we would 'read' getfields output to determine if
// we are looking at a DB field or a custom / pseudofield.
foreach ($properties['properties'] as $name => $property_info) {
if (!empty($property_info['schema field']) && !empty($schema['fields']) &&
isset($schema['fields'][$property_info['schema field']])
) {
if ($views_info = $this->map_from_schema_info($name, $schema['fields'][$property_info['schema field']], $property_info)) {
$data[$name] = $views_info;
}
}
}
return $data;
}
/**
* Comes up with views information based on the given schema and property
* info.
*/
protected function map_from_schema_info($property_name, $schema_field_info, $property_info) {
$type = isset($property_info['type']) ? $property_info['type'] : 'text';
$views_field_name = $property_info['schema field'];
$return = array();
if (!empty($schema_field_info['serialize'])) {
return FALSE;
}
$description = array(
'title' => $property_info['label'],
'help' => 'Provided by CiviCRM Entity' . (isset($property_info['description']) ? ': ' . $property_info['description'] : NULL),
);
// Add in relationships to related entities.
if (($info = entity_get_info($type)) && !empty($info['base table'])) {
// Prepare reversed relationship data.
$label_lowercase = drupal_strtolower($this->info['label'][0]) . drupal_substr($this->info['label'], 1);
$property_label_lowercase = drupal_strtolower($property_info['label'][0]) . drupal_substr($property_info['label'], 1);
// We name the field of the first reverse-relationship just with the
// base table to be backward compatible, for subsequents relationships we
// append the views field name in order to get a unique name.
$name = !isset($this->relationships[$info['base table']][$this->info['base table']]) ? $this->info['base table'] : $this->info['base table'] . '_' . $views_field_name;
$this->relationships[$info['base table']][$name] = array(
'title' => $this->info['label'],
'help' => t("Associated @label via the @label's @property.", array(
'@label' => $label_lowercase,
'@property' => $property_label_lowercase,
)),
'relationship' => array(
'label' => $this->info['label'],
'handler' => $this->getRelationshipHandlerClass($this->type, $type),
'base' => $this->info['base table'],
'base field' => $views_field_name,
'relationship field' => isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'],
),
);
$return['relationship'] = array(
'label' => drupal_ucfirst($info['label']),
'handler' => $this->getRelationshipHandlerClass($type, $this->type),
'base' => $info['base table'],
'base field' => isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'],
'relationship field' => $views_field_name,
);
// Add in direct field/filters/sorts for the id itself too.
$type = isset($info['entity keys']['name']) ? 'token' : 'integer';
// Append the views-field-name to the title if it is different to the
// property name.
if ($property_name != $views_field_name) {
$description['title'] .= ' ' . $views_field_name;
}
}
if (CRM_Utils_Array::value('mysql_type', $property_info)) {
$type = $property_info['mysql_type'];
}
switch ($type) {
case 'token':
case 'text':
$return += $description + array(
'field' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_sort',
),
'filter' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_argument_string',
),
);
break;
case 'decimal':
case 'integer':
$return += $description + array(
'field' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
'float' => ($type == 'decimal'),
),
'sort' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_sort',
),
'filter' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_filter_numeric',
),
'argument' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_argument_numeric',
),
);
break;
case 'datetime':
$return += $description + array(
'field' => array(
'real field' => $views_field_name,
'handler' => 'civicrm_handler_field_datetime',
'click sortable' => TRUE,
),
'sort' => array(
'real field' => $views_field_name,
'handler' => 'civicrm_handler_sort_date',
),
'filter' => array(
'real field' => $views_field_name,
'handler' => 'civicrm_handler_filter_datetime',
'is date' => TRUE,
),
'argument' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_argument_date',
),
);
break;
case 'date':
$return += $description + array(
'field' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_filter_date',
),
'argument' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_argument_date',
),
);
break;
case 'uri':
$return += $description + array(
'field' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_field_url',
'click sortable' => TRUE,
),
'sort' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_sort',
),
'filter' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_argument_string',
),
);
break;
case 'boolean':
$return += $description + array(
'field' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_field_boolean',
'click sortable' => TRUE,
),
'sort' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_sort',
),
'filter' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_filter_boolean_operator',
),
'argument' => array(
'real field' => $views_field_name,
'handler' => 'views_handler_argument_string',
),
);
break;
}
// If there is an options list callback, add to the filter and field.
if (isset($return['filter']) && !empty($property_info['options list'])) {
$return['filter']['handler'] = 'views_handler_filter_in_operator';
$return['filter']['options callback'] = array(
'EntityDefaultViewsController',
'optionsListCallback',
);
$return['filter']['options arguments'] = array(
$this->type,
$property_name,
'view',
);
}
// @todo: This class_exists is needed until views 3.2.
if (isset($return['field']) && !empty($property_info['options list']) && class_exists('views_handler_field_machine_name')) {
$return['field']['handler'] = 'views_handler_field_machine_name';
$return['field']['options callback'] = array(
'EntityDefaultViewsController',
'optionsListCallback',
);
$return['field']['options arguments'] = array(
$this->type,
$property_name,
'view',
);
}
return $return;
}
}