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