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) {
875 $locationId = $mappingLocation[$x][$i] ??
0;
876 if (isset($mappingName[$x][$i])) {
877 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
879 if (isset($mappingRelation[$x][$i])) {
880 $relLocationId = $mappingLocation[$x][$i] ??
0;
881 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
882 $relLocationId = " ";
885 $relPhoneType = $mappingPhoneType[$x][$i] ??
NULL;
887 $defaults["mapper[$x][$i]"] = [
888 $mappingContactType[$x][$i],
889 $mappingRelation[$x][$i],
892 $mappingName[$x][$i],
898 $noneArray[] = [$x, $i, 2];
900 if (!$phoneType && !$imProvider) {
901 $noneArray[] = [$x, $i, 3];
903 if (!$mappingName[$x][$i]) {
904 $noneArray[] = [$x, $i, 4];
906 if (!$relLocationId) {
907 $noneArray[] = [$x, $i, 5];
909 if (!$relPhoneType) {
910 $noneArray[] = [$x, $i, 6];
912 $noneArray[] = [$x, $i, 2];
915 $phoneType = $mappingPhoneType[$x][$i] ??
NULL;
916 $imProvider = $mappingImProvider[$x][$i] ??
NULL;
917 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
921 $defaults["mapper[$x][$i]"] = [
922 $mappingContactType[$x][$i],
923 $mappingName[$x][$i],
927 if (!$mappingName[$x][$i]) {
928 $noneArray[] = [$x, $i, 1];
931 $noneArray[] = [$x, $i, 2];
933 if (!$phoneType && !$imProvider) {
934 $noneArray[] = [$x, $i, 3];
937 $noneArray[] = [$x, $i, 4];
938 $noneArray[] = [$x, $i, 5];
939 $noneArray[] = [$x, $i, 6];
944 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
945 $defaults["operator[$x][$i]"] = $mappingOperator[$x][$i] ??
NULL;
948 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
949 $defaults["value[$x][$i]"] = $mappingValue[$x][$i] ??
NULL;
953 return [$mappingName, $defaults, $noneArray, $jsSet];
957 * Function returns all custom fields with group title and
960 * @param int $relationshipTypeId
961 * Related relationship type id.
964 * all custom field titles
966 public function getRelationTypeCustomGroupData($relationshipTypeId) {
968 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
970 foreach ($customFields as $krelation => $vrelation) {
971 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
977 * Function returns all Custom group Names.
979 * @param int $customfieldId
982 * @return null|string
983 * $customGroupName all custom group names
985 public static function getCustomGroupName($customfieldId) {
986 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
987 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
988 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
990 $customGroupName = CRM_Utils_String
::ellipsify($customGroupName, 13);
992 return $customGroupName;
997 * Function returns associated array of elements, that will be passed for search
999 * @param array $params
1000 * Associated array of submitted values.
1002 * Row no of the fields.
1006 * formatted associated array of elements
1007 * @throws CRM_Core_Exception
1009 public static function formattedFields(&$params, $row = FALSE) {
1012 if (empty($params) ||
!isset($params['mapper'])) {
1016 $types = ['Individual', 'Organization', 'Household'];
1017 foreach ($params['mapper'] as $key => $value) {
1018 $contactType = NULL;
1019 foreach ($value as $k => $v) {
1020 if (in_array($v[0], $types)) {
1021 if ($contactType && $contactType != $v[0]) {
1022 throw new CRM_Core_Exception(ts("Cannot have two clauses with different types: %1, %2",
1023 [1 => $contactType, 2 => $v[0]]
1026 $contactType = $v[0];
1028 if (!empty($v['1'])) {
1030 $v2 = $v['2'] ??
NULL;
1031 if ($v2 && trim($v2)) {
1032 $fldName .= "-{$v[2]}";
1035 $v3 = $v['3'] ??
NULL;
1036 if ($v3 && trim($v3)) {
1037 $fldName .= "-{$v[3]}";
1040 $value = $params['value'][$key][$k];
1042 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1043 && substr($fldName, 0, 10) != 'financial_'
1044 && substr($fldName, 0, 8) != 'payment_') {
1045 if (substr($fldName, 0, 13) != 'contribution_') {
1046 $fldName = 'contribution_' . $fldName;
1050 // CRM-14983: verify if values are comma separated convert to array
1051 if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) {
1052 $value = explode(',', $value);
1053 $value = [$params['operator'][$key][$k] => $value];
1055 // CRM-19081 Fix legacy StateProvince Field Values.
1056 // These derive from smart groups created using search builder under older
1057 // CiviCRM versions.
1058 if (!is_numeric($value) && $fldName == 'state_province') {
1059 $value = CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'state_province_id', $value);
1065 $params['operator'][$key][$k],
1074 $params['operator'][$key][$k],
1093 //add sortByCharacter values
1094 if (isset($params['sortByCharacter'])) {
1098 $params['sortByCharacter'],
1107 * @param array $params
1111 public static function &returnProperties(&$params) {
1113 'contact_type' => 1,
1114 'contact_sub_type' => 1,
1118 if (empty($params) ||
empty($params['mapper'])) {
1122 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
1123 foreach ($params['mapper'] as $key => $value) {
1124 foreach ($value as $k => $v) {
1126 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1130 if (isset($v[2]) && is_numeric($v[2])) {
1131 if (!array_key_exists('location', $fields)) {
1132 $fields['location'] = [];
1135 // make sure that we have a location fields and a location type for this
1136 $locationName = $locationTypes[$v[2]];
1137 if (!array_key_exists($locationName, $fields['location'])) {
1138 $fields['location'][$locationName] = [];
1139 $fields['location'][$locationName]['location_type'] = $v[2];
1142 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1143 // phone type handling
1145 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1148 $fields['location'][$locationName][$v[1]] = 1;
1152 $fields['location'][$locationName][$v[1]] = 1;
1166 * Save the mapping field info for search builder / export given the formvalues
1168 * @param array $params
1169 * Asscociated array of formvalues.
1170 * @param int $mappingId
1175 public static function saveMappingFields($params, $mappingId) {
1176 //delete mapping fields records for existing mapping
1177 $mappingFields = new CRM_Core_DAO_MappingField();
1178 $mappingFields->mapping_id
= $mappingId;
1179 $mappingFields->delete();
1181 if (empty($params['mapper'])) {
1185 //save record in mapping field table
1186 foreach ($params['mapper'] as $key => $value) {
1188 foreach ($value as $k => $v) {
1190 if (!empty($v['1'])) {
1191 $saveMappingParams = self
::getMappingParams(
1193 'mapping_id' => $mappingId,
1195 'operator' => $params['operator'][$key][$k] ??
NULL,
1196 'value' => $params['value'][$key][$k] ??
NULL,
1197 'column_number' => $colCnt,
1199 $saveMappingField = new CRM_Core_DAO_MappingField();
1200 $saveMappingField->copyValues($saveMappingParams);
1201 $saveMappingField->save();