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