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