Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { | |
18 | ||
19 | /** | |
fe482240 | 20 | * Class constructor. |
6a488035 | 21 | */ |
00be9182 | 22 | public function __construct() { |
6a488035 TO |
23 | parent::__construct(); |
24 | } | |
25 | ||
26 | /** | |
fe482240 | 27 | * Fetch object based on array of properties. |
6a488035 | 28 | * |
6a0b768e TO |
29 | * @param array $params |
30 | * (reference ) an assoc array of name/value pairs. | |
31 | * @param array $defaults | |
32 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 33 | * |
a6c01b45 CW |
34 | * @return object |
35 | * CRM_Core_DAO_Mapping object on success, otherwise NULL | |
6a488035 | 36 | */ |
00be9182 | 37 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
38 | $mapping = new CRM_Core_DAO_Mapping(); |
39 | $mapping->copyValues($params); | |
40 | if ($mapping->find(TRUE)) { | |
41 | CRM_Core_DAO::storeValues($mapping, $defaults); | |
42 | return $mapping; | |
43 | } | |
44 | return NULL; | |
45 | } | |
46 | ||
47 | /** | |
fe482240 | 48 | * Delete the mapping. |
6a488035 | 49 | * |
6a0b768e TO |
50 | * @param int $id |
51 | * Mapping id. | |
6a488035 | 52 | * |
7d1e1dd5 | 53 | * @return bool |
6a488035 | 54 | */ |
00be9182 | 55 | public static function del($id) { |
6a488035 TO |
56 | // delete from mapping_field table |
57 | $mappingField = new CRM_Core_DAO_MappingField(); | |
58 | $mappingField->mapping_id = $id; | |
39a3d35f | 59 | $mappingField->delete(); |
6a488035 TO |
60 | |
61 | // delete from mapping table | |
62 | $mapping = new CRM_Core_DAO_Mapping(); | |
63 | $mapping->id = $id; | |
ddfe3ced SB |
64 | if ($mapping->find(TRUE)) { |
65 | $result = $mapping->delete(); | |
ddfe3ced SB |
66 | return $result; |
67 | } | |
68 | return FALSE; | |
6a488035 TO |
69 | } |
70 | ||
71 | /** | |
fe482240 | 72 | * Takes an associative array and creates a contact object. |
6a488035 TO |
73 | * |
74 | * The function extract all the params it needs to initialize the create a | |
75 | * contact object. the params array could contain additional unused name/value | |
76 | * pairs | |
77 | * | |
6a0b768e TO |
78 | * @param array $params |
79 | * An array of name/value pairs. | |
6a488035 | 80 | * |
a6c01b45 CW |
81 | * @return object |
82 | * CRM_Core_DAO_Mapper object on success, otherwise NULL | |
6a488035 | 83 | */ |
00be9182 | 84 | public static function add($params) { |
6a488035 TO |
85 | $mapping = new CRM_Core_DAO_Mapping(); |
86 | $mapping->copyValues($params); | |
87 | $mapping->save(); | |
88 | ||
89 | return $mapping; | |
90 | } | |
91 | ||
92 | /** | |
4f55a797 | 93 | * Get the list of mappings for a select or select2 element. |
6a488035 | 94 | * |
f8df7165 | 95 | * @param string $mappingType |
96 | * Mapping type name. | |
4f55a797 CW |
97 | * @param bool $select2 |
98 | * Format for select2 | |
77b97be7 | 99 | * |
a6c01b45 | 100 | * @return array |
f8df7165 | 101 | * Array of mapping names, keyed by id. |
6a488035 | 102 | */ |
4f55a797 | 103 | public static function getMappings($mappingType, $select2 = FALSE) { |
be2fb01f | 104 | $result = civicrm_api3('Mapping', 'get', [ |
10b3287e | 105 | 'mapping_type_id' => $mappingType, |
4f55a797 | 106 | 'return' => ['name', 'description'], |
be2fb01f | 107 | 'options' => [ |
10b3287e BS |
108 | 'sort' => 'name', |
109 | 'limit' => 0, | |
be2fb01f CW |
110 | ], |
111 | ]); | |
112 | $mapping = []; | |
6a488035 | 113 | |
4f55a797 CW |
114 | foreach ($result['values'] as $id => $value) { |
115 | if ($select2) { | |
116 | $item = ['id' => $id, 'text' => $value['name']]; | |
117 | if (!empty($value['description'])) { | |
118 | $item['description'] = $value['description']; | |
119 | } | |
120 | $mapping[] = $item; | |
121 | } | |
122 | else { | |
123 | $mapping[$id] = $value['name']; | |
124 | } | |
6a488035 | 125 | } |
6a488035 TO |
126 | return $mapping; |
127 | } | |
128 | ||
412585fb | 129 | /** |
130 | * Get the mappings array, creating if it does not exist. | |
131 | * | |
132 | * @param string $mappingType | |
133 | * Mapping type name. | |
134 | * | |
135 | * @return array | |
136 | * Array of mapping names, keyed by id. | |
137 | * | |
138 | * @throws \CiviCRM_API3_Exception | |
139 | */ | |
140 | public static function getCreateMappingValues($mappingType) { | |
141 | try { | |
142 | return CRM_Core_BAO_Mapping::getMappings($mappingType); | |
143 | } | |
144 | catch (CiviCRM_API3_Exception $e) { | |
145 | // Having a valid mapping_type_id is now enforced. However, rather than error let's | |
146 | // add it. This is required for Multi value which could be done by upgrade script, but | |
147 | // it feels like there could be other instances so this is safer. | |
148 | $errorParams = $e->getExtraParams(); | |
149 | if ($errorParams['error_field'] === 'mapping_type_id') { | |
be2fb01f CW |
150 | $mappingValues = civicrm_api3('Mapping', 'getoptions', ['field' => 'mapping_type_id']); |
151 | civicrm_api3('OptionValue', 'create', [ | |
412585fb | 152 | 'option_group_id' => 'mapping_type', |
153 | 'label' => $mappingType, | |
154 | 'value' => max(array_keys($mappingValues['values'])) + 1, | |
155 | 'is_reserved' => 1, | |
be2fb01f | 156 | ]); |
412585fb | 157 | return CRM_Core_BAO_Mapping::getMappings($mappingType); |
158 | } | |
159 | throw $e; | |
160 | } | |
161 | } | |
162 | ||
6a488035 | 163 | /** |
fe482240 | 164 | * Get the mapping fields. |
6a488035 | 165 | * |
6a0b768e TO |
166 | * @param int $mappingId |
167 | * Mapping id. | |
77b97be7 | 168 | * |
6ebecfea | 169 | * @param bool $addPrimary |
170 | * Add the key 'Primary' when the field is a location field AND there is | |
171 | * no location type (meaning Primary)? | |
172 | * | |
a6c01b45 CW |
173 | * @return array |
174 | * array of mapping fields | |
6a488035 | 175 | */ |
6ebecfea | 176 | public static function getMappingFields($mappingId, $addPrimary = FALSE) { |
6a488035 TO |
177 | //mapping is to be loaded from database |
178 | $mapping = new CRM_Core_DAO_MappingField(); | |
179 | $mapping->mapping_id = $mappingId; | |
180 | $mapping->orderBy('column_number'); | |
181 | $mapping->find(); | |
182 | ||
be2fb01f CW |
183 | $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = []; |
184 | $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = []; | |
6a488035 TO |
185 | while ($mapping->fetch()) { |
186 | $mappingName[$mapping->grouping][$mapping->column_number] = $mapping->name; | |
187 | $mappingContactType[$mapping->grouping][$mapping->column_number] = $mapping->contact_type; | |
188 | ||
189 | if (!empty($mapping->location_type_id)) { | |
190 | $mappingLocation[$mapping->grouping][$mapping->column_number] = $mapping->location_type_id; | |
191 | } | |
6ebecfea | 192 | elseif ($addPrimary) { |
193 | if (CRM_Contact_BAO_Contact::isFieldHasLocationType($mapping->name)) { | |
194 | $mappingLocation[$mapping->grouping][$mapping->column_number] = ts('Primary'); | |
195 | } | |
196 | else { | |
197 | $mappingLocation[$mapping->grouping][$mapping->column_number] = NULL; | |
198 | } | |
199 | } | |
6a488035 TO |
200 | |
201 | if (!empty($mapping->phone_type_id)) { | |
202 | $mappingPhoneType[$mapping->grouping][$mapping->column_number] = $mapping->phone_type_id; | |
203 | } | |
204 | ||
205 | // get IM service provider type id from mapping fields | |
206 | if (!empty($mapping->im_provider_id)) { | |
207 | $mappingImProvider[$mapping->grouping][$mapping->column_number] = $mapping->im_provider_id; | |
208 | } | |
209 | ||
210 | if (!empty($mapping->website_type_id)) { | |
211 | $mappingWebsiteType[$mapping->grouping][$mapping->column_number] = $mapping->website_type_id; | |
212 | } | |
213 | ||
214 | if (!empty($mapping->relationship_type_id)) { | |
215 | $mappingRelation[$mapping->grouping][$mapping->column_number] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}"; | |
216 | } | |
217 | ||
218 | if (!empty($mapping->operator)) { | |
219 | $mappingOperator[$mapping->grouping][$mapping->column_number] = $mapping->operator; | |
220 | } | |
221 | ||
222 | if (!empty($mapping->value)) { | |
223 | $mappingValue[$mapping->grouping][$mapping->column_number] = $mapping->value; | |
224 | } | |
225 | } | |
226 | ||
be2fb01f | 227 | return [ |
353ffa53 TO |
228 | $mappingName, |
229 | $mappingContactType, | |
230 | $mappingLocation, | |
231 | $mappingPhoneType, | |
232 | $mappingImProvider, | |
233 | $mappingRelation, | |
234 | $mappingOperator, | |
235 | $mappingValue, | |
236 | $mappingWebsiteType, | |
be2fb01f | 237 | ]; |
6a488035 TO |
238 | } |
239 | ||
8bfb13b9 | 240 | /** |
241 | * Get un-indexed array of the field values for the given mapping id. | |
242 | * | |
243 | * For example if passing a mapping ID & name the returned array would look like | |
244 | * ['First field name', 'second field name'] | |
245 | * | |
246 | * @param int $mappingID | |
247 | * @param string $fieldName | |
248 | * | |
249 | * @return array | |
250 | * @throws \CiviCRM_API3_Exception | |
251 | */ | |
252 | public static function getMappingFieldValues($mappingID, $fieldName) { | |
253 | return array_merge(CRM_Utils_Array::collect($fieldName, civicrm_api3('MappingField', 'get', ['mapping_id' => $mappingID, 'return' => $fieldName])['values'])); | |
254 | } | |
255 | ||
6a488035 | 256 | /** |
fe482240 | 257 | * Check Duplicate Mapping Name. |
6a488035 | 258 | * |
5a4f6742 CW |
259 | * @param string $nameField |
260 | * mapping Name. | |
261 | * @param string $mapTypeId | |
262 | * mapping Type. | |
6a488035 | 263 | * |
7d1e1dd5 | 264 | * @return bool |
6a488035 | 265 | */ |
00be9182 | 266 | public static function checkMapping($nameField, $mapTypeId) { |
6a488035 TO |
267 | $mapping = new CRM_Core_DAO_Mapping(); |
268 | $mapping->name = $nameField; | |
269 | $mapping->mapping_type_id = $mapTypeId; | |
c490a46a | 270 | return (bool) $mapping->find(TRUE); |
6a488035 TO |
271 | } |
272 | ||
273 | /** | |
2b4bc760 | 274 | * Function returns associated array of elements, that will be passed for search. |
6a488035 | 275 | * |
6a0b768e TO |
276 | * @param int $smartGroupId |
277 | * Smart group id. | |
6a488035 | 278 | * |
a6c01b45 CW |
279 | * @return array |
280 | * associated array of elements | |
6a488035 | 281 | */ |
00be9182 | 282 | public static function getFormattedFields($smartGroupId) { |
be2fb01f | 283 | $returnFields = []; |
6a488035 TO |
284 | |
285 | //get the fields from mapping table | |
286 | $dao = new CRM_Core_DAO_MappingField(); | |
287 | $dao->mapping_id = $smartGroupId; | |
288 | $dao->find(); | |
289 | while ($dao->fetch()) { | |
290 | $fldName = $dao->name; | |
291 | if ($dao->location_type_id) { | |
292 | $fldName .= "-{$dao->location_type_id}"; | |
293 | } | |
294 | if ($dao->phone_type) { | |
295 | $fldName .= "-{$dao->phone_type}"; | |
296 | } | |
297 | $returnFields[$fldName]['value'] = $dao->value; | |
298 | $returnFields[$fldName]['op'] = $dao->operator; | |
299 | $returnFields[$fldName]['grouping'] = $dao->grouping; | |
300 | } | |
301 | return $returnFields; | |
302 | } | |
303 | ||
304 | /** | |
fe482240 | 305 | * Build the mapping form. |
6a488035 | 306 | * |
c490a46a | 307 | * @param CRM_Core_Form $form |
6a0b768e | 308 | * @param string $mappingType |
35801459 | 309 | * (Export/Search Builder). (Import apparently used to use this but does no longer). |
c490a46a CW |
310 | * @param int $mappingId |
311 | * @param int $columnNo | |
6a0b768e TO |
312 | * @param int $blockCount |
313 | * (no of blocks shown). | |
041ecc95 | 314 | * @param int $exportMode |
6a488035 | 315 | */ |
35801459 | 316 | public static function buildMappingForm(&$form, $mappingType, $mappingId, $columnNo, $blockCount, $exportMode = NULL) { |
317 | ||
be2fb01f CW |
318 | $hasLocationTypes = []; |
319 | $hasRelationTypes = []; | |
6a488035 TO |
320 | |
321 | //get the saved mapping details | |
322 | ||
323 | if ($mappingType == 'Export') { | |
be2fb01f | 324 | $columnCount = ['1' => $columnNo]; |
6a488035 TO |
325 | $form->applyFilter('saveMappingName', 'trim'); |
326 | ||
327 | //to save the current mappings | |
328 | if (!isset($mappingId)) { | |
329 | $saveDetailsName = ts('Save this field mapping'); | |
330 | $form->add('text', 'saveMappingName', ts('Name')); | |
331 | $form->add('text', 'saveMappingDesc', ts('Description')); | |
332 | } | |
333 | else { | |
334 | $form->assign('loadedMapping', $mappingId); | |
335 | ||
be2fb01f CW |
336 | $params = ['id' => $mappingId]; |
337 | $temp = []; | |
6a488035 TO |
338 | $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); |
339 | ||
340 | $form->assign('savedName', $mappingDetails->name); | |
341 | ||
342 | $form->add('hidden', 'mappingId', $mappingId); | |
343 | ||
344 | $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); | |
345 | $saveDetailsName = ts('Save as a new field mapping'); | |
346 | $form->add('text', 'saveMappingName', ts('Name')); | |
347 | $form->add('text', 'saveMappingDesc', ts('Description')); | |
348 | } | |
349 | ||
be2fb01f CW |
350 | $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]); |
351 | $form->addFormRule(['CRM_Export_Form_Map', 'formRule'], $form->get('mappingTypeId')); | |
6a488035 TO |
352 | } |
353 | elseif ($mappingType == 'Search Builder') { | |
35801459 | 354 | $columnCount = $columnNo; |
6a488035 | 355 | $form->addElement('submit', 'addBlock', ts('Also include contacts where'), |
be2fb01f | 356 | ['class' => 'submit-link'] |
6a488035 TO |
357 | ); |
358 | } | |
359 | ||
ea762d46 | 360 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); |
3a95b2ee | 361 | $fields = self::getBasicFields($mappingType); |
6a488035 | 362 | |
d509ee87 | 363 | // Unset groups, tags, notes for component export |
6a488035 | 364 | if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) { |
d509ee87 CW |
365 | foreach (array_keys($fields) as $type) { |
366 | CRM_Utils_Array::remove($fields[$type], 'groups', 'tags', 'notes'); | |
6a488035 TO |
367 | } |
368 | } | |
369 | ||
370 | if ($mappingType == 'Search Builder') { | |
ea762d46 | 371 | // Build the common contact fields array. |
be2fb01f | 372 | $fields['Contact'] = []; |
ea762d46 | 373 | foreach ($fields[$contactTypes[0]] as $key => $value) { |
374 | // If a field exists across all contact types, move it to the "Contact" selector | |
375 | $ubiquitious = TRUE; | |
376 | foreach ($contactTypes as $type) { | |
377 | if (!isset($fields[$type][$key])) { | |
378 | $ubiquitious = FALSE; | |
379 | } | |
380 | } | |
381 | if ($ubiquitious) { | |
6a488035 | 382 | $fields['Contact'][$key] = $value; |
ea762d46 | 383 | foreach ($contactTypes as $type) { |
384 | unset($fields[$type][$key]); | |
385 | } | |
6a488035 TO |
386 | } |
387 | } | |
388 | if (array_key_exists('note', $fields['Contact'])) { | |
389 | $noteTitle = $fields['Contact']['note']['title']; | |
390 | $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); | |
be2fb01f CW |
391 | $fields['Contact']['note_body'] = ['title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body']; |
392 | $fields['Contact']['note_subject'] = [ | |
353ffa53 | 393 | 'title' => $noteTitle . ': ' . ts('Subject Only'), |
7c550ca0 | 394 | 'name' => 'note_subject', |
be2fb01f | 395 | ]; |
6a488035 TO |
396 | } |
397 | } | |
398 | ||
d509ee87 | 399 | // add component fields |
573da9a3 | 400 | $compArray = self::addComponentFields($fields, $mappingType, $exportMode); |
6a488035 | 401 | |
6a488035 TO |
402 | foreach ($fields as $key => $value) { |
403 | ||
404 | foreach ($value as $key1 => $value1) { | |
405 | //CRM-2676, replacing the conflict for same custom field name from different custom group. | |
406 | $customGroupName = self::getCustomGroupName($key1); | |
407 | ||
408 | if ($customGroupName) { | |
409 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; | |
410 | } | |
411 | else { | |
412 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; | |
413 | } | |
414 | if (isset($value1['hasLocationType'])) { | |
415 | $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; | |
416 | } | |
417 | ||
418 | if (isset($value1['hasRelationType'])) { | |
419 | $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; | |
420 | unset($relatedMapperFields[$key][$key1]); | |
421 | } | |
422 | } | |
423 | ||
ea762d46 | 424 | if (isset($relatedMapperFields[$key]['related'])) { |
6a488035 TO |
425 | unset($relatedMapperFields[$key]['related']); |
426 | } | |
427 | } | |
428 | ||
b2b0530a | 429 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
430 | |
431 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
432 | ||
c490a46a CW |
433 | // FIXME: dirty hack to make the default option show up first. This |
434 | // avoids a mozilla browser bug with defaults on dynamically constructed | |
435 | // selector widgets. | |
6a488035 TO |
436 | if ($defaultLocationType) { |
437 | $defaultLocation = $locationTypes[$defaultLocationType->id]; | |
438 | unset($locationTypes[$defaultLocationType->id]); | |
be2fb01f | 439 | $locationTypes = [$defaultLocationType->id => $defaultLocation] + $locationTypes; |
6a488035 TO |
440 | } |
441 | ||
be2fb01f | 442 | $locationTypes = [' ' => ts('Primary')] + $locationTypes; |
6a488035 TO |
443 | |
444 | // since we need a hierarchical list to display contact types & subtypes, | |
445 | // this is what we going to display in first selector | |
ea762d46 | 446 | $contactTypeSelect = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); |
6a488035 | 447 | if ($mappingType == 'Search Builder') { |
ea762d46 | 448 | $contactTypeSelect = ['Contact' => ts('Contacts')] + $contactTypeSelect; |
6a488035 TO |
449 | } |
450 | ||
ea762d46 | 451 | $sel1 = ['' => ts('- select record type -')] + $contactTypeSelect + $compArray; |
6a488035 TO |
452 | |
453 | foreach ($sel1 as $key => $sel) { | |
454 | if ($key) { | |
b44e3f84 | 455 | // sort everything BUT the contactType which is sorted separately by |
82a06bfc | 456 | // an initial commit of CRM-13278 (check ksort above) |
ea762d46 | 457 | if (!in_array($key, $contactTypes)) { |
82a06bfc DL |
458 | asort($mapperFields[$key]); |
459 | } | |
be2fb01f | 460 | $sel2[$key] = ['' => ts('- select field -')] + $mapperFields[$key]; |
6a488035 TO |
461 | } |
462 | } | |
463 | ||
353ffa53 TO |
464 | $sel3[''] = NULL; |
465 | $sel5[''] = NULL; | |
466 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); | |
e7e657f0 | 467 | $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); |
6a488035 TO |
468 | asort($phoneTypes); |
469 | ||
470 | foreach ($sel1 as $k => $sel) { | |
471 | if ($k) { | |
472 | foreach ($locationTypes as $key => $value) { | |
473 | if (trim($key) != '') { | |
474 | $sel4[$k]['phone'][$key] = &$phoneTypes; | |
475 | $sel4[$k]['im'][$key] = &$imProviders; | |
476 | } | |
477 | } | |
478 | } | |
479 | } | |
480 | ||
481 | foreach ($sel1 as $k => $sel) { | |
482 | if ($k) { | |
483 | foreach ($mapperFields[$k] as $key => $value) { | |
484 | if (isset($hasLocationTypes[$k][$key])) { | |
485 | $sel3[$k][$key] = $locationTypes; | |
486 | } | |
487 | else { | |
488 | $sel3[$key] = NULL; | |
489 | } | |
490 | } | |
491 | } | |
492 | } | |
493 | ||
c490a46a | 494 | // Array for core fields and relationship custom data |
6a488035 TO |
495 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); |
496 | ||
497 | if ($mappingType == 'Export') { | |
498 | foreach ($sel1 as $k => $sel) { | |
499 | if ($k) { | |
500 | foreach ($mapperFields[$k] as $field => $dontCare) { | |
501 | if (isset($hasRelationTypes[$k][$field])) { | |
502 | list($id, $first, $second) = explode('_', $field); | |
503 | // FIX ME: For now let's not expose custom data related to relationship | |
be2fb01f | 504 | $relationshipCustomFields = []; |
6a488035 | 505 | //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id ); |
33421d01 | 506 | //asort($relationshipCustomFields); |
6a488035 | 507 | |
be2fb01f | 508 | $relatedFields = []; |
6a488035 TO |
509 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); |
510 | $relationshipType->id = $id; | |
511 | if ($relationshipType->find(TRUE)) { | |
512 | $direction = "contact_sub_type_$second"; | |
d631cdc8 | 513 | $target_type = 'contact_type_' . $second; |
6a488035 | 514 | if (isset($relationshipType->$direction)) { |
353ffa53 | 515 | $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$direction], (array) $relationshipCustomFields); |
6a488035 | 516 | } |
5dcb9417 | 517 | elseif (isset($relationshipType->$target_type)) { |
353ffa53 | 518 | $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$target_type], (array) $relationshipCustomFields); |
6a488035 | 519 | } |
606f3c49 EL |
520 | //CRM-20672 If contact target type not set e.g. "All Contacts" relationship - present user with all field options and let them determine what they expect to work |
521 | else { | |
86d079a2 EL |
522 | $types = CRM_Contact_BAO_ContactType::basicTypes(FALSE); |
523 | foreach ($types as $contactType => $label) { | |
606f3c49 EL |
524 | $relatedFields = array_merge($relatedFields, (array) $relatedMapperFields[$label]); |
525 | } | |
526 | $relatedFields = array_merge($relatedFields, (array) $relationshipCustomFields); | |
527 | } | |
6a488035 | 528 | } |
6a488035 TO |
529 | asort($relatedFields); |
530 | $sel5[$k][$field] = $relatedFields; | |
531 | } | |
532 | } | |
533 | } | |
534 | } | |
535 | ||
536 | //Location Type for relationship fields | |
6a488035 TO |
537 | foreach ($sel5 as $k => $v) { |
538 | if ($v) { | |
539 | foreach ($v as $rel => $fields) { | |
540 | foreach ($fields as $field => $fieldLabel) { | |
541 | if (isset($hasLocationTypes[$k][$field])) { | |
542 | $sel6[$k][$rel][$field] = $locationTypes; | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | } | |
548 | ||
549 | //PhoneTypes for relationship fields | |
550 | $sel7[''] = NULL; | |
551 | foreach ($sel6 as $k => $rel) { | |
552 | if ($k) { | |
553 | foreach ($rel as $phonekey => $phonevalue) { | |
554 | foreach ($locationTypes as $locType => $loc) { | |
555 | if (trim($locType) != '') { | |
556 | $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes; | |
557 | $sel7[$k][$phonekey]['im'][$locType] = &$imProviders; | |
558 | } | |
559 | } | |
560 | } | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | //special fields that have location, hack for primary location | |
be2fb01f | 566 | $specialFields = [ |
353ffa53 TO |
567 | 'street_address', |
568 | 'supplemental_address_1', | |
569 | 'supplemental_address_2', | |
207f62c6 | 570 | 'supplemental_address_3', |
353ffa53 TO |
571 | 'city', |
572 | 'postal_code', | |
573 | 'postal_code_suffix', | |
574 | 'geo_code_1', | |
575 | 'geo_code_2', | |
576 | 'state_province', | |
577 | 'country', | |
578 | 'phone', | |
579 | 'email', | |
580 | 'im', | |
be2fb01f | 581 | ]; |
6a488035 TO |
582 | |
583 | if (isset($mappingId)) { | |
6a488035 TO |
584 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, |
585 | $mappingRelation, $mappingOperator, $mappingValue | |
353ffa53 | 586 | ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); |
6a488035 TO |
587 | |
588 | $blkCnt = count($mappingName); | |
589 | if ($blkCnt >= $blockCount) { | |
590 | $blockCount = $blkCnt + 1; | |
591 | } | |
592 | for ($x = 1; $x < $blockCount; $x++) { | |
593 | if (isset($mappingName[$x])) { | |
594 | $colCnt = count($mappingName[$x]); | |
595 | if ($colCnt >= $columnCount[$x]) { | |
596 | $columnCount[$x] = $colCnt; | |
597 | } | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | $form->_blockCount = $blockCount; | |
603 | $form->_columnCount = $columnCount; | |
604 | ||
605 | $form->set('blockCount', $form->_blockCount); | |
606 | $form->set('columnCount', $form->_columnCount); | |
607 | ||
be2fb01f | 608 | $defaults = $noneArray = $nullArray = []; |
6a488035 | 609 | |
6a488035 TO |
610 | for ($x = 1; $x < $blockCount; $x++) { |
611 | ||
612 | for ($i = 0; $i < $columnCount[$x]; $i++) { | |
613 | ||
be2fb01f | 614 | $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL); |
6a488035 TO |
615 | $jsSet = FALSE; |
616 | ||
617 | if (isset($mappingId)) { | |
5c322e13 | 618 | list($mappingName, $defaults, $noneArray, $jsSet) = self::loadSavedMapping($mappingLocation, $x, $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, $specialFields, $mappingPhoneType, $defaults, $noneArray, $mappingImProvider, $mappingOperator, $mappingValue); |
6a488035 TO |
619 | } |
620 | //Fix for Search Builder | |
621 | if ($mappingType == 'Export') { | |
622 | $j = 7; | |
623 | } | |
624 | else { | |
625 | $j = 4; | |
626 | } | |
627 | ||
628 | $formValues = $form->exportValues(); | |
629 | if (!$jsSet) { | |
630 | if (empty($formValues)) { | |
631 | // Incremented length for third select box(relationship type) | |
632 | for ($k = 1; $k < $j; $k++) { | |
be2fb01f | 633 | $noneArray[] = [$x, $i, $k]; |
6a488035 TO |
634 | } |
635 | } | |
636 | else { | |
637 | if (!empty($formValues['mapper'][$x])) { | |
638 | foreach ($formValues['mapper'][$x] as $value) { | |
639 | for ($k = 1; $k < $j; $k++) { | |
640 | if (!isset($formValues['mapper'][$x][$i][$k]) || | |
641 | (!$formValues['mapper'][$x][$i][$k]) | |
642 | ) { | |
be2fb01f | 643 | $noneArray[] = [$x, $i, $k]; |
6a488035 TO |
644 | } |
645 | else { | |
be2fb01f | 646 | $nullArray[] = [$x, $i, $k]; |
6a488035 TO |
647 | } |
648 | } | |
649 | } | |
650 | } | |
651 | else { | |
652 | for ($k = 1; $k < $j; $k++) { | |
be2fb01f | 653 | $noneArray[] = [$x, $i, $k]; |
6a488035 TO |
654 | } |
655 | } | |
656 | } | |
657 | } | |
658 | //Fix for Search Builder | |
659 | if ($mappingType == 'Export') { | |
15aee3ff | 660 | if (!isset($mappingId) || $i >= count(reset($mappingName))) { |
6a488035 TO |
661 | if (isset($formValues['mapper']) && |
662 | isset($formValues['mapper'][$x][$i][1]) && | |
663 | array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes) | |
664 | ) { | |
be2fb01f | 665 | $sel->setOptions([$sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4]); |
6a488035 TO |
666 | } |
667 | else { | |
be2fb01f | 668 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7]); |
6a488035 TO |
669 | } |
670 | } | |
671 | else { | |
be2fb01f | 672 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7]); |
6a488035 TO |
673 | } |
674 | } | |
675 | else { | |
be2fb01f | 676 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); |
6a488035 TO |
677 | } |
678 | ||
679 | if ($mappingType == 'Search Builder') { | |
680 | //CRM -2292, restricted array set | |
be2fb01f | 681 | $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues::getSearchBuilderOperators(); |
6a488035 TO |
682 | |
683 | $form->add('select', "operator[$x][$i]", '', $operatorArray); | |
684 | $form->add('text', "value[$x][$i]", ''); | |
685 | } | |
686 | } | |
687 | //end of columnCnt for | |
688 | if ($mappingType == 'Search Builder') { | |
689 | $title = ts('Another search field'); | |
690 | } | |
691 | else { | |
692 | $title = ts('Select more fields'); | |
693 | } | |
694 | ||
be2fb01f | 695 | $form->addElement('submit', "addMore[$x]", $title, ['class' => 'submit-link']); |
6a488035 TO |
696 | } |
697 | //end of block for | |
698 | ||
699 | $js = "<script type='text/javascript'>\n"; | |
35801459 | 700 | $formName = "document." . (($mappingType == 'Export') ? 'Map' : 'Builder'); |
6a488035 | 701 | if (!empty($nullArray)) { |
353ffa53 | 702 | $js .= "var nullArray = ["; |
be2fb01f CW |
703 | $elements = []; |
704 | $seen = []; | |
6a488035 TO |
705 | foreach ($nullArray as $element) { |
706 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
707 | if (!isset($seen[$key])) { | |
708 | $elements[] = "[$key]"; | |
709 | $seen[$key] = 1; | |
710 | } | |
711 | } | |
712 | $js .= implode(', ', $elements); | |
713 | $js .= "]"; | |
714 | $js .= " | |
22e263ad | 715 | for (var i=0;i<nullArray.length;i++) { |
6a488035 TO |
716 | if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) { |
717 | {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = ''; | |
718 | } | |
719 | } | |
720 | "; | |
721 | } | |
722 | if (!empty($noneArray)) { | |
353ffa53 | 723 | $js .= "var noneArray = ["; |
be2fb01f CW |
724 | $elements = []; |
725 | $seen = []; | |
6a488035 TO |
726 | foreach ($noneArray as $element) { |
727 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
728 | if (!isset($seen[$key])) { | |
729 | $elements[] = "[$key]"; | |
730 | $seen[$key] = 1; | |
731 | } | |
732 | } | |
733 | $js .= implode(', ', $elements); | |
734 | $js .= "]"; | |
735 | $js .= " | |
22e263ad | 736 | for (var i=0;i<noneArray.length;i++) { |
6a488035 TO |
737 | if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) { |
738 | {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none'; | |
739 | } | |
740 | } | |
741 | "; | |
742 | } | |
743 | $js .= "</script>\n"; | |
744 | ||
745 | $form->assign('initHideBoxes', $js); | |
746 | $form->assign('columnCount', $columnCount); | |
747 | $form->assign('blockCount', $blockCount); | |
748 | $form->setDefaults($defaults); | |
749 | ||
750 | $form->setDefaultAction('refresh'); | |
751 | } | |
752 | ||
3a95b2ee CW |
753 | /** |
754 | * @param string $mappingType | |
755 | * @return array | |
756 | */ | |
757 | public static function getBasicFields($mappingType) { | |
224b94cc | 758 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); |
3a95b2ee CW |
759 | $fields = []; |
760 | foreach ($contactTypes as $contactType) { | |
761 | if ($mappingType == 'Search Builder') { | |
762 | // Get multiple custom group fields in this context | |
763 | $contactFields = CRM_Contact_BAO_Contact::exportableFields($contactType, FALSE, FALSE, FALSE, TRUE); | |
764 | } | |
765 | else { | |
766 | $contactFields = CRM_Contact_BAO_Contact::exportableFields($contactType, FALSE, TRUE); | |
767 | } | |
768 | $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook::singleton()->getFields()); | |
769 | ||
770 | // Exclude the address options disabled in the Address Settings | |
771 | $fields[$contactType] = CRM_Core_BAO_Address::validateAddressOptions($contactFields); | |
772 | ksort($fields[$contactType]); | |
773 | if ($mappingType == 'Export') { | |
774 | $relationships = []; | |
78f3f7c0 | 775 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $contactType); |
3a95b2ee CW |
776 | asort($relationshipTypes); |
777 | ||
778 | foreach ($relationshipTypes as $key => $var) { | |
779 | list($type) = explode('_', $key); | |
780 | ||
781 | $relationships[$key]['title'] = $var; | |
782 | $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
783 | $relationships[$key]['export'] = TRUE; | |
784 | $relationships[$key]['relationship_type_id'] = $type; | |
785 | $relationships[$key]['related'] = TRUE; | |
786 | $relationships[$key]['hasRelationType'] = 1; | |
787 | } | |
788 | ||
789 | if (!empty($relationships)) { | |
790 | $fields[$contactType] = array_merge($fields[$contactType], | |
791 | ['related' => ['title' => ts('- related contact info -')]], | |
792 | $relationships | |
793 | ); | |
794 | } | |
795 | } | |
796 | } | |
797 | ||
798 | // Get the current employer for mapping. | |
799 | if ($mappingType == 'Export') { | |
800 | $fields['Individual']['current_employer_id']['title'] = ts('Current Employer ID'); | |
801 | } | |
802 | ||
d509ee87 CW |
803 | // Contact Sub Type For export |
804 | $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo(); | |
805 | foreach ($subTypes as $subType => $info) { | |
806 | //adding subtype specific relationships CRM-5256 | |
807 | $csRelationships = []; | |
808 | ||
809 | if ($mappingType == 'Export') { | |
810 | $subTypeRelationshipTypes | |
811 | = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $info['parent'], | |
812 | FALSE, 'label', TRUE, $subType); | |
813 | ||
814 | foreach ($subTypeRelationshipTypes as $key => $var) { | |
815 | if (!array_key_exists($key, $fields[$info['parent']])) { | |
816 | list($type) = explode('_', $key); | |
817 | ||
818 | $csRelationships[$key]['title'] = $var; | |
819 | $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
820 | $csRelationships[$key]['export'] = TRUE; | |
821 | $csRelationships[$key]['relationship_type_id'] = $type; | |
822 | $csRelationships[$key]['related'] = TRUE; | |
823 | $csRelationships[$key]['hasRelationType'] = 1; | |
824 | } | |
825 | } | |
826 | } | |
827 | ||
828 | $fields[$subType] = $fields[$info['parent']] + $csRelationships; | |
829 | ||
830 | //custom fields for sub type | |
831 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType); | |
832 | $fields[$subType] += $subTypeFields; | |
833 | } | |
834 | ||
3a95b2ee CW |
835 | return $fields; |
836 | } | |
837 | ||
573da9a3 CW |
838 | /** |
839 | * Adds component fields to the export fields array; returns list of components. | |
840 | * | |
841 | * @param array $fields | |
842 | * @param string $mappingType | |
843 | * @param int $exportMode | |
844 | * @return array | |
845 | */ | |
846 | public static function addComponentFields(&$fields, $mappingType, $exportMode) { | |
847 | $compArray = []; | |
848 | ||
849 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT)) { | |
850 | if (CRM_Core_Permission::access('CiviContribute')) { | |
851 | $fields['Contribution'] = CRM_Core_DAO::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution'); | |
852 | unset($fields['Contribution']['contribution_contact_id']); | |
853 | $compArray['Contribution'] = ts('Contribution'); | |
854 | } | |
855 | } | |
856 | ||
857 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT)) { | |
858 | if (CRM_Core_Permission::access('CiviEvent')) { | |
859 | $fields['Participant'] = CRM_Event_BAO_Participant::exportableFields(); | |
860 | //get the component payment fields | |
861 | // @todo - review this - inconsistent with other entities & hacky. | |
862 | if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { | |
863 | $componentPaymentFields = []; | |
e6a94a98 | 864 | foreach ([ |
865 | 'componentPaymentField_total_amount' => ts('Total Amount'), | |
866 | 'componentPaymentField_contribution_status' => ts('Contribution Status'), | |
867 | 'componentPaymentField_received_date' => ts('Date Received'), | |
868 | 'componentPaymentField_payment_instrument' => ts('Payment Method'), | |
869 | 'componentPaymentField_transaction_id' => ts('Transaction ID'), | |
870 | ] as $payField => $payTitle) { | |
573da9a3 CW |
871 | $componentPaymentFields[$payField] = ['title' => $payTitle]; |
872 | } | |
873 | $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields); | |
874 | } | |
875 | ||
876 | $compArray['Participant'] = ts('Participant'); | |
877 | } | |
878 | } | |
879 | ||
880 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT)) { | |
881 | if (CRM_Core_Permission::access('CiviMember')) { | |
882 | $fields['Membership'] = CRM_Member_BAO_Membership::getMembershipFields($exportMode); | |
883 | unset($fields['Membership']['membership_contact_id']); | |
884 | $compArray['Membership'] = ts('Membership'); | |
885 | } | |
886 | } | |
887 | ||
888 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT)) { | |
889 | if (CRM_Core_Permission::access('CiviPledge')) { | |
890 | $fields['Pledge'] = CRM_Pledge_BAO_Pledge::exportableFields(); | |
891 | unset($fields['Pledge']['pledge_contact_id']); | |
892 | $compArray['Pledge'] = ts('Pledge'); | |
893 | } | |
894 | } | |
895 | ||
896 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CASE_EXPORT)) { | |
897 | if (CRM_Core_Permission::access('CiviCase')) { | |
898 | $fields['Case'] = CRM_Case_BAO_Case::exportableFields(); | |
899 | $compArray['Case'] = ts('Case'); | |
900 | ||
901 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Case'); | |
902 | $compArray['Activity'] = ts('Case Activity'); | |
903 | ||
904 | unset($fields['Case']['case_contact_id']); | |
905 | } | |
906 | } | |
907 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT)) { | |
908 | if (CRM_Core_Permission::access('CiviGrant')) { | |
909 | $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); | |
910 | unset($fields['Grant']['grant_contact_id']); | |
911 | if ($mappingType == 'Search Builder') { | |
912 | unset($fields['Grant']['grant_type_id']); | |
913 | } | |
914 | $compArray['Grant'] = ts('Grant'); | |
915 | } | |
916 | } | |
917 | ||
918 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT)) { | |
919 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Activity'); | |
920 | $compArray['Activity'] = ts('Activity'); | |
921 | } | |
922 | ||
923 | return $compArray; | |
924 | } | |
925 | ||
257195f9 | 926 | /** |
927 | * Get the parameters for a mapping field in a saveable format from the quickform mapping format. | |
928 | * | |
d67808ac | 929 | * @param array $defaults |
257195f9 | 930 | * @param array $v |
257195f9 | 931 | * |
932 | * @return array | |
933 | */ | |
04f8f758 | 934 | public static function getMappingParams($defaults, $v) { |
257195f9 | 935 | $locationTypeId = NULL; |
d67808ac | 936 | $saveMappingFields = $defaults; |
257195f9 | 937 | |
d67808ac | 938 | $saveMappingFields['name'] = CRM_Utils_Array::value('1', $v); |
939 | $saveMappingFields['contact_type'] = CRM_Utils_Array::value('0', $v); | |
257195f9 | 940 | $locationId = CRM_Utils_Array::value('2', $v); |
d67808ac | 941 | $saveMappingFields['location_type_id'] = is_numeric($locationId) ? $locationId : NULL; |
257195f9 | 942 | |
943 | if ($v[1] == 'phone') { | |
d67808ac | 944 | $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('3', $v); |
257195f9 | 945 | } |
946 | elseif ($v[1] == 'im') { | |
d67808ac | 947 | $saveMappingFields['im_provider_id'] = CRM_Utils_Array::value('3', $v); |
257195f9 | 948 | } |
949 | ||
257195f9 | 950 | // Handle mapping for 'related contact' fields |
951 | if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { | |
952 | list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); | |
953 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
954 | ||
955 | if (!empty($v['2'])) { | |
d67808ac | 956 | $saveMappingFields['name'] = CRM_Utils_Array::value('2', $v); |
257195f9 | 957 | } |
958 | elseif (!empty($v['4'])) { | |
d67808ac | 959 | $saveMappingFields['name'] = CRM_Utils_Array::value('4', $v); |
257195f9 | 960 | } |
961 | ||
962 | if (is_numeric(CRM_Utils_Array::value('3', $v))) { | |
963 | $locationTypeId = CRM_Utils_Array::value('3', $v); | |
964 | } | |
965 | elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { | |
966 | $locationTypeId = CRM_Utils_Array::value('5', $v); | |
967 | } | |
968 | ||
969 | if (is_numeric(CRM_Utils_Array::value('4', $v))) { | |
04f8f758 | 970 | if ($saveMappingFields['name'] === 'im') { |
971 | $saveMappingFields['im_provider_id'] = $v[4]; | |
972 | } | |
973 | else { | |
974 | $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('4', $v); | |
975 | } | |
257195f9 | 976 | } |
977 | elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { | |
04f8f758 | 978 | $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('6', $v); |
257195f9 | 979 | } |
980 | ||
d67808ac | 981 | $saveMappingFields['location_type_id'] = is_numeric($locationTypeId) ? $locationTypeId : NULL; |
d67808ac | 982 | $saveMappingFields['relationship_type_id'] = $id; |
983 | $saveMappingFields['relationship_direction'] = "{$first}_{$second}"; | |
257195f9 | 984 | } |
985 | } | |
986 | ||
d67808ac | 987 | return $saveMappingFields; |
257195f9 | 988 | } |
989 | ||
5c322e13 | 990 | /** |
991 | * Load saved mapping. | |
992 | * | |
993 | * @param $mappingLocation | |
994 | * @param int $x | |
995 | * @param int $i | |
996 | * @param $mappingName | |
997 | * @param $mapperFields | |
998 | * @param $mappingContactType | |
999 | * @param $mappingRelation | |
1000 | * @param array $specialFields | |
1001 | * @param $mappingPhoneType | |
1002 | * @param $phoneType | |
1003 | * @param array $defaults | |
1004 | * @param array $noneArray | |
1005 | * @param $imProvider | |
1006 | * @param $mappingImProvider | |
1007 | * @param $mappingOperator | |
1008 | * @param $mappingValue | |
1009 | * | |
1010 | * @return array | |
1011 | */ | |
1012 | protected static function loadSavedMapping($mappingLocation, int $x, int $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, array $specialFields, $mappingPhoneType, array $defaults, array $noneArray, $mappingImProvider, $mappingOperator, $mappingValue) { | |
1013 | $locationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; | |
1014 | if (isset($mappingName[$x][$i])) { | |
1015 | if (is_array($mapperFields[$mappingContactType[$x][$i]])) { | |
1016 | ||
1017 | if (isset($mappingRelation[$x][$i])) { | |
1018 | $relLocationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; | |
1019 | if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) { | |
1020 | $relLocationId = " "; | |
1021 | } | |
1022 | ||
1023 | $relPhoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; | |
1024 | ||
1025 | $defaults["mapper[$x][$i]"] = [ | |
1026 | $mappingContactType[$x][$i], | |
1027 | $mappingRelation[$x][$i], | |
1028 | $locationId, | |
1029 | $phoneType, | |
1030 | $mappingName[$x][$i], | |
1031 | $relLocationId, | |
1032 | $relPhoneType, | |
1033 | ]; | |
1034 | ||
1035 | if (!$locationId) { | |
1036 | $noneArray[] = [$x, $i, 2]; | |
1037 | } | |
1038 | if (!$phoneType && !$imProvider) { | |
1039 | $noneArray[] = [$x, $i, 3]; | |
1040 | } | |
1041 | if (!$mappingName[$x][$i]) { | |
1042 | $noneArray[] = [$x, $i, 4]; | |
1043 | } | |
1044 | if (!$relLocationId) { | |
1045 | $noneArray[] = [$x, $i, 5]; | |
1046 | } | |
1047 | if (!$relPhoneType) { | |
1048 | $noneArray[] = [$x, $i, 6]; | |
1049 | } | |
1050 | $noneArray[] = [$x, $i, 2]; | |
1051 | } | |
1052 | else { | |
1053 | $phoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; | |
1054 | $imProvider = isset($mappingImProvider[$x][$i]) ? $mappingImProvider[$x][$i] : NULL; | |
1055 | if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) { | |
1056 | $locationId = " "; | |
1057 | } | |
1058 | ||
1059 | $defaults["mapper[$x][$i]"] = [ | |
1060 | $mappingContactType[$x][$i], | |
1061 | $mappingName[$x][$i], | |
1062 | $locationId, | |
1063 | $phoneType, | |
1064 | ]; | |
1065 | if (!$mappingName[$x][$i]) { | |
1066 | $noneArray[] = [$x, $i, 1]; | |
1067 | } | |
1068 | if (!$locationId) { | |
1069 | $noneArray[] = [$x, $i, 2]; | |
1070 | } | |
1071 | if (!$phoneType && !$imProvider) { | |
1072 | $noneArray[] = [$x, $i, 3]; | |
1073 | } | |
1074 | ||
1075 | $noneArray[] = [$x, $i, 4]; | |
1076 | $noneArray[] = [$x, $i, 5]; | |
1077 | $noneArray[] = [$x, $i, 6]; | |
1078 | } | |
1079 | ||
1080 | $jsSet = TRUE; | |
1081 | ||
1082 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingOperator))) { | |
1083 | $defaults["operator[$x][$i]"] = CRM_Utils_Array::value($i, $mappingOperator[$x]); | |
1084 | } | |
1085 | ||
1086 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingValue))) { | |
1087 | $defaults["value[$x][$i]"] = CRM_Utils_Array::value($i, $mappingValue[$x]); | |
1088 | } | |
1089 | } | |
1090 | } | |
1091 | return [$mappingName, $defaults, $noneArray, $jsSet]; | |
1092 | } | |
1093 | ||
b5c2afd0 | 1094 | /** |
c490a46a CW |
1095 | * Function returns all custom fields with group title and |
1096 | * field label | |
b5c2afd0 | 1097 | * |
6a0b768e TO |
1098 | * @param int $relationshipTypeId |
1099 | * Related relationship type id. | |
c490a46a | 1100 | * |
a6c01b45 CW |
1101 | * @return array |
1102 | * all custom field titles | |
b5c2afd0 | 1103 | */ |
00be9182 | 1104 | public function getRelationTypeCustomGroupData($relationshipTypeId) { |
6a488035 TO |
1105 | |
1106 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL); | |
be2fb01f | 1107 | $groupTitle = []; |
6a488035 TO |
1108 | foreach ($customFields as $krelation => $vrelation) { |
1109 | $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label']; | |
1110 | } | |
1111 | return $groupTitle; | |
1112 | } | |
1113 | ||
6a488035 | 1114 | /** |
fe482240 | 1115 | * Function returns all Custom group Names. |
6a488035 | 1116 | * |
6a0b768e TO |
1117 | * @param int $customfieldId |
1118 | * Related file id. | |
77b97be7 | 1119 | * |
72b3a70c CW |
1120 | * @return null|string |
1121 | * $customGroupName all custom group names | |
6a488035 | 1122 | */ |
00be9182 | 1123 | public static function getCustomGroupName($customfieldId) { |
6a488035 TO |
1124 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($customfieldId)) { |
1125 | $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id'); | |
1126 | $customGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title'); | |
1127 | ||
e429c35a | 1128 | $customGroupName = CRM_Utils_String::ellipsify($customGroupName, 13); |
6a488035 TO |
1129 | |
1130 | return $customGroupName; | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * Function returns associated array of elements, that will be passed for search | |
1136 | * | |
6a0b768e TO |
1137 | * @param array $params |
1138 | * Associated array of submitted values. | |
1139 | * @param bool $row | |
1140 | * Row no of the fields. | |
6a488035 | 1141 | * |
6a488035 | 1142 | * |
a6c01b45 CW |
1143 | * @return array |
1144 | * formatted associated array of elements | |
6a488035 | 1145 | */ |
00be9182 | 1146 | public static function formattedFields(&$params, $row = FALSE) { |
be2fb01f | 1147 | $fields = []; |
6a488035 TO |
1148 | |
1149 | if (empty($params) || !isset($params['mapper'])) { | |
1150 | return $fields; | |
1151 | } | |
1152 | ||
be2fb01f | 1153 | $types = ['Individual', 'Organization', 'Household']; |
6a488035 TO |
1154 | foreach ($params['mapper'] as $key => $value) { |
1155 | $contactType = NULL; | |
1156 | foreach ($value as $k => $v) { | |
1157 | if (in_array($v[0], $types)) { | |
1158 | if ($contactType && $contactType != $v[0]) { | |
1159 | CRM_Core_Error::fatal(ts("Cannot have two clauses with different types: %1, %2", | |
be2fb01f | 1160 | [1 => $contactType, 2 => $v[0]] |
353ffa53 | 1161 | )); |
6a488035 TO |
1162 | } |
1163 | $contactType = $v[0]; | |
1164 | } | |
a7488080 | 1165 | if (!empty($v['1'])) { |
6a488035 TO |
1166 | $fldName = $v[1]; |
1167 | $v2 = CRM_Utils_Array::value('2', $v); | |
1168 | if ($v2 && trim($v2)) { | |
1169 | $fldName .= "-{$v[2]}"; | |
1170 | } | |
1171 | ||
1172 | $v3 = CRM_Utils_Array::value('3', $v); | |
1173 | if ($v3 && trim($v3)) { | |
1174 | $fldName .= "-{$v[3]}"; | |
1175 | } | |
1176 | ||
1177 | $value = $params['value'][$key][$k]; | |
6a488035 | 1178 | |
8ef12e64 | 1179 | if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_' |
353ffa53 | 1180 | && substr($fldName, 0, 10) != 'financial_' |
afa0b07c | 1181 | && substr($fldName, 0, 8) != 'payment_') { |
6a488035 TO |
1182 | if (substr($fldName, 0, 13) != 'contribution_') { |
1183 | $fldName = 'contribution_' . $fldName; | |
1184 | } | |
1185 | } | |
1186 | ||
dbc6f6d6 | 1187 | // CRM-14983: verify if values are comma separated convert to array |
fccb6a0f | 1188 | if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) { |
1189 | $value = explode(',', $value); | |
be2fb01f | 1190 | $value = [$params['operator'][$key][$k] => $value]; |
dbc6f6d6 | 1191 | } |
fce45661 SL |
1192 | // CRM-19081 Fix legacy StateProvince Field Values. |
1193 | // These derive from smart groups created using search builder under older | |
1194 | // CiviCRM versions. | |
1195 | if (!is_numeric($value) && $fldName == 'state_province') { | |
1196 | $value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'state_province_id', $value); | |
1197 | } | |
dbc6f6d6 | 1198 | |
6a488035 | 1199 | if ($row) { |
be2fb01f | 1200 | $fields[] = [ |
6a488035 TO |
1201 | $fldName, |
1202 | $params['operator'][$key][$k], | |
1203 | $value, | |
1204 | $key, | |
1205 | $k, | |
be2fb01f | 1206 | ]; |
6a488035 TO |
1207 | } |
1208 | else { | |
be2fb01f | 1209 | $fields[] = [ |
6a488035 TO |
1210 | $fldName, |
1211 | $params['operator'][$key][$k], | |
1212 | $value, | |
1213 | $key, | |
1214 | 0, | |
be2fb01f | 1215 | ]; |
6a488035 TO |
1216 | } |
1217 | } | |
1218 | } | |
1219 | if ($contactType) { | |
be2fb01f | 1220 | $fields[] = [ |
6a488035 TO |
1221 | 'contact_type', |
1222 | '=', | |
1223 | $contactType, | |
1224 | $key, | |
1225 | 0, | |
be2fb01f | 1226 | ]; |
6a488035 TO |
1227 | } |
1228 | } | |
1229 | ||
1230 | //add sortByCharacter values | |
1231 | if (isset($params['sortByCharacter'])) { | |
be2fb01f | 1232 | $fields[] = [ |
6a488035 TO |
1233 | 'sortByCharacter', |
1234 | '=', | |
1235 | $params['sortByCharacter'], | |
1236 | 0, | |
1237 | 0, | |
be2fb01f | 1238 | ]; |
6a488035 | 1239 | } |
6a488035 TO |
1240 | return $fields; |
1241 | } | |
1242 | ||
b5c2afd0 | 1243 | /** |
c490a46a | 1244 | * @param array $params |
b5c2afd0 EM |
1245 | * |
1246 | * @return array | |
1247 | */ | |
00be9182 | 1248 | public static function &returnProperties(&$params) { |
be2fb01f | 1249 | $fields = [ |
6a488035 TO |
1250 | 'contact_type' => 1, |
1251 | 'contact_sub_type' => 1, | |
1252 | 'sort_name' => 1, | |
be2fb01f | 1253 | ]; |
6a488035 TO |
1254 | |
1255 | if (empty($params) || empty($params['mapper'])) { | |
1256 | return $fields; | |
1257 | } | |
1258 | ||
f9ba4d01 | 1259 | $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); |
6a488035 TO |
1260 | foreach ($params['mapper'] as $key => $value) { |
1261 | foreach ($value as $k => $v) { | |
1262 | if (isset($v[1])) { | |
1263 | if ($v[1] == 'groups' || $v[1] == 'tags') { | |
1264 | continue; | |
1265 | } | |
1266 | ||
1267 | if (isset($v[2]) && is_numeric($v[2])) { | |
1268 | if (!array_key_exists('location', $fields)) { | |
be2fb01f | 1269 | $fields['location'] = []; |
6a488035 TO |
1270 | } |
1271 | ||
1272 | // make sure that we have a location fields and a location type for this | |
1273 | $locationName = $locationTypes[$v[2]]; | |
1274 | if (!array_key_exists($locationName, $fields['location'])) { | |
be2fb01f | 1275 | $fields['location'][$locationName] = []; |
6a488035 TO |
1276 | $fields['location'][$locationName]['location_type'] = $v[2]; |
1277 | } | |
1278 | ||
1279 | if ($v[1] == 'phone' || $v[1] == 'email' || $v[1] == 'im') { | |
1280 | // phone type handling | |
1281 | if (isset($v[3])) { | |
1282 | $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1; | |
1283 | } | |
1284 | else { | |
1285 | $fields['location'][$locationName][$v[1]] = 1; | |
1286 | } | |
1287 | } | |
1288 | else { | |
1289 | $fields['location'][$locationName][$v[1]] = 1; | |
1290 | } | |
1291 | } | |
1292 | else { | |
1293 | $fields[$v[1]] = 1; | |
1294 | } | |
1295 | } | |
1296 | } | |
1297 | } | |
1298 | ||
1299 | return $fields; | |
1300 | } | |
1301 | ||
1302 | /** | |
100fef9d | 1303 | * Save the mapping field info for search builder / export given the formvalues |
6a488035 | 1304 | * |
6a0b768e TO |
1305 | * @param array $params |
1306 | * Asscociated array of formvalues. | |
1307 | * @param int $mappingId | |
1308 | * Mapping id. | |
6a488035 | 1309 | * |
e60f24eb | 1310 | * @return NULL |
6a488035 | 1311 | */ |
5bc608d3 | 1312 | public static function saveMappingFields($params, $mappingId) { |
3af96592 | 1313 | //delete mapping fields records for existing mapping |
6a488035 TO |
1314 | $mappingFields = new CRM_Core_DAO_MappingField(); |
1315 | $mappingFields->mapping_id = $mappingId; | |
1316 | $mappingFields->delete(); | |
1317 | ||
1318 | if (empty($params['mapper'])) { | |
7c550ca0 | 1319 | return NULL; |
6a488035 TO |
1320 | } |
1321 | ||
1322 | //save record in mapping field table | |
1323 | foreach ($params['mapper'] as $key => $value) { | |
1324 | $colCnt = 0; | |
1325 | foreach ($value as $k => $v) { | |
1326 | ||
a7488080 | 1327 | if (!empty($v['1'])) { |
d67808ac | 1328 | $saveMappingParams = self::getMappingParams( |
1329 | [ | |
1330 | 'mapping_id' => $mappingId, | |
1331 | 'grouping' => $key, | |
1332 | 'operator' => $params['operator'][$key][$k] ?? NULL, | |
1333 | 'value' => $params['value'][$key][$k] ?? NULL, | |
1334 | 'column_number' => $colCnt, | |
1335 | ], $v); | |
257195f9 | 1336 | $saveMappingField = new CRM_Core_DAO_MappingField(); |
1337 | $saveMappingField->copyValues($saveMappingParams, TRUE); | |
1338 | $saveMappingField->save(); | |
6a488035 | 1339 | $colCnt++; |
6a488035 TO |
1340 | } |
1341 | } | |
1342 | } | |
1343 | } | |
96025800 | 1344 | |
6a488035 | 1345 | } |