3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Core_BAO_Mapping
extends CRM_Core_DAO_Mapping
{
22 public function __construct() {
23 parent
::__construct();
27 * Fetch object based on array of properties.
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.
35 * CRM_Core_DAO_Mapping object on success, otherwise NULL
37 public static function retrieve(&$params, &$defaults) {
38 $mapping = new CRM_Core_DAO_Mapping();
39 $mapping->copyValues($params);
40 if ($mapping->find(TRUE)) {
41 CRM_Core_DAO
::storeValues($mapping, $defaults);
55 public static function del($id) {
56 // delete from mapping_field table
57 $mappingField = new CRM_Core_DAO_MappingField();
58 $mappingField->mapping_id
= $id;
59 $mappingField->delete();
61 // delete from mapping table
62 $mapping = new CRM_Core_DAO_Mapping();
64 if ($mapping->find(TRUE)) {
65 $result = $mapping->delete();
72 * Takes an associative array and creates a contact object.
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
78 * @param array $params
79 * An array of name/value pairs.
82 * CRM_Core_DAO_Mapper object on success, otherwise NULL
84 public static function add($params) {
85 $mapping = new CRM_Core_DAO_Mapping();
86 $mapping->copyValues($params);
93 * Get the list of mappings for a select or select2 element.
95 * @param string $mappingType
97 * @param bool $select2
101 * Array of mapping names, keyed by id.
103 public static function getMappings($mappingType, $select2 = FALSE) {
104 $result = civicrm_api3('Mapping', 'get', [
105 'mapping_type_id' => $mappingType,
106 'return' => ['name', 'description'],
114 foreach ($result['values'] as $id => $value) {
116 $item = ['id' => $id, 'text' => $value['name']];
117 if (!empty($value['description'])) {
118 $item['description'] = $value['description'];
123 $mapping[$id] = $value['name'];
130 * Get the mappings array, creating if it does not exist.
132 * @param string $mappingType
136 * Array of mapping names, keyed by id.
138 * @throws \CiviCRM_API3_Exception
140 public static function getCreateMappingValues($mappingType) {
142 return CRM_Core_BAO_Mapping
::getMappings($mappingType);
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') {
150 $mappingValues = civicrm_api3('Mapping', 'getoptions', ['field' => 'mapping_type_id']);
151 civicrm_api3('OptionValue', 'create', [
152 'option_group_id' => 'mapping_type',
153 'label' => $mappingType,
154 'value' => max(array_keys($mappingValues['values'])) +
1,
157 return CRM_Core_BAO_Mapping
::getMappings($mappingType);
164 * Get the mapping fields.
166 * @param int $mappingId
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)?
174 * array of mapping fields
176 public static function getMappingFields($mappingId, $addPrimary = FALSE) {
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');
183 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = [];
184 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = [];
185 while ($mapping->fetch()) {
186 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
187 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
189 if (!empty($mapping->location_type_id
)) {
190 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
192 elseif ($addPrimary) {
193 if (CRM_Contact_BAO_Contact
::isFieldHasLocationType($mapping->name
)) {
194 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = ts('Primary');
197 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = NULL;
201 if (!empty($mapping->phone_type_id
)) {
202 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
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
;
210 if (!empty($mapping->website_type_id
)) {
211 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
214 if (!empty($mapping->relationship_type_id
)) {
215 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
218 if (!empty($mapping->operator
)) {
219 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
222 if (!empty($mapping->value
)) {
223 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
241 * Get un-indexed array of the field values for the given mapping id.
243 * For example if passing a mapping ID & name the returned array would look like
244 * ['First field name', 'second field name']
246 * @param int $mappingID
247 * @param string $fieldName
250 * @throws \CiviCRM_API3_Exception
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']));
257 * Check Duplicate Mapping Name.
259 * @param string $nameField
261 * @param string $mapTypeId
266 public static function checkMapping($nameField, $mapTypeId) {
267 $mapping = new CRM_Core_DAO_Mapping();
268 $mapping->name
= $nameField;
269 $mapping->mapping_type_id
= $mapTypeId;
270 return (bool) $mapping->find(TRUE);
274 * Function returns associated array of elements, that will be passed for search.
276 * @param int $smartGroupId
280 * associated array of elements
282 public static function getFormattedFields($smartGroupId) {
285 //get the fields from mapping table
286 $dao = new CRM_Core_DAO_MappingField();
287 $dao->mapping_id
= $smartGroupId;
289 while ($dao->fetch()) {
290 $fldName = $dao->name
;
291 if ($dao->location_type_id
) {
292 $fldName .= "-{$dao->location_type_id}";
294 if ($dao->phone_type
) {
295 $fldName .= "-{$dao->phone_type}";
297 $returnFields[$fldName]['value'] = $dao->value
;
298 $returnFields[$fldName]['op'] = $dao->operator
;
299 $returnFields[$fldName]['grouping'] = $dao->grouping
;
301 return $returnFields;
305 * Build the mapping form for Search Builder.
307 * @param CRM_Core_Form $form
308 * @param int $mappingId
309 * @param int $columnNo
310 * @param int $blockCount
311 * (no of blocks shown).
312 * @param int $exportMode
314 public static function buildMappingForm(&$form, $mappingId, $columnNo, $blockCount, $exportMode = NULL) {
316 $hasLocationTypes = [];
317 $hasRelationTypes = [];
319 $columnCount = $columnNo;
320 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
321 ['class' => 'submit-link']
324 $contactTypes = CRM_Contact_BAO_ContactType
::basicTypes();
325 $fields = self
::getBasicFields('Search Builder');
327 // Unset groups, tags, notes for component export
328 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
329 foreach (array_keys($fields) as $type) {
330 CRM_Utils_Array
::remove($fields[$type], 'groups', 'tags', 'notes');
334 // Build the common contact fields array.
335 $fields['Contact'] = [];
336 foreach ($fields[$contactTypes[0]] as $key => $value) {
337 // If a field exists across all contact types, move it to the "Contact" selector
339 foreach ($contactTypes as $type) {
340 if (!isset($fields[$type][$key])) {
341 $ubiquitious = FALSE;
345 $fields['Contact'][$key] = $value;
346 foreach ($contactTypes as $type) {
347 unset($fields[$type][$key]);
351 if (array_key_exists('note', $fields['Contact'])) {
352 $noteTitle = $fields['Contact']['note']['title'];
353 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
354 $fields['Contact']['note_body'] = ['title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body'];
355 $fields['Contact']['note_subject'] = [
356 'title' => $noteTitle . ': ' . ts('Subject Only'),
357 'name' => 'note_subject',
361 // add component fields
362 $compArray = self
::addComponentFields($fields, 'Search Builder', $exportMode);
364 foreach ($fields as $key => $value) {
366 foreach ($value as $key1 => $value1) {
367 //CRM-2676, replacing the conflict for same custom field name from different custom group.
368 $customGroupName = self
::getCustomGroupName($key1);
370 if ($customGroupName) {
371 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
374 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
376 if (isset($value1['hasLocationType'])) {
377 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
380 if (isset($value1['hasRelationType'])) {
381 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
382 unset($relatedMapperFields[$key][$key1]);
386 if (isset($relatedMapperFields[$key]['related'])) {
387 unset($relatedMapperFields[$key]['related']);
391 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
393 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
395 // FIXME: dirty hack to make the default option show up first. This
396 // avoids a mozilla browser bug with defaults on dynamically constructed
398 if ($defaultLocationType) {
399 $defaultLocation = $locationTypes[$defaultLocationType->id
];
400 unset($locationTypes[$defaultLocationType->id
]);
401 $locationTypes = [$defaultLocationType->id
=> $defaultLocation] +
$locationTypes;
404 $locationTypes = [' ' => ts('Primary')] +
$locationTypes;
406 // since we need a hierarchical list to display contact types & subtypes,
407 // this is what we going to display in first selector
408 $contactTypeSelect = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
409 $contactTypeSelect = ['Contact' => ts('Contacts')] +
$contactTypeSelect;
411 $sel1 = ['' => ts('- select record type -')] +
$contactTypeSelect +
$compArray;
413 foreach ($sel1 as $key => $sel) {
415 // sort everything BUT the contactType which is sorted separately by
416 // an initial commit of CRM-13278 (check ksort above)
417 if (!in_array($key, $contactTypes)) {
418 asort($mapperFields[$key]);
420 $sel2[$key] = ['' => ts('- select field -')] +
$mapperFields[$key];
426 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
427 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
430 foreach ($sel1 as $k => $sel) {
432 foreach ($locationTypes as $key => $value) {
433 if (trim($key) != '') {
434 $sel4[$k]['phone'][$key] = &$phoneTypes;
435 $sel4[$k]['im'][$key] = &$imProviders;
441 foreach ($sel1 as $k => $sel) {
443 foreach ($mapperFields[$k] as $key => $value) {
444 if (isset($hasLocationTypes[$k][$key])) {
445 $sel3[$k][$key] = $locationTypes;
454 // Array for core fields and relationship custom data
455 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
457 //special fields that have location, hack for primary location
460 'supplemental_address_1',
461 'supplemental_address_2',
462 'supplemental_address_3',
465 'postal_code_suffix',
475 if (isset($mappingId)) {
476 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
477 $mappingRelation, $mappingOperator, $mappingValue
478 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
480 $blkCnt = count($mappingName);
481 if ($blkCnt >= $blockCount) {
482 $blockCount = $blkCnt +
1;
484 for ($x = 1; $x < $blockCount; $x++
) {
485 if (isset($mappingName[$x])) {
486 $colCnt = count($mappingName[$x]);
487 if ($colCnt >= $columnCount[$x]) {
488 $columnCount[$x] = $colCnt;
494 $form->_blockCount
= $blockCount;
495 $form->_columnCount
= $columnCount;
497 $form->set('blockCount', $form->_blockCount
);
498 $form->set('columnCount', $form->_columnCount
);
500 $defaults = $noneArray = $nullArray = [];
502 for ($x = 1; $x < $blockCount; $x++
) {
504 for ($i = 0; $i < $columnCount[$x]; $i++
) {
506 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
509 if (isset($mappingId)) {
510 list($mappingName, $defaults, $noneArray, $jsSet) = self
::loadSavedMapping($mappingLocation, $x, $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, $specialFields, $mappingPhoneType, $defaults, $noneArray, $mappingImProvider, $mappingOperator, $mappingValue);
512 //Fix for Search Builder
515 $formValues = $form->exportValues();
517 if (empty($formValues)) {
518 // Incremented length for third select box(relationship type)
519 for ($k = 1; $k < $j; $k++
) {
520 $noneArray[] = [$x, $i, $k];
524 if (!empty($formValues['mapper'][$x])) {
525 foreach ($formValues['mapper'][$x] as $value) {
526 for ($k = 1; $k < $j; $k++
) {
527 if (!isset($formValues['mapper'][$x][$i][$k]) ||
528 (!$formValues['mapper'][$x][$i][$k])
530 $noneArray[] = [$x, $i, $k];
533 $nullArray[] = [$x, $i, $k];
539 for ($k = 1; $k < $j; $k++
) {
540 $noneArray[] = [$x, $i, $k];
545 //Fix for Search Builder
546 $sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
548 //CRM -2292, restricted array set
549 $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues
::getSearchBuilderOperators();
551 $form->add('select', "operator[$x][$i]", '', $operatorArray);
552 $form->add('text', "value[$x][$i]", '');
555 $form->addElement('submit', "addMore[$x]", ts('Another search field'), ['class' => 'submit-link']);
559 $js = "<script type='text/javascript'>\n";
560 $formName = "document.Builder";
561 if (!empty($nullArray)) {
562 $js .= "var nullArray = [";
565 foreach ($nullArray as $element) {
566 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
567 if (!isset($seen[$key])) {
568 $elements[] = "[$key]";
572 $js .= implode(', ', $elements);
575 for (var i=0;i<nullArray.length;i++) {
576 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
577 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
582 if (!empty($noneArray)) {
583 $js .= "var noneArray = [";
586 foreach ($noneArray as $element) {
587 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
588 if (!isset($seen[$key])) {
589 $elements[] = "[$key]";
593 $js .= implode(', ', $elements);
596 for (var i=0;i<noneArray.length;i++) {
597 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
598 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
603 $js .= "</script>\n";
605 $form->assign('initHideBoxes', $js);
606 $form->assign('columnCount', $columnCount);
607 $form->assign('blockCount', $blockCount);
608 $form->setDefaults($defaults);
610 $form->setDefaultAction('refresh');
614 * @param string $mappingType
617 public static function getBasicFields($mappingType) {
618 $contactTypes = CRM_Contact_BAO_ContactType
::basicTypes();
620 foreach ($contactTypes as $contactType) {
621 if ($mappingType == 'Search Builder') {
622 // Get multiple custom group fields in this context
623 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($contactType, FALSE, FALSE, FALSE, TRUE);
626 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($contactType, FALSE, TRUE);
628 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
630 // Exclude the address options disabled in the Address Settings
631 $fields[$contactType] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
632 ksort($fields[$contactType]);
633 if ($mappingType == 'Export') {
635 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $contactType);
636 asort($relationshipTypes);
638 foreach ($relationshipTypes as $key => $var) {
639 list($type) = explode('_', $key);
641 $relationships[$key]['title'] = $var;
642 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
643 $relationships[$key]['export'] = TRUE;
644 $relationships[$key]['relationship_type_id'] = $type;
645 $relationships[$key]['related'] = TRUE;
646 $relationships[$key]['hasRelationType'] = 1;
649 if (!empty($relationships)) {
650 $fields[$contactType] = array_merge($fields[$contactType],
651 ['related' => ['title' => ts('- related contact info -')]],
658 // Get the current employer for mapping.
659 if ($mappingType == 'Export') {
660 $fields['Individual']['current_employer_id']['title'] = ts('Current Employer ID');
663 // Contact Sub Type For export
664 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
665 foreach ($subTypes as $subType => $info) {
666 //adding subtype specific relationships CRM-5256
667 $csRelationships = [];
669 if ($mappingType == 'Export') {
670 $subTypeRelationshipTypes
671 = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $info['parent'],
672 FALSE, 'label', TRUE, $subType);
674 foreach ($subTypeRelationshipTypes as $key => $var) {
675 if (!array_key_exists($key, $fields[$info['parent']])) {
676 list($type) = explode('_', $key);
678 $csRelationships[$key]['title'] = $var;
679 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
680 $csRelationships[$key]['export'] = TRUE;
681 $csRelationships[$key]['relationship_type_id'] = $type;
682 $csRelationships[$key]['related'] = TRUE;
683 $csRelationships[$key]['hasRelationType'] = 1;
688 $fields[$subType] = $fields[$info['parent']] +
$csRelationships;
690 //custom fields for sub type
691 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
692 $fields[$subType] +
= $subTypeFields;
699 * Adds component fields to the export fields array; returns list of components.
701 * @param array $fields
702 * @param string $mappingType
703 * @param int $exportMode
706 public static function addComponentFields(&$fields, $mappingType, $exportMode) {
709 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
710 if (CRM_Core_Permission
::access('CiviContribute')) {
711 $fields['Contribution'] = CRM_Core_DAO
::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution');
712 unset($fields['Contribution']['contribution_contact_id']);
713 $compArray['Contribution'] = ts('Contribution');
717 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
718 if (CRM_Core_Permission
::access('CiviEvent')) {
719 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
720 //get the component payment fields
721 // @todo - review this - inconsistent with other entities & hacky.
722 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
723 $componentPaymentFields = [];
725 'componentPaymentField_total_amount' => ts('Total Amount'),
726 'componentPaymentField_contribution_status' => ts('Contribution Status'),
727 'componentPaymentField_received_date' => ts('Date Received'),
728 'componentPaymentField_payment_instrument' => ts('Payment Method'),
729 'componentPaymentField_transaction_id' => ts('Transaction ID'),
730 ] as $payField => $payTitle) {
731 $componentPaymentFields[$payField] = ['title' => $payTitle];
733 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
736 $compArray['Participant'] = ts('Participant');
740 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
741 if (CRM_Core_Permission
::access('CiviMember')) {
742 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
743 unset($fields['Membership']['membership_contact_id']);
744 $compArray['Membership'] = ts('Membership');
748 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
749 if (CRM_Core_Permission
::access('CiviPledge')) {
750 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
751 unset($fields['Pledge']['pledge_contact_id']);
752 $compArray['Pledge'] = ts('Pledge');
756 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
757 if (CRM_Core_Permission
::access('CiviCase')) {
758 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
759 $compArray['Case'] = ts('Case');
761 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
762 $compArray['Activity'] = ts('Case Activity');
764 unset($fields['Case']['case_contact_id']);
767 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
768 if (CRM_Core_Permission
::access('CiviGrant')) {
769 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
770 unset($fields['Grant']['grant_contact_id']);
771 if ($mappingType == 'Search Builder') {
772 unset($fields['Grant']['grant_type_id']);
774 $compArray['Grant'] = ts('Grant');
778 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
779 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
780 $compArray['Activity'] = ts('Activity');
787 * Get the parameters for a mapping field in a saveable format from the quickform mapping format.
789 * @param array $defaults
794 public static function getMappingParams($defaults, $v) {
795 $locationTypeId = NULL;
796 $saveMappingFields = $defaults;
798 $saveMappingFields['name'] = CRM_Utils_Array
::value('1', $v);
799 $saveMappingFields['contact_type'] = CRM_Utils_Array
::value('0', $v);
800 $locationId = CRM_Utils_Array
::value('2', $v);
801 $saveMappingFields['location_type_id'] = is_numeric($locationId) ?
$locationId : NULL;
803 if ($v[1] == 'phone') {
804 $saveMappingFields['phone_type_id'] = CRM_Utils_Array
::value('3', $v);
806 elseif ($v[1] == 'im') {
807 $saveMappingFields['im_provider_id'] = CRM_Utils_Array
::value('3', $v);
810 // Handle mapping for 'related contact' fields
811 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
812 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
813 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
815 if (!empty($v['2'])) {
816 $saveMappingFields['name'] = CRM_Utils_Array
::value('2', $v);
818 elseif (!empty($v['4'])) {
819 $saveMappingFields['name'] = CRM_Utils_Array
::value('4', $v);
822 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
823 $locationTypeId = CRM_Utils_Array
::value('3', $v);
825 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
826 $locationTypeId = CRM_Utils_Array
::value('5', $v);
829 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
830 if ($saveMappingFields['name'] === 'im') {
831 $saveMappingFields['im_provider_id'] = $v[4];
834 $saveMappingFields['phone_type_id'] = CRM_Utils_Array
::value('4', $v);
837 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
838 $saveMappingFields['phone_type_id'] = CRM_Utils_Array
::value('6', $v);
841 $saveMappingFields['location_type_id'] = is_numeric($locationTypeId) ?
$locationTypeId : NULL;
842 $saveMappingFields['relationship_type_id'] = $id;
843 $saveMappingFields['relationship_direction'] = "{$first}_{$second}";
847 return $saveMappingFields;
851 * Load saved mapping.
853 * @param $mappingLocation
856 * @param $mappingName
857 * @param $mapperFields
858 * @param $mappingContactType
859 * @param $mappingRelation
860 * @param array $specialFields
861 * @param $mappingPhoneType
863 * @param array $defaults
864 * @param array $noneArray
866 * @param $mappingImProvider
867 * @param $mappingOperator
868 * @param $mappingValue
872 protected static function loadSavedMapping($mappingLocation, int $x, int $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, array $specialFields, $mappingPhoneType, array $defaults, array $noneArray, $mappingImProvider, $mappingOperator, $mappingValue) {
873 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
874 if (isset($mappingName[$x][$i])) {
875 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
877 if (isset($mappingRelation[$x][$i])) {
878 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
879 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
880 $relLocationId = " ";
883 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
885 $defaults["mapper[$x][$i]"] = [
886 $mappingContactType[$x][$i],
887 $mappingRelation[$x][$i],
890 $mappingName[$x][$i],
896 $noneArray[] = [$x, $i, 2];
898 if (!$phoneType && !$imProvider) {
899 $noneArray[] = [$x, $i, 3];
901 if (!$mappingName[$x][$i]) {
902 $noneArray[] = [$x, $i, 4];
904 if (!$relLocationId) {
905 $noneArray[] = [$x, $i, 5];
907 if (!$relPhoneType) {
908 $noneArray[] = [$x, $i, 6];
910 $noneArray[] = [$x, $i, 2];
913 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
914 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
915 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
919 $defaults["mapper[$x][$i]"] = [
920 $mappingContactType[$x][$i],
921 $mappingName[$x][$i],
925 if (!$mappingName[$x][$i]) {
926 $noneArray[] = [$x, $i, 1];
929 $noneArray[] = [$x, $i, 2];
931 if (!$phoneType && !$imProvider) {
932 $noneArray[] = [$x, $i, 3];
935 $noneArray[] = [$x, $i, 4];
936 $noneArray[] = [$x, $i, 5];
937 $noneArray[] = [$x, $i, 6];
942 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
943 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
946 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
947 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
951 return [$mappingName, $defaults, $noneArray, $jsSet];
955 * Function returns all custom fields with group title and
958 * @param int $relationshipTypeId
959 * Related relationship type id.
962 * all custom field titles
964 public function getRelationTypeCustomGroupData($relationshipTypeId) {
966 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
968 foreach ($customFields as $krelation => $vrelation) {
969 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
975 * Function returns all Custom group Names.
977 * @param int $customfieldId
980 * @return null|string
981 * $customGroupName all custom group names
983 public static function getCustomGroupName($customfieldId) {
984 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
985 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
986 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
988 $customGroupName = CRM_Utils_String
::ellipsify($customGroupName, 13);
990 return $customGroupName;
995 * Function returns associated array of elements, that will be passed for search
997 * @param array $params
998 * Associated array of submitted values.
1000 * Row no of the fields.
1004 * formatted associated array of elements
1006 public static function formattedFields(&$params, $row = FALSE) {
1009 if (empty($params) ||
!isset($params['mapper'])) {
1013 $types = ['Individual', 'Organization', 'Household'];
1014 foreach ($params['mapper'] as $key => $value) {
1015 $contactType = NULL;
1016 foreach ($value as $k => $v) {
1017 if (in_array($v[0], $types)) {
1018 if ($contactType && $contactType != $v[0]) {
1019 CRM_Core_Error
::fatal(ts("Cannot have two clauses with different types: %1, %2",
1020 [1 => $contactType, 2 => $v[0]]
1023 $contactType = $v[0];
1025 if (!empty($v['1'])) {
1027 $v2 = CRM_Utils_Array
::value('2', $v);
1028 if ($v2 && trim($v2)) {
1029 $fldName .= "-{$v[2]}";
1032 $v3 = CRM_Utils_Array
::value('3', $v);
1033 if ($v3 && trim($v3)) {
1034 $fldName .= "-{$v[3]}";
1037 $value = $params['value'][$key][$k];
1039 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1040 && substr($fldName, 0, 10) != 'financial_'
1041 && substr($fldName, 0, 8) != 'payment_') {
1042 if (substr($fldName, 0, 13) != 'contribution_') {
1043 $fldName = 'contribution_' . $fldName;
1047 // CRM-14983: verify if values are comma separated convert to array
1048 if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) {
1049 $value = explode(',', $value);
1050 $value = [$params['operator'][$key][$k] => $value];
1052 // CRM-19081 Fix legacy StateProvince Field Values.
1053 // These derive from smart groups created using search builder under older
1054 // CiviCRM versions.
1055 if (!is_numeric($value) && $fldName == 'state_province') {
1056 $value = CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'state_province_id', $value);
1062 $params['operator'][$key][$k],
1071 $params['operator'][$key][$k],
1090 //add sortByCharacter values
1091 if (isset($params['sortByCharacter'])) {
1095 $params['sortByCharacter'],
1104 * @param array $params
1108 public static function &returnProperties(&$params) {
1110 'contact_type' => 1,
1111 'contact_sub_type' => 1,
1115 if (empty($params) ||
empty($params['mapper'])) {
1119 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
1120 foreach ($params['mapper'] as $key => $value) {
1121 foreach ($value as $k => $v) {
1123 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1127 if (isset($v[2]) && is_numeric($v[2])) {
1128 if (!array_key_exists('location', $fields)) {
1129 $fields['location'] = [];
1132 // make sure that we have a location fields and a location type for this
1133 $locationName = $locationTypes[$v[2]];
1134 if (!array_key_exists($locationName, $fields['location'])) {
1135 $fields['location'][$locationName] = [];
1136 $fields['location'][$locationName]['location_type'] = $v[2];
1139 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1140 // phone type handling
1142 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1145 $fields['location'][$locationName][$v[1]] = 1;
1149 $fields['location'][$locationName][$v[1]] = 1;
1163 * Save the mapping field info for search builder / export given the formvalues
1165 * @param array $params
1166 * Asscociated array of formvalues.
1167 * @param int $mappingId
1172 public static function saveMappingFields($params, $mappingId) {
1173 //delete mapping fields records for existing mapping
1174 $mappingFields = new CRM_Core_DAO_MappingField();
1175 $mappingFields->mapping_id
= $mappingId;
1176 $mappingFields->delete();
1178 if (empty($params['mapper'])) {
1182 //save record in mapping field table
1183 foreach ($params['mapper'] as $key => $value) {
1185 foreach ($value as $k => $v) {
1187 if (!empty($v['1'])) {
1188 $saveMappingParams = self
::getMappingParams(
1190 'mapping_id' => $mappingId,
1192 'operator' => $params['operator'][$key][$k] ??
NULL,
1193 'value' => $params['value'][$key][$k] ??
NULL,
1194 'column_number' => $colCnt,
1196 $saveMappingField = new CRM_Core_DAO_MappingField();
1197 $saveMappingField->copyValues($saveMappingParams);
1198 $saveMappingField->save();