| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { |
| 18 | |
| 19 | /** |
| 20 | * Class constructor. |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | parent::__construct(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Fetch object based on array of properties. |
| 28 | * |
| 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. |
| 33 | * |
| 34 | * @return object |
| 35 | * CRM_Core_DAO_Mapping object on success, otherwise NULL |
| 36 | */ |
| 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); |
| 42 | return $mapping; |
| 43 | } |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Delete the mapping. |
| 49 | * |
| 50 | * @param int $id |
| 51 | * Mapping id. |
| 52 | * |
| 53 | * @return bool |
| 54 | */ |
| 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(); |
| 60 | |
| 61 | // delete from mapping table |
| 62 | $mapping = new CRM_Core_DAO_Mapping(); |
| 63 | $mapping->id = $id; |
| 64 | if ($mapping->find(TRUE)) { |
| 65 | $result = $mapping->delete(); |
| 66 | return $result; |
| 67 | } |
| 68 | return FALSE; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Takes an associative array and creates a contact object. |
| 73 | * |
| 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 |
| 76 | * pairs |
| 77 | * |
| 78 | * @param array $params |
| 79 | * An array of name/value pairs. |
| 80 | * |
| 81 | * @return object |
| 82 | * CRM_Core_DAO_Mapper object on success, otherwise NULL |
| 83 | */ |
| 84 | public static function add($params) { |
| 85 | $mapping = new CRM_Core_DAO_Mapping(); |
| 86 | $mapping->copyValues($params); |
| 87 | $mapping->save(); |
| 88 | |
| 89 | return $mapping; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the list of mappings for a select or select2 element. |
| 94 | * |
| 95 | * @param string $mappingType |
| 96 | * Mapping type name. |
| 97 | * @param bool $select2 |
| 98 | * Format for select2 |
| 99 | * |
| 100 | * @return array |
| 101 | * Array of mapping names, keyed by id. |
| 102 | */ |
| 103 | public static function getMappings($mappingType, $select2 = FALSE) { |
| 104 | $result = civicrm_api3('Mapping', 'get', [ |
| 105 | 'mapping_type_id' => $mappingType, |
| 106 | 'return' => ['name', 'description'], |
| 107 | 'options' => [ |
| 108 | 'sort' => 'name', |
| 109 | 'limit' => 0, |
| 110 | ], |
| 111 | ]); |
| 112 | $mapping = []; |
| 113 | |
| 114 | foreach ($result['values'] as $id => $value) { |
| 115 | if ($select2) { |
| 116 | $item = ['id' => $id, 'text' => $value['name']]; |
| 117 | if (!empty($value['description'])) { |
| 118 | $item['description'] = $value['description']; |
| 119 | } |
| 120 | $mapping[] = $item; |
| 121 | } |
| 122 | else { |
| 123 | $mapping[$id] = $value['name']; |
| 124 | } |
| 125 | } |
| 126 | return $mapping; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the mappings array, creating if it does not exist. |
| 131 | * |
| 132 | * @param string $mappingType |
| 133 | * Mapping type name. |
| 134 | * |
| 135 | * @return array |
| 136 | * Array of mapping names, keyed by id. |
| 137 | * |
| 138 | * @throws \CiviCRM_API3_Exception |
| 139 | */ |
| 140 | public static function getCreateMappingValues($mappingType) { |
| 141 | try { |
| 142 | return CRM_Core_BAO_Mapping::getMappings($mappingType); |
| 143 | } |
| 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, |
| 155 | 'is_reserved' => 1, |
| 156 | ]); |
| 157 | return CRM_Core_BAO_Mapping::getMappings($mappingType); |
| 158 | } |
| 159 | throw $e; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the mapping fields. |
| 165 | * |
| 166 | * @param int $mappingId |
| 167 | * Mapping id. |
| 168 | * |
| 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)? |
| 172 | * |
| 173 | * @return array |
| 174 | * array of mapping fields |
| 175 | */ |
| 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'); |
| 181 | $mapping->find(); |
| 182 | |
| 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; |
| 188 | |
| 189 | if (!empty($mapping->location_type_id)) { |
| 190 | $mappingLocation[$mapping->grouping][$mapping->column_number] = $mapping->location_type_id; |
| 191 | } |
| 192 | elseif ($addPrimary) { |
| 193 | if (CRM_Contact_BAO_Contact::isFieldHasLocationType($mapping->name)) { |
| 194 | $mappingLocation[$mapping->grouping][$mapping->column_number] = ts('Primary'); |
| 195 | } |
| 196 | else { |
| 197 | $mappingLocation[$mapping->grouping][$mapping->column_number] = NULL; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (!empty($mapping->phone_type_id)) { |
| 202 | $mappingPhoneType[$mapping->grouping][$mapping->column_number] = $mapping->phone_type_id; |
| 203 | } |
| 204 | |
| 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; |
| 208 | } |
| 209 | |
| 210 | if (!empty($mapping->website_type_id)) { |
| 211 | $mappingWebsiteType[$mapping->grouping][$mapping->column_number] = $mapping->website_type_id; |
| 212 | } |
| 213 | |
| 214 | if (!empty($mapping->relationship_type_id)) { |
| 215 | $mappingRelation[$mapping->grouping][$mapping->column_number] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}"; |
| 216 | } |
| 217 | |
| 218 | if (!empty($mapping->operator)) { |
| 219 | $mappingOperator[$mapping->grouping][$mapping->column_number] = $mapping->operator; |
| 220 | } |
| 221 | |
| 222 | if (!empty($mapping->value)) { |
| 223 | $mappingValue[$mapping->grouping][$mapping->column_number] = $mapping->value; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return [ |
| 228 | $mappingName, |
| 229 | $mappingContactType, |
| 230 | $mappingLocation, |
| 231 | $mappingPhoneType, |
| 232 | $mappingImProvider, |
| 233 | $mappingRelation, |
| 234 | $mappingOperator, |
| 235 | $mappingValue, |
| 236 | $mappingWebsiteType, |
| 237 | ]; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get un-indexed array of the field values for the given mapping id. |
| 242 | * |
| 243 | * For example if passing a mapping ID & name the returned array would look like |
| 244 | * ['First field name', 'second field name'] |
| 245 | * |
| 246 | * @param int $mappingID |
| 247 | * @param string $fieldName |
| 248 | * |
| 249 | * @return array |
| 250 | * @throws \CiviCRM_API3_Exception |
| 251 | */ |
| 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'])); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Check Duplicate Mapping Name. |
| 258 | * |
| 259 | * @param string $nameField |
| 260 | * mapping Name. |
| 261 | * @param string $mapTypeId |
| 262 | * mapping Type. |
| 263 | * |
| 264 | * @return bool |
| 265 | */ |
| 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); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Function returns associated array of elements, that will be passed for search. |
| 275 | * |
| 276 | * @param int $smartGroupId |
| 277 | * Smart group id. |
| 278 | * |
| 279 | * @return array |
| 280 | * associated array of elements |
| 281 | */ |
| 282 | public static function getFormattedFields($smartGroupId) { |
| 283 | $returnFields = []; |
| 284 | |
| 285 | //get the fields from mapping table |
| 286 | $dao = new CRM_Core_DAO_MappingField(); |
| 287 | $dao->mapping_id = $smartGroupId; |
| 288 | $dao->find(); |
| 289 | while ($dao->fetch()) { |
| 290 | $fldName = $dao->name; |
| 291 | if ($dao->location_type_id) { |
| 292 | $fldName .= "-{$dao->location_type_id}"; |
| 293 | } |
| 294 | if ($dao->phone_type) { |
| 295 | $fldName .= "-{$dao->phone_type}"; |
| 296 | } |
| 297 | $returnFields[$fldName]['value'] = $dao->value; |
| 298 | $returnFields[$fldName]['op'] = $dao->operator; |
| 299 | $returnFields[$fldName]['grouping'] = $dao->grouping; |
| 300 | } |
| 301 | return $returnFields; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Build the mapping form for Search Builder. |
| 306 | * |
| 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 |
| 313 | */ |
| 314 | public static function buildMappingForm(&$form, $mappingId, $columnNo, $blockCount, $exportMode = NULL) { |
| 315 | |
| 316 | $hasLocationTypes = []; |
| 317 | $hasRelationTypes = []; |
| 318 | |
| 319 | $columnCount = $columnNo; |
| 320 | $form->addElement('submit', 'addBlock', ts('Also include contacts where'), |
| 321 | ['class' => 'submit-link'] |
| 322 | ); |
| 323 | |
| 324 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); |
| 325 | $fields = self::getBasicFields('Search Builder'); |
| 326 | |
| 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'); |
| 331 | } |
| 332 | } |
| 333 | |
| 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 |
| 338 | $ubiquitious = TRUE; |
| 339 | foreach ($contactTypes as $type) { |
| 340 | if (!isset($fields[$type][$key])) { |
| 341 | $ubiquitious = FALSE; |
| 342 | } |
| 343 | } |
| 344 | if ($ubiquitious) { |
| 345 | $fields['Contact'][$key] = $value; |
| 346 | foreach ($contactTypes as $type) { |
| 347 | unset($fields[$type][$key]); |
| 348 | } |
| 349 | } |
| 350 | } |
| 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', |
| 358 | ]; |
| 359 | } |
| 360 | |
| 361 | // add component fields |
| 362 | $compArray = self::addComponentFields($fields, 'Search Builder', $exportMode); |
| 363 | |
| 364 | foreach ($fields as $key => $value) { |
| 365 | |
| 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); |
| 369 | |
| 370 | if ($customGroupName) { |
| 371 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; |
| 372 | } |
| 373 | else { |
| 374 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; |
| 375 | } |
| 376 | if (isset($value1['hasLocationType'])) { |
| 377 | $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; |
| 378 | } |
| 379 | |
| 380 | if (isset($value1['hasRelationType'])) { |
| 381 | $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; |
| 382 | unset($relatedMapperFields[$key][$key1]); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if (isset($relatedMapperFields[$key]['related'])) { |
| 387 | unset($relatedMapperFields[$key]['related']); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 392 | |
| 393 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
| 394 | |
| 395 | // FIXME: dirty hack to make the default option show up first. This |
| 396 | // avoids a mozilla browser bug with defaults on dynamically constructed |
| 397 | // selector widgets. |
| 398 | if ($defaultLocationType) { |
| 399 | $defaultLocation = $locationTypes[$defaultLocationType->id]; |
| 400 | unset($locationTypes[$defaultLocationType->id]); |
| 401 | $locationTypes = [$defaultLocationType->id => $defaultLocation] + $locationTypes; |
| 402 | } |
| 403 | |
| 404 | $locationTypes = [' ' => ts('Primary')] + $locationTypes; |
| 405 | |
| 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; |
| 410 | |
| 411 | $sel1 = ['' => ts('- select record type -')] + $contactTypeSelect + $compArray; |
| 412 | |
| 413 | foreach ($sel1 as $key => $sel) { |
| 414 | if ($key) { |
| 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]); |
| 419 | } |
| 420 | $sel2[$key] = ['' => ts('- select field -')] + $mapperFields[$key]; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | $sel3[''] = NULL; |
| 425 | $sel5[''] = NULL; |
| 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'); |
| 428 | asort($phoneTypes); |
| 429 | |
| 430 | foreach ($sel1 as $k => $sel) { |
| 431 | if ($k) { |
| 432 | foreach ($locationTypes as $key => $value) { |
| 433 | if (trim($key) != '') { |
| 434 | $sel4[$k]['phone'][$key] = &$phoneTypes; |
| 435 | $sel4[$k]['im'][$key] = &$imProviders; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | foreach ($sel1 as $k => $sel) { |
| 442 | if ($k) { |
| 443 | foreach ($mapperFields[$k] as $key => $value) { |
| 444 | if (isset($hasLocationTypes[$k][$key])) { |
| 445 | $sel3[$k][$key] = $locationTypes; |
| 446 | } |
| 447 | else { |
| 448 | $sel3[$key] = NULL; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // Array for core fields and relationship custom data |
| 455 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); |
| 456 | |
| 457 | //special fields that have location, hack for primary location |
| 458 | $specialFields = [ |
| 459 | 'street_address', |
| 460 | 'supplemental_address_1', |
| 461 | 'supplemental_address_2', |
| 462 | 'supplemental_address_3', |
| 463 | 'city', |
| 464 | 'postal_code', |
| 465 | 'postal_code_suffix', |
| 466 | 'geo_code_1', |
| 467 | 'geo_code_2', |
| 468 | 'state_province', |
| 469 | 'country', |
| 470 | 'phone', |
| 471 | 'email', |
| 472 | 'im', |
| 473 | ]; |
| 474 | |
| 475 | if (isset($mappingId)) { |
| 476 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, |
| 477 | $mappingRelation, $mappingOperator, $mappingValue |
| 478 | ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); |
| 479 | |
| 480 | $blkCnt = count($mappingName); |
| 481 | if ($blkCnt >= $blockCount) { |
| 482 | $blockCount = $blkCnt + 1; |
| 483 | } |
| 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; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | $form->_blockCount = $blockCount; |
| 495 | $form->_columnCount = $columnCount; |
| 496 | |
| 497 | $form->set('blockCount', $form->_blockCount); |
| 498 | $form->set('columnCount', $form->_columnCount); |
| 499 | |
| 500 | $defaults = $noneArray = $nullArray = []; |
| 501 | |
| 502 | for ($x = 1; $x < $blockCount; $x++) { |
| 503 | |
| 504 | for ($i = 0; $i < $columnCount[$x]; $i++) { |
| 505 | |
| 506 | $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL); |
| 507 | $jsSet = FALSE; |
| 508 | |
| 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); |
| 511 | } |
| 512 | //Fix for Search Builder |
| 513 | $j = 4; |
| 514 | |
| 515 | $formValues = $form->exportValues(); |
| 516 | if (!$jsSet) { |
| 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]; |
| 521 | } |
| 522 | } |
| 523 | else { |
| 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]) |
| 529 | ) { |
| 530 | $noneArray[] = [$x, $i, $k]; |
| 531 | } |
| 532 | else { |
| 533 | $nullArray[] = [$x, $i, $k]; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | else { |
| 539 | for ($k = 1; $k < $j; $k++) { |
| 540 | $noneArray[] = [$x, $i, $k]; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | //Fix for Search Builder |
| 546 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); |
| 547 | |
| 548 | //CRM -2292, restricted array set |
| 549 | $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues::getSearchBuilderOperators(); |
| 550 | |
| 551 | $form->add('select', "operator[$x][$i]", '', $operatorArray); |
| 552 | $form->add('text', "value[$x][$i]", ''); |
| 553 | } |
| 554 | |
| 555 | $form->addElement('submit', "addMore[$x]", ts('Another search field'), ['class' => 'submit-link']); |
| 556 | } |
| 557 | //end of block for |
| 558 | |
| 559 | $js = "<script type='text/javascript'>\n"; |
| 560 | $formName = "document.Builder"; |
| 561 | if (!empty($nullArray)) { |
| 562 | $js .= "var nullArray = ["; |
| 563 | $elements = []; |
| 564 | $seen = []; |
| 565 | foreach ($nullArray as $element) { |
| 566 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; |
| 567 | if (!isset($seen[$key])) { |
| 568 | $elements[] = "[$key]"; |
| 569 | $seen[$key] = 1; |
| 570 | } |
| 571 | } |
| 572 | $js .= implode(', ', $elements); |
| 573 | $js .= "]"; |
| 574 | $js .= " |
| 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 = ''; |
| 578 | } |
| 579 | } |
| 580 | "; |
| 581 | } |
| 582 | if (!empty($noneArray)) { |
| 583 | $js .= "var noneArray = ["; |
| 584 | $elements = []; |
| 585 | $seen = []; |
| 586 | foreach ($noneArray as $element) { |
| 587 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; |
| 588 | if (!isset($seen[$key])) { |
| 589 | $elements[] = "[$key]"; |
| 590 | $seen[$key] = 1; |
| 591 | } |
| 592 | } |
| 593 | $js .= implode(', ', $elements); |
| 594 | $js .= "]"; |
| 595 | $js .= " |
| 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'; |
| 599 | } |
| 600 | } |
| 601 | "; |
| 602 | } |
| 603 | $js .= "</script>\n"; |
| 604 | |
| 605 | $form->assign('initHideBoxes', $js); |
| 606 | $form->assign('columnCount', $columnCount); |
| 607 | $form->assign('blockCount', $blockCount); |
| 608 | $form->setDefaults($defaults); |
| 609 | |
| 610 | $form->setDefaultAction('refresh'); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * @param string $mappingType |
| 615 | * @return array |
| 616 | */ |
| 617 | public static function getBasicFields($mappingType) { |
| 618 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); |
| 619 | $fields = []; |
| 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); |
| 624 | } |
| 625 | else { |
| 626 | $contactFields = CRM_Contact_BAO_Contact::exportableFields($contactType, FALSE, TRUE); |
| 627 | } |
| 628 | $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook::singleton()->getFields()); |
| 629 | |
| 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') { |
| 634 | $relationships = []; |
| 635 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $contactType); |
| 636 | asort($relationshipTypes); |
| 637 | |
| 638 | foreach ($relationshipTypes as $key => $var) { |
| 639 | list($type) = explode('_', $key); |
| 640 | |
| 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; |
| 647 | } |
| 648 | |
| 649 | if (!empty($relationships)) { |
| 650 | $fields[$contactType] = array_merge($fields[$contactType], |
| 651 | ['related' => ['title' => ts('- related contact info -')]], |
| 652 | $relationships |
| 653 | ); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // Get the current employer for mapping. |
| 659 | if ($mappingType == 'Export') { |
| 660 | $fields['Individual']['current_employer_id']['title'] = ts('Current Employer ID'); |
| 661 | } |
| 662 | |
| 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 = []; |
| 668 | |
| 669 | if ($mappingType == 'Export') { |
| 670 | $subTypeRelationshipTypes |
| 671 | = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $info['parent'], |
| 672 | FALSE, 'label', TRUE, $subType); |
| 673 | |
| 674 | foreach ($subTypeRelationshipTypes as $key => $var) { |
| 675 | if (!array_key_exists($key, $fields[$info['parent']])) { |
| 676 | list($type) = explode('_', $key); |
| 677 | |
| 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; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | $fields[$subType] = $fields[$info['parent']] + $csRelationships; |
| 689 | |
| 690 | //custom fields for sub type |
| 691 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType); |
| 692 | $fields[$subType] += $subTypeFields; |
| 693 | } |
| 694 | |
| 695 | return $fields; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Adds component fields to the export fields array; returns list of components. |
| 700 | * |
| 701 | * @param array $fields |
| 702 | * @param string $mappingType |
| 703 | * @param int $exportMode |
| 704 | * @return array |
| 705 | */ |
| 706 | public static function addComponentFields(&$fields, $mappingType, $exportMode) { |
| 707 | $compArray = []; |
| 708 | |
| 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'); |
| 714 | } |
| 715 | } |
| 716 | |
| 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 = []; |
| 724 | foreach ([ |
| 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]; |
| 732 | } |
| 733 | $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields); |
| 734 | } |
| 735 | |
| 736 | $compArray['Participant'] = ts('Participant'); |
| 737 | } |
| 738 | } |
| 739 | |
| 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'); |
| 745 | } |
| 746 | } |
| 747 | |
| 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'); |
| 753 | } |
| 754 | } |
| 755 | |
| 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'); |
| 760 | |
| 761 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Case'); |
| 762 | $compArray['Activity'] = ts('Case Activity'); |
| 763 | |
| 764 | unset($fields['Case']['case_contact_id']); |
| 765 | } |
| 766 | } |
| 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']); |
| 773 | } |
| 774 | $compArray['Grant'] = ts('Grant'); |
| 775 | } |
| 776 | } |
| 777 | |
| 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'); |
| 781 | } |
| 782 | |
| 783 | return $compArray; |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * Get the parameters for a mapping field in a saveable format from the quickform mapping format. |
| 788 | * |
| 789 | * @param array $defaults |
| 790 | * @param array $v |
| 791 | * |
| 792 | * @return array |
| 793 | */ |
| 794 | public static function getMappingParams($defaults, $v) { |
| 795 | $locationTypeId = NULL; |
| 796 | $saveMappingFields = $defaults; |
| 797 | |
| 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; |
| 802 | |
| 803 | if ($v[1] == 'phone') { |
| 804 | $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('3', $v); |
| 805 | } |
| 806 | elseif ($v[1] == 'im') { |
| 807 | $saveMappingFields['im_provider_id'] = CRM_Utils_Array::value('3', $v); |
| 808 | } |
| 809 | |
| 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')) { |
| 814 | |
| 815 | if (!empty($v['2'])) { |
| 816 | $saveMappingFields['name'] = CRM_Utils_Array::value('2', $v); |
| 817 | } |
| 818 | elseif (!empty($v['4'])) { |
| 819 | $saveMappingFields['name'] = CRM_Utils_Array::value('4', $v); |
| 820 | } |
| 821 | |
| 822 | if (is_numeric(CRM_Utils_Array::value('3', $v))) { |
| 823 | $locationTypeId = CRM_Utils_Array::value('3', $v); |
| 824 | } |
| 825 | elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { |
| 826 | $locationTypeId = CRM_Utils_Array::value('5', $v); |
| 827 | } |
| 828 | |
| 829 | if (is_numeric(CRM_Utils_Array::value('4', $v))) { |
| 830 | if ($saveMappingFields['name'] === 'im') { |
| 831 | $saveMappingFields['im_provider_id'] = $v[4]; |
| 832 | } |
| 833 | else { |
| 834 | $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('4', $v); |
| 835 | } |
| 836 | } |
| 837 | elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { |
| 838 | $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('6', $v); |
| 839 | } |
| 840 | |
| 841 | $saveMappingFields['location_type_id'] = is_numeric($locationTypeId) ? $locationTypeId : NULL; |
| 842 | $saveMappingFields['relationship_type_id'] = $id; |
| 843 | $saveMappingFields['relationship_direction'] = "{$first}_{$second}"; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return $saveMappingFields; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Load saved mapping. |
| 852 | * |
| 853 | * @param $mappingLocation |
| 854 | * @param int $x |
| 855 | * @param int $i |
| 856 | * @param $mappingName |
| 857 | * @param $mapperFields |
| 858 | * @param $mappingContactType |
| 859 | * @param $mappingRelation |
| 860 | * @param array $specialFields |
| 861 | * @param $mappingPhoneType |
| 862 | * @param $phoneType |
| 863 | * @param array $defaults |
| 864 | * @param array $noneArray |
| 865 | * @param $imProvider |
| 866 | * @param $mappingImProvider |
| 867 | * @param $mappingOperator |
| 868 | * @param $mappingValue |
| 869 | * |
| 870 | * @return array |
| 871 | */ |
| 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]])) { |
| 876 | |
| 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 = " "; |
| 881 | } |
| 882 | |
| 883 | $relPhoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; |
| 884 | |
| 885 | $defaults["mapper[$x][$i]"] = [ |
| 886 | $mappingContactType[$x][$i], |
| 887 | $mappingRelation[$x][$i], |
| 888 | $locationId, |
| 889 | $phoneType, |
| 890 | $mappingName[$x][$i], |
| 891 | $relLocationId, |
| 892 | $relPhoneType, |
| 893 | ]; |
| 894 | |
| 895 | if (!$locationId) { |
| 896 | $noneArray[] = [$x, $i, 2]; |
| 897 | } |
| 898 | if (!$phoneType && !$imProvider) { |
| 899 | $noneArray[] = [$x, $i, 3]; |
| 900 | } |
| 901 | if (!$mappingName[$x][$i]) { |
| 902 | $noneArray[] = [$x, $i, 4]; |
| 903 | } |
| 904 | if (!$relLocationId) { |
| 905 | $noneArray[] = [$x, $i, 5]; |
| 906 | } |
| 907 | if (!$relPhoneType) { |
| 908 | $noneArray[] = [$x, $i, 6]; |
| 909 | } |
| 910 | $noneArray[] = [$x, $i, 2]; |
| 911 | } |
| 912 | else { |
| 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)) { |
| 916 | $locationId = " "; |
| 917 | } |
| 918 | |
| 919 | $defaults["mapper[$x][$i]"] = [ |
| 920 | $mappingContactType[$x][$i], |
| 921 | $mappingName[$x][$i], |
| 922 | $locationId, |
| 923 | $phoneType, |
| 924 | ]; |
| 925 | if (!$mappingName[$x][$i]) { |
| 926 | $noneArray[] = [$x, $i, 1]; |
| 927 | } |
| 928 | if (!$locationId) { |
| 929 | $noneArray[] = [$x, $i, 2]; |
| 930 | } |
| 931 | if (!$phoneType && !$imProvider) { |
| 932 | $noneArray[] = [$x, $i, 3]; |
| 933 | } |
| 934 | |
| 935 | $noneArray[] = [$x, $i, 4]; |
| 936 | $noneArray[] = [$x, $i, 5]; |
| 937 | $noneArray[] = [$x, $i, 6]; |
| 938 | } |
| 939 | |
| 940 | $jsSet = TRUE; |
| 941 | |
| 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]); |
| 944 | } |
| 945 | |
| 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]); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | return [$mappingName, $defaults, $noneArray, $jsSet]; |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * Function returns all custom fields with group title and |
| 956 | * field label |
| 957 | * |
| 958 | * @param int $relationshipTypeId |
| 959 | * Related relationship type id. |
| 960 | * |
| 961 | * @return array |
| 962 | * all custom field titles |
| 963 | */ |
| 964 | public function getRelationTypeCustomGroupData($relationshipTypeId) { |
| 965 | |
| 966 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL); |
| 967 | $groupTitle = []; |
| 968 | foreach ($customFields as $krelation => $vrelation) { |
| 969 | $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label']; |
| 970 | } |
| 971 | return $groupTitle; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Function returns all Custom group Names. |
| 976 | * |
| 977 | * @param int $customfieldId |
| 978 | * Related file id. |
| 979 | * |
| 980 | * @return null|string |
| 981 | * $customGroupName all custom group names |
| 982 | */ |
| 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'); |
| 987 | |
| 988 | $customGroupName = CRM_Utils_String::ellipsify($customGroupName, 13); |
| 989 | |
| 990 | return $customGroupName; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Function returns associated array of elements, that will be passed for search |
| 996 | * |
| 997 | * @param array $params |
| 998 | * Associated array of submitted values. |
| 999 | * @param bool $row |
| 1000 | * Row no of the fields. |
| 1001 | * |
| 1002 | * |
| 1003 | * @return array |
| 1004 | * formatted associated array of elements |
| 1005 | */ |
| 1006 | public static function formattedFields(&$params, $row = FALSE) { |
| 1007 | $fields = []; |
| 1008 | |
| 1009 | if (empty($params) || !isset($params['mapper'])) { |
| 1010 | return $fields; |
| 1011 | } |
| 1012 | |
| 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]] |
| 1021 | )); |
| 1022 | } |
| 1023 | $contactType = $v[0]; |
| 1024 | } |
| 1025 | if (!empty($v['1'])) { |
| 1026 | $fldName = $v[1]; |
| 1027 | $v2 = CRM_Utils_Array::value('2', $v); |
| 1028 | if ($v2 && trim($v2)) { |
| 1029 | $fldName .= "-{$v[2]}"; |
| 1030 | } |
| 1031 | |
| 1032 | $v3 = CRM_Utils_Array::value('3', $v); |
| 1033 | if ($v3 && trim($v3)) { |
| 1034 | $fldName .= "-{$v[3]}"; |
| 1035 | } |
| 1036 | |
| 1037 | $value = $params['value'][$key][$k]; |
| 1038 | |
| 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; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 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]; |
| 1051 | } |
| 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); |
| 1057 | } |
| 1058 | |
| 1059 | if ($row) { |
| 1060 | $fields[] = [ |
| 1061 | $fldName, |
| 1062 | $params['operator'][$key][$k], |
| 1063 | $value, |
| 1064 | $key, |
| 1065 | $k, |
| 1066 | ]; |
| 1067 | } |
| 1068 | else { |
| 1069 | $fields[] = [ |
| 1070 | $fldName, |
| 1071 | $params['operator'][$key][$k], |
| 1072 | $value, |
| 1073 | $key, |
| 1074 | 0, |
| 1075 | ]; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | if ($contactType) { |
| 1080 | $fields[] = [ |
| 1081 | 'contact_type', |
| 1082 | '=', |
| 1083 | $contactType, |
| 1084 | $key, |
| 1085 | 0, |
| 1086 | ]; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | //add sortByCharacter values |
| 1091 | if (isset($params['sortByCharacter'])) { |
| 1092 | $fields[] = [ |
| 1093 | 'sortByCharacter', |
| 1094 | '=', |
| 1095 | $params['sortByCharacter'], |
| 1096 | 0, |
| 1097 | 0, |
| 1098 | ]; |
| 1099 | } |
| 1100 | return $fields; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * @param array $params |
| 1105 | * |
| 1106 | * @return array |
| 1107 | */ |
| 1108 | public static function &returnProperties(&$params) { |
| 1109 | $fields = [ |
| 1110 | 'contact_type' => 1, |
| 1111 | 'contact_sub_type' => 1, |
| 1112 | 'sort_name' => 1, |
| 1113 | ]; |
| 1114 | |
| 1115 | if (empty($params) || empty($params['mapper'])) { |
| 1116 | return $fields; |
| 1117 | } |
| 1118 | |
| 1119 | $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); |
| 1120 | foreach ($params['mapper'] as $key => $value) { |
| 1121 | foreach ($value as $k => $v) { |
| 1122 | if (isset($v[1])) { |
| 1123 | if ($v[1] == 'groups' || $v[1] == 'tags') { |
| 1124 | continue; |
| 1125 | } |
| 1126 | |
| 1127 | if (isset($v[2]) && is_numeric($v[2])) { |
| 1128 | if (!array_key_exists('location', $fields)) { |
| 1129 | $fields['location'] = []; |
| 1130 | } |
| 1131 | |
| 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]; |
| 1137 | } |
| 1138 | |
| 1139 | if ($v[1] == 'phone' || $v[1] == 'email' || $v[1] == 'im') { |
| 1140 | // phone type handling |
| 1141 | if (isset($v[3])) { |
| 1142 | $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1; |
| 1143 | } |
| 1144 | else { |
| 1145 | $fields['location'][$locationName][$v[1]] = 1; |
| 1146 | } |
| 1147 | } |
| 1148 | else { |
| 1149 | $fields['location'][$locationName][$v[1]] = 1; |
| 1150 | } |
| 1151 | } |
| 1152 | else { |
| 1153 | $fields[$v[1]] = 1; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | return $fields; |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * Save the mapping field info for search builder / export given the formvalues |
| 1164 | * |
| 1165 | * @param array $params |
| 1166 | * Asscociated array of formvalues. |
| 1167 | * @param int $mappingId |
| 1168 | * Mapping id. |
| 1169 | * |
| 1170 | * @return NULL |
| 1171 | */ |
| 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(); |
| 1177 | |
| 1178 | if (empty($params['mapper'])) { |
| 1179 | return NULL; |
| 1180 | } |
| 1181 | |
| 1182 | //save record in mapping field table |
| 1183 | foreach ($params['mapper'] as $key => $value) { |
| 1184 | $colCnt = 0; |
| 1185 | foreach ($value as $k => $v) { |
| 1186 | |
| 1187 | if (!empty($v['1'])) { |
| 1188 | $saveMappingParams = self::getMappingParams( |
| 1189 | [ |
| 1190 | 'mapping_id' => $mappingId, |
| 1191 | 'grouping' => $key, |
| 1192 | 'operator' => $params['operator'][$key][$k] ?? NULL, |
| 1193 | 'value' => $params['value'][$key][$k] ?? NULL, |
| 1194 | 'column_number' => $colCnt, |
| 1195 | ], $v); |
| 1196 | $saveMappingField = new CRM_Core_DAO_MappingField(); |
| 1197 | $saveMappingField->copyValues($saveMappingParams); |
| 1198 | $saveMappingField->save(); |
| 1199 | $colCnt++; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | } |