Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Migrate_Export { | |
36 | ||
4f652853 TO |
37 | const XML_VALUE_SEPARATOR = ":;:;:;"; |
38 | ||
18315de9 TO |
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 | */ | |
3302658e TO |
53 | protected $_xml; |
54 | ||
5bc392e6 EM |
55 | /** |
56 | * | |
57 | */ | |
3302658e | 58 | function __construct() { |
6a488035 | 59 | $this->_xml = array( |
3302658e | 60 | 'customGroup' => array( |
9593cf03 | 61 | 'data' => array(), |
6a488035 TO |
62 | 'name' => 'CustomGroup', |
63 | 'scope' => 'CustomGroups', | |
64 | 'required' => FALSE, | |
8632d2d7 | 65 | 'idNameFields' => array('id', 'name'), |
9910627b | 66 | 'idNameMap' => array(), |
6a488035 TO |
67 | ), |
68 | 'customField' => array( | |
9593cf03 | 69 | 'data' => array(), |
6a488035 TO |
70 | 'name' => 'CustomField', |
71 | 'scope' => 'CustomFields', | |
72 | 'required' => FALSE, | |
8632d2d7 | 73 | 'idNameFields' => array('id', 'column_name'), |
9910627b | 74 | 'idNameMap' => array(), |
8632d2d7 TO |
75 | 'mappedFields' => array( |
76 | array('optionGroup', 'option_group_id', 'option_group_name'), | |
77 | array('customGroup', 'custom_group_id', 'custom_group_name'), | |
78 | ) | |
6a488035 TO |
79 | ), |
80 | 'optionGroup' => array( | |
9593cf03 | 81 | 'data' => array(), |
6a488035 TO |
82 | 'name' => 'OptionGroup', |
83 | 'scope' => 'OptionGroups', | |
84 | 'required' => FALSE, | |
9910627b | 85 | 'idNameMap' => array(), |
8632d2d7 | 86 | 'idNameFields' => array('id', 'name'), |
6a488035 TO |
87 | ), |
88 | 'relationshipType' => array( | |
9593cf03 | 89 | 'data' => array(), |
6a488035 TO |
90 | 'name' => 'RelationshipType', |
91 | 'scope' => 'RelationshipTypes', | |
92 | 'required' => FALSE, | |
8632d2d7 | 93 | 'idNameFields' => array('id', 'name_a_b'), |
9910627b | 94 | 'idNameMap' => array(), |
6a488035 TO |
95 | ), |
96 | 'locationType' => array( | |
9593cf03 | 97 | 'data' => array(), |
6a488035 TO |
98 | 'name' => 'LocationType', |
99 | 'scope' => 'LocationTypes', | |
100 | 'required' => FALSE, | |
8632d2d7 | 101 | 'idNameFields' => array('id', 'name'), |
9910627b | 102 | 'idNameMap' => array(), |
6a488035 TO |
103 | ), |
104 | 'optionValue' => array( | |
9593cf03 | 105 | 'data' => array(), |
6a488035 TO |
106 | 'name' => 'OptionValue', |
107 | 'scope' => 'OptionValues', | |
108 | 'required' => FALSE, | |
9910627b | 109 | 'idNameMap' => array(), |
8632d2d7 TO |
110 | 'idNameFields' => array('value', 'name', 'prefix'), |
111 | 'mappedFields' => array( | |
112 | array('optionGroup', 'option_group_id', 'option_group_name'), | |
113 | ), | |
6a488035 TO |
114 | ), |
115 | 'profileGroup' => array( | |
9593cf03 | 116 | 'data' => array(), |
6a488035 TO |
117 | 'name' => 'ProfileGroup', |
118 | 'scope' => 'ProfileGroups', | |
119 | 'required' => FALSE, | |
8632d2d7 | 120 | 'idNameFields' => array('id', 'title'), |
9910627b | 121 | 'idNameMap' => array(), |
6a488035 TO |
122 | ), |
123 | 'profileField' => array( | |
9593cf03 | 124 | 'data' => array(), |
6a488035 TO |
125 | 'name' => 'ProfileField', |
126 | 'scope' => 'ProfileFields', | |
127 | 'required' => FALSE, | |
9910627b | 128 | 'idNameMap' => array(), |
8632d2d7 TO |
129 | 'mappedFields' => array( |
130 | array('profileGroup', 'uf_group_id', 'profile_group_name') | |
131 | ), | |
6a488035 TO |
132 | ), |
133 | 'profileJoin' => array( | |
9593cf03 | 134 | 'data' => array(), |
6a488035 TO |
135 | 'name' => 'ProfileJoin', |
136 | 'scope' => 'ProfileJoins', | |
137 | 'required' => FALSE, | |
9910627b | 138 | 'idNameMap' => array(), |
8632d2d7 TO |
139 | 'mappedFields' => array( |
140 | array('profileGroup', 'uf_group_id', 'profile_group_name') | |
141 | ), | |
6a488035 TO |
142 | ), |
143 | 'mappingGroup' => array( | |
9593cf03 | 144 | 'data' => array(), |
6a488035 TO |
145 | 'name' => 'MappingGroup', |
146 | 'scope' => 'MappingGroups', | |
147 | 'required' => FALSE, | |
8632d2d7 | 148 | 'idNameFields' => array('id', 'name'), |
9910627b | 149 | 'idNameMap' => array(), |
8632d2d7 TO |
150 | 'mappedFields' => array( |
151 | array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'), | |
152 | ) | |
6a488035 TO |
153 | ), |
154 | 'mappingField' => array( | |
9593cf03 | 155 | 'data' => array(), |
6a488035 TO |
156 | 'name' => 'MappingField', |
157 | 'scope' => 'MappingFields', | |
158 | 'required' => FALSE, | |
9910627b | 159 | 'idNameMap' => array(), |
8632d2d7 TO |
160 | 'mappedFields' => array( |
161 | array('mappingGroup', 'mapping_id', 'mapping_group_name'), | |
162 | array('locationType', 'location_type_id', 'location_type_name'), | |
163 | array('relationshipType', 'relationship_type_id', 'relationship_type_name'), | |
164 | ), | |
6a488035 TO |
165 | ), |
166 | ); | |
167 | } | |
168 | ||
8575f879 TO |
169 | /** |
170 | * Scan local customizations and build an in-memory representation | |
171 | * | |
172 | * @return void | |
173 | */ | |
174 | function build() { | |
6a488035 TO |
175 | // fetch the option group / values for |
176 | // activity type and event_type | |
177 | ||
178 | $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )"; | |
179 | ||
180 | $sql = " | |
0c52a8f5 TO |
181 | SELECT distinct(g.id), g.* |
182 | FROM civicrm_option_group g | |
183 | WHERE g.name IN $optionGroups | |
184 | "; | |
8632d2d7 | 185 | $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql); |
6a488035 TO |
186 | |
187 | $sql = " | |
0c52a8f5 TO |
188 | SELECT distinct(g.id), g.* |
189 | FROM civicrm_option_group g, | |
190 | civicrm_custom_field f, | |
191 | civicrm_custom_group cg | |
192 | WHERE f.option_group_id = g.id | |
193 | AND f.custom_group_id = cg.id | |
194 | AND cg.is_active = 1 | |
195 | "; | |
8632d2d7 | 196 | $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql); |
6a488035 TO |
197 | |
198 | $sql = " | |
0c52a8f5 TO |
199 | SELECT v.*, g.name as prefix |
200 | FROM civicrm_option_value v, | |
201 | civicrm_option_group g | |
202 | WHERE v.option_group_id = g.id | |
203 | AND g.name IN $optionGroups | |
204 | "; | |
6a488035 | 205 | |
8632d2d7 | 206 | $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql); |
6a488035 TO |
207 | |
208 | $sql = " | |
0c52a8f5 TO |
209 | SELECT distinct(v.id), v.*, g.name as prefix |
210 | FROM civicrm_option_value v, | |
211 | civicrm_option_group g, | |
212 | civicrm_custom_field f, | |
213 | civicrm_custom_group cg | |
214 | WHERE v.option_group_id = g.id | |
215 | AND f.option_group_id = g.id | |
216 | AND f.custom_group_id = cg.id | |
217 | AND cg.is_active = 1 | |
218 | "; | |
6a488035 | 219 | |
8632d2d7 | 220 | $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql); |
6a488035 TO |
221 | |
222 | $sql = " | |
0c52a8f5 TO |
223 | SELECT rt.* |
224 | FROM civicrm_relationship_type rt | |
225 | WHERE rt.is_active = 1 | |
226 | "; | |
8632d2d7 | 227 | $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql); |
6a488035 TO |
228 | |
229 | $sql = " | |
0c52a8f5 TO |
230 | SELECT lt.* |
231 | FROM civicrm_location_type lt | |
232 | WHERE lt.is_active = 1 | |
233 | "; | |
8632d2d7 | 234 | $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql); |
6a488035 TO |
235 | |
236 | $sql = " | |
0c52a8f5 TO |
237 | SELECT cg.* |
238 | FROM civicrm_custom_group cg | |
239 | WHERE cg.is_active = 1 | |
240 | "; | |
8632d2d7 | 241 | $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql); |
6a488035 TO |
242 | |
243 | $sql = " | |
0c52a8f5 TO |
244 | SELECT f.* |
245 | FROM civicrm_custom_field f, | |
246 | civicrm_custom_group cg | |
247 | WHERE f.custom_group_id = cg.id | |
248 | AND cg.is_active = 1 | |
249 | "; | |
8632d2d7 | 250 | $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql); |
6a488035 | 251 | |
8632d2d7 | 252 | $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup'); |
6a488035 | 253 | |
8632d2d7 | 254 | $this->fetch('profileField', 'CRM_Core_DAO_UFField'); |
6a488035 TO |
255 | |
256 | $sql = " | |
0c52a8f5 TO |
257 | SELECT * |
258 | FROM civicrm_uf_join | |
259 | WHERE entity_table IS NULL | |
260 | AND entity_id IS NULL | |
261 | "; | |
8632d2d7 | 262 | $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql); |
6a488035 | 263 | |
8632d2d7 | 264 | $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping'); |
6a488035 | 265 | |
8632d2d7 | 266 | $this->fetch('mappingField', 'CRM_Core_DAO_MappingField'); |
8575f879 | 267 | } |
6a488035 | 268 | |
d18d8cd0 TO |
269 | /** |
270 | * @param array $customGroupIds list of custom groups to export | |
271 | * @return void | |
272 | */ | |
273 | function buildCustomGroups($customGroupIds) { | |
274 | $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric')); | |
275 | if (empty($customGroupIdsSql)) { | |
276 | return; | |
277 | } | |
278 | ||
279 | $sql = " | |
280 | SELECT distinct(g.id), g.* | |
281 | FROM civicrm_option_group g, | |
282 | civicrm_custom_field f, | |
283 | civicrm_custom_group cg | |
284 | WHERE f.option_group_id = g.id | |
285 | AND f.custom_group_id = cg.id | |
286 | AND cg.id in ($customGroupIdsSql) | |
287 | "; | |
288 | $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql); | |
289 | ||
290 | $sql = " | |
291 | SELECT distinct(v.id), v.*, g.name as prefix | |
292 | FROM civicrm_option_value v, | |
293 | civicrm_option_group g, | |
294 | civicrm_custom_field f, | |
295 | civicrm_custom_group cg | |
296 | WHERE v.option_group_id = g.id | |
297 | AND f.option_group_id = g.id | |
298 | AND f.custom_group_id = cg.id | |
299 | AND cg.id in ($customGroupIdsSql) | |
300 | "; | |
301 | ||
302 | $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql); | |
303 | ||
304 | $sql = " | |
305 | SELECT cg.* | |
306 | FROM civicrm_custom_group cg | |
307 | WHERE cg.id in ($customGroupIdsSql) | |
308 | ||
309 | "; | |
310 | $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql); | |
311 | ||
312 | $sql = " | |
313 | SELECT f.* | |
314 | FROM civicrm_custom_field f, | |
315 | civicrm_custom_group cg | |
316 | WHERE f.custom_group_id = cg.id | |
317 | AND cg.id in ($customGroupIdsSql) | |
318 | "; | |
319 | $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql); | |
320 | } | |
321 | ||
a304337b TO |
322 | /** |
323 | * @param array $ufGroupIds list of custom groups to export | |
324 | * @return void | |
325 | */ | |
326 | function buildUFGroups($ufGroupIds) { | |
327 | $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric')); | |
328 | if (empty($ufGroupIdsSql)) { | |
329 | return; | |
330 | } | |
331 | ||
332 | $sql = " | |
333 | SELECT cg.* | |
334 | FROM civicrm_uf_group cg | |
335 | WHERE cg.id IN ($ufGroupIdsSql) | |
336 | ||
337 | "; | |
338 | $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql); | |
339 | ||
340 | $sql = " | |
341 | SELECT f.* | |
342 | FROM civicrm_uf_field f, | |
343 | civicrm_uf_group cg | |
344 | WHERE f.uf_group_id = cg.id | |
345 | AND cg.id IN ($ufGroupIdsSql) | |
346 | "; | |
347 | $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql); | |
348 | ||
349 | $sql = " | |
350 | SELECT * | |
351 | FROM civicrm_uf_join | |
352 | WHERE entity_table IS NULL | |
353 | AND entity_id IS NULL | |
354 | AND uf_group_id IN ($ufGroupIdsSql) | |
355 | "; | |
356 | $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql); | |
357 | } | |
358 | ||
8575f879 TO |
359 | /** |
360 | * Render the in-memory representation as XML | |
361 | * | |
362 | * @return string XML | |
363 | */ | |
364 | function toXML() { | |
6a488035 TO |
365 | $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>'; |
366 | $buffer .= "\n\n<CustomData>\n"; | |
367 | foreach (array_keys($this->_xml) as $key) { | |
368 | if (!empty($this->_xml[$key]['data'])) { | |
9593cf03 TO |
369 | $buffer .= " <{$this->_xml[$key]['scope']}>\n"; |
370 | foreach ($this->_xml[$key]['data'] as $item) { | |
371 | $buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item); | |
372 | } | |
373 | $buffer .= " </{$this->_xml[$key]['scope']}>\n"; | |
6a488035 TO |
374 | } |
375 | elseif ($this->_xml[$key]['required']) { | |
376 | CRM_Core_Error::fatal("No records in DB for $key"); | |
377 | } | |
378 | } | |
379 | $buffer .= "</CustomData>\n"; | |
8575f879 | 380 | return $buffer; |
6a488035 TO |
381 | } |
382 | ||
5ba9f99c TO |
383 | /** |
384 | * Generate an array-tree representation of the exported elements. | |
385 | * | |
386 | * @return array | |
387 | */ | |
388 | function toArray() { | |
389 | $result = array(); | |
390 | foreach (array_keys($this->_xml) as $key) { | |
391 | if (!empty($this->_xml[$key]['data'])) { | |
d18d8cd0 | 392 | $result[ $this->_xml[$key]['name'] ] = array_values($this->_xml[$key]['data']); |
5ba9f99c TO |
393 | } |
394 | } | |
395 | return $result; | |
396 | } | |
397 | ||
5bc392e6 EM |
398 | /** |
399 | * @param $groupName | |
400 | * @param $daoName | |
401 | * @param null $sql | |
402 | */ | |
8632d2d7 | 403 | function fetch($groupName, $daoName, $sql = NULL) { |
880a956b TO |
404 | $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL; |
405 | $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL; | |
8632d2d7 | 406 | |
a0d9d279 | 407 | $dao = new $daoName(); |
6a488035 TO |
408 | if ($sql) { |
409 | $dao->query($sql); | |
410 | } | |
411 | else { | |
412 | $dao->find(); | |
413 | } | |
414 | ||
415 | while ($dao->fetch()) { | |
d18d8cd0 | 416 | $this->_xml[$groupName]['data'][$dao->id] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields); |
880a956b TO |
417 | if ($idNameFields) { |
418 | // index the id/name fields so that we can translate from FK ids to FK names | |
419 | if (isset($idNameFields[2])) { | |
9910627b | 420 | $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]}; |
6a488035 TO |
421 | } |
422 | else { | |
9910627b | 423 | $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]}; |
6a488035 TO |
424 | } |
425 | } | |
426 | } | |
427 | } | |
428 | ||
a2bc8bf7 | 429 | /** |
2b2a2d10 | 430 | * Compute any fields of the entity defined by the $mappedFields specification |
a2bc8bf7 TO |
431 | * |
432 | * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix) | |
2b2a2d10 TO |
433 | * @param CRM_Core_DAO $dao the entity for which we want to prepare mapped fields |
434 | * @return array new fields | |
a2bc8bf7 | 435 | */ |
2b2a2d10 TO |
436 | public function computeMappedFields($mappedFields, $dao) { |
437 | $keyValues = array(); | |
a2bc8bf7 TO |
438 | if ($mappedFields) { |
439 | foreach ($mappedFields as $mappedField) { | |
440 | if (isset($dao->{$mappedField[1]})) { | |
441 | if (isset($mappedField[3])) { | |
9910627b | 442 | $label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}]; |
a2bc8bf7 TO |
443 | } |
444 | else { | |
9910627b | 445 | $label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}]; |
a2bc8bf7 | 446 | } |
2b2a2d10 | 447 | $keyValues[$mappedField[2]] = $label; |
a2bc8bf7 TO |
448 | } |
449 | } | |
a2bc8bf7 | 450 | } |
2b2a2d10 | 451 | return $keyValues; |
a2bc8bf7 TO |
452 | } |
453 | ||
454 | /** | |
a2bc8bf7 | 455 | * @param string $objectName business-entity/xml-tag name |
2a6da8d7 EM |
456 | * @param CRM_Core_DAO $object |
457 | * @param $mappedFields | |
458 | * | |
2b2a2d10 | 459 | * @return array |
a2bc8bf7 | 460 | */ |
2b2a2d10 | 461 | function exportDAO($objectName, $object, $mappedFields) { |
3302658e | 462 | $dbFields = & $object->fields(); |
6a488035 | 463 | |
d6379d54 TO |
464 | // Filter the list of keys and values so that we only export interesting stuff |
465 | $keyValues = array(); | |
6a488035 TO |
466 | foreach ($dbFields as $name => $dontCare) { |
467 | // ignore all ids | |
54fe18bf | 468 | if ($name == 'id' || substr($name, -3, 3) == '_id') { |
6a488035 TO |
469 | continue; |
470 | } | |
54fe18bf | 471 | if (isset($object->$name) && $object->$name !== NULL) { |
6a488035 TO |
472 | // hack for extends_entity_column_value |
473 | if ($name == 'extends_entity_column_value') { | |
e3bc60ab | 474 | if (in_array($object->extends, array('Event', 'Activity', 'Relationship', 'Individual', 'Organization', 'Household', 'Case'))) { |
6a488035 TO |
475 | if ($object->extends == 'Event') { |
476 | $key = 'event_type'; | |
477 | } | |
478 | elseif ($object->extends == 'Activity') { | |
479 | $key = 'activity_type'; | |
480 | } | |
481 | elseif ($object->extends == 'Relationship') { | |
482 | $key = 'relationship_type'; | |
483 | } | |
ccc78496 | 484 | elseif($object->extends == 'Case') { |
485 | $key = 'case_type'; | |
486 | } | |
18e61355 | 487 | $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1)); |
464cb8fd | 488 | $values = array(); |
9d1c4909 RN |
489 | if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) { |
490 | $key = 'contact_type'; | |
491 | $values = $types; | |
492 | } | |
493 | else { | |
494 | foreach ($types as $type) { | |
0ebdbd37 | 495 | if (in_array($key, array('activity_type', 'event_type', 'case_type'))) { |
9d1c4909 RN |
496 | $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name'); |
497 | $ovParams = array('option_group_id' => $ogID, 'value' => $type); | |
498 | CRM_Core_BAO_OptionValue::retrieve($ovParams, $oValue); | |
499 | $values[] = $oValue['name']; | |
500 | } | |
501 | else { | |
502 | $relTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id'); | |
503 | $values[] = $relTypeName; | |
504 | } | |
0f2ea47d | 505 | } |
6a488035 | 506 | } |
9d1c4909 | 507 | $keyValues['extends_entity_column_value_option_group'] = $key; |
6a488035 | 508 | $value = implode(',', $values); |
0f2ea47d | 509 | $object->extends_entity_column_value = $value; |
6a488035 TO |
510 | } |
511 | else { | |
512 | echo "This extension: {$object->extends} is not yet handled"; | |
513 | exit(); | |
514 | } | |
515 | } | |
23c41970 TO |
516 | |
517 | $value = $object->$name; | |
6a488035 | 518 | if ($name == 'field_name') { |
6a488035 TO |
519 | // hack for profile field_name |
520 | if (substr($value, 0, 7) == 'custom_') { | |
521 | $cfID = substr($value, 7); | |
522 | list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID); | |
523 | $value = "custom.{$tableName}.{$columnName}"; | |
524 | } | |
6a488035 | 525 | } |
d6379d54 | 526 | $keyValues[$name] = $value; |
6a488035 TO |
527 | } |
528 | } | |
d6379d54 | 529 | |
2b2a2d10 TO |
530 | $keyValues += $this->computeMappedFields($mappedFields, $object); |
531 | ||
532 | return $keyValues; | |
82a166f0 TO |
533 | } |
534 | ||
535 | /** | |
536 | * @param string $tagName | |
537 | * @param array $keyValues | |
2a6da8d7 EM |
538 | * @throws Exception |
539 | * @internal param string $additional XML | |
82a166f0 TO |
540 | * @return string XML |
541 | */ | |
2b2a2d10 | 542 | public function renderKeyValueXML($tagName, $keyValues) { |
82a166f0 | 543 | $xml = " <$tagName>"; |
d6379d54 | 544 | foreach ($keyValues as $k => $v) { |
23c41970 | 545 | $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $v)); |
d6379d54 | 546 | } |
82a166f0 | 547 | $xml .= "\n </$tagName>\n"; |
6a488035 TO |
548 | return $xml; |
549 | } | |
fc44bb03 TO |
550 | |
551 | /** | |
552 | * @param string $name tag name | |
553 | * @param string $value text | |
554 | * @param string $prefix | |
77b97be7 EM |
555 | * |
556 | * @throws Exception | |
fc44bb03 TO |
557 | * @return string XML |
558 | */ | |
8632d2d7 | 559 | function renderTextTag($name, $value, $prefix = '') { |
fc44bb03 TO |
560 | if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) { |
561 | throw new Exception("Malformed tag name: $name"); | |
562 | } | |
563 | return $prefix . "<$name>" . htmlentities($value) . "</$name>"; | |
564 | } | |
6a488035 TO |
565 | } |
566 |