From 57de041f697aba5adf807881b0bb61d2d0884ab8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 11 Aug 2022 12:30:47 +1200 Subject: [PATCH] Move non-shared function to private function on using class --- .toxic.json | 2 +- CRM/Contact/Form/Search/Builder.php | 315 ++++++++++++++++++++++++++- CRM/Core/BAO/Mapping.php | 317 ---------------------------- 3 files changed, 315 insertions(+), 319 deletions(-) diff --git a/.toxic.json b/.toxic.json index a2620d1316..d71db79921 100644 --- a/.toxic.json +++ b/.toxic.json @@ -34,7 +34,7 @@ "CRM_Core_BAO_ActionScheduleTest::setUp()": "toxicAlert", "CRM_Core_BAO_CustomField::formatCustomField()": "toxicAlert", "CRM_Core_BAO_CustomGroup::getTree()": "toxicAlert", - "CRM_Core_BAO_Mapping::buildMappingForm()": "toxicAlert", + "CRM_Contact_Form_Search_Builder::buildMappingForm()": "toxicAlert", "CRM_Core_BAO_UFGroup::buildProfile()": "toxicAlert", "CRM_Core_BAO_UFGroup::getValues()": "toxicAlert", "CRM_Core_I18n_SchemaStructure::widgets()": "toxicAlert", diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 312a01b41e..4046d48c65 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -113,7 +113,7 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { $mappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id'); } - CRM_Core_BAO_Mapping::buildMappingForm($this, $mappingId, $this->_columnCount, $this->_blockCount); + $this->buildMappingForm($this, $mappingId, $this->_columnCount, $this->_blockCount); parent::buildQuickForm(); } @@ -513,4 +513,317 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { return $val; } + /** + * Build the mapping form for Search Builder. + * + * @param CRM_Core_Form $form + * @param int $mappingId + * @param int $columnNo + * @param int $blockCount + * (no of blocks shown). + */ + private function buildMappingForm(&$form, $mappingId, $columnNo, $blockCount) { + + $hasLocationTypes = []; + $hasRelationTypes = []; + + $columnCount = $columnNo; + $form->addElement('xbutton', 'addBlock', ts('Also include contacts where'), + [ + 'type' => 'submit', + 'class' => 'submit-link', + 'value' => 1, + ] + ); + + $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); + $fields = CRM_Core_BAO_Mapping::getBasicFields('Search Builder'); + + // Unset groups, tags, notes for component export + foreach (array_keys($fields) as $type) { + CRM_Utils_Array::remove($fields[$type], 'groups', 'tags', 'notes'); + } + // Build the common contact fields array. + $fields['Contact'] = []; + foreach ($fields[$contactTypes[0]] as $key => $value) { + // If a field exists across all contact types, move it to the "Contact" selector + $ubiquitious = TRUE; + foreach ($contactTypes as $type) { + if (!isset($fields[$type][$key])) { + $ubiquitious = FALSE; + } + } + if ($ubiquitious) { + $fields['Contact'][$key] = $value; + foreach ($contactTypes as $type) { + unset($fields[$type][$key]); + } + } + } + if (array_key_exists('note', $fields['Contact'])) { + $noteTitle = $fields['Contact']['note']['title']; + $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); + $fields['Contact']['note_body'] = ['title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body']; + $fields['Contact']['note_subject'] = [ + 'title' => $noteTitle . ': ' . ts('Subject Only'), + 'name' => 'note_subject', + ]; + } + + // add component fields + $compArray = CRM_Core_BAO_Mapping::addComponentFields($fields, 'Search Builder', NULL); + + foreach ($fields as $key => $value) { + + foreach ($value as $key1 => $value1) { + //CRM-2676, replacing the conflict for same custom field name from different custom group. + $customGroupName = CRM_Core_BAO_Mapping::getCustomGroupName($key1); + + if ($customGroupName) { + $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; + } + else { + $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; + } + if (isset($value1['hasLocationType'])) { + $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; + } + + if (isset($value1['hasRelationType'])) { + $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; + unset($relatedMapperFields[$key][$key1]); + } + } + + if (isset($relatedMapperFields[$key]['related'])) { + unset($relatedMapperFields[$key]['related']); + } + } + + $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); + + $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); + + // FIXME: dirty hack to make the default option show up first. This + // avoids a mozilla browser bug with defaults on dynamically constructed + // selector widgets. + if ($defaultLocationType) { + $defaultLocation = $locationTypes[$defaultLocationType->id]; + unset($locationTypes[$defaultLocationType->id]); + $locationTypes = [$defaultLocationType->id => $defaultLocation] + $locationTypes; + } + + $locationTypes = [' ' => ts('Primary')] + $locationTypes; + + // since we need a hierarchical list to display contact types & subtypes, + // this is what we going to display in first selector + $contactTypeSelect = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); + $contactTypeSelect = ['Contact' => ts('Contacts')] + $contactTypeSelect; + + $sel1 = ['' => ts('- select record type -')] + $contactTypeSelect + $compArray; + + foreach ($sel1 as $key => $sel) { + if ($key) { + // sort everything BUT the contactType which is sorted separately by + // an initial commit of CRM-13278 (check ksort above) + if (!in_array($key, $contactTypes)) { + asort($mapperFields[$key]); + } + $sel2[$key] = ['' => ts('- select field -')] + $mapperFields[$key]; + } + } + + $sel3[''] = NULL; + $sel5[''] = NULL; + $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); + $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); + asort($phoneTypes); + + foreach ($sel1 as $k => $sel) { + if ($k) { + foreach ($locationTypes as $key => $value) { + if (trim($key) != '') { + $sel4[$k]['phone'][$key] = &$phoneTypes; + $sel4[$k]['im'][$key] = &$imProviders; + } + } + } + } + + foreach ($sel1 as $k => $sel) { + if ($k) { + foreach ($mapperFields[$k] as $key => $value) { + if (isset($hasLocationTypes[$k][$key])) { + $sel3[$k][$key] = $locationTypes; + } + else { + $sel3[$key] = NULL; + } + } + } + } + + // Array for core fields and relationship custom data + $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); + + //special fields that have location, hack for primary location + $specialFields = [ + 'street_address', + 'supplemental_address_1', + 'supplemental_address_2', + 'supplemental_address_3', + 'city', + 'postal_code', + 'postal_code_suffix', + 'geo_code_1', + 'geo_code_2', + 'state_province', + 'country', + 'phone', + 'email', + 'im', + ]; + + if (isset($mappingId)) { + list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, + $mappingRelation, $mappingOperator, $mappingValue + ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); + + $blkCnt = count($mappingName); + if ($blkCnt >= $blockCount) { + $blockCount = $blkCnt + 1; + } + for ($x = 1; $x < $blockCount; $x++) { + if (isset($mappingName[$x])) { + $colCnt = count($mappingName[$x]); + if ($colCnt >= $columnCount[$x]) { + $columnCount[$x] = $colCnt; + } + } + } + } + + $form->_blockCount = $blockCount; + $form->_columnCount = $columnCount; + + $form->set('blockCount', $form->_blockCount); + $form->set('columnCount', $form->_columnCount); + + $defaults = $noneArray = $nullArray = []; + + for ($x = 1; $x < $blockCount; $x++) { + + for ($i = 0; $i < $columnCount[$x]; $i++) { + + $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL); + $jsSet = FALSE; + + if (isset($mappingId)) { + list($mappingName, $defaults, $noneArray, $jsSet) = CRM_Core_BAO_Mapping::loadSavedMapping($mappingLocation, $x, $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, $specialFields, $mappingPhoneType, $defaults, $noneArray, $mappingImProvider, $mappingOperator, $mappingValue); + } + //Fix for Search Builder + $j = 4; + + $formValues = $form->exportValues(); + if (!$jsSet) { + if (empty($formValues)) { + // Incremented length for third select box(relationship type) + for ($k = 1; $k < $j; $k++) { + $noneArray[] = [$x, $i, $k]; + } + } + else { + if (!empty($formValues['mapper'][$x])) { + foreach ($formValues['mapper'][$x] as $value) { + for ($k = 1; $k < $j; $k++) { + if (!isset($formValues['mapper'][$x][$i][$k]) || + (!$formValues['mapper'][$x][$i][$k]) + ) { + $noneArray[] = [$x, $i, $k]; + } + else { + $nullArray[] = [$x, $i, $k]; + } + } + } + } + else { + for ($k = 1; $k < $j; $k++) { + $noneArray[] = [$x, $i, $k]; + } + } + } + } + //Fix for Search Builder + $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); + + //CRM -2292, restricted array set + $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues::getSearchBuilderOperators(); + + $form->add('select', "operator[$x][$i]", '', $operatorArray); + $form->add('text', "value[$x][$i]", ''); + } + + $form->addElement('xbutton', "addMore[$x]", ts('Another search field'), [ + 'type' => 'submit', + 'class' => 'submit-link', + 'value' => 1, + ]); + } + //end of block for + + $js = "\n"; + + $form->assign('initHideBoxes', $js); + $form->assign('columnCount', $columnCount); + $form->assign('blockCount', $blockCount); + $form->setDefaults($defaults); + + $form->setDefaultAction('refresh'); + } + } diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index ee1ade7d58..2878f9a7e3 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -279,323 +279,6 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping implements \Civi\Core\Ho return $returnFields; } - /** - * Build the mapping form for Search Builder. - * - * @param CRM_Core_Form $form - * @param int $mappingId - * @param int $columnNo - * @param int $blockCount - * (no of blocks shown). - * @param int $exportMode - */ - public static function buildMappingForm(&$form, $mappingId, $columnNo, $blockCount, $exportMode = NULL) { - - $hasLocationTypes = []; - $hasRelationTypes = []; - - $columnCount = $columnNo; - $form->addElement('xbutton', 'addBlock', ts('Also include contacts where'), - [ - 'type' => 'submit', - 'class' => 'submit-link', - 'value' => 1, - ] - ); - - $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); - $fields = self::getBasicFields('Search Builder'); - - // Unset groups, tags, notes for component export - if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) { - foreach (array_keys($fields) as $type) { - CRM_Utils_Array::remove($fields[$type], 'groups', 'tags', 'notes'); - } - } - - // Build the common contact fields array. - $fields['Contact'] = []; - foreach ($fields[$contactTypes[0]] as $key => $value) { - // If a field exists across all contact types, move it to the "Contact" selector - $ubiquitious = TRUE; - foreach ($contactTypes as $type) { - if (!isset($fields[$type][$key])) { - $ubiquitious = FALSE; - } - } - if ($ubiquitious) { - $fields['Contact'][$key] = $value; - foreach ($contactTypes as $type) { - unset($fields[$type][$key]); - } - } - } - if (array_key_exists('note', $fields['Contact'])) { - $noteTitle = $fields['Contact']['note']['title']; - $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); - $fields['Contact']['note_body'] = ['title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body']; - $fields['Contact']['note_subject'] = [ - 'title' => $noteTitle . ': ' . ts('Subject Only'), - 'name' => 'note_subject', - ]; - } - - // add component fields - $compArray = self::addComponentFields($fields, 'Search Builder', $exportMode); - - foreach ($fields as $key => $value) { - - foreach ($value as $key1 => $value1) { - //CRM-2676, replacing the conflict for same custom field name from different custom group. - $customGroupName = self::getCustomGroupName($key1); - - if ($customGroupName) { - $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; - } - else { - $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; - } - if (isset($value1['hasLocationType'])) { - $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; - } - - if (isset($value1['hasRelationType'])) { - $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; - unset($relatedMapperFields[$key][$key1]); - } - } - - if (isset($relatedMapperFields[$key]['related'])) { - unset($relatedMapperFields[$key]['related']); - } - } - - $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); - - $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); - - // FIXME: dirty hack to make the default option show up first. This - // avoids a mozilla browser bug with defaults on dynamically constructed - // selector widgets. - if ($defaultLocationType) { - $defaultLocation = $locationTypes[$defaultLocationType->id]; - unset($locationTypes[$defaultLocationType->id]); - $locationTypes = [$defaultLocationType->id => $defaultLocation] + $locationTypes; - } - - $locationTypes = [' ' => ts('Primary')] + $locationTypes; - - // since we need a hierarchical list to display contact types & subtypes, - // this is what we going to display in first selector - $contactTypeSelect = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); - $contactTypeSelect = ['Contact' => ts('Contacts')] + $contactTypeSelect; - - $sel1 = ['' => ts('- select record type -')] + $contactTypeSelect + $compArray; - - foreach ($sel1 as $key => $sel) { - if ($key) { - // sort everything BUT the contactType which is sorted separately by - // an initial commit of CRM-13278 (check ksort above) - if (!in_array($key, $contactTypes)) { - asort($mapperFields[$key]); - } - $sel2[$key] = ['' => ts('- select field -')] + $mapperFields[$key]; - } - } - - $sel3[''] = NULL; - $sel5[''] = NULL; - $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); - $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); - asort($phoneTypes); - - foreach ($sel1 as $k => $sel) { - if ($k) { - foreach ($locationTypes as $key => $value) { - if (trim($key) != '') { - $sel4[$k]['phone'][$key] = &$phoneTypes; - $sel4[$k]['im'][$key] = &$imProviders; - } - } - } - } - - foreach ($sel1 as $k => $sel) { - if ($k) { - foreach ($mapperFields[$k] as $key => $value) { - if (isset($hasLocationTypes[$k][$key])) { - $sel3[$k][$key] = $locationTypes; - } - else { - $sel3[$key] = NULL; - } - } - } - } - - // Array for core fields and relationship custom data - $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); - - //special fields that have location, hack for primary location - $specialFields = [ - 'street_address', - 'supplemental_address_1', - 'supplemental_address_2', - 'supplemental_address_3', - 'city', - 'postal_code', - 'postal_code_suffix', - 'geo_code_1', - 'geo_code_2', - 'state_province', - 'country', - 'phone', - 'email', - 'im', - ]; - - if (isset($mappingId)) { - list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, - $mappingRelation, $mappingOperator, $mappingValue - ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); - - $blkCnt = count($mappingName); - if ($blkCnt >= $blockCount) { - $blockCount = $blkCnt + 1; - } - for ($x = 1; $x < $blockCount; $x++) { - if (isset($mappingName[$x])) { - $colCnt = count($mappingName[$x]); - if ($colCnt >= $columnCount[$x]) { - $columnCount[$x] = $colCnt; - } - } - } - } - - $form->_blockCount = $blockCount; - $form->_columnCount = $columnCount; - - $form->set('blockCount', $form->_blockCount); - $form->set('columnCount', $form->_columnCount); - - $defaults = $noneArray = $nullArray = []; - - for ($x = 1; $x < $blockCount; $x++) { - - for ($i = 0; $i < $columnCount[$x]; $i++) { - - $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL); - $jsSet = FALSE; - - if (isset($mappingId)) { - list($mappingName, $defaults, $noneArray, $jsSet) = self::loadSavedMapping($mappingLocation, $x, $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, $specialFields, $mappingPhoneType, $defaults, $noneArray, $mappingImProvider, $mappingOperator, $mappingValue); - } - //Fix for Search Builder - $j = 4; - - $formValues = $form->exportValues(); - if (!$jsSet) { - if (empty($formValues)) { - // Incremented length for third select box(relationship type) - for ($k = 1; $k < $j; $k++) { - $noneArray[] = [$x, $i, $k]; - } - } - else { - if (!empty($formValues['mapper'][$x])) { - foreach ($formValues['mapper'][$x] as $value) { - for ($k = 1; $k < $j; $k++) { - if (!isset($formValues['mapper'][$x][$i][$k]) || - (!$formValues['mapper'][$x][$i][$k]) - ) { - $noneArray[] = [$x, $i, $k]; - } - else { - $nullArray[] = [$x, $i, $k]; - } - } - } - } - else { - for ($k = 1; $k < $j; $k++) { - $noneArray[] = [$x, $i, $k]; - } - } - } - } - //Fix for Search Builder - $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); - - //CRM -2292, restricted array set - $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues::getSearchBuilderOperators(); - - $form->add('select', "operator[$x][$i]", '', $operatorArray); - $form->add('text', "value[$x][$i]", ''); - } - - $form->addElement('xbutton', "addMore[$x]", ts('Another search field'), [ - 'type' => 'submit', - 'class' => 'submit-link', - 'value' => 1, - ]); - } - //end of block for - - $js = "\n"; - - $form->assign('initHideBoxes', $js); - $form->assign('columnCount', $columnCount); - $form->assign('blockCount', $blockCount); - $form->setDefaults($defaults); - - $form->setDefaultAction('refresh'); - } - /** * @param string $mappingType * @return array -- 2.25.1