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