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