-
Notifications
You must be signed in to change notification settings - Fork 5
/
civimigrateoptions.class.inc
176 lines (158 loc) · 4.6 KB
/
civimigrateoptions.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
<?php
/**
* @file
* Migrate members from ORCAS to CiviCRM.
*/
/**
*
*/
class CiviMigrateOptions extends Civimigration {
protected $debug = 0; // Set to 1 for debug info.
protected $json_folder = NULL;
protected $options = array();
protected $entity = 'option_group';
public function __construct($arguments = array()) {
parent::__construct($arguments);
$this->description = t('Create custom values.');
// 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 . '/optionvalues.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, 'name'),
new CiviMigrateFileItemJSON($item_url, $http_options, 'name'),
$this->fields()
);
$this->addFieldMapping('name', 'name');
$this->addFieldMapping('label', 'label');
$this->addFieldMapping('value', 'value');
$this->addFieldMapping('is_active')->defaultValue(1);
$this->addFieldMapping('description', 'description')->defaultValue('String');
}
/**
* 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',
'name' => t('Option Group Name'),
'title' => t('title'),
'label' => t('value'),
'data_type' => t('description'),
);
}
/**
* (non-PHPdoc)
* @see Civimigration::prepare()
*/
function prepare(&$entity, &$row){
if(empty($entity->id)){
$entity->id = $this->getOptionGroupID($row->name);
}
foreach ($row->options as $option){
$this->ensureOptionValueID($option, $entity);
}
}
/**
*
* @param object $entity
* @param stdClass $row
function complete($entity, $row){
}
*/
/**
* 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 getOptionGroupID($groupTitle){
$group = civicrm_api('option_group', 'get', array(
'version' => 3,
'name' => $groupTitle,
'return' => 'id',
)
);
if(!empty($group['id'])){
//make sure it's active
civicrm_api('option_group', 'create', array(
'version' => 3,
'id' => $group['id'],
'is_active' => 1,
));
return $group['id'];
}
$group = civicrm_api('option_group', 'create', array(
'version' => 3,
'name' => $groupTitle,
'is_active' => 1,
)
);
if($group['is_error']){
throw new MigrateException("failed to create $groupTitle : " . $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 ensureOptionValueID($option, $entity){
$params = array(
'version' => 3,
'option_group_id' => $entity->id,
);
foreach ($option as $property => $value){
$params[$property] = $value;
}
$optionValue = civicrm_api('option_value', 'get', $params);
if(empty($optionValue['id'])){
$result = civicrm_api('option_value', 'create', $params);
drupal_set_message("<pre>" . print_r($result ,1) . "</pre>");
if($result['is_error']){
throw new MigrateException('failed to create value ' . print_r($result,1));
}
}
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();
}
}