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