forked from eileenmcnaughton/civimigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civimigrate.module
executable file
·258 lines (245 loc) · 7.01 KB
/
civimigrate.module
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
<?php
/*
* Implementation of hook_migrate_api().
*/
function civimigrate_migrate_api() {
$api = array(
'api' => 2,
);
return $api;
}
function civimigrate_menu() {
return array(
'civimigrate-mapper' => array(
'page callback' => 'civimigrate_callback',
'access arguments' => array(
'administer nodes'
),
'type' => MENU_CALLBACK
),
'civimigrate-mapper/%cmid/edit' => array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'civimigrate_form',
1
),
'type' => MENU_CALLBACK,
'access arguments' => array(
'administer nodes'
)
)
);
}
function cmid_load($cmid) {
$results = db_query("SELECT * FROM {civimigrate_mappings} WHERE cmid = :cmid", array(
':cmid' => $cmid
));
$cmid = array();
foreach ($results as $result) {
$cmid = array(
'cmid' => $result->cmid,
'source_field' => $result->source_field,
'destination_field' => $result->destination_field,
'description' => $result->description,
'table_name' => $result->table_name,
'real_name' => $result->real_name,
'issuegroup' => $result->issuegroup
);
}
return $cmid;
}
function civimigrate_callback() {
drupal_set_title("Civimigrate Mappings");
$results = db_query("SELECT * FROM {civimigrate_mappings}");
$table = array(
'header' => array(
'cmid',
'source_field',
'destination_field',
'description',
'real_name',
'table_name',
'issuegroup',
'distinct row count',
'sample data',
'Edit'
),
'rows' => array()
);
foreach ($results as $result) {
$rowDetail = array(
$result->cmid,
$result->source_field,
$result->destination_field,
$result->description,
$result->real_name,
$result->table_name,
$result->issuegroup,
);
if (!empty($result->table_name) && !empty($result->real_name)) {
try {
$count = $rowDetail[] = db_query(
"SELECT COUNT(DISTINCT {$result->real_name}) FROM {{$result->table_name}}")->fetchField();
if ($count) {
$rowDetail[] = substr(implode(',', db_query(
"SELECT DISTINCT {$result->real_name} FROM {{$result->table_name}} WHERE {$result->real_name} IS NOT NULL LIMIT 5"
)->fetchAssoc()), 0, 32);
}
else {
$rowDetail[] = '';
}
}
catch (Exception $e) {
$rowDetail[] = '?';
$rowDetail[] = '?';
}
}
else {
$rowDetail[] = '';
$rowDetail[] = '';
}
$rowDetail[] = l('Edit', "civimigrate-mapper/{$result->cmid}/edit");
$table['rows'][] = $rowDetail;
}
return theme('table', $table);
}
function civimigrate_form($form, &$form_state, $cmid) {
drupal_set_title("Editing mapping {$cmid['cmid']}");
$form['source_field'] = array(
'#title' => 'Source Field',
'#type' => 'textfield',
'#default_value' => $cmid['source_field']
);
$form['destination_field'] = array(
'#title' => 'Destination Field',
'#type' => 'textfield',
'#default_value' => $cmid['destination_field']
);
$form['description'] = array(
'#title' => 'Description',
'#type' => 'textarea',
'#default_value' => $cmid['description']
);
$form['default_value'] = array(
'#title' => 'Default value',
'#type' => 'textarea',
'#default_value' => $cmid['default_value']
);
$form['table_name'] = array(
'#title' => 'Table',
'#type' => 'textfield',
'#default_value' => $cmid['table_name']
);
$form['real_name'] = array(
'#title' => 'Real Field Name',
'#type' => 'textfield',
'#default_value' => $cmid['real_name']
);
$form['issuegroup'] = array(
'#title' => 'Issue Group',
'#type' => 'textfield',
'#default_value' => $cmid['issuegroup']
);
$form['cmid'] = array(
'#type' => 'hidden',
'#value' => $cmid['cmid']
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save'
);
return $form;
}
function civimigrate_form_validate($form, &$form_state) {
foreach ($form_state['values'] as $key => $value) {
$value = trim($value);
if (empty($value) && $value != 0) {
$form_state['values'][$key] = NULL;
}
}
}
function civimigrate_form_submit($form, &$form_state) {
$record = array(
'source_field' => $form_state['values']['source_field'],
'destination_field' => $form_state['values']['destination_field'],
'description' => $form_state['values']['description'],
'table_name' => $form_state['values']['table_name'],
'real_name' => $form_state['values']['real_name'],
'issuegroup' => $form_state['values']['issuegroup']
);
$result = db_update('civimigrate_mappings')->fields($record)->condition('cmid', $form_state['values']['cmid'])->execute();
drupal_set_message("$result row(s) updated.");
drupal_goto('civimigrate-mapper');
}
/**
* Implements hook_entity_info().
*/
function civimigrate_entity_info() {
$entity_info['civimigrate_mapping'] = array(
'label' => t('CiviMigrate Mappings'),
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'cmid'
),
'entity class' => 'Entity',
'controller class' => 'EntityAPIController',
'base table' => 'civimigrate_mappings',
'uri callback' => 'civimigrate_uri',
'label callback' => 'entity_class_label',
'static cache' => TRUE,
'module' => 'civimigrate',
'access callback' => 'civimigrate_mapping_access',
'views controller class' => 'EntityDefaultViewsController',
'view modes' => array(
'full' => array(
'label' => t('CiviMigrate Mapping'),
'custom settings' => FALSE,
),
)
);
return $entity_info;
}
/**
* @param $mapping
*
* @return array
*/
function civimigrate_uri($mapping){
return array(
'path' => 'civimigrate_mapping/' . $mapping->id,
);
}
/**
* Placeholder access function
* @return bool
*/
function civimigrate_mapping_access() {
return TRUE;
}
/**
* fill the mappings table - ie
* civimigrate_populate_mappings('records', array('migration' => 'Contacts', 'issuegroup' => 'needs mapping'));
*
* e.g
* drush eval "civimigrate_populate_mappings('source_contacts', array('migration' => 'Contacts', 'issuegroup' => 'needs mapping'))"
*
* @param $table
* @param array $defaults
*/
function civimigrate_populate_mappings($table, $defaults = array()) {
$columns = db_query("SHOW columns from {" . $table . "}")->fetchAllAssoc('Field');
$tableColumns = explode(',', strtolower(implode(',', array_keys($columns))));
$existingColumns = db_query("SELECT real_name FROM {civimigrate_mappings} WHERE table_name = :1", array(':1' => $table))->fetchAllAssoc('real_name');
$missingFields = array_diff_key(array_flip($tableColumns), $existingColumns);
foreach (array_keys($missingFields) as $missingField) {
$entity = entity_create('civimigrate_mapping', array_merge(
array(
//'default' => '',
'source_field' => $table . '_' . $missingField,
'real_name' => $missingField,
'table_name' => $table,
),
$defaults));
$entity->save();
}
}