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