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