-
Notifications
You must be signed in to change notification settings - Fork 5
/
civimigratecustomfields.class.inc
225 lines (210 loc) · 6.33 KB
/
civimigratecustomfields.class.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
<?php
/**
*
*/
abstract class CiviMigrateCustomFields extends Civimigration {
protected $debug = 0; // Set to 1 for debug info.
protected $json_folder = NULL;
protected $customFields = array();
protected $entity = 'custom_field';
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Create custom fields.');
// This can also be an URL instead of a file path but we are assuming it shipe with the migrate module
$json_folder = $this->json_folder;
if(empty($json_folder)){
throw new MigrateException('json folder must be defined');
}
$list_url = $json_folder . 'list.json';
$item_url = $json_folder . '/customfields.json';
$http_options = array();
$this->map = new MigrateSQLMap($this->machineName,
array(
'fieldname' => array(
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
),
),
MigrateDestinationCivicrmApi::getKeySchema()
);
$this->source = new MigrateSourceList(
new CiviMigrateListJSON($item_url, 'fieldname'),
new CiviMigrateFileItemJSON($item_url, $http_options, 'fieldname'),
$this->fields()
);
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('label', 'label');
$this->addFieldMapping('is_active')->defaultValue(1);
$this->addFieldMapping('data_type', 'data_type')->defaultValue('String');
$this->addFieldMapping('html_type', 'html_type')->defaultValue('Text');
}
/**
* Return the fields (this is cleaner than passing in the array in the MigrateSourceList class above)
* @return array
*/
function fields() {
return array(
'title' => 'The title of the content',
'fieldname' => t('fieldname'),
'title' => t('title'),
'label' => t('Label'),
'data_type' => t('data type'),
'html_type' => t('html_type'),
);
}
/**
* (non-PHPdoc)
* @see Civimigration::prepare()
*/
function prepare(&$entity, &$row){
if(empty($entity->custom_group_id)){
if(isset($row->custom_group->extends)){
$extends = $row->custom_group->extends;
}
else{
$extends = 'Contact';
}
$entity->custom_group_id = $this->getCustomGroupID($row->custom_group->title, $extends);
}
if(empty($entity->id)){
$entity->id = $this->getCustomFieldID($entity);
}
if(!empty($row->options) && $row->options->type == 'shared'){
// we are sharing with another field
$entity->option_group_id = civicrm_api('custom_field', 'getvalue', array(
'version' => 3,
'name' => $row->options->fieldname,
'return' => 'option_group_id',
));
}
}
/**
*
* @param object $entity
* @param stdClass $row
*/
function complete($entity, $row){
if(isset($row->options)){
$optionSpec = $row->options;
}
else{
return;
}
if($optionSpec->type == 'query'){
$options = $this->getFieldOptions($optionSpec->table, $optionSpec->key, $optionSpec->label);
$this->createCustomFieldOptions($entity['id'], $options);
}
}
/**
* Here we establish all our custom fields
* (non-PHPdoc)
* @see Migration::preImport()
*/
public function postImport() {
parent::postImport();
if(!civicrm_initialize()){
return;
}
// need to clear the cache once the fields are created
civicrm_api('system', 'flush', array('version' => 3));
}
/**
* Get or create relevant group id
* @param integer $groupTitle
* @param string $extends
* @throws MigrateException
* @return integer groupID
*/
function getCustomGroupID($groupTitle, $extends = 'Contact'){
$group = civicrm_api('custom_group', 'get', array(
'version' => 3,
'title' => $groupTitle,
'return' => 'id',
)
);
if(!empty($group['id'])){
//make sure it's active
civicrm_api('custom_group', 'create', array(
'version' => 3,
'id' => $group['id'],
'is_active' => 1,
));
return $group['id'];
}
$group = civicrm_api('custom_group', 'create', array(
'version' => 3,
'title' => $groupTitle,
'extends' => $extends,
'is_active' => 1,
'style' => 'Inline',
)
);
if($group['is_error']){
throw new MigrateException("failed to create $groupTitle extending $extends : " . $group['error_message']);
}
return $group['id'];
}
/**
* Get / create custom field
* @param integer $customGroupID
* @param string $field - this will be the key of a field in $this->customFields
*/
function getCustomFieldID($entity){
$customField = civicrm_api('custom_field', 'get', array(
'version' => 3,
'custom_group_id' => $entity->custom_group_id,
'label' => $entity->label,
)
);
if(!empty($customField['id'])){
return $customField['id'];
}
return NULL;
}
/**
* Get required options set
* @param string $field - this will be the key of a field in $this->customFields
*/
function getFieldOptions($table, $keyField, $labelField){
$query = db_select($table, 'tbl');
$query->addField('tbl', $keyField, 'key_field');
$query->addField('tbl', $labelField, 'label');
return $query->execute()->fetchAllKeyed();
}
/**
* check / create options for field
* @param integer $fieldID
* @param array $options
*/
function createCustomFieldOptions($fieldID, $options){
$optionGroupID = civicrm_api('custom_field', 'getvalue', array(
'version' => 3,
'id' => $fieldID,
'return' => 'option_group_id',
));
if(!is_numeric($optionGroupID)){
throw new MigrateException('no option group id for field' . $fieldID);
}
foreach ($options as $key => $label){
$exists = civicrm_api('option_value', 'getcount', array(
'version' => 3,
'option_group_id' => $optionGroupID,
'value' => $key
)
);
if(!$exists){
$optionValue = civicrm_api('option_value', 'create', array(
'version' => 3,
'option_group_id' => $optionGroupID,
'value' => $key,
'label' => $label,
)
);
if($optionValue['is_error']){
throw new MigrateException("could not create option value $key : $label for option group $optionGroupID for field $fieldID");
}
}
}
}
}