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 'name' => $mappingType,
155 'value' => max(array_keys($mappingValues['values'])) +
1,
158 return CRM_Core_BAO_Mapping
::getMappings($mappingType);
165 * Get the mapping fields.
167 * @param int $mappingId
170 * @param bool $addPrimary
171 * Add the key 'Primary' when the field is a location field AND there is
172 * no location type (meaning Primary)?
175 * array of mapping fields
177 public static function getMappingFields($mappingId, $addPrimary = FALSE) {
178 //mapping is to be loaded from database
179 $mapping = new CRM_Core_DAO_MappingField();
180 $mapping->mapping_id
= $mappingId;
181 $mapping->orderBy('column_number');
184 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = [];
185 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = [];
186 while ($mapping->fetch()) {
187 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
188 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
190 if (!empty($mapping->location_type_id
)) {
191 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
193 elseif ($addPrimary) {
194 if (CRM_Contact_BAO_Contact
::isFieldHasLocationType($mapping->name
)) {
195 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = ts('Primary');
198 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = NULL;
202 if (!empty($mapping->phone_type_id
)) {
203 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
206 // get IM service provider type id from mapping fields
207 if (!empty($mapping->im_provider_id
)) {
208 $mappingImProvider[$mapping->grouping
][$mapping->column_number
] = $mapping->im_provider_id
;
211 if (!empty($mapping->website_type_id
)) {
212 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
215 if (!empty($mapping->relationship_type_id
)) {
216 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
219 if (!empty($mapping->operator
)) {
220 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
223 if (!empty($mapping->value
)) {
224 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
242 * Get un-indexed array of the field values for the given mapping id.
244 * For example if passing a mapping ID & name the returned array would look like
245 * ['First field name', 'second field name']
247 * @param int $mappingID
248 * @param string $fieldName
251 * @throws \CiviCRM_API3_Exception
253 public static function getMappingFieldValues($mappingID, $fieldName) {
254 return array_merge(CRM_Utils_Array
::collect($fieldName, civicrm_api3('MappingField', 'get', ['mapping_id' => $mappingID, 'return' => $fieldName])['values']));
258 * Check Duplicate Mapping Name.
260 * @param string $nameField
262 * @param string $mapTypeId
267 public static function checkMapping($nameField, $mapTypeId) {
268 $mapping = new CRM_Core_DAO_Mapping();
269 $mapping->name
= $nameField;
270 $mapping->mapping_type_id
= $mapTypeId;
271 return (bool) $mapping->find(TRUE);
275 * Function returns associated array of elements, that will be passed for search.
277 * @param int $smartGroupId
281 * associated array of elements
283 public static function getFormattedFields($smartGroupId) {
286 //get the fields from mapping table
287 $dao = new CRM_Core_DAO_MappingField();
288 $dao->mapping_id
= $smartGroupId;
290 while ($dao->fetch()) {
291 $fldName = $dao->name
;
292 if ($dao->location_type_id
) {
293 $fldName .= "-{$dao->location_type_id}";
295 if ($dao->phone_type
) {
296 $fldName .= "-{$dao->phone_type}";
298 $returnFields[$fldName]['value'] = $dao->value
;
299 $returnFields[$fldName]['op'] = $dao->operator
;
300 $returnFields[$fldName]['grouping'] = $dao->grouping
;
302 return $returnFields;
306 * Build the mapping form for Search Builder.
308 * @param CRM_Core_Form $form
309 * @param int $mappingId
310 * @param int $columnNo
311 * @param int $blockCount
312 * (no of blocks shown).
313 * @param int $exportMode
315 public static function buildMappingForm(&$form, $mappingId, $columnNo, $blockCount, $exportMode = NULL) {
317 $hasLocationTypes = [];
318 $hasRelationTypes = [];
320 $columnCount = $columnNo;
321 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
322 ['class' => 'submit-link']
325 $contactTypes = CRM_Contact_BAO_ContactType
::basicTypes();
326 $fields = self
::getBasicFields('Search Builder');
328 // Unset groups, tags, notes for component export
329 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
330 foreach (array_keys($fields) as $type) {
331 CRM_Utils_Array
::remove($fields[$type], 'groups', 'tags', 'notes');
335 // Build the common contact fields array.
336 $fields['Contact'] = [];
337 foreach ($fields[$contactTypes[0]] as $key => $value) {
338 // If a field exists across all contact types, move it to the "Contact" selector
340 foreach ($contactTypes as $type) {
341 if (!isset($fields[$type][$key])) {
342 $ubiquitious = FALSE;
346 $fields['Contact'][$key] = $value;
347 foreach ($contactTypes as $type) {
348 unset($fields[$type][$key]);
352 if (array_key_exists('note', $fields['Contact'])) {
353 $noteTitle = $fields['Contact']['note']['title'];
354 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
355 $fields['Contact']['note_body'] = ['title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body'];
356 $fields['Contact']['note_subject'] = [
357 'title' => $noteTitle . ': ' . ts('Subject Only'),
358 'name' => 'note_subject',
362 // add component fields
363 $compArray = self
::addComponentFields($fields, 'Search Builder', $exportMode);
365 foreach ($fields as $key => $value) {
367 foreach ($value as $key1 => $value1) {
368 //CRM-2676, replacing the conflict for same custom field name from different custom group.
369 $customGroupName = self
::getCustomGroupName($key1);
371 if ($customGroupName) {
372 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
375 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
377 if (isset($value1['hasLocationType'])) {
378 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
381 if (isset($value1['hasRelationType'])) {
382 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
383 unset($relatedMapperFields[$key][$key1]);
387 if (isset($relatedMapperFields[$key]['related'])) {
388 unset($relatedMapperFields[$key]['related']);
392 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
394 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
396 // FIXME: dirty hack to make the default option show up first. This
397 // avoids a mozilla browser bug with defaults on dynamically constructed
399 if ($defaultLocationType) {
400 $defaultLocation = $locationTypes[$defaultLocationType->id
];
401 unset($locationTypes[$defaultLocationType->id
]);
402 $locationTypes = [$defaultLocationType->id
=> $defaultLocation] +
$locationTypes;
405 $locationTypes = [' ' => ts('Primary')] +
$locationTypes;
407 // since we need a hierarchical list to display contact types & subtypes,
408 // this is what we going to display in first selector
409 $contactTypeSelect = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
410 $contactTypeSelect = ['Contact' => ts('Contacts')] +
$contactTypeSelect;
412 $sel1 = ['' => ts('- select record type -')] +
$contactTypeSelect +
$compArray;
414 foreach ($sel1 as $key => $sel) {
416 // sort everything BUT the contactType which is sorted separately by
417 // an initial commit of CRM-13278 (check ksort above)
418 if (!in_array($key, $contactTypes)) {
419 asort($mapperFields[$key]);
421 $sel2[$key] = ['' => ts('- select field -')] +
$mapperFields[$key];
427 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
428 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
431 foreach ($sel1 as $k => $sel) {
433 foreach ($locationTypes as $key => $value) {
434 if (trim($key) != '') {
435 $sel4[$k]['phone'][$key] = &$phoneTypes;
436 $sel4[$k]['im'][$key] = &$imProviders;
442 foreach ($sel1 as $k => $sel) {
444 foreach ($mapperFields[$k] as $key => $value) {
445 if (isset($hasLocationTypes[$k][$key])) {
446 $sel3[$k][$key] = $locationTypes;
455 // Array for core fields and relationship custom data
456 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
458 //special fields that have location, hack for primary location
461 'supplemental_address_1',
462 'supplemental_address_2',
463 'supplemental_address_3',
466 'postal_code_suffix',
476 if (isset($mappingId)) {
477 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
478 $mappingRelation, $mappingOperator, $mappingValue
479 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
481 $blkCnt = count($mappingName);
482 if ($blkCnt >= $blockCount) {
483 $blockCount = $blkCnt +
1;
485 for ($x = 1; $x < $blockCount; $x++
) {
486 if (isset($mappingName[$x])) {
487 $colCnt = count($mappingName[$x]);
488 if ($colCnt >= $columnCount[$x]) {
489 $columnCount[$x] = $colCnt;
495 $form->_blockCount
= $blockCount;
496 $form->_columnCount
= $columnCount;
498 $form->set('blockCount', $form->_blockCount
);
499 $form->set('columnCount', $form->_columnCount
);
501 $defaults = $noneArray = $nullArray = [];
503 for ($x = 1; $x < $blockCount; $x++
) {
505 for ($i = 0; $i < $columnCount[$x]; $i++
) {
507 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
510 if (isset($mappingId)) {
511 list($mappingName, $defaults, $noneArray, $jsSet) = self
::loadSavedMapping($mappingLocation, $x, $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, $specialFields, $mappingPhoneType, $defaults, $noneArray, $mappingImProvider, $mappingOperator, $mappingValue);
513 //Fix for Search Builder
516 $formValues = $form->exportValues();
518 if (empty($formValues)) {
519 // Incremented length for third select box(relationship type)
520 for ($k = 1; $k < $j; $k++
) {
521 $noneArray[] = [$x, $i, $k];
525 if (!empty($formValues['mapper'][$x])) {
526 foreach ($formValues['mapper'][$x] as $value) {
527 for ($k = 1; $k < $j; $k++
) {
528 if (!isset($formValues['mapper'][$x][$i][$k]) ||
529 (!$formValues['mapper'][$x][$i][$k])
531 $noneArray[] = [$x, $i, $k];
534 $nullArray[] = [$x, $i, $k];
540 for ($k = 1; $k < $j; $k++
) {
541 $noneArray[] = [$x, $i, $k];
546 //Fix for Search Builder
547 $sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
549 //CRM -2292, restricted array set
550 $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues
::getSearchBuilderOperators();
552 $form->add('select', "operator[$x][$i]", '', $operatorArray);
553 $form->add('text', "value[$x][$i]", '');
556 $form->addElement('submit', "addMore[$x]", ts('Another search field'), ['class' => 'submit-link']);
560 $js = "<script type='text/javascript'>\n";
561 $formName = "document.Builder";
562 if (!empty($nullArray)) {
563 $js .= "var nullArray = [";
566 foreach ($nullArray as $element) {
567 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
568 if (!isset($seen[$key])) {
569 $elements[] = "[$key]";
573 $js .= implode(', ', $elements);
576 for (var i=0;i<nullArray.length;i++) {
577 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
578 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
583 if (!empty($noneArray)) {
584 $js .= "var noneArray = [";
587 foreach ($noneArray as $element) {
588 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
589 if (!isset($seen[$key])) {
590 $elements[] = "[$key]";
594 $js .= implode(', ', $elements);
597 for (var i=0;i<noneArray.length;i++) {
598 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
599 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
604 $js .= "</script>\n";
606 $form->assign('initHideBoxes', $js);
607 $form->assign('columnCount', $columnCount);
608 $form->assign('blockCount', $blockCount);
609 $form->setDefaults($defaults);
611 $form->setDefaultAction('refresh');
615 * @param string $mappingType
618 public static function getBasicFields($mappingType) {
619 $contactTypes = CRM_Contact_BAO_ContactType
::basicTypes();
621 foreach ($contactTypes as $contactType) {
622 if ($mappingType == 'Search Builder') {
623 // Get multiple custom group fields in this context
624 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($contactType, FALSE, FALSE, FALSE, TRUE);
627 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($contactType, FALSE, TRUE);
629 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
631 // Exclude the address options disabled in the Address Settings
632 $fields[$contactType] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
633 ksort($fields[$contactType]);
634 if ($mappingType == 'Export') {
636 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $contactType);
637 asort($relationshipTypes);
639 foreach ($relationshipTypes as $key => $var) {
640 list($type) = explode('_', $key);
642 $relationships[$key]['title'] = $var;
643 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
644 $relationships[$key]['export'] = TRUE;
645 $relationships[$key]['relationship_type_id'] = $type;
646 $relationships[$key]['related'] = TRUE;
647 $relationships[$key]['hasRelationType'] = 1;
650 if (!empty($relationships)) {
651 $fields[$contactType] = array_merge($fields[$contactType],
652 ['related' => ['title' => ts('- related contact info -')]],
659 // Get the current employer for mapping.
660 if ($mappingType == 'Export') {
661 $fields['Individual']['current_employer_id']['title'] = ts('Current Employer ID');
664 // Contact Sub Type For export
665 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
666 foreach ($subTypes as $subType => $info) {
667 //adding subtype specific relationships CRM-5256
668 $csRelationships = [];
670 if ($mappingType == 'Export') {
671 $subTypeRelationshipTypes
672 = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $info['parent'],
673 FALSE, 'label', TRUE, $subType);
675 foreach ($subTypeRelationshipTypes as $key => $var) {
676 if (!array_key_exists($key, $fields[$info['parent']])) {
677 list($type) = explode('_', $key);
679 $csRelationships[$key]['title'] = $var;
680 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
681 $csRelationships[$key]['export'] = TRUE;
682 $csRelationships[$key]['relationship_type_id'] = $type;
683 $csRelationships[$key]['related'] = TRUE;
684 $csRelationships[$key]['hasRelationType'] = 1;
689 $fields[$subType] = $fields[$info['parent']] +
$csRelationships;
691 //custom fields for sub type
692 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
693 $fields[$subType] +
= $subTypeFields;
700 * Adds component fields to the export fields array; returns list of components.
702 * @param array $fields
703 * @param string $mappingType
704 * @param int $exportMode
707 public static function addComponentFields(&$fields, $mappingType, $exportMode) {
710 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
711 if (CRM_Core_Permission
::access('CiviContribute')) {
712 $fields['Contribution'] = CRM_Core_DAO
::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution');
713 unset($fields['Contribution']['contribution_contact_id']);
714 $compArray['Contribution'] = ts('Contribution');
718 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
719 if (CRM_Core_Permission
::access('CiviEvent')) {
720 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
721 //get the component payment fields
722 // @todo - review this - inconsistent with other entities & hacky.
723 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
724 $componentPaymentFields = [];
726 'componentPaymentField_total_amount' => ts('Total Amount'),
727 'componentPaymentField_contribution_status' => ts('Contribution Status'),
728 'componentPaymentField_received_date' => ts('Date Received'),
729 'componentPaymentField_payment_instrument' => ts('Payment Method'),
730 'componentPaymentField_transaction_id' => ts('Transaction ID'),
731 ] as $payField => $payTitle) {
732 $componentPaymentFields[$payField] = ['title' => $payTitle];
734 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
737 $compArray['Participant'] = ts('Participant');
741 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
742 if (CRM_Core_Permission
::access('CiviMember')) {
743 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
744 unset($fields['Membership']['membership_contact_id']);
745 $compArray['Membership'] = ts('Membership');
749 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
750 if (CRM_Core_Permission
::access('CiviPledge')) {
751 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
752 unset($fields['Pledge']['pledge_contact_id']);
753 $compArray['Pledge'] = ts('Pledge');
757 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
758 if (CRM_Core_Permission
::access('CiviCase')) {
759 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
760 $compArray['Case'] = ts('Case');
762 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
763 $compArray['Activity'] = ts('Case Activity');
765 unset($fields['Case']['case_contact_id']);
768 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
769 if (CRM_Core_Permission
::access('CiviGrant')) {
770 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
771 unset($fields['Grant']['grant_contact_id']);
772 if ($mappingType == 'Search Builder') {
773 unset($fields['Grant']['grant_type_id']);
775 $compArray['Grant'] = ts('Grant');
779 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
780 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
781 $compArray['Activity'] = ts('Activity');
788 * Get the parameters for a mapping field in a saveable format from the quickform mapping format.
790 * @param array $defaults
795 public static function getMappingParams($defaults, $v) {
796 $locationTypeId = NULL;
797 $saveMappingFields = $defaults;
799 $saveMappingFields['name'] = $v['1'] ??
NULL;
800 $saveMappingFields['contact_type'] = $v['0'] ??
NULL;
801 $locationId = $v['2'] ??
NULL;
802 $saveMappingFields['location_type_id'] = is_numeric($locationId) ?
$locationId : NULL;
804 if ($v[1] == 'phone') {
805 $saveMappingFields['phone_type_id'] = $v['3'] ??
NULL;
807 elseif ($v[1] == 'im') {
808 $saveMappingFields['im_provider_id'] = $v['3'] ??
NULL;
811 // Handle mapping for 'related contact' fields
812 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
813 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
814 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
816 if (!empty($v['2'])) {
817 $saveMappingFields['name'] = $v['2'] ??
NULL;
819 elseif (!empty($v['4'])) {
820 $saveMappingFields['name'] = $v['4'] ??
NULL;
823 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
824 $locationTypeId = $v['3'] ??
NULL;
826 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
827 $locationTypeId = $v['5'] ??
NULL;
830 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
831 if ($saveMappingFields['name'] === 'im') {
832 $saveMappingFields['im_provider_id'] = $v[4];
835 $saveMappingFields['phone_type_id'] = $v['4'] ??
NULL;
838 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
839 $saveMappingFields['phone_type_id'] = $v['6'] ??
NULL;
842 $saveMappingFields['location_type_id'] = is_numeric($locationTypeId) ?
$locationTypeId : NULL;
843 $saveMappingFields['relationship_type_id'] = $id;
844 $saveMappingFields['relationship_direction'] = "{$first}_{$second}";
848 return $saveMappingFields;
852 * Load saved mapping.
854 * @param $mappingLocation
857 * @param $mappingName
858 * @param $mapperFields
859 * @param $mappingContactType
860 * @param $mappingRelation
861 * @param array $specialFields
862 * @param $mappingPhoneType
864 * @param array $defaults
865 * @param array $noneArray
867 * @param $mappingImProvider
868 * @param $mappingOperator
869 * @param $mappingValue
873 protected static function loadSavedMapping($mappingLocation, int $x, int $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, array $specialFields, $mappingPhoneType, array $defaults, array $noneArray, $mappingImProvider, $mappingOperator, $mappingValue) {
874 $locationId = $mappingLocation[$x][$i] ??
0;
875 if (isset($mappingName[$x][$i])) {
876 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
878 if (isset($mappingRelation[$x][$i])) {
879 $relLocationId = $mappingLocation[$x][$i] ??
0;
880 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
881 $relLocationId = " ";
884 $relPhoneType = $mappingPhoneType[$x][$i] ??
NULL;
886 $defaults["mapper[$x][$i]"] = [
887 $mappingContactType[$x][$i],
888 $mappingRelation[$x][$i],
891 $mappingName[$x][$i],
897 $noneArray[] = [$x, $i, 2];
899 if (!$phoneType && !$imProvider) {
900 $noneArray[] = [$x, $i, 3];
902 if (!$mappingName[$x][$i]) {
903 $noneArray[] = [$x, $i, 4];
905 if (!$relLocationId) {
906 $noneArray[] = [$x, $i, 5];
908 if (!$relPhoneType) {
909 $noneArray[] = [$x, $i, 6];
911 $noneArray[] = [$x, $i, 2];
914 $phoneType = $mappingPhoneType[$x][$i] ??
NULL;
915 $imProvider = $mappingImProvider[$x][$i] ??
NULL;
916 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
920 $defaults["mapper[$x][$i]"] = [
921 $mappingContactType[$x][$i],
922 $mappingName[$x][$i],
926 if (!$mappingName[$x][$i]) {
927 $noneArray[] = [$x, $i, 1];
930 $noneArray[] = [$x, $i, 2];
932 if (!$phoneType && !$imProvider) {
933 $noneArray[] = [$x, $i, 3];
936 $noneArray[] = [$x, $i, 4];
937 $noneArray[] = [$x, $i, 5];
938 $noneArray[] = [$x, $i, 6];
943 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
944 $defaults["operator[$x][$i]"] = $mappingOperator[$x][$i] ??
NULL;
947 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
948 $defaults["value[$x][$i]"] = $mappingValue[$x][$i] ??
NULL;
952 return [$mappingName, $defaults, $noneArray, $jsSet];
956 * Function returns all custom fields with group title and
959 * @param int $relationshipTypeId
960 * Related relationship type id.
963 * all custom field titles
965 public function getRelationTypeCustomGroupData($relationshipTypeId) {
967 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
969 foreach ($customFields as $krelation => $vrelation) {
970 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
976 * Function returns all Custom group Names.
978 * @param int $customfieldId
981 * @return null|string
982 * $customGroupName all custom group names
984 public static function getCustomGroupName($customfieldId) {
985 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
986 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
987 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
989 $customGroupName = CRM_Utils_String
::ellipsify($customGroupName, 13);
991 return $customGroupName;
996 * Function returns associated array of elements, that will be passed for search
998 * @param array $params
999 * Associated array of submitted values.
1001 * Row no of the fields.
1005 * formatted associated array of elements
1006 * @throws CRM_Core_Exception
1008 public static function formattedFields(&$params, $row = FALSE) {
1011 if (empty($params) ||
!isset($params['mapper'])) {
1015 $types = ['Individual', 'Organization', 'Household'];
1016 foreach ($params['mapper'] as $key => $value) {
1017 $contactType = NULL;
1018 foreach ($value as $k => $v) {
1019 if (in_array($v[0], $types)) {
1020 if ($contactType && $contactType != $v[0]) {
1021 throw new CRM_Core_Exception(ts("Cannot have two clauses with different types: %1, %2",
1022 [1 => $contactType, 2 => $v[0]]
1025 $contactType = $v[0];
1027 if (!empty($v['1'])) {
1029 $v2 = $v['2'] ??
NULL;
1030 if ($v2 && trim($v2)) {
1031 $fldName .= "-{$v[2]}";
1034 $v3 = $v['3'] ??
NULL;
1035 if ($v3 && trim($v3)) {
1036 $fldName .= "-{$v[3]}";
1039 $value = $params['value'][$key][$k];
1041 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1042 && substr($fldName, 0, 10) != 'financial_'
1043 && substr($fldName, 0, 8) != 'payment_') {
1044 if (substr($fldName, 0, 13) != 'contribution_') {
1045 $fldName = 'contribution_' . $fldName;
1049 // CRM-14983: verify if values are comma separated convert to array
1050 if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) {
1051 $value = explode(',', $value);
1052 $value = [$params['operator'][$key][$k] => $value];
1054 // CRM-19081 Fix legacy StateProvince Field Values.
1055 // These derive from smart groups created using search builder under older
1056 // CiviCRM versions.
1057 if (!is_numeric($value) && $fldName == 'state_province') {
1058 $value = CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'state_province_id', $value);
1064 $params['operator'][$key][$k],
1073 $params['operator'][$key][$k],
1092 //add sortByCharacter values
1093 if (isset($params['sortByCharacter'])) {
1097 $params['sortByCharacter'],
1106 * @param array $params
1110 public static function &returnProperties(&$params) {
1112 'contact_type' => 1,
1113 'contact_sub_type' => 1,
1117 if (empty($params) ||
empty($params['mapper'])) {
1121 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
1122 foreach ($params['mapper'] as $key => $value) {
1123 foreach ($value as $k => $v) {
1125 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1129 if (isset($v[2]) && is_numeric($v[2])) {
1130 if (!array_key_exists('location', $fields)) {
1131 $fields['location'] = [];
1134 // make sure that we have a location fields and a location type for this
1135 $locationName = $locationTypes[$v[2]];
1136 if (!array_key_exists($locationName, $fields['location'])) {
1137 $fields['location'][$locationName] = [];
1138 $fields['location'][$locationName]['location_type'] = $v[2];
1141 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1142 // phone type handling
1144 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1147 $fields['location'][$locationName][$v[1]] = 1;
1151 $fields['location'][$locationName][$v[1]] = 1;
1165 * Save the mapping field info for search builder / export given the formvalues
1167 * @param array $params
1168 * Asscociated array of formvalues.
1169 * @param int $mappingId
1174 public static function saveMappingFields($params, $mappingId) {
1175 //delete mapping fields records for existing mapping
1176 $mappingFields = new CRM_Core_DAO_MappingField();
1177 $mappingFields->mapping_id
= $mappingId;
1178 $mappingFields->delete();
1180 if (empty($params['mapper'])) {
1184 //save record in mapping field table
1185 foreach ($params['mapper'] as $key => $value) {
1187 foreach ($value as $k => $v) {
1189 if (!empty($v['1'])) {
1190 $saveMappingParams = self
::getMappingParams(
1192 'mapping_id' => $mappingId,
1194 'operator' => $params['operator'][$key][$k] ??
NULL,
1195 'value' => $params['value'][$key][$k] ??
NULL,
1196 'column_number' => $colCnt,
1198 $saveMappingField = new CRM_Core_DAO_MappingField();
1199 $saveMappingField->copyValues($saveMappingParams);
1200 $saveMappingField->save();