Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 | 16 | */ |
ee44263e | 17 | class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping implements \Civi\Core\HookInterface { |
6a488035 | 18 | |
6a488035 | 19 | /** |
4f940304 | 20 | * Retrieve DB object and copy to defaults array. |
6a488035 | 21 | * |
6a0b768e | 22 | * @param array $params |
4f940304 | 23 | * Array of criteria values. |
6a0b768e | 24 | * @param array $defaults |
4f940304 | 25 | * Array to be populated with found values. |
6a488035 | 26 | * |
4f940304 CW |
27 | * @return self|null |
28 | * The DAO object, if found. | |
29 | * | |
30 | * @deprecated | |
6a488035 | 31 | */ |
4f940304 CW |
32 | public static function retrieve($params, &$defaults) { |
33 | return self::commonRetrieve(self::class, $params, $defaults); | |
6a488035 TO |
34 | } |
35 | ||
36 | /** | |
fe482240 | 37 | * Delete the mapping. |
6a488035 | 38 | * |
6a0b768e | 39 | * @param int $id |
6a488035 | 40 | * |
9f3b9e96 | 41 | * @deprecated |
7d1e1dd5 | 42 | * @return bool |
6a488035 | 43 | */ |
00be9182 | 44 | public static function del($id) { |
9f3b9e96 | 45 | return (bool) static::deleteRecord(['id' => $id]); |
6a488035 TO |
46 | } |
47 | ||
48 | /** | |
fe482240 | 49 | * Takes an associative array and creates a contact object. |
6a488035 TO |
50 | * |
51 | * The function extract all the params it needs to initialize the create a | |
52 | * contact object. the params array could contain additional unused name/value | |
53 | * pairs | |
54 | * | |
6a0b768e TO |
55 | * @param array $params |
56 | * An array of name/value pairs. | |
6a488035 | 57 | * |
a6c01b45 CW |
58 | * @return object |
59 | * CRM_Core_DAO_Mapper object on success, otherwise NULL | |
6a488035 | 60 | */ |
00be9182 | 61 | public static function add($params) { |
6a488035 TO |
62 | $mapping = new CRM_Core_DAO_Mapping(); |
63 | $mapping->copyValues($params); | |
64 | $mapping->save(); | |
65 | ||
66 | return $mapping; | |
67 | } | |
68 | ||
69 | /** | |
4f55a797 | 70 | * Get the list of mappings for a select or select2 element. |
6a488035 | 71 | * |
f8df7165 | 72 | * @param string $mappingType |
73 | * Mapping type name. | |
4f55a797 CW |
74 | * @param bool $select2 |
75 | * Format for select2 | |
77b97be7 | 76 | * |
a6c01b45 | 77 | * @return array |
f8df7165 | 78 | * Array of mapping names, keyed by id. |
6a488035 | 79 | */ |
4f55a797 | 80 | public static function getMappings($mappingType, $select2 = FALSE) { |
be2fb01f | 81 | $result = civicrm_api3('Mapping', 'get', [ |
10b3287e | 82 | 'mapping_type_id' => $mappingType, |
4f55a797 | 83 | 'return' => ['name', 'description'], |
be2fb01f | 84 | 'options' => [ |
10b3287e BS |
85 | 'sort' => 'name', |
86 | 'limit' => 0, | |
be2fb01f CW |
87 | ], |
88 | ]); | |
89 | $mapping = []; | |
6a488035 | 90 | |
4f55a797 CW |
91 | foreach ($result['values'] as $id => $value) { |
92 | if ($select2) { | |
93 | $item = ['id' => $id, 'text' => $value['name']]; | |
94 | if (!empty($value['description'])) { | |
95 | $item['description'] = $value['description']; | |
96 | } | |
97 | $mapping[] = $item; | |
98 | } | |
99 | else { | |
100 | $mapping[$id] = $value['name']; | |
101 | } | |
6a488035 | 102 | } |
6a488035 TO |
103 | return $mapping; |
104 | } | |
105 | ||
412585fb | 106 | /** |
107 | * Get the mappings array, creating if it does not exist. | |
108 | * | |
109 | * @param string $mappingType | |
110 | * Mapping type name. | |
111 | * | |
112 | * @return array | |
113 | * Array of mapping names, keyed by id. | |
114 | * | |
115 | * @throws \CiviCRM_API3_Exception | |
116 | */ | |
117 | public static function getCreateMappingValues($mappingType) { | |
118 | try { | |
119 | return CRM_Core_BAO_Mapping::getMappings($mappingType); | |
120 | } | |
121 | catch (CiviCRM_API3_Exception $e) { | |
122 | // Having a valid mapping_type_id is now enforced. However, rather than error let's | |
123 | // add it. This is required for Multi value which could be done by upgrade script, but | |
124 | // it feels like there could be other instances so this is safer. | |
125 | $errorParams = $e->getExtraParams(); | |
126 | if ($errorParams['error_field'] === 'mapping_type_id') { | |
be2fb01f CW |
127 | $mappingValues = civicrm_api3('Mapping', 'getoptions', ['field' => 'mapping_type_id']); |
128 | civicrm_api3('OptionValue', 'create', [ | |
412585fb | 129 | 'option_group_id' => 'mapping_type', |
130 | 'label' => $mappingType, | |
a55b3c0b | 131 | 'name' => $mappingType, |
412585fb | 132 | 'value' => max(array_keys($mappingValues['values'])) + 1, |
133 | 'is_reserved' => 1, | |
be2fb01f | 134 | ]); |
412585fb | 135 | return CRM_Core_BAO_Mapping::getMappings($mappingType); |
136 | } | |
137 | throw $e; | |
138 | } | |
139 | } | |
140 | ||
6a488035 | 141 | /** |
fe482240 | 142 | * Get the mapping fields. |
6a488035 | 143 | * |
6a0b768e TO |
144 | * @param int $mappingId |
145 | * Mapping id. | |
77b97be7 | 146 | * |
6ebecfea | 147 | * @param bool $addPrimary |
148 | * Add the key 'Primary' when the field is a location field AND there is | |
149 | * no location type (meaning Primary)? | |
150 | * | |
a6c01b45 CW |
151 | * @return array |
152 | * array of mapping fields | |
6a488035 | 153 | */ |
6ebecfea | 154 | public static function getMappingFields($mappingId, $addPrimary = FALSE) { |
6a488035 TO |
155 | //mapping is to be loaded from database |
156 | $mapping = new CRM_Core_DAO_MappingField(); | |
157 | $mapping->mapping_id = $mappingId; | |
158 | $mapping->orderBy('column_number'); | |
159 | $mapping->find(); | |
160 | ||
be2fb01f CW |
161 | $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = []; |
162 | $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = []; | |
6a488035 TO |
163 | while ($mapping->fetch()) { |
164 | $mappingName[$mapping->grouping][$mapping->column_number] = $mapping->name; | |
165 | $mappingContactType[$mapping->grouping][$mapping->column_number] = $mapping->contact_type; | |
166 | ||
167 | if (!empty($mapping->location_type_id)) { | |
168 | $mappingLocation[$mapping->grouping][$mapping->column_number] = $mapping->location_type_id; | |
169 | } | |
6ebecfea | 170 | elseif ($addPrimary) { |
171 | if (CRM_Contact_BAO_Contact::isFieldHasLocationType($mapping->name)) { | |
172 | $mappingLocation[$mapping->grouping][$mapping->column_number] = ts('Primary'); | |
173 | } | |
174 | else { | |
175 | $mappingLocation[$mapping->grouping][$mapping->column_number] = NULL; | |
176 | } | |
177 | } | |
6a488035 TO |
178 | |
179 | if (!empty($mapping->phone_type_id)) { | |
180 | $mappingPhoneType[$mapping->grouping][$mapping->column_number] = $mapping->phone_type_id; | |
181 | } | |
182 | ||
183 | // get IM service provider type id from mapping fields | |
184 | if (!empty($mapping->im_provider_id)) { | |
185 | $mappingImProvider[$mapping->grouping][$mapping->column_number] = $mapping->im_provider_id; | |
186 | } | |
187 | ||
188 | if (!empty($mapping->website_type_id)) { | |
189 | $mappingWebsiteType[$mapping->grouping][$mapping->column_number] = $mapping->website_type_id; | |
190 | } | |
191 | ||
192 | if (!empty($mapping->relationship_type_id)) { | |
193 | $mappingRelation[$mapping->grouping][$mapping->column_number] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}"; | |
194 | } | |
195 | ||
196 | if (!empty($mapping->operator)) { | |
197 | $mappingOperator[$mapping->grouping][$mapping->column_number] = $mapping->operator; | |
198 | } | |
199 | ||
d1cb08fd | 200 | if (isset($mapping->value)) { |
6a488035 TO |
201 | $mappingValue[$mapping->grouping][$mapping->column_number] = $mapping->value; |
202 | } | |
203 | } | |
204 | ||
be2fb01f | 205 | return [ |
353ffa53 TO |
206 | $mappingName, |
207 | $mappingContactType, | |
208 | $mappingLocation, | |
209 | $mappingPhoneType, | |
210 | $mappingImProvider, | |
211 | $mappingRelation, | |
212 | $mappingOperator, | |
213 | $mappingValue, | |
214 | $mappingWebsiteType, | |
be2fb01f | 215 | ]; |
6a488035 TO |
216 | } |
217 | ||
8bfb13b9 | 218 | /** |
219 | * Get un-indexed array of the field values for the given mapping id. | |
220 | * | |
221 | * For example if passing a mapping ID & name the returned array would look like | |
222 | * ['First field name', 'second field name'] | |
223 | * | |
224 | * @param int $mappingID | |
225 | * @param string $fieldName | |
226 | * | |
227 | * @return array | |
228 | * @throws \CiviCRM_API3_Exception | |
229 | */ | |
230 | public static function getMappingFieldValues($mappingID, $fieldName) { | |
231 | return array_merge(CRM_Utils_Array::collect($fieldName, civicrm_api3('MappingField', 'get', ['mapping_id' => $mappingID, 'return' => $fieldName])['values'])); | |
232 | } | |
233 | ||
6a488035 | 234 | /** |
fe482240 | 235 | * Check Duplicate Mapping Name. |
6a488035 | 236 | * |
5a4f6742 CW |
237 | * @param string $nameField |
238 | * mapping Name. | |
239 | * @param string $mapTypeId | |
240 | * mapping Type. | |
6a488035 | 241 | * |
7d1e1dd5 | 242 | * @return bool |
6a488035 | 243 | */ |
00be9182 | 244 | public static function checkMapping($nameField, $mapTypeId) { |
6a488035 TO |
245 | $mapping = new CRM_Core_DAO_Mapping(); |
246 | $mapping->name = $nameField; | |
247 | $mapping->mapping_type_id = $mapTypeId; | |
c490a46a | 248 | return (bool) $mapping->find(TRUE); |
6a488035 TO |
249 | } |
250 | ||
251 | /** | |
2b4bc760 | 252 | * Function returns associated array of elements, that will be passed for search. |
6a488035 | 253 | * |
6a0b768e TO |
254 | * @param int $smartGroupId |
255 | * Smart group id. | |
6a488035 | 256 | * |
a6c01b45 CW |
257 | * @return array |
258 | * associated array of elements | |
6a488035 | 259 | */ |
00be9182 | 260 | public static function getFormattedFields($smartGroupId) { |
be2fb01f | 261 | $returnFields = []; |
6a488035 TO |
262 | |
263 | //get the fields from mapping table | |
264 | $dao = new CRM_Core_DAO_MappingField(); | |
265 | $dao->mapping_id = $smartGroupId; | |
266 | $dao->find(); | |
267 | while ($dao->fetch()) { | |
268 | $fldName = $dao->name; | |
269 | if ($dao->location_type_id) { | |
270 | $fldName .= "-{$dao->location_type_id}"; | |
271 | } | |
272 | if ($dao->phone_type) { | |
273 | $fldName .= "-{$dao->phone_type}"; | |
274 | } | |
275 | $returnFields[$fldName]['value'] = $dao->value; | |
276 | $returnFields[$fldName]['op'] = $dao->operator; | |
277 | $returnFields[$fldName]['grouping'] = $dao->grouping; | |
278 | } | |
279 | return $returnFields; | |
280 | } | |
281 | ||
282 | /** | |
46770058 | 283 | * Build the mapping form for Search Builder. |
6a488035 | 284 | * |
c490a46a | 285 | * @param CRM_Core_Form $form |
c490a46a CW |
286 | * @param int $mappingId |
287 | * @param int $columnNo | |
6a0b768e TO |
288 | * @param int $blockCount |
289 | * (no of blocks shown). | |
041ecc95 | 290 | * @param int $exportMode |
6a488035 | 291 | */ |
46770058 | 292 | public static function buildMappingForm(&$form, $mappingId, $columnNo, $blockCount, $exportMode = NULL) { |
35801459 | 293 | |
be2fb01f CW |
294 | $hasLocationTypes = []; |
295 | $hasRelationTypes = []; | |
6a488035 | 296 | |
46770058 | 297 | $columnCount = $columnNo; |
cbd83dde AH |
298 | $form->addElement('xbutton', 'addBlock', ts('Also include contacts where'), |
299 | [ | |
300 | 'type' => 'submit', | |
301 | 'class' => 'submit-link', | |
f7083334 | 302 | 'value' => 1, |
cbd83dde | 303 | ] |
46770058 | 304 | ); |
6a488035 | 305 | |
ea762d46 | 306 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); |
46770058 | 307 | $fields = self::getBasicFields('Search Builder'); |
6a488035 | 308 | |
d509ee87 | 309 | // Unset groups, tags, notes for component export |
6a488035 | 310 | if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) { |
d509ee87 CW |
311 | foreach (array_keys($fields) as $type) { |
312 | CRM_Utils_Array::remove($fields[$type], 'groups', 'tags', 'notes'); | |
6a488035 TO |
313 | } |
314 | } | |
315 | ||
46770058 CW |
316 | // Build the common contact fields array. |
317 | $fields['Contact'] = []; | |
318 | foreach ($fields[$contactTypes[0]] as $key => $value) { | |
319 | // If a field exists across all contact types, move it to the "Contact" selector | |
320 | $ubiquitious = TRUE; | |
321 | foreach ($contactTypes as $type) { | |
322 | if (!isset($fields[$type][$key])) { | |
323 | $ubiquitious = FALSE; | |
6a488035 TO |
324 | } |
325 | } | |
46770058 CW |
326 | if ($ubiquitious) { |
327 | $fields['Contact'][$key] = $value; | |
328 | foreach ($contactTypes as $type) { | |
329 | unset($fields[$type][$key]); | |
330 | } | |
6a488035 TO |
331 | } |
332 | } | |
46770058 CW |
333 | if (array_key_exists('note', $fields['Contact'])) { |
334 | $noteTitle = $fields['Contact']['note']['title']; | |
335 | $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); | |
336 | $fields['Contact']['note_body'] = ['title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body']; | |
337 | $fields['Contact']['note_subject'] = [ | |
338 | 'title' => $noteTitle . ': ' . ts('Subject Only'), | |
339 | 'name' => 'note_subject', | |
340 | ]; | |
341 | } | |
6a488035 | 342 | |
d509ee87 | 343 | // add component fields |
46770058 | 344 | $compArray = self::addComponentFields($fields, 'Search Builder', $exportMode); |
6a488035 | 345 | |
6a488035 TO |
346 | foreach ($fields as $key => $value) { |
347 | ||
348 | foreach ($value as $key1 => $value1) { | |
349 | //CRM-2676, replacing the conflict for same custom field name from different custom group. | |
350 | $customGroupName = self::getCustomGroupName($key1); | |
351 | ||
352 | if ($customGroupName) { | |
353 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; | |
354 | } | |
355 | else { | |
356 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; | |
357 | } | |
358 | if (isset($value1['hasLocationType'])) { | |
359 | $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; | |
360 | } | |
361 | ||
362 | if (isset($value1['hasRelationType'])) { | |
363 | $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; | |
364 | unset($relatedMapperFields[$key][$key1]); | |
365 | } | |
366 | } | |
367 | ||
ea762d46 | 368 | if (isset($relatedMapperFields[$key]['related'])) { |
6a488035 TO |
369 | unset($relatedMapperFields[$key]['related']); |
370 | } | |
371 | } | |
372 | ||
b2b0530a | 373 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
374 | |
375 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
376 | ||
c490a46a CW |
377 | // FIXME: dirty hack to make the default option show up first. This |
378 | // avoids a mozilla browser bug with defaults on dynamically constructed | |
379 | // selector widgets. | |
6a488035 TO |
380 | if ($defaultLocationType) { |
381 | $defaultLocation = $locationTypes[$defaultLocationType->id]; | |
382 | unset($locationTypes[$defaultLocationType->id]); | |
be2fb01f | 383 | $locationTypes = [$defaultLocationType->id => $defaultLocation] + $locationTypes; |
6a488035 TO |
384 | } |
385 | ||
be2fb01f | 386 | $locationTypes = [' ' => ts('Primary')] + $locationTypes; |
6a488035 TO |
387 | |
388 | // since we need a hierarchical list to display contact types & subtypes, | |
389 | // this is what we going to display in first selector | |
ea762d46 | 390 | $contactTypeSelect = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); |
46770058 | 391 | $contactTypeSelect = ['Contact' => ts('Contacts')] + $contactTypeSelect; |
6a488035 | 392 | |
ea762d46 | 393 | $sel1 = ['' => ts('- select record type -')] + $contactTypeSelect + $compArray; |
6a488035 TO |
394 | |
395 | foreach ($sel1 as $key => $sel) { | |
396 | if ($key) { | |
b44e3f84 | 397 | // sort everything BUT the contactType which is sorted separately by |
82a06bfc | 398 | // an initial commit of CRM-13278 (check ksort above) |
ea762d46 | 399 | if (!in_array($key, $contactTypes)) { |
82a06bfc DL |
400 | asort($mapperFields[$key]); |
401 | } | |
be2fb01f | 402 | $sel2[$key] = ['' => ts('- select field -')] + $mapperFields[$key]; |
6a488035 TO |
403 | } |
404 | } | |
405 | ||
353ffa53 TO |
406 | $sel3[''] = NULL; |
407 | $sel5[''] = NULL; | |
408 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); | |
e7e657f0 | 409 | $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); |
6a488035 TO |
410 | asort($phoneTypes); |
411 | ||
412 | foreach ($sel1 as $k => $sel) { | |
413 | if ($k) { | |
414 | foreach ($locationTypes as $key => $value) { | |
415 | if (trim($key) != '') { | |
416 | $sel4[$k]['phone'][$key] = &$phoneTypes; | |
417 | $sel4[$k]['im'][$key] = &$imProviders; | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
423 | foreach ($sel1 as $k => $sel) { | |
424 | if ($k) { | |
425 | foreach ($mapperFields[$k] as $key => $value) { | |
426 | if (isset($hasLocationTypes[$k][$key])) { | |
427 | $sel3[$k][$key] = $locationTypes; | |
428 | } | |
429 | else { | |
430 | $sel3[$key] = NULL; | |
431 | } | |
432 | } | |
433 | } | |
434 | } | |
435 | ||
c490a46a | 436 | // Array for core fields and relationship custom data |
6a488035 TO |
437 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); |
438 | ||
6a488035 | 439 | //special fields that have location, hack for primary location |
be2fb01f | 440 | $specialFields = [ |
353ffa53 TO |
441 | 'street_address', |
442 | 'supplemental_address_1', | |
443 | 'supplemental_address_2', | |
207f62c6 | 444 | 'supplemental_address_3', |
353ffa53 TO |
445 | 'city', |
446 | 'postal_code', | |
447 | 'postal_code_suffix', | |
448 | 'geo_code_1', | |
449 | 'geo_code_2', | |
450 | 'state_province', | |
451 | 'country', | |
452 | 'phone', | |
453 | 'email', | |
454 | 'im', | |
be2fb01f | 455 | ]; |
6a488035 TO |
456 | |
457 | if (isset($mappingId)) { | |
6a488035 TO |
458 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, |
459 | $mappingRelation, $mappingOperator, $mappingValue | |
353ffa53 | 460 | ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); |
6a488035 TO |
461 | |
462 | $blkCnt = count($mappingName); | |
463 | if ($blkCnt >= $blockCount) { | |
464 | $blockCount = $blkCnt + 1; | |
465 | } | |
466 | for ($x = 1; $x < $blockCount; $x++) { | |
467 | if (isset($mappingName[$x])) { | |
468 | $colCnt = count($mappingName[$x]); | |
469 | if ($colCnt >= $columnCount[$x]) { | |
470 | $columnCount[$x] = $colCnt; | |
471 | } | |
472 | } | |
473 | } | |
474 | } | |
475 | ||
476 | $form->_blockCount = $blockCount; | |
477 | $form->_columnCount = $columnCount; | |
478 | ||
479 | $form->set('blockCount', $form->_blockCount); | |
480 | $form->set('columnCount', $form->_columnCount); | |
481 | ||
be2fb01f | 482 | $defaults = $noneArray = $nullArray = []; |
6a488035 | 483 | |
6a488035 TO |
484 | for ($x = 1; $x < $blockCount; $x++) { |
485 | ||
486 | for ($i = 0; $i < $columnCount[$x]; $i++) { | |
487 | ||
be2fb01f | 488 | $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', [1 => $i]), NULL); |
6a488035 TO |
489 | $jsSet = FALSE; |
490 | ||
491 | if (isset($mappingId)) { | |
5c322e13 | 492 | list($mappingName, $defaults, $noneArray, $jsSet) = self::loadSavedMapping($mappingLocation, $x, $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, $specialFields, $mappingPhoneType, $defaults, $noneArray, $mappingImProvider, $mappingOperator, $mappingValue); |
6a488035 TO |
493 | } |
494 | //Fix for Search Builder | |
46770058 | 495 | $j = 4; |
6a488035 TO |
496 | |
497 | $formValues = $form->exportValues(); | |
498 | if (!$jsSet) { | |
499 | if (empty($formValues)) { | |
500 | // Incremented length for third select box(relationship type) | |
501 | for ($k = 1; $k < $j; $k++) { | |
be2fb01f | 502 | $noneArray[] = [$x, $i, $k]; |
6a488035 TO |
503 | } |
504 | } | |
505 | else { | |
506 | if (!empty($formValues['mapper'][$x])) { | |
507 | foreach ($formValues['mapper'][$x] as $value) { | |
508 | for ($k = 1; $k < $j; $k++) { | |
509 | if (!isset($formValues['mapper'][$x][$i][$k]) || | |
510 | (!$formValues['mapper'][$x][$i][$k]) | |
511 | ) { | |
be2fb01f | 512 | $noneArray[] = [$x, $i, $k]; |
6a488035 TO |
513 | } |
514 | else { | |
be2fb01f | 515 | $nullArray[] = [$x, $i, $k]; |
6a488035 TO |
516 | } |
517 | } | |
518 | } | |
519 | } | |
520 | else { | |
521 | for ($k = 1; $k < $j; $k++) { | |
be2fb01f | 522 | $noneArray[] = [$x, $i, $k]; |
6a488035 TO |
523 | } |
524 | } | |
525 | } | |
526 | } | |
527 | //Fix for Search Builder | |
46770058 | 528 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); |
6a488035 | 529 | |
46770058 CW |
530 | //CRM -2292, restricted array set |
531 | $operatorArray = ['' => ts('-operator-')] + CRM_Core_SelectValues::getSearchBuilderOperators(); | |
6a488035 | 532 | |
46770058 CW |
533 | $form->add('select', "operator[$x][$i]", '', $operatorArray); |
534 | $form->add('text', "value[$x][$i]", ''); | |
6a488035 TO |
535 | } |
536 | ||
cbd83dde AH |
537 | $form->addElement('xbutton', "addMore[$x]", ts('Another search field'), [ |
538 | 'type' => 'submit', | |
539 | 'class' => 'submit-link', | |
f7083334 | 540 | 'value' => 1, |
cbd83dde | 541 | ]); |
6a488035 TO |
542 | } |
543 | //end of block for | |
544 | ||
545 | $js = "<script type='text/javascript'>\n"; | |
46770058 | 546 | $formName = "document.Builder"; |
6a488035 | 547 | if (!empty($nullArray)) { |
353ffa53 | 548 | $js .= "var nullArray = ["; |
be2fb01f CW |
549 | $elements = []; |
550 | $seen = []; | |
6a488035 TO |
551 | foreach ($nullArray as $element) { |
552 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
553 | if (!isset($seen[$key])) { | |
554 | $elements[] = "[$key]"; | |
555 | $seen[$key] = 1; | |
556 | } | |
557 | } | |
558 | $js .= implode(', ', $elements); | |
559 | $js .= "]"; | |
560 | $js .= " | |
22e263ad | 561 | for (var i=0;i<nullArray.length;i++) { |
6a488035 TO |
562 | if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) { |
563 | {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = ''; | |
564 | } | |
565 | } | |
566 | "; | |
567 | } | |
568 | if (!empty($noneArray)) { | |
353ffa53 | 569 | $js .= "var noneArray = ["; |
be2fb01f CW |
570 | $elements = []; |
571 | $seen = []; | |
6a488035 TO |
572 | foreach ($noneArray as $element) { |
573 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
574 | if (!isset($seen[$key])) { | |
575 | $elements[] = "[$key]"; | |
576 | $seen[$key] = 1; | |
577 | } | |
578 | } | |
579 | $js .= implode(', ', $elements); | |
580 | $js .= "]"; | |
581 | $js .= " | |
22e263ad | 582 | for (var i=0;i<noneArray.length;i++) { |
6a488035 TO |
583 | if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) { |
584 | {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none'; | |
585 | } | |
586 | } | |
587 | "; | |
588 | } | |
589 | $js .= "</script>\n"; | |
590 | ||
591 | $form->assign('initHideBoxes', $js); | |
592 | $form->assign('columnCount', $columnCount); | |
593 | $form->assign('blockCount', $blockCount); | |
594 | $form->setDefaults($defaults); | |
595 | ||
596 | $form->setDefaultAction('refresh'); | |
597 | } | |
598 | ||
3a95b2ee CW |
599 | /** |
600 | * @param string $mappingType | |
601 | * @return array | |
602 | */ | |
603 | public static function getBasicFields($mappingType) { | |
224b94cc | 604 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(); |
3a95b2ee CW |
605 | $fields = []; |
606 | foreach ($contactTypes as $contactType) { | |
607 | if ($mappingType == 'Search Builder') { | |
608 | // Get multiple custom group fields in this context | |
609 | $contactFields = CRM_Contact_BAO_Contact::exportableFields($contactType, FALSE, FALSE, FALSE, TRUE); | |
610 | } | |
611 | else { | |
612 | $contactFields = CRM_Contact_BAO_Contact::exportableFields($contactType, FALSE, TRUE); | |
613 | } | |
614 | $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook::singleton()->getFields()); | |
615 | ||
616 | // Exclude the address options disabled in the Address Settings | |
617 | $fields[$contactType] = CRM_Core_BAO_Address::validateAddressOptions($contactFields); | |
618 | ksort($fields[$contactType]); | |
619 | if ($mappingType == 'Export') { | |
620 | $relationships = []; | |
78f3f7c0 | 621 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $contactType); |
3a95b2ee CW |
622 | asort($relationshipTypes); |
623 | ||
624 | foreach ($relationshipTypes as $key => $var) { | |
625 | list($type) = explode('_', $key); | |
626 | ||
627 | $relationships[$key]['title'] = $var; | |
628 | $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
629 | $relationships[$key]['export'] = TRUE; | |
630 | $relationships[$key]['relationship_type_id'] = $type; | |
631 | $relationships[$key]['related'] = TRUE; | |
632 | $relationships[$key]['hasRelationType'] = 1; | |
633 | } | |
634 | ||
635 | if (!empty($relationships)) { | |
636 | $fields[$contactType] = array_merge($fields[$contactType], | |
637 | ['related' => ['title' => ts('- related contact info -')]], | |
638 | $relationships | |
639 | ); | |
640 | } | |
e58cbdfc | 641 | if (!empty($fields[$contactType]['state_province']) && empty($fields[$contactType]['state_province_name'])) { |
642 | $fields[$contactType]['state_province_name'] = $fields[$contactType]['state_province']; | |
643 | $fields[$contactType]['state_province_name']['title'] = ts('State/Province Name'); | |
644 | $fields[$contactType]['state_province']['title'] = ts('State/Province Abbreviation'); | |
645 | } | |
3a95b2ee CW |
646 | } |
647 | } | |
648 | ||
649 | // Get the current employer for mapping. | |
650 | if ($mappingType == 'Export') { | |
651 | $fields['Individual']['current_employer_id']['title'] = ts('Current Employer ID'); | |
652 | } | |
653 | ||
d509ee87 CW |
654 | // Contact Sub Type For export |
655 | $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo(); | |
656 | foreach ($subTypes as $subType => $info) { | |
657 | //adding subtype specific relationships CRM-5256 | |
658 | $csRelationships = []; | |
659 | ||
660 | if ($mappingType == 'Export') { | |
661 | $subTypeRelationshipTypes | |
662 | = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $info['parent'], | |
663 | FALSE, 'label', TRUE, $subType); | |
664 | ||
665 | foreach ($subTypeRelationshipTypes as $key => $var) { | |
666 | if (!array_key_exists($key, $fields[$info['parent']])) { | |
667 | list($type) = explode('_', $key); | |
668 | ||
669 | $csRelationships[$key]['title'] = $var; | |
670 | $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
671 | $csRelationships[$key]['export'] = TRUE; | |
672 | $csRelationships[$key]['relationship_type_id'] = $type; | |
673 | $csRelationships[$key]['related'] = TRUE; | |
674 | $csRelationships[$key]['hasRelationType'] = 1; | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | $fields[$subType] = $fields[$info['parent']] + $csRelationships; | |
680 | ||
681 | //custom fields for sub type | |
682 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType); | |
683 | $fields[$subType] += $subTypeFields; | |
684 | } | |
685 | ||
3a95b2ee CW |
686 | return $fields; |
687 | } | |
688 | ||
573da9a3 CW |
689 | /** |
690 | * Adds component fields to the export fields array; returns list of components. | |
691 | * | |
692 | * @param array $fields | |
693 | * @param string $mappingType | |
694 | * @param int $exportMode | |
695 | * @return array | |
696 | */ | |
697 | public static function addComponentFields(&$fields, $mappingType, $exportMode) { | |
698 | $compArray = []; | |
699 | ||
700 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT)) { | |
701 | if (CRM_Core_Permission::access('CiviContribute')) { | |
702 | $fields['Contribution'] = CRM_Core_DAO::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution'); | |
703 | unset($fields['Contribution']['contribution_contact_id']); | |
704 | $compArray['Contribution'] = ts('Contribution'); | |
705 | } | |
706 | } | |
707 | ||
708 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT)) { | |
709 | if (CRM_Core_Permission::access('CiviEvent')) { | |
710 | $fields['Participant'] = CRM_Event_BAO_Participant::exportableFields(); | |
711 | //get the component payment fields | |
712 | // @todo - review this - inconsistent with other entities & hacky. | |
713 | if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { | |
714 | $componentPaymentFields = []; | |
e6a94a98 | 715 | foreach ([ |
716 | 'componentPaymentField_total_amount' => ts('Total Amount'), | |
717 | 'componentPaymentField_contribution_status' => ts('Contribution Status'), | |
718 | 'componentPaymentField_received_date' => ts('Date Received'), | |
719 | 'componentPaymentField_payment_instrument' => ts('Payment Method'), | |
720 | 'componentPaymentField_transaction_id' => ts('Transaction ID'), | |
721 | ] as $payField => $payTitle) { | |
573da9a3 CW |
722 | $componentPaymentFields[$payField] = ['title' => $payTitle]; |
723 | } | |
724 | $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields); | |
725 | } | |
726 | ||
727 | $compArray['Participant'] = ts('Participant'); | |
728 | } | |
729 | } | |
730 | ||
731 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT)) { | |
732 | if (CRM_Core_Permission::access('CiviMember')) { | |
733 | $fields['Membership'] = CRM_Member_BAO_Membership::getMembershipFields($exportMode); | |
734 | unset($fields['Membership']['membership_contact_id']); | |
735 | $compArray['Membership'] = ts('Membership'); | |
736 | } | |
737 | } | |
738 | ||
739 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT)) { | |
740 | if (CRM_Core_Permission::access('CiviPledge')) { | |
741 | $fields['Pledge'] = CRM_Pledge_BAO_Pledge::exportableFields(); | |
742 | unset($fields['Pledge']['pledge_contact_id']); | |
743 | $compArray['Pledge'] = ts('Pledge'); | |
744 | } | |
745 | } | |
746 | ||
747 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CASE_EXPORT)) { | |
748 | if (CRM_Core_Permission::access('CiviCase')) { | |
749 | $fields['Case'] = CRM_Case_BAO_Case::exportableFields(); | |
750 | $compArray['Case'] = ts('Case'); | |
751 | ||
752 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Case'); | |
753 | $compArray['Activity'] = ts('Case Activity'); | |
754 | ||
755 | unset($fields['Case']['case_contact_id']); | |
756 | } | |
757 | } | |
758 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT)) { | |
5dcfb8ef | 759 | if (method_exists('CRM_Grant_BAO_Grant', 'exportableFields') && CRM_Core_Permission::check('access CiviGrant')) { |
573da9a3 CW |
760 | $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); |
761 | unset($fields['Grant']['grant_contact_id']); | |
762 | if ($mappingType == 'Search Builder') { | |
763 | unset($fields['Grant']['grant_type_id']); | |
764 | } | |
765 | $compArray['Grant'] = ts('Grant'); | |
766 | } | |
767 | } | |
768 | ||
769 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT)) { | |
770 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Activity'); | |
771 | $compArray['Activity'] = ts('Activity'); | |
772 | } | |
773 | ||
774 | return $compArray; | |
775 | } | |
776 | ||
257195f9 | 777 | /** |
778 | * Get the parameters for a mapping field in a saveable format from the quickform mapping format. | |
779 | * | |
d67808ac | 780 | * @param array $defaults |
257195f9 | 781 | * @param array $v |
257195f9 | 782 | * |
783 | * @return array | |
784 | */ | |
04f8f758 | 785 | public static function getMappingParams($defaults, $v) { |
257195f9 | 786 | $locationTypeId = NULL; |
d67808ac | 787 | $saveMappingFields = $defaults; |
257195f9 | 788 | |
9c1bc317 CW |
789 | $saveMappingFields['name'] = $v['1'] ?? NULL; |
790 | $saveMappingFields['contact_type'] = $v['0'] ?? NULL; | |
791 | $locationId = $v['2'] ?? NULL; | |
d67808ac | 792 | $saveMappingFields['location_type_id'] = is_numeric($locationId) ? $locationId : NULL; |
257195f9 | 793 | |
794 | if ($v[1] == 'phone') { | |
9c1bc317 | 795 | $saveMappingFields['phone_type_id'] = $v['3'] ?? NULL; |
257195f9 | 796 | } |
797 | elseif ($v[1] == 'im') { | |
9c1bc317 | 798 | $saveMappingFields['im_provider_id'] = $v['3'] ?? NULL; |
257195f9 | 799 | } |
800 | ||
257195f9 | 801 | // Handle mapping for 'related contact' fields |
802 | if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { | |
803 | list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); | |
804 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
805 | ||
806 | if (!empty($v['2'])) { | |
9c1bc317 | 807 | $saveMappingFields['name'] = $v['2'] ?? NULL; |
257195f9 | 808 | } |
809 | elseif (!empty($v['4'])) { | |
9c1bc317 | 810 | $saveMappingFields['name'] = $v['4'] ?? NULL; |
257195f9 | 811 | } |
812 | ||
813 | if (is_numeric(CRM_Utils_Array::value('3', $v))) { | |
9c1bc317 | 814 | $locationTypeId = $v['3'] ?? NULL; |
257195f9 | 815 | } |
816 | elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { | |
9c1bc317 | 817 | $locationTypeId = $v['5'] ?? NULL; |
257195f9 | 818 | } |
819 | ||
820 | if (is_numeric(CRM_Utils_Array::value('4', $v))) { | |
04f8f758 | 821 | if ($saveMappingFields['name'] === 'im') { |
822 | $saveMappingFields['im_provider_id'] = $v[4]; | |
823 | } | |
824 | else { | |
9c1bc317 | 825 | $saveMappingFields['phone_type_id'] = $v['4'] ?? NULL; |
04f8f758 | 826 | } |
257195f9 | 827 | } |
828 | elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { | |
9c1bc317 | 829 | $saveMappingFields['phone_type_id'] = $v['6'] ?? NULL; |
257195f9 | 830 | } |
831 | ||
d67808ac | 832 | $saveMappingFields['location_type_id'] = is_numeric($locationTypeId) ? $locationTypeId : NULL; |
d67808ac | 833 | $saveMappingFields['relationship_type_id'] = $id; |
834 | $saveMappingFields['relationship_direction'] = "{$first}_{$second}"; | |
257195f9 | 835 | } |
836 | } | |
837 | ||
d67808ac | 838 | return $saveMappingFields; |
257195f9 | 839 | } |
840 | ||
5c322e13 | 841 | /** |
842 | * Load saved mapping. | |
843 | * | |
844 | * @param $mappingLocation | |
845 | * @param int $x | |
846 | * @param int $i | |
847 | * @param $mappingName | |
848 | * @param $mapperFields | |
849 | * @param $mappingContactType | |
850 | * @param $mappingRelation | |
851 | * @param array $specialFields | |
852 | * @param $mappingPhoneType | |
853 | * @param $phoneType | |
854 | * @param array $defaults | |
855 | * @param array $noneArray | |
856 | * @param $imProvider | |
857 | * @param $mappingImProvider | |
858 | * @param $mappingOperator | |
859 | * @param $mappingValue | |
860 | * | |
861 | * @return array | |
862 | */ | |
863 | protected static function loadSavedMapping($mappingLocation, int $x, int $i, $mappingName, $mapperFields, $mappingContactType, $mappingRelation, array $specialFields, $mappingPhoneType, array $defaults, array $noneArray, $mappingImProvider, $mappingOperator, $mappingValue) { | |
cee87c9e | 864 | $jsSet = FALSE; |
2e1f50d6 | 865 | $locationId = $mappingLocation[$x][$i] ?? 0; |
5c322e13 | 866 | if (isset($mappingName[$x][$i])) { |
867 | if (is_array($mapperFields[$mappingContactType[$x][$i]])) { | |
868 | ||
869 | if (isset($mappingRelation[$x][$i])) { | |
2e1f50d6 | 870 | $relLocationId = $mappingLocation[$x][$i] ?? 0; |
5c322e13 | 871 | if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) { |
872 | $relLocationId = " "; | |
873 | } | |
874 | ||
2e1f50d6 | 875 | $relPhoneType = $mappingPhoneType[$x][$i] ?? NULL; |
5c322e13 | 876 | |
877 | $defaults["mapper[$x][$i]"] = [ | |
878 | $mappingContactType[$x][$i], | |
879 | $mappingRelation[$x][$i], | |
880 | $locationId, | |
881 | $phoneType, | |
882 | $mappingName[$x][$i], | |
883 | $relLocationId, | |
884 | $relPhoneType, | |
885 | ]; | |
886 | ||
887 | if (!$locationId) { | |
888 | $noneArray[] = [$x, $i, 2]; | |
889 | } | |
890 | if (!$phoneType && !$imProvider) { | |
891 | $noneArray[] = [$x, $i, 3]; | |
892 | } | |
893 | if (!$mappingName[$x][$i]) { | |
894 | $noneArray[] = [$x, $i, 4]; | |
895 | } | |
896 | if (!$relLocationId) { | |
897 | $noneArray[] = [$x, $i, 5]; | |
898 | } | |
899 | if (!$relPhoneType) { | |
900 | $noneArray[] = [$x, $i, 6]; | |
901 | } | |
902 | $noneArray[] = [$x, $i, 2]; | |
903 | } | |
904 | else { | |
2e1f50d6 CW |
905 | $phoneType = $mappingPhoneType[$x][$i] ?? NULL; |
906 | $imProvider = $mappingImProvider[$x][$i] ?? NULL; | |
5c322e13 | 907 | if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) { |
908 | $locationId = " "; | |
909 | } | |
910 | ||
911 | $defaults["mapper[$x][$i]"] = [ | |
912 | $mappingContactType[$x][$i], | |
913 | $mappingName[$x][$i], | |
914 | $locationId, | |
915 | $phoneType, | |
916 | ]; | |
917 | if (!$mappingName[$x][$i]) { | |
918 | $noneArray[] = [$x, $i, 1]; | |
919 | } | |
920 | if (!$locationId) { | |
921 | $noneArray[] = [$x, $i, 2]; | |
922 | } | |
923 | if (!$phoneType && !$imProvider) { | |
924 | $noneArray[] = [$x, $i, 3]; | |
925 | } | |
926 | ||
927 | $noneArray[] = [$x, $i, 4]; | |
928 | $noneArray[] = [$x, $i, 5]; | |
929 | $noneArray[] = [$x, $i, 6]; | |
930 | } | |
931 | ||
932 | $jsSet = TRUE; | |
933 | ||
934 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingOperator))) { | |
9c1bc317 | 935 | $defaults["operator[$x][$i]"] = $mappingOperator[$x][$i] ?? NULL; |
5c322e13 | 936 | } |
937 | ||
d1cb08fd | 938 | if (isset($mappingValue[$x][$i])) { |
9c1bc317 | 939 | $defaults["value[$x][$i]"] = $mappingValue[$x][$i] ?? NULL; |
5c322e13 | 940 | } |
941 | } | |
942 | } | |
943 | return [$mappingName, $defaults, $noneArray, $jsSet]; | |
944 | } | |
945 | ||
b5c2afd0 | 946 | /** |
c490a46a CW |
947 | * Function returns all custom fields with group title and |
948 | * field label | |
b5c2afd0 | 949 | * |
6a0b768e TO |
950 | * @param int $relationshipTypeId |
951 | * Related relationship type id. | |
c490a46a | 952 | * |
a6c01b45 CW |
953 | * @return array |
954 | * all custom field titles | |
b5c2afd0 | 955 | */ |
00be9182 | 956 | public function getRelationTypeCustomGroupData($relationshipTypeId) { |
6a488035 TO |
957 | |
958 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL); | |
be2fb01f | 959 | $groupTitle = []; |
6a488035 TO |
960 | foreach ($customFields as $krelation => $vrelation) { |
961 | $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label']; | |
962 | } | |
963 | return $groupTitle; | |
964 | } | |
965 | ||
6a488035 | 966 | /** |
fe482240 | 967 | * Function returns all Custom group Names. |
6a488035 | 968 | * |
6a0b768e TO |
969 | * @param int $customfieldId |
970 | * Related file id. | |
77b97be7 | 971 | * |
72b3a70c CW |
972 | * @return null|string |
973 | * $customGroupName all custom group names | |
6a488035 | 974 | */ |
00be9182 | 975 | public static function getCustomGroupName($customfieldId) { |
6a488035 TO |
976 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($customfieldId)) { |
977 | $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id'); | |
978 | $customGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title'); | |
979 | ||
e429c35a | 980 | $customGroupName = CRM_Utils_String::ellipsify($customGroupName, 13); |
6a488035 TO |
981 | |
982 | return $customGroupName; | |
983 | } | |
984 | } | |
985 | ||
986 | /** | |
987 | * Function returns associated array of elements, that will be passed for search | |
988 | * | |
6a0b768e TO |
989 | * @param array $params |
990 | * Associated array of submitted values. | |
991 | * @param bool $row | |
992 | * Row no of the fields. | |
6a488035 | 993 | * |
6a488035 | 994 | * |
a6c01b45 CW |
995 | * @return array |
996 | * formatted associated array of elements | |
ac15829d | 997 | * @throws CRM_Core_Exception |
6a488035 | 998 | */ |
00be9182 | 999 | public static function formattedFields(&$params, $row = FALSE) { |
be2fb01f | 1000 | $fields = []; |
6a488035 TO |
1001 | |
1002 | if (empty($params) || !isset($params['mapper'])) { | |
1003 | return $fields; | |
1004 | } | |
1005 | ||
2d35bc78 | 1006 | $types = CRM_Contact_BAO_ContactType::basicTypes(TRUE); |
6a488035 TO |
1007 | foreach ($params['mapper'] as $key => $value) { |
1008 | $contactType = NULL; | |
1009 | foreach ($value as $k => $v) { | |
2d35bc78 | 1010 | if (in_array($v[0], $types, TRUE)) { |
6a488035 | 1011 | if ($contactType && $contactType != $v[0]) { |
ac15829d | 1012 | throw new CRM_Core_Exception(ts("Cannot have two clauses with different types: %1, %2", |
be2fb01f | 1013 | [1 => $contactType, 2 => $v[0]] |
353ffa53 | 1014 | )); |
6a488035 TO |
1015 | } |
1016 | $contactType = $v[0]; | |
1017 | } | |
a7488080 | 1018 | if (!empty($v['1'])) { |
6a488035 | 1019 | $fldName = $v[1]; |
9c1bc317 | 1020 | $v2 = $v['2'] ?? NULL; |
6a488035 TO |
1021 | if ($v2 && trim($v2)) { |
1022 | $fldName .= "-{$v[2]}"; | |
1023 | } | |
1024 | ||
9c1bc317 | 1025 | $v3 = $v['3'] ?? NULL; |
6a488035 TO |
1026 | if ($v3 && trim($v3)) { |
1027 | $fldName .= "-{$v[3]}"; | |
1028 | } | |
1029 | ||
1030 | $value = $params['value'][$key][$k]; | |
6a488035 | 1031 | |
8ef12e64 | 1032 | if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_' |
353ffa53 | 1033 | && substr($fldName, 0, 10) != 'financial_' |
afa0b07c | 1034 | && substr($fldName, 0, 8) != 'payment_') { |
6a488035 TO |
1035 | if (substr($fldName, 0, 13) != 'contribution_') { |
1036 | $fldName = 'contribution_' . $fldName; | |
1037 | } | |
1038 | } | |
1039 | ||
dbc6f6d6 | 1040 | // CRM-14983: verify if values are comma separated convert to array |
fccb6a0f | 1041 | if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) { |
1042 | $value = explode(',', $value); | |
be2fb01f | 1043 | $value = [$params['operator'][$key][$k] => $value]; |
dbc6f6d6 | 1044 | } |
fce45661 SL |
1045 | // CRM-19081 Fix legacy StateProvince Field Values. |
1046 | // These derive from smart groups created using search builder under older | |
1047 | // CiviCRM versions. | |
0f7d79e9 | 1048 | if ($fldName == 'state_province' && !is_numeric($value) && !is_array($value)) { |
fce45661 SL |
1049 | $value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'state_province_id', $value); |
1050 | } | |
dbc6f6d6 | 1051 | |
6a488035 | 1052 | if ($row) { |
be2fb01f | 1053 | $fields[] = [ |
6a488035 TO |
1054 | $fldName, |
1055 | $params['operator'][$key][$k], | |
1056 | $value, | |
1057 | $key, | |
1058 | $k, | |
be2fb01f | 1059 | ]; |
6a488035 TO |
1060 | } |
1061 | else { | |
be2fb01f | 1062 | $fields[] = [ |
6a488035 TO |
1063 | $fldName, |
1064 | $params['operator'][$key][$k], | |
1065 | $value, | |
1066 | $key, | |
1067 | 0, | |
be2fb01f | 1068 | ]; |
6a488035 TO |
1069 | } |
1070 | } | |
1071 | } | |
1072 | if ($contactType) { | |
be2fb01f | 1073 | $fields[] = [ |
6a488035 TO |
1074 | 'contact_type', |
1075 | '=', | |
1076 | $contactType, | |
1077 | $key, | |
1078 | 0, | |
be2fb01f | 1079 | ]; |
6a488035 TO |
1080 | } |
1081 | } | |
1082 | ||
1083 | //add sortByCharacter values | |
1084 | if (isset($params['sortByCharacter'])) { | |
be2fb01f | 1085 | $fields[] = [ |
6a488035 TO |
1086 | 'sortByCharacter', |
1087 | '=', | |
1088 | $params['sortByCharacter'], | |
1089 | 0, | |
1090 | 0, | |
be2fb01f | 1091 | ]; |
6a488035 | 1092 | } |
6a488035 TO |
1093 | return $fields; |
1094 | } | |
1095 | ||
b5c2afd0 | 1096 | /** |
c490a46a | 1097 | * @param array $params |
b5c2afd0 EM |
1098 | * |
1099 | * @return array | |
1100 | */ | |
00be9182 | 1101 | public static function &returnProperties(&$params) { |
be2fb01f | 1102 | $fields = [ |
6a488035 TO |
1103 | 'contact_type' => 1, |
1104 | 'contact_sub_type' => 1, | |
1105 | 'sort_name' => 1, | |
be2fb01f | 1106 | ]; |
6a488035 TO |
1107 | |
1108 | if (empty($params) || empty($params['mapper'])) { | |
1109 | return $fields; | |
1110 | } | |
1111 | ||
f9ba4d01 | 1112 | $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); |
6a488035 TO |
1113 | foreach ($params['mapper'] as $key => $value) { |
1114 | foreach ($value as $k => $v) { | |
1115 | if (isset($v[1])) { | |
1116 | if ($v[1] == 'groups' || $v[1] == 'tags') { | |
1117 | continue; | |
1118 | } | |
1119 | ||
1120 | if (isset($v[2]) && is_numeric($v[2])) { | |
1121 | if (!array_key_exists('location', $fields)) { | |
be2fb01f | 1122 | $fields['location'] = []; |
6a488035 TO |
1123 | } |
1124 | ||
1125 | // make sure that we have a location fields and a location type for this | |
1126 | $locationName = $locationTypes[$v[2]]; | |
1127 | if (!array_key_exists($locationName, $fields['location'])) { | |
be2fb01f | 1128 | $fields['location'][$locationName] = []; |
6a488035 TO |
1129 | $fields['location'][$locationName]['location_type'] = $v[2]; |
1130 | } | |
1131 | ||
1132 | if ($v[1] == 'phone' || $v[1] == 'email' || $v[1] == 'im') { | |
1133 | // phone type handling | |
1134 | if (isset($v[3])) { | |
1135 | $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1; | |
1136 | } | |
1137 | else { | |
1138 | $fields['location'][$locationName][$v[1]] = 1; | |
1139 | } | |
1140 | } | |
1141 | else { | |
1142 | $fields['location'][$locationName][$v[1]] = 1; | |
1143 | } | |
1144 | } | |
1145 | else { | |
1146 | $fields[$v[1]] = 1; | |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | return $fields; | |
1153 | } | |
1154 | ||
1155 | /** | |
100fef9d | 1156 | * Save the mapping field info for search builder / export given the formvalues |
6a488035 | 1157 | * |
6a0b768e TO |
1158 | * @param array $params |
1159 | * Asscociated array of formvalues. | |
1160 | * @param int $mappingId | |
1161 | * Mapping id. | |
6a488035 | 1162 | * |
e60f24eb | 1163 | * @return NULL |
6a488035 | 1164 | */ |
5bc608d3 | 1165 | public static function saveMappingFields($params, $mappingId) { |
3af96592 | 1166 | //delete mapping fields records for existing mapping |
6a488035 TO |
1167 | $mappingFields = new CRM_Core_DAO_MappingField(); |
1168 | $mappingFields->mapping_id = $mappingId; | |
1169 | $mappingFields->delete(); | |
1170 | ||
1171 | if (empty($params['mapper'])) { | |
7c550ca0 | 1172 | return NULL; |
6a488035 TO |
1173 | } |
1174 | ||
1175 | //save record in mapping field table | |
1176 | foreach ($params['mapper'] as $key => $value) { | |
1177 | $colCnt = 0; | |
1178 | foreach ($value as $k => $v) { | |
1179 | ||
a7488080 | 1180 | if (!empty($v['1'])) { |
d67808ac | 1181 | $saveMappingParams = self::getMappingParams( |
1182 | [ | |
1183 | 'mapping_id' => $mappingId, | |
1184 | 'grouping' => $key, | |
1185 | 'operator' => $params['operator'][$key][$k] ?? NULL, | |
1186 | 'value' => $params['value'][$key][$k] ?? NULL, | |
1187 | 'column_number' => $colCnt, | |
1188 | ], $v); | |
257195f9 | 1189 | $saveMappingField = new CRM_Core_DAO_MappingField(); |
fc944198 | 1190 | $saveMappingField->copyValues($saveMappingParams); |
257195f9 | 1191 | $saveMappingField->save(); |
6a488035 | 1192 | $colCnt++; |
6a488035 TO |
1193 | } |
1194 | } | |
1195 | } | |
1196 | } | |
96025800 | 1197 | |
751dec04 SL |
1198 | /** |
1199 | * Remove references to a specific field from save Mappings | |
1200 | * @param string $fieldName | |
1201 | */ | |
1202 | public static function removeFieldFromMapping($fieldName): void { | |
1203 | $mappingField = new CRM_Core_DAO_MappingField(); | |
1204 | $mappingField->name = $fieldName; | |
1205 | $mappingField->delete(); | |
1206 | } | |
1207 | ||
6552bd20 CW |
1208 | /** |
1209 | * Callback for hook_civicrm_pre(). | |
1210 | * @param \Civi\Core\Event\PreEvent $event | |
1211 | * @throws CRM_Core_Exception | |
1212 | */ | |
1213 | public static function on_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { | |
1214 | if ($event->action === 'delete' && $event->entity === 'RelationshipType') { | |
1215 | // CRM-3323 - Delete mappingField records when deleting relationship type | |
1216 | \Civi\Api4\MappingField::delete(FALSE) | |
1217 | ->addWhere('relationship_type_id', '=', $event->id) | |
1218 | ->execute(); | |
1219 | } | |
1220 | } | |
1221 | ||
6a488035 | 1222 | } |