CRM_Utils_Migrate_Export - Doc comment
[civicrm-core.git] / CRM / Utils / Migrate / Export.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_Export {
36
37 const XML_VALUE_SEPARATOR = ":;:;:;";
38
39 /**
40 * @var array description of export field mapping
41 *
42 * @code
43 * 'exampleEntityMappingName' => array(
44 * 'data' => array(), // placeholder; this will get filled-in during execution
45 * 'name' => 'CustomGroup', // per-item XML tag name
46 * 'scope' => 'CustomGroups', // container XML tag name
47 * 'required' => FALSE, // whether we *must* find records of this type
48 * 'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
49 * 'idNameMap' => array(), // placeholder; this will get filled-in during execution
50 * ),
51 * @endcode
52 */
53 protected $_xml;
54
55 function __construct() {
56 $this->_xml = array(
57 'customGroup' => array(
58 'data' => array(),
59 'name' => 'CustomGroup',
60 'scope' => 'CustomGroups',
61 'required' => FALSE,
62 'idNameFields' => array('id', 'name'),
63 'idNameMap' => array(),
64 ),
65 'customField' => array(
66 'data' => array(),
67 'name' => 'CustomField',
68 'scope' => 'CustomFields',
69 'required' => FALSE,
70 'idNameFields' => array('id', 'column_name'),
71 'idNameMap' => array(),
72 'mappedFields' => array(
73 array('optionGroup', 'option_group_id', 'option_group_name'),
74 array('customGroup', 'custom_group_id', 'custom_group_name'),
75 )
76 ),
77 'optionGroup' => array(
78 'data' => array(),
79 'name' => 'OptionGroup',
80 'scope' => 'OptionGroups',
81 'required' => FALSE,
82 'idNameMap' => array(),
83 'idNameFields' => array('id', 'name'),
84 ),
85 'relationshipType' => array(
86 'data' => array(),
87 'name' => 'RelationshipType',
88 'scope' => 'RelationshipTypes',
89 'required' => FALSE,
90 'idNameFields' => array('id', 'name_a_b'),
91 'idNameMap' => array(),
92 ),
93 'locationType' => array(
94 'data' => array(),
95 'name' => 'LocationType',
96 'scope' => 'LocationTypes',
97 'required' => FALSE,
98 'idNameFields' => array('id', 'name'),
99 'idNameMap' => array(),
100 ),
101 'optionValue' => array(
102 'data' => array(),
103 'name' => 'OptionValue',
104 'scope' => 'OptionValues',
105 'required' => FALSE,
106 'idNameMap' => array(),
107 'idNameFields' => array('value', 'name', 'prefix'),
108 'mappedFields' => array(
109 array('optionGroup', 'option_group_id', 'option_group_name'),
110 ),
111 ),
112 'profileGroup' => array(
113 'data' => array(),
114 'name' => 'ProfileGroup',
115 'scope' => 'ProfileGroups',
116 'required' => FALSE,
117 'idNameFields' => array('id', 'title'),
118 'idNameMap' => array(),
119 ),
120 'profileField' => array(
121 'data' => array(),
122 'name' => 'ProfileField',
123 'scope' => 'ProfileFields',
124 'required' => FALSE,
125 'idNameMap' => array(),
126 'mappedFields' => array(
127 array('profileGroup', 'uf_group_id', 'profile_group_name')
128 ),
129 ),
130 'profileJoin' => array(
131 'data' => array(),
132 'name' => 'ProfileJoin',
133 'scope' => 'ProfileJoins',
134 'required' => FALSE,
135 'idNameMap' => array(),
136 'mappedFields' => array(
137 array('profileGroup', 'uf_group_id', 'profile_group_name')
138 ),
139 ),
140 'mappingGroup' => array(
141 'data' => array(),
142 'name' => 'MappingGroup',
143 'scope' => 'MappingGroups',
144 'required' => FALSE,
145 'idNameFields' => array('id', 'name'),
146 'idNameMap' => array(),
147 'mappedFields' => array(
148 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
149 )
150 ),
151 'mappingField' => array(
152 'data' => array(),
153 'name' => 'MappingField',
154 'scope' => 'MappingFields',
155 'required' => FALSE,
156 'idNameMap' => array(),
157 'mappedFields' => array(
158 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
159 array('locationType', 'location_type_id', 'location_type_name'),
160 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
161 ),
162 ),
163 );
164 }
165
166 /**
167 * Scan local customizations and build an in-memory representation
168 *
169 * @return void
170 */
171 function build() {
172 // fetch the option group / values for
173 // activity type and event_type
174
175 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
176
177 $sql = "
178 SELECT distinct(g.id), g.*
179 FROM civicrm_option_group g
180 WHERE g.name IN $optionGroups
181 ";
182 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
183
184 $sql = "
185 SELECT distinct(g.id), g.*
186 FROM civicrm_option_group g,
187 civicrm_custom_field f,
188 civicrm_custom_group cg
189 WHERE f.option_group_id = g.id
190 AND f.custom_group_id = cg.id
191 AND cg.is_active = 1
192 ";
193 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
194
195 $sql = "
196 SELECT v.*, g.name as prefix
197 FROM civicrm_option_value v,
198 civicrm_option_group g
199 WHERE v.option_group_id = g.id
200 AND g.name IN $optionGroups
201 ";
202
203 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
204
205 $sql = "
206 SELECT distinct(v.id), v.*, g.name as prefix
207 FROM civicrm_option_value v,
208 civicrm_option_group g,
209 civicrm_custom_field f,
210 civicrm_custom_group cg
211 WHERE v.option_group_id = g.id
212 AND f.option_group_id = g.id
213 AND f.custom_group_id = cg.id
214 AND cg.is_active = 1
215 ";
216
217 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
218
219 $sql = "
220 SELECT rt.*
221 FROM civicrm_relationship_type rt
222 WHERE rt.is_active = 1
223 ";
224 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
225
226 $sql = "
227 SELECT lt.*
228 FROM civicrm_location_type lt
229 WHERE lt.is_active = 1
230 ";
231 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
232
233 $sql = "
234 SELECT cg.*
235 FROM civicrm_custom_group cg
236 WHERE cg.is_active = 1
237 ";
238 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
239
240 $sql = "
241 SELECT f.*
242 FROM civicrm_custom_field f,
243 civicrm_custom_group cg
244 WHERE f.custom_group_id = cg.id
245 AND cg.is_active = 1
246 ";
247 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
248
249 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
250
251 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
252
253 $sql = "
254 SELECT *
255 FROM civicrm_uf_join
256 WHERE entity_table IS NULL
257 AND entity_id IS NULL
258 ";
259 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
260
261 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
262
263 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
264 }
265
266 /**
267 * Render the in-memory representation as XML
268 *
269 * @return string XML
270 */
271 function toXML() {
272 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
273 $buffer .= "\n\n<CustomData>\n";
274 foreach (array_keys($this->_xml) as $key) {
275 if (!empty($this->_xml[$key]['data'])) {
276 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
277 foreach ($this->_xml[$key]['data'] as $item) {
278 $buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item);
279 }
280 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
281 }
282 elseif ($this->_xml[$key]['required']) {
283 CRM_Core_Error::fatal("No records in DB for $key");
284 }
285 }
286 $buffer .= "</CustomData>\n";
287 return $buffer;
288 }
289
290 function fetch($groupName, $daoName, $sql = NULL) {
291 $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
292 $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
293
294 $dao = new $daoName();
295 if ($sql) {
296 $dao->query($sql);
297 }
298 else {
299 $dao->find();
300 }
301
302 while ($dao->fetch()) {
303 $this->_xml[$groupName]['data'][] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
304 if ($idNameFields) {
305 // index the id/name fields so that we can translate from FK ids to FK names
306 if (isset($idNameFields[2])) {
307 $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
308 }
309 else {
310 $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
311 }
312 }
313 }
314 }
315
316 /**
317 * Compute any fields of the entity defined by the $mappedFields specification
318 *
319 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
320 * @param CRM_Core_DAO $dao the entity for which we want to prepare mapped fields
321 * @return array new fields
322 */
323 public function computeMappedFields($mappedFields, $dao) {
324 $keyValues = array();
325 if ($mappedFields) {
326 foreach ($mappedFields as $mappedField) {
327 if (isset($dao->{$mappedField[1]})) {
328 if (isset($mappedField[3])) {
329 $label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
330 }
331 else {
332 $label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
333 }
334 $keyValues[$mappedField[2]] = $label;
335 }
336 }
337 }
338 return $keyValues;
339 }
340
341 /**
342 * @param CRM_Core_DAO $object
343 * @param string $objectName business-entity/xml-tag name
344 * @return array
345 */
346 function exportDAO($objectName, $object, $mappedFields) {
347 $dbFields = & $object->fields();
348
349 // Filter the list of keys and values so that we only export interesting stuff
350 $keyValues = array();
351 foreach ($dbFields as $name => $dontCare) {
352 // ignore all ids
353 if ($name == 'id' || substr($name, -3, 3) == '_id') {
354 continue;
355 }
356 if (isset($object->$name) && $object->$name !== NULL) {
357 // hack for extends_entity_column_value
358 if ($name == 'extends_entity_column_value') {
359 if ($object->extends == 'Event' ||
360 $object->extends == 'Activity' ||
361 $object->extends == 'Relationship'
362 ) {
363 if ($object->extends == 'Event') {
364 $key = 'event_type';
365 }
366 elseif ($object->extends == 'Activity') {
367 $key = 'activity_type';
368 }
369 elseif ($object->extends == 'Relationship') {
370 $key = 'relationship_type';
371 }
372 $keyValues['extends_entity_column_value_option_group'] = $key;
373 $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
374 $values = array();
375 foreach ($types as $type) {
376 $values[] = $this->_xml['optionValue']['idNameMap']["$key.{$type}"];
377 }
378 $value = implode(',', $values);
379 $keyValues['extends_entity_column_value_option_value'] = $value;
380 }
381 else {
382 echo "This extension: {$object->extends} is not yet handled";
383 exit();
384 }
385 }
386
387 $value = $object->$name;
388 if ($name == 'field_name') {
389 // hack for profile field_name
390 if (substr($value, 0, 7) == 'custom_') {
391 $cfID = substr($value, 7);
392 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID);
393 $value = "custom.{$tableName}.{$columnName}";
394 }
395 }
396 $keyValues[$name] = $value;
397 }
398 }
399
400 $keyValues += $this->computeMappedFields($mappedFields, $object);
401
402 return $keyValues;
403 }
404
405 /**
406 * @param string $tagName
407 * @param array $keyValues
408 * @param string $additional XML
409 * @return string XML
410 */
411 public function renderKeyValueXML($tagName, $keyValues) {
412 $xml = " <$tagName>";
413 foreach ($keyValues as $k => $v) {
414 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $v));
415 }
416 $xml .= "\n </$tagName>\n";
417 return $xml;
418 }
419
420 /**
421 * @param string $name tag name
422 * @param string $value text
423 * @param string $prefix
424 * @return string XML
425 */
426 function renderTextTag($name, $value, $prefix = '') {
427 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
428 throw new Exception("Malformed tag name: $name");
429 }
430 return $prefix . "<$name>" . htmlentities($value) . "</$name>";
431 }
432 }
433