Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Migrate_Import { | |
3302658e TO |
36 | function __construct() { |
37 | } | |
6a488035 TO |
38 | |
39 | function run($file) { | |
40 | ||
41 | // read xml file | |
8080d1e0 TO |
42 | $dom = new DomDocument(); |
43 | if (! $dom->load($file)) { | |
44 | throw new CRM_Core_Exception("Failed to parse XML file \"$file\""); | |
45 | } | |
6a488035 TO |
46 | $dom->xinclude(); |
47 | $xml = simplexml_import_dom($dom); | |
48 | ||
3302658e TO |
49 | $idMap = array( |
50 | 'custom_group' => array(), | |
6a488035 TO |
51 | 'option_group' => array(), |
52 | ); | |
53 | ||
54 | // first create option groups and values if any | |
55 | $this->optionGroups($xml, $idMap); | |
56 | $this->optionValues($xml, $idMap); | |
57 | ||
58 | $this->relationshipTypes($xml); | |
59 | $this->contributionTypes($xml); | |
60 | ||
61 | // now create custom groups | |
62 | $this->customGroups($xml, $idMap); | |
63 | $this->customFields($xml, $idMap); | |
64 | ||
65 | // now create profile groups | |
66 | $this->profileGroups($xml, $idMap); | |
67 | $this->profileFields($xml, $idMap); | |
68 | $this->profileJoins($xml, $idMap); | |
69 | ||
70 | //create DB Template String sample data | |
71 | $this->dbTemplateString($xml, $idMap); | |
72 | ||
73 | // clean up all caches etc | |
74 | CRM_Core_Config::clearDBCache(); | |
75 | } | |
76 | ||
77 | function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) { | |
78 | if ($keyName) { | |
79 | if (isset($xml->$keyName)) { | |
80 | $dao->$keyName = (string ) $xml->$keyName; | |
81 | if ($dao->find(TRUE)) { | |
82 | CRM_Core_Session::setStatus(ts("Found %1, %2, %3", | |
3302658e TO |
83 | array( |
84 | 1 => $keyName, | |
85 | 2 => $dao->$keyName, | |
86 | 3 => $dao->__table | |
87 | ) | |
88 | ), '', 'info'); | |
6a488035 TO |
89 | return FALSE; |
90 | } | |
91 | } | |
92 | } | |
93 | ||
3302658e | 94 | $fields = & $dao->fields(); |
6a488035 TO |
95 | foreach ($fields as $name => $dontCare) { |
96 | if (isset($xml->$name)) { | |
97 | $value = (string ) $xml->$name; | |
4f652853 | 98 | $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR, |
6a488035 TO |
99 | CRM_Core_DAO::VALUE_SEPARATOR, |
100 | $value | |
101 | ); | |
102 | $dao->$name = $value; | |
103 | } | |
104 | } | |
105 | if ($save) { | |
106 | $dao->save(); | |
107 | } | |
108 | return TRUE; | |
109 | } | |
110 | ||
111 | function optionGroups(&$xml, &$idMap) { | |
112 | foreach ($xml->OptionGroups as $optionGroupsXML) { | |
113 | foreach ($optionGroupsXML->OptionGroup as $optionGroupXML) { | |
114 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
115 | $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name'); | |
116 | $idMap['option_group'][$optionGroup->name] = $optionGroup->id; | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | function optionValues(&$xml, &$idMap) { | |
122 | foreach ($xml->OptionValues as $optionValuesXML) { | |
123 | foreach ($optionValuesXML->OptionValue as $optionValueXML) { | |
124 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
125 | $optionValue->option_group_id = $idMap['option_group'][(string ) $optionValueXML->option_group_name]; | |
126 | $this->copyData($optionValue, $optionValueXML, FALSE, 'label'); | |
127 | if (!isset($optionValue->value)) { | |
128 | $sql = " | |
129 | SELECT MAX(ROUND(v.value)) + 1 | |
130 | FROM civicrm_option_value v | |
131 | WHERE v.option_group_id = %1 | |
132 | "; | |
133 | $params = array(1 => array($optionValue->option_group_id, 'Integer')); | |
134 | $optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params); | |
135 | } | |
136 | $optionValue->save(); | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | function relationshipTypes(&$xml) { | |
142 | ||
143 | foreach ($xml->RelationshipTypes as $relationshipTypesXML) { | |
144 | foreach ($relationshipTypesXML->RelationshipType as $relationshipTypeXML) { | |
145 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); | |
146 | $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b'); | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | function contributionTypes(&$xml) { | |
152 | ||
153 | foreach ($xml->ContributionTypes as $contributionTypesXML) { | |
154 | foreach ($contributionTypesXML->ContributionType as $contributionTypeXML) { | |
3302658e | 155 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
6a488035 TO |
156 | $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name'); |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | function customGroups(&$xml, &$idMap) { | |
162 | foreach ($xml->CustomGroups as $customGroupsXML) { | |
163 | foreach ($customGroupsXML->CustomGroup as $customGroupXML) { | |
164 | $customGroup = new CRM_Core_DAO_CustomGroup(); | |
165 | if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) { | |
166 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
167 | continue; | |
168 | } | |
169 | ||
170 | $saveAgain = FALSE; | |
171 | if (!isset($customGroup->table_name) || | |
172 | empty($customGroup->table_name) | |
173 | ) { | |
174 | // fix table name | |
175 | $customGroup->table_name = "civicrm_value_" . strtolower(CRM_Utils_String::munge($customGroup->title, '_', 32)) . "_{$customGroup->id}"; | |
176 | ||
177 | $saveAgain = TRUE; | |
178 | } | |
179 | ||
180 | // fix extends stuff if it exists | |
181 | if (isset($customGroupXML->extends_entity_column_value_option_group) && | |
182 | isset($customGroupXML->extends_entity_column_value_option_value) | |
183 | ) { | |
184 | $optValues = explode(",", $customGroupXML->extends_entity_column_value_option_value); | |
185 | $optValues = implode("','", $optValues); | |
186 | if (trim($customGroup->extends) != 'Participant') { | |
187 | $sql = " | |
188 | SELECT v.value | |
189 | FROM civicrm_option_value v | |
190 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
191 | WHERE g.name = %1 | |
192 | AND v.name IN (%2) | |
193 | "; | |
194 | $params = array( | |
3302658e TO |
195 | 1 => array( |
196 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
6a488035 TO |
197 | 'String', |
198 | ), | |
199 | 2 => array((string ) $optValues, 'String'), | |
200 | ); | |
3302658e | 201 | $dao = & CRM_Core_DAO::executeQuery($sql, $params); |
6a488035 TO |
202 | |
203 | $valueIDs = array(); | |
204 | while ($dao->fetch()) { | |
205 | $valueIDs[] = $dao->value; | |
206 | } | |
207 | if (!empty($valueIDs)) { | |
208 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
209 | $valueIDs | |
210 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
211 | ||
212 | // Note: No need to set extends_entity_column_id here. | |
213 | ||
214 | $saveAgain = TRUE; | |
215 | } | |
216 | } | |
217 | else { | |
218 | // when custom group extends 'Participant' | |
219 | $sql = " | |
220 | SELECT v.value | |
221 | FROM civicrm_option_value v | |
222 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
223 | WHERE g.name = 'custom_data_type' | |
224 | AND v.name = %1 | |
225 | "; | |
226 | $params = array( | |
3302658e TO |
227 | 1 => array( |
228 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
6a488035 | 229 | 'String', |
3302658e TO |
230 | ) |
231 | ); | |
6a488035 TO |
232 | $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params); |
233 | if ($valueID) { | |
234 | $customGroup->extends_entity_column_id = $valueID; | |
235 | } | |
236 | ||
237 | $optionIDs = array(); | |
238 | switch ($valueID) { | |
239 | case 1: | |
240 | // ParticipantRole | |
241 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
242 | $optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name'); | |
243 | break; | |
244 | ||
245 | case 2: | |
246 | // ParticipantEventName | |
247 | $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )"; | |
248 | $optionIDs = CRM_Event_PseudoConstant::event(NULL, FALSE, $condition); | |
249 | break; | |
250 | ||
251 | case 3: | |
252 | // ParticipantEventType | |
253 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
254 | $optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name'); | |
255 | break; | |
256 | } | |
257 | ||
258 | if (is_array($optionIDs) && !empty($optionIDs)) { | |
259 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
260 | array_keys($optionIDs) | |
261 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
262 | ||
263 | $saveAgain = TRUE; | |
264 | } | |
265 | } | |
266 | } | |
267 | ||
268 | if ($saveAgain) { | |
269 | $customGroup->save(); | |
270 | } | |
271 | ||
272 | CRM_Core_BAO_CustomGroup::createTable($customGroup); | |
273 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | function customFields(&$xml, &$idMap) { | |
279 | // Re-index by group id so we can build out the custom fields one table | |
280 | // at a time, and then rebuild the table triggers at the end, rather than | |
281 | // rebuilding the table triggers after each field is added (which is | |
282 | // painfully slow). | |
283 | $fields_indexed_by_group_id = array(); | |
284 | foreach ($xml->CustomFields as $customFieldsXML) { | |
285 | $total = count($customFieldsXML->CustomField); | |
286 | foreach ($customFieldsXML->CustomField as $customFieldXML) { | |
287 | $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name]; | |
288 | $fields_indexed_by_group_id[$id][] = $customFieldXML; | |
289 | } | |
290 | } | |
3302658e | 291 | while (list($group_id, $fields) = each($fields_indexed_by_group_id)) { |
6a488035 TO |
292 | $total = count($fields); |
293 | $count = 0; | |
3302658e | 294 | while (list(, $customFieldXML) = each($fields)) { |
6a488035 TO |
295 | $count++; |
296 | $customField = new CRM_Core_DAO_CustomField(); | |
297 | $customField->custom_group_id = $group_id; | |
298 | $skipStore = FALSE; | |
299 | if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) { | |
300 | $skipStore = TRUE; | |
301 | } | |
302 | ||
303 | if (empty($customField->option_group_id) && | |
304 | isset($customFieldXML->option_group_name) | |
305 | ) { | |
306 | $customField->option_group_id = $idMap['option_group'][(string ) $customFieldXML->option_group_name]; | |
307 | } | |
308 | if ($skipStore) { | |
309 | continue; | |
310 | } | |
311 | $customField->save(); | |
312 | ||
313 | // Only rebuild the table's trigger on the last field added to avoid un-necessary | |
314 | // and slow rebuilds when adding many fields at the same time. | |
315 | $triggerRebuild = FALSE; | |
3302658e | 316 | if ($count == $total) { |
6a488035 | 317 | $triggerRebuild = TRUE; |
3302658e | 318 | } |
6a488035 TO |
319 | $indexExist = FALSE; |
320 | CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild); | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | function dbTemplateString(&$xml, &$idMap) { | |
326 | foreach ($xml->Persistent as $persistentXML) { | |
327 | foreach ($persistentXML->Persistent as $persistent) { | |
328 | $persistentObj = new CRM_Core_DAO_Persistent(); | |
329 | ||
330 | if ($persistent->is_config == 1) { | |
331 | $persistent->data = serialize(explode(',', $persistent->data)); | |
332 | } | |
333 | $this->copyData($persistentObj, $persistent, TRUE, 'context'); | |
334 | } | |
335 | } | |
336 | } | |
337 | ||
338 | function profileGroups(&$xml, &$idMap) { | |
339 | foreach ($xml->ProfileGroups as $profileGroupsXML) { | |
340 | foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) { | |
341 | $profileGroup = new CRM_Core_DAO_UFGroup(); | |
342 | $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title'); | |
343 | $idMap['profile_group'][$profileGroup->name] = $profileGroup->id; | |
344 | $idMap['profile_group'][$profileGroup->title] = $profileGroup->id; | |
345 | } | |
346 | } | |
347 | } | |
348 | ||
349 | function profileFields(&$xml, &$idMap) { | |
350 | foreach ($xml->ProfileFields as $profileFieldsXML) { | |
351 | foreach ($profileFieldsXML->ProfileField as $profileFieldXML) { | |
352 | $profileField = new CRM_Core_DAO_UFField(); | |
353 | $profileField->uf_group_id = $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name]; | |
354 | $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name'); | |
355 | ||
356 | // fix field name | |
357 | if (substr($profileField->field_name, 0, 7) == 'custom.') { | |
358 | list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name); | |
359 | $sql = " | |
360 | SELECT f.id | |
361 | FROM civicrm_custom_field f | |
362 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id | |
363 | WHERE g.table_name = %1 | |
364 | AND f.column_name = %2 | |
365 | "; | |
3302658e TO |
366 | $params = array( |
367 | 1 => array($tableName, 'String'), | |
6a488035 TO |
368 | 2 => array($columnName, 'String'), |
369 | ); | |
370 | $cfID = CRM_Core_DAO::singleValueQuery($sql, $params); | |
371 | if (!$cfID) { | |
372 | CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3", | |
3302658e TO |
373 | array( |
374 | 1 => $profileField->field_name, | |
375 | 2 => $tableName, | |
376 | 3 => $columnName | |
377 | ) | |
378 | ) . "<br />"); | |
6a488035 TO |
379 | } |
380 | $profileField->field_name = "custom_{$cfID}"; | |
381 | } | |
382 | $profileField->save(); | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
387 | function profileJoins(&$xml, &$idMap) { | |
388 | foreach ($xml->ProfileJoins as $profileJoinsXML) { | |
389 | foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) { | |
390 | $profileJoin = new CRM_Core_DAO_UFJoin(); | |
391 | $profileJoin->uf_group_id = $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name]; | |
392 | $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module'); | |
393 | $profileJoin->save(); | |
394 | } | |
395 | } | |
396 | } | |
397 | } | |
398 |