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