CRM-12647 - CRM_Utils_Migrate_Export - Use htmlentities
[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 $this->_xml[$groupName]['data'] .= $this->exportDAO($dao,
325 $this->_xml[$groupName]['name'],
326 $this->addMappedXMLFields($add, $dao)
327 );
328 if ($map) {
329 if (isset($map[2])) {
330 $this->_xml[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
331 }
332 else {
333 $this->_xml[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
334 }
335 }
336 }
337 }
338
339 /**
340 * Given a set of field mappings, generate XML for the mapped fields
341 *
342 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
343 * @param CRM_Core_DAO $dao
344 * @return null|string XML
345 */
346 public function addMappedXMLFields($mappedFields, $dao) {
347 $additional = NULL;
348 if ($mappedFields) {
349 foreach ($mappedFields as $mappedField) {
350 if (isset($dao->{$mappedField[1]})) {
351 if (isset($mappedField[3])) {
352 $label = $this->_xml[$mappedField[0]]['map']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
353 }
354 else {
355 $label = $this->_xml[$mappedField[0]]['map'][$dao->{$mappedField[1]}];
356 }
357 $additional .= "\n " . $this->renderTextTag($mappedField[2], $label);
358 }
359 }
360 return $additional;
361 }
362 return $additional;
363 }
364
365 /**
366 * @param CRM_Core_DAO $object
367 * @param string $objectName business-entity/xml-tag name
368 * @param string $additional XML
369 * @return string XML
370 */
371 function exportDAO($object, $objectName, $additional = NULL) {
372 $dbFields = & $object->fields();
373
374 $xml = " <$objectName>";
375 foreach ($dbFields as $name => $dontCare) {
376 // ignore all ids
377 if ($name == 'id' ||
378 substr($name, -3, 3) == '_id'
379 ) {
380 continue;
381 }
382 if (isset($object->$name) &&
383 $object->$name !== NULL
384 ) {
385 // hack for extends_entity_column_value
386 if ($name == 'extends_entity_column_value') {
387 if ($object->extends == 'Event' ||
388 $object->extends == 'Activity' ||
389 $object->extends == 'Relationship'
390 ) {
391 if ($object->extends == 'Event') {
392 $key = 'event_type';
393 }
394 elseif ($object->extends == 'Activity') {
395 $key = 'activity_type';
396 }
397 elseif ($object->extends == 'Relationship') {
398 $key = 'relationship_type';
399 }
400 $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_group', $key);
401 $types = explode(CRM_Core_DAO::VALUE_SEPARATOR,
402 substr($object->$name, 1, -1)
403 );
404 $value = array();
405 foreach ($types as $type) {
406 $values[] = $this->_xml['optionValue']['map']["$key.{$type}"];
407 }
408 $value = implode(',', $values);
409 $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_value', $value);
410 }
411 else {
412 echo "This extension: {$object->extends} is not yet handled";
413 exit();
414 }
415 }
416 if ($name == 'field_name') {
417 $value = $object->$name;
418 // hack for profile field_name
419 if (substr($value, 0, 7) == 'custom_') {
420 $cfID = substr($value, 7);
421 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID);
422 $value = "custom.{$tableName}.{$columnName}";
423 }
424 $xml .= "\n " . $this->renderTextTag($name, $value);
425 }
426 else {
427 $value = str_replace(CRM_Core_DAO::VALUE_SEPARATOR,
428 self::XML_VALUE_SEPARATOR,
429 $object->$name
430 );
431 $xml .= "\n " . $this->renderTextTag($name, $value);
432 }
433 }
434 }
435 if ($additional) {
436 $xml .= $additional;
437 }
438 $xml .= "\n </$objectName>\n";
439 return $xml;
440 }
441
442 /**
443 * @param string $name tag name
444 * @param string $value text
445 * @param string $prefix
446 * @return string XML
447 */
448 function renderTextTag($name, $value, $prefix ='') {
449 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
450 throw new Exception("Malformed tag name: $name");
451 }
452 return $prefix . "<$name>" . htmlentities($value) . "</$name>";
453 }
454 }
455