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 | ||
34 | /** | |
f12c6f7d | 35 | * This class gets the name of the file to upload. |
6a488035 | 36 | */ |
b26295b8 | 37 | class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { |
6a488035 | 38 | |
6a488035 TO |
39 | |
40 | /** | |
100fef9d | 41 | * An array of all contact fields with |
6a488035 TO |
42 | * formatted custom field names. |
43 | * | |
44 | * @var array | |
6a488035 | 45 | */ |
d4c8a770 | 46 | protected $_formattedFieldNames; |
6a488035 TO |
47 | |
48 | /** | |
fe482240 | 49 | * On duplicate. |
6a488035 TO |
50 | * |
51 | * @var int | |
52 | */ | |
53 | public $_onDuplicate; | |
54 | ||
55 | protected $_dedupeFields; | |
56 | ||
817cf27c | 57 | protected static $customFields; |
58 | ||
6a488035 | 59 | /** |
fe482240 | 60 | * Attempt to match header labels with our mapper fields. |
2b4bc760 | 61 | * |
b26295b8 | 62 | * FIXME: This is essentially the same function as parent::defaultFromHeader |
6a488035 | 63 | * |
2b4bc760 | 64 | * @param string $columnName name of column header |
65 | * @param array $patterns pattern to match for the column | |
6a488035 TO |
66 | * |
67 | * @return string | |
6a488035 | 68 | */ |
56dd62a0 | 69 | public function defaultFromColumnName($columnName, $patterns) { |
6a488035 TO |
70 | |
71 | if (!preg_match('/^[a-z0-9 ]$/i', $columnName)) { | |
72 | if ($columnKey = array_search($columnName, $this->_mapperFields)) { | |
73 | $this->_fieldUsed[$columnKey] = TRUE; | |
74 | return $columnKey; | |
75 | } | |
76 | } | |
77 | ||
78 | foreach ($patterns as $key => $re) { | |
79 | // Skip empty key/patterns | |
80 | if (!$key || !$re || strlen("$re") < 5) { | |
81 | continue; | |
82 | } | |
83 | ||
84 | if (preg_match($re, $columnName)) { | |
85 | $this->_fieldUsed[$key] = TRUE; | |
86 | return $key; | |
87 | } | |
88 | } | |
89 | return ''; | |
90 | } | |
91 | ||
6a488035 | 92 | /** |
fe482240 | 93 | * Set variables up before form is built. |
6a488035 TO |
94 | */ |
95 | public function preProcess() { | |
96 | $dataSource = $this->get('dataSource'); | |
97 | $skipColumnHeader = $this->get('skipColumnHeader'); | |
98 | $this->_mapperFields = $this->get('fields'); | |
99 | $this->_importTableName = $this->get('importTableName'); | |
100 | $this->_onDuplicate = $this->get('onDuplicate'); | |
c16da28a | 101 | $highlightedFields = []; |
6a488035 TO |
102 | $highlightedFields[] = 'email'; |
103 | $highlightedFields[] = 'external_identifier'; | |
104 | //format custom field names, CRM-2676 | |
105 | switch ($this->get('contactType')) { | |
a05662ef | 106 | case CRM_Import_Parser::CONTACT_INDIVIDUAL: |
353ffa53 | 107 | $contactType = 'Individual'; |
6a488035 TO |
108 | $highlightedFields[] = 'first_name'; |
109 | $highlightedFields[] = 'last_name'; | |
110 | break; | |
111 | ||
a05662ef | 112 | case CRM_Import_Parser::CONTACT_HOUSEHOLD: |
6a488035 TO |
113 | $contactType = 'Household'; |
114 | $highlightedFields[] = 'household_name'; | |
115 | break; | |
116 | ||
a05662ef | 117 | case CRM_Import_Parser::CONTACT_ORGANIZATION: |
6a488035 TO |
118 | $contactType = 'Organization'; |
119 | $highlightedFields[] = 'organization_name'; | |
120 | break; | |
121 | } | |
122 | $this->_contactType = $contactType; | |
a05662ef | 123 | if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 TO |
124 | unset($this->_mapperFields['id']); |
125 | } | |
126 | else { | |
127 | $highlightedFields[] = 'id'; | |
128 | } | |
129 | ||
a05662ef | 130 | if ($this->_onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { |
6a488035 | 131 | //Mark Dedupe Rule Fields as required, since it's used in matching contact |
c16da28a | 132 | foreach (['Individual', 'Household', 'Organization'] as $cType) { |
133 | $ruleParams = [ | |
6a488035 | 134 | 'contact_type' => $cType, |
353ffa53 | 135 | 'used' => 'Unsupervised', |
c16da28a | 136 | ]; |
6a488035 TO |
137 | $this->_dedupeFields[$cType] = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
138 | } | |
139 | ||
140 | //Modify mapper fields title if fields are present in dedupe rule | |
141 | if (is_array($this->_dedupeFields[$contactType])) { | |
142 | foreach ($this->_dedupeFields[$contactType] as $val) { | |
143 | if ($valTitle = CRM_Utils_Array::value($val, $this->_mapperFields)) { | |
144 | $this->_mapperFields[$val] = $valTitle . ' (match to contact)'; | |
145 | } | |
146 | } | |
147 | } | |
148 | } | |
7b00a95d | 149 | // retrieve and highlight required custom fields |
817cf27c | 150 | $formattedFieldNames = $this->formatCustomFieldName($this->_mapperFields); |
151 | self::$customFields = CRM_Core_BAO_CustomField::getFields($this->_contactType); | |
22e263ad | 152 | foreach (self::$customFields as $key => $attr) { |
817cf27c | 153 | if (!empty($attr['is_required'])) { |
154 | $highlightedFields[] = "custom_$key"; | |
155 | } | |
156 | } | |
6a488035 | 157 | $this->assign('highlightedFields', $highlightedFields); |
817cf27c | 158 | $this->_formattedFieldNames[$contactType] = $this->_mapperFields = array_merge($this->_mapperFields, $formattedFieldNames); |
6a488035 | 159 | |
c16da28a | 160 | $columnNames = []; |
6a488035 TO |
161 | //get original col headers from csv if present. |
162 | if ($dataSource == 'CRM_Import_DataSource_CSV' && $skipColumnHeader) { | |
163 | $columnNames = $this->get('originalColHeader'); | |
164 | } | |
165 | else { | |
166 | // get the field names from the temp. DB table | |
167 | $dao = new CRM_Core_DAO(); | |
168 | $db = $dao->getDatabaseConnection(); | |
169 | ||
170 | $columnsQuery = "SHOW FIELDS FROM $this->_importTableName | |
171 | WHERE Field NOT LIKE '\_%'"; | |
172 | $columnsResult = $db->query($columnsQuery); | |
173 | while ($row = $columnsResult->fetchRow(DB_FETCHMODE_ASSOC)) { | |
174 | $columnNames[] = $row['Field']; | |
175 | } | |
176 | } | |
177 | ||
178 | $showColNames = TRUE; | |
4009859d | 179 | if ($dataSource === 'CRM_Import_DataSource_CSV' && !$skipColumnHeader) { |
6a488035 TO |
180 | $showColNames = FALSE; |
181 | } | |
182 | $this->assign('showColNames', $showColNames); | |
183 | ||
184 | $this->_columnCount = count($columnNames); | |
185 | $this->_columnNames = $columnNames; | |
186 | $this->assign('columnNames', $columnNames); | |
187 | //$this->_columnCount = $this->get( 'columnCount' ); | |
188 | $this->assign('columnCount', $this->_columnCount); | |
189 | $this->_dataValues = $this->get('dataValues'); | |
190 | $this->assign('dataValues', $this->_dataValues); | |
191 | $this->assign('rowDisplayCount', 2); | |
192 | } | |
193 | ||
194 | /** | |
fe482240 | 195 | * Build the form object. |
6a488035 TO |
196 | */ |
197 | public function buildQuickForm() { | |
198 | //to save the current mappings | |
199 | if (!$this->get('savedMapping')) { | |
200 | $saveDetailsName = ts('Save this field mapping'); | |
201 | $this->applyFilter('saveMappingName', 'trim'); | |
202 | $this->add('text', 'saveMappingName', ts('Name')); | |
203 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
204 | } | |
205 | else { | |
206 | $savedMapping = $this->get('savedMapping'); | |
207 | ||
6ebecfea | 208 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping, TRUE); |
6a488035 TO |
209 | |
210 | //get loaded Mapping Fields | |
353ffa53 | 211 | $mappingName = CRM_Utils_Array::value(1, $mappingName); |
353ffa53 TO |
212 | $mappingLocation = CRM_Utils_Array::value(1, $mappingLocation); |
213 | $mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType); | |
214 | $mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider); | |
215 | $mappingRelation = CRM_Utils_Array::value(1, $mappingRelation); | |
6a488035 TO |
216 | $mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType); |
217 | ||
218 | $this->assign('loadedMapping', $savedMapping); | |
219 | $this->set('loadedMapping', $savedMapping); | |
220 | ||
c16da28a | 221 | $params = ['id' => $savedMapping]; |
222 | $temp = []; | |
6a488035 TO |
223 | $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); |
224 | ||
225 | $this->assign('savedName', $mappingDetails->name); | |
226 | ||
227 | $this->add('hidden', 'mappingId', $savedMapping); | |
228 | ||
229 | $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); | |
230 | $saveDetailsName = ts('Save as a new field mapping'); | |
231 | $this->add('text', 'saveMappingName', ts('Name')); | |
232 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
233 | } | |
234 | ||
c16da28a | 235 | $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]); |
6a488035 | 236 | |
c16da28a | 237 | $this->addFormRule(['CRM_Contact_Import_Form_MapField', 'formRule']); |
6a488035 TO |
238 | |
239 | //-------- end of saved mapping stuff --------- | |
240 | ||
c16da28a | 241 | $defaults = []; |
353ffa53 TO |
242 | $mapperKeys = array_keys($this->_mapperFields); |
243 | $hasColumnNames = !empty($this->_columnNames); | |
244 | $columnPatterns = $this->get('columnPatterns'); | |
245 | $dataPatterns = $this->get('dataPatterns'); | |
6a488035 TO |
246 | $hasLocationTypes = $this->get('fieldTypes'); |
247 | ||
c16da28a | 248 | $this->_location_types = ['Primary' => ts('Primary')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
249 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
250 | ||
31b8ac82 | 251 | // Pass default location to js |
6a488035 | 252 | if ($defaultLocationType) { |
31b8ac82 CW |
253 | $this->assign('defaultLocationType', $defaultLocationType->id); |
254 | $this->assign('defaultLocationTypeLabel', $this->_location_types[$defaultLocationType->id]); | |
6a488035 TO |
255 | } |
256 | ||
257 | /* Initialize all field usages to false */ | |
6a488035 TO |
258 | foreach ($mapperKeys as $key) { |
259 | $this->_fieldUsed[$key] = FALSE; | |
260 | } | |
261 | ||
262 | $sel1 = $this->_mapperFields; | |
263 | $sel2[''] = NULL; | |
264 | ||
353ffa53 TO |
265 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
266 | $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); | |
cbf48754 | 267 | $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); |
6a488035 TO |
268 | |
269 | foreach ($this->_location_types as $key => $value) { | |
270 | $sel3['phone'][$key] = &$phoneTypes; | |
271 | //build array for IM service provider type for contact | |
272 | $sel3['im'][$key] = &$imProviders; | |
273 | } | |
274 | ||
275 | $sel4 = NULL; | |
276 | ||
277 | // store and cache all relationship types | |
278 | $contactRelation = new CRM_Contact_DAO_RelationshipType(); | |
279 | $contactRelation->find(); | |
280 | while ($contactRelation->fetch()) { | |
c16da28a | 281 | $contactRelationCache[$contactRelation->id] = []; |
6a488035 TO |
282 | $contactRelationCache[$contactRelation->id]['contact_type_a'] = $contactRelation->contact_type_a; |
283 | $contactRelationCache[$contactRelation->id]['contact_sub_type_a'] = $contactRelation->contact_sub_type_a; | |
284 | $contactRelationCache[$contactRelation->id]['contact_type_b'] = $contactRelation->contact_type_b; | |
285 | $contactRelationCache[$contactRelation->id]['contact_sub_type_b'] = $contactRelation->contact_sub_type_b; | |
286 | } | |
c16da28a | 287 | $highlightedFields = $highlightedRelFields = []; |
6a488035 TO |
288 | |
289 | $highlightedFields['email'] = 'All'; | |
290 | $highlightedFields['external_identifier'] = 'All'; | |
291 | $highlightedFields['first_name'] = 'Individual'; | |
292 | $highlightedFields['last_name'] = 'Individual'; | |
293 | $highlightedFields['household_name'] = 'Household'; | |
294 | $highlightedFields['organization_name'] = 'Organization'; | |
295 | ||
296 | foreach ($mapperKeys as $key) { | |
297 | // check if there is a _a_b or _b_a in the key | |
298 | if (strpos($key, '_a_b') || strpos($key, '_b_a')) { | |
299 | list($id, $first, $second) = explode('_', $key); | |
300 | } | |
301 | else { | |
302 | $id = $first = $second = NULL; | |
303 | } | |
4009859d | 304 | if (($first === 'a' && $second === 'b') || ($first === 'b' && $second === 'a')) { |
6a488035 TO |
305 | $cType = $contactRelationCache[$id]["contact_type_{$second}"]; |
306 | ||
307 | //CRM-5125 for contact subtype specific relationshiptypes | |
308 | $cSubType = NULL; | |
a7488080 | 309 | if (!empty($contactRelationCache[$id]["contact_sub_type_{$second}"])) { |
6a488035 TO |
310 | $cSubType = $contactRelationCache[$id]["contact_sub_type_{$second}"]; |
311 | } | |
312 | ||
313 | if (!$cType) { | |
314 | $cType = 'All'; | |
315 | } | |
316 | ||
6a488035 TO |
317 | $relatedFields = CRM_Contact_BAO_Contact::importableFields($cType); |
318 | unset($relatedFields['']); | |
c16da28a | 319 | $values = []; |
6a488035 TO |
320 | foreach ($relatedFields as $name => $field) { |
321 | $values[$name] = $field['title']; | |
322 | if (isset($hasLocationTypes[$name])) { | |
323 | $sel3[$key][$name] = $this->_location_types; | |
324 | } | |
4009859d | 325 | elseif ($name === 'url') { |
6a488035 TO |
326 | $sel3[$key][$name] = $websiteTypes; |
327 | } | |
328 | else { | |
329 | $sel3[$name] = NULL; | |
330 | } | |
331 | } | |
332 | ||
333 | //fix to append custom group name to field name, CRM-2676 | |
a7488080 | 334 | if (empty($this->_formattedFieldNames[$cType]) || $cType == $this->_contactType) { |
6a488035 TO |
335 | $this->_formattedFieldNames[$cType] = $this->formatCustomFieldName($values); |
336 | } | |
337 | ||
338 | $this->_formattedFieldNames[$cType] = array_merge($values, $this->_formattedFieldNames[$cType]); | |
339 | ||
340 | //Modified the Relationship fields if the fields are | |
341 | //present in dedupe rule | |
8cc574cf | 342 | if ($this->_onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK && !empty($this->_dedupeFields[$cType]) && |
6a488035 TO |
343 | is_array($this->_dedupeFields[$cType]) |
344 | ) { | |
c16da28a | 345 | static $cTypeArray = []; |
6a488035 TO |
346 | if ($cType != $this->_contactType && !in_array($cType, $cTypeArray)) { |
347 | foreach ($this->_dedupeFields[$cType] as $val) { | |
348 | if ($valTitle = CRM_Utils_Array::value($val, $this->_formattedFieldNames[$cType])) { | |
349 | $this->_formattedFieldNames[$cType][$val] = $valTitle . ' (match to contact)'; | |
350 | } | |
351 | } | |
352 | $cTypeArray[] = $cType; | |
353 | } | |
354 | } | |
355 | ||
356 | foreach ($highlightedFields as $k => $v) { | |
4009859d | 357 | if ($v == $cType || $v === 'All') { |
6a488035 TO |
358 | $highlightedRelFields[$key][] = $k; |
359 | } | |
360 | } | |
361 | $this->assign('highlightedRelFields', $highlightedRelFields); | |
362 | $sel2[$key] = $this->_formattedFieldNames[$cType]; | |
363 | ||
364 | if (!empty($cSubType)) { | |
365 | //custom fields for sub type | |
366 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($cSubType); | |
367 | ||
368 | if (!empty($subTypeFields)) { | |
369 | $subType = NULL; | |
370 | foreach ($subTypeFields as $customSubTypeField => $details) { | |
371 | $subType[$customSubTypeField] = $details['title']; | |
372 | $sel2[$key] = array_merge($sel2[$key], $this->formatCustomFieldName($subType)); | |
373 | } | |
374 | } | |
375 | } | |
376 | ||
377 | foreach ($this->_location_types as $k => $value) { | |
378 | $sel4[$key]['phone'][$k] = &$phoneTypes; | |
379 | //build array of IM service provider for related contact | |
380 | $sel4[$key]['im'][$k] = &$imProviders; | |
381 | } | |
382 | } | |
383 | else { | |
384 | $options = NULL; | |
d454582f | 385 | if (!empty($hasLocationTypes[$key])) { |
6a488035 TO |
386 | $options = $this->_location_types; |
387 | } | |
4009859d | 388 | elseif ($key === 'url') { |
6a488035 TO |
389 | $options = $websiteTypes; |
390 | } | |
391 | $sel2[$key] = $options; | |
392 | } | |
393 | } | |
394 | ||
395 | $js = "<script type='text/javascript'>\n"; | |
396 | $formName = 'document.forms.' . $this->_name; | |
397 | //used to warn for mismatch column count or mismatch mapping | |
0b786e49 | 398 | CRM_Core_Session::singleton()->setStatus(NULL); |
399 | ||
6a488035 | 400 | for ($i = 0; $i < $this->_columnCount; $i++) { |
c16da28a | 401 | $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL); |
6a488035 | 402 | |
0e3fc022 | 403 | if ($this->get('savedMapping')) { |
47a8acbb | 404 | list($mappingName, $defaults, $js) = $this->loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns); |
6a488035 TO |
405 | } |
406 | else { | |
407 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; | |
408 | if ($hasColumnNames) { | |
409 | // do array search first to see if has mapped key | |
6a488035 TO |
410 | $columnKey = array_search($this->_columnNames[$i], $this->_mapperFields); |
411 | if (isset($this->_fieldUsed[$columnKey])) { | |
412 | $defaults["mapper[$i]"] = $columnKey; | |
413 | $this->_fieldUsed[$key] = TRUE; | |
414 | } | |
415 | else { | |
416 | // Infer the default from the column names if we have them | |
c16da28a | 417 | $defaults["mapper[$i]"] = [ |
6a488035 TO |
418 | $this->defaultFromColumnName($this->_columnNames[$i], |
419 | $columnPatterns | |
420 | ), | |
421 | 0, | |
c16da28a | 422 | ]; |
6a488035 TO |
423 | } |
424 | } | |
425 | else { | |
426 | // Otherwise guess the default from the form of the data | |
c16da28a | 427 | $defaults["mapper[$i]"] = [ |
6a488035 TO |
428 | $this->defaultFromData($dataPatterns, $i), |
429 | // $defaultLocationType->id | |
430 | 0, | |
c16da28a | 431 | ]; |
6a488035 TO |
432 | } |
433 | } | |
c16da28a | 434 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); |
6a488035 TO |
435 | } |
436 | ||
437 | $js .= "</script>\n"; | |
438 | $this->assign('initHideBoxes', $js); | |
439 | ||
440 | //set warning if mismatch in more than | |
441 | if (isset($mappingName) && | |
442 | ($this->_columnCount != count($mappingName)) | |
443 | ) { | |
0b786e49 | 444 | CRM_Core_Session::singleton()->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.')); |
6a488035 TO |
445 | } |
446 | ||
447 | $this->setDefaults($defaults); | |
448 | ||
c16da28a | 449 | $this->addButtons([ |
450 | [ | |
451 | 'type' => 'back', | |
452 | 'name' => ts('Previous'), | |
453 | ], | |
454 | [ | |
455 | 'type' => 'next', | |
456 | 'name' => ts('Continue'), | |
457 | 'spacing' => ' ', | |
458 | 'isDefault' => TRUE, | |
459 | ], | |
460 | [ | |
461 | 'type' => 'cancel', | |
462 | 'name' => ts('Cancel'), | |
463 | ], | |
464 | ]); | |
6a488035 TO |
465 | } |
466 | ||
467 | /** | |
fe482240 | 468 | * Global validation rules for the form. |
6a488035 | 469 | * |
77c5b619 TO |
470 | * @param array $fields |
471 | * Posted values of the form. | |
6a488035 | 472 | * |
a6c01b45 CW |
473 | * @return array |
474 | * list of errors to be posted back to the form | |
6a488035 | 475 | */ |
00be9182 | 476 | public static function formRule($fields) { |
c16da28a | 477 | $errors = []; |
a7488080 | 478 | if (!empty($fields['saveMapping'])) { |
6a488035 TO |
479 | $nameField = CRM_Utils_Array::value('saveMappingName', $fields); |
480 | if (empty($nameField)) { | |
481 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
482 | } | |
483 | else { | |
c0a760fc | 484 | $mappingTypeId = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contact'); |
6a488035 TO |
485 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { |
486 | $errors['saveMappingName'] = ts('Duplicate Import Mapping Name'); | |
487 | } | |
488 | } | |
489 | } | |
490 | $template = CRM_Core_Smarty::singleton(); | |
a7488080 | 491 | if (!empty($fields['saveMapping'])) { |
6a488035 TO |
492 | $template->assign('isCheked', TRUE); |
493 | } | |
494 | ||
495 | if (!empty($errors)) { | |
496 | $_flag = 1; | |
497 | $assignError = new CRM_Core_Page(); | |
498 | $assignError->assign('mappingDetailsError', $_flag); | |
499 | return $errors; | |
500 | } | |
501 | else { | |
502 | return TRUE; | |
503 | } | |
504 | } | |
505 | ||
506 | /** | |
f12c6f7d | 507 | * Process the mapped fields and map it into the uploaded file. |
6a488035 TO |
508 | */ |
509 | public function postProcess() { | |
510 | $params = $this->controller->exportValues('MapField'); | |
511 | ||
512 | //reload the mapfield if load mapping is pressed | |
513 | if (!empty($params['savedMapping'])) { | |
514 | $this->set('savedMapping', $params['savedMapping']); | |
515 | $this->controller->resetPage($this->_name); | |
516 | return; | |
517 | } | |
353ffa53 | 518 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); |
52356383 | 519 | |
520 | $parser = $this->submit($params, $mapperKeys); | |
521 | ||
522 | // add all the necessary variables to the form | |
523 | $parser->set($this); | |
524 | } | |
525 | ||
526 | /** | |
527 | * Format custom field name. | |
528 | * | |
529 | * Combine group and field name to avoid conflict. | |
530 | * | |
531 | * @param array $fields | |
532 | * | |
533 | * @return array | |
534 | */ | |
56dd62a0 | 535 | public function formatCustomFieldName($fields) { |
52356383 | 536 | //CRM-2676, replacing the conflict for same custom field name from different custom group. |
c16da28a | 537 | $fieldIds = $formattedFieldNames = []; |
52356383 | 538 | foreach ($fields as $key => $value) { |
539 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
540 | $fieldIds[] = $customFieldId; | |
541 | } | |
542 | } | |
543 | ||
544 | if (!empty($fieldIds) && is_array($fieldIds)) { | |
545 | $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldIds); | |
546 | ||
547 | if (!empty($groupTitles)) { | |
548 | foreach ($groupTitles as $fId => $values) { | |
549 | $key = "custom_{$fId}"; | |
550 | $groupTitle = $values['groupTitle']; | |
551 | $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle; | |
552 | } | |
553 | } | |
554 | } | |
555 | ||
556 | return $formattedFieldNames; | |
557 | } | |
558 | ||
559 | /** | |
560 | * Main submit function. | |
561 | * | |
562 | * Extracted to add testing & start refactoring. | |
563 | * | |
564 | * @param $params | |
565 | * @param $mapperKeys | |
566 | * | |
567 | * @return \CRM_Contact_Import_Parser_Contact | |
449fda1c | 568 | * @throws \CiviCRM_API3_Exception |
52356383 | 569 | */ |
570 | public function submit($params, $mapperKeys) { | |
c16da28a | 571 | $mapper = $mapperKeysMain = $locations = []; |
1881b7b0 | 572 | $parserParameters = CRM_Contact_Import_Parser_Contact::getParameterForParser($this->_columnCount); |
6a488035 | 573 | |
353ffa53 TO |
574 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
575 | $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); | |
576 | $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); | |
b2b0530a | 577 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6ebecfea | 578 | $locationTypes['Primary'] = ts('Primary'); |
6a488035 | 579 | |
6a488035 | 580 | for ($i = 0; $i < $this->_columnCount; $i++) { |
6a488035 | 581 | |
353ffa53 TO |
582 | $fldName = CRM_Utils_Array::value(0, $mapperKeys[$i]); |
583 | $selOne = CRM_Utils_Array::value(1, $mapperKeys[$i]); | |
584 | $selTwo = CRM_Utils_Array::value(2, $mapperKeys[$i]); | |
585 | $selThree = CRM_Utils_Array::value(3, $mapperKeys[$i]); | |
586 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
6a488035 TO |
587 | $mapperKeysMain[$i] = $fldName; |
588 | ||
589 | //need to differentiate non location elements. | |
6ebecfea | 590 | if ($selOne && (is_numeric($selOne) || $selOne === 'Primary')) { |
4009859d | 591 | if ($fldName === 'url') { |
1881b7b0 | 592 | $parserParameters['mapperWebsiteType'][$i] = $websiteTypes[$selOne]; |
6a488035 TO |
593 | } |
594 | else { | |
1881b7b0 | 595 | $locations[$i] = $locationTypes[$selOne]; |
596 | $parserParameters['mapperLocType'][$i] = $selOne; | |
6a488035 | 597 | if ($selTwo && is_numeric($selTwo)) { |
4009859d | 598 | if ($fldName === 'phone') { |
1881b7b0 | 599 | $parserParameters['mapperPhoneType'][$i] = $phoneTypes[$selTwo]; |
6a488035 | 600 | } |
4009859d | 601 | elseif ($fldName === 'im') { |
1881b7b0 | 602 | $parserParameters['mapperImProvider'][$i] = $imProviders[$selTwo]; |
6a488035 TO |
603 | } |
604 | } | |
605 | } | |
606 | } | |
607 | ||
608 | //relationship contact mapper info. | |
609 | list($id, $first, $second) = CRM_Utils_System::explode('_', $fldName, 3); | |
4009859d | 610 | if (($first === 'a' && $second === 'b') || |
611 | ($first === 'b' && $second === 'a') | |
6a488035 | 612 | ) { |
1881b7b0 | 613 | $parserParameters['mapperRelated'][$i] = $this->_mapperFields[$fldName]; |
6a488035 | 614 | if ($selOne) { |
4009859d | 615 | if ($selOne === 'url') { |
1881b7b0 | 616 | $parserParameters['relatedContactWebsiteType'][$i] = $websiteTypes[$selTwo]; |
6a488035 TO |
617 | } |
618 | else { | |
1881b7b0 | 619 | $parserParameters['relatedContactLocType'][$i] = CRM_Utils_Array::value($selTwo, $locationTypes); |
6a488035 | 620 | if ($selThree) { |
4009859d | 621 | if ($selOne === 'phone') { |
1881b7b0 | 622 | $parserParameters['relatedContactPhoneType'][$i] = $phoneTypes[$selThree]; |
6a488035 | 623 | } |
4009859d | 624 | elseif ($selOne === 'im') { |
1881b7b0 | 625 | $parserParameters['relatedContactImProvider'][$i] = $imProviders[$selThree]; |
6a488035 TO |
626 | } |
627 | } | |
628 | } | |
629 | ||
630 | //get the related contact type. | |
631 | $relationType = new CRM_Contact_DAO_RelationshipType(); | |
632 | $relationType->id = $id; | |
633 | $relationType->find(TRUE); | |
1881b7b0 | 634 | $parserParameters['relatedContactType'][$i] = $relationType->{"contact_type_$second"}; |
635 | $parserParameters['relatedContactDetails'][$i] = $this->_formattedFieldNames[$parserParameters['relatedContactType'][$i]][$selOne]; | |
6a488035 TO |
636 | } |
637 | } | |
6a488035 TO |
638 | } |
639 | ||
640 | $this->set('columnNames', $this->_columnNames); | |
1881b7b0 | 641 | $this->set('websites', $parserParameters['mapperWebsiteType']); |
642 | $this->set('locations', $locations); | |
643 | $this->set('phones', $parserParameters['mapperPhoneType']); | |
644 | $this->set('ims', $parserParameters['mapperImProvider']); | |
645 | $this->set('related', $parserParameters['mapperRelated']); | |
646 | $this->set('relatedContactType', $parserParameters['relatedContactType']); | |
647 | $this->set('relatedContactDetails', $parserParameters['relatedContactDetails']); | |
648 | $this->set('relatedContactLocType', $parserParameters['relatedContactLocType']); | |
649 | $this->set('relatedContactPhoneType', $parserParameters['relatedContactPhoneType']); | |
650 | $this->set('relatedContactImProvider', $parserParameters['relatedContactImProvider']); | |
651 | $this->set('relatedContactWebsiteType', $parserParameters['relatedContactWebsiteType']); | |
652 | $this->set('mapper', $mapper); | |
6a488035 TO |
653 | |
654 | // store mapping Id to display it in the preview page | |
655 | $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); | |
656 | ||
657 | //Updating Mapping Records | |
a7488080 | 658 | if (!empty($params['updateMapping'])) { |
6a488035 | 659 | |
6a488035 TO |
660 | $mappingFields = new CRM_Core_DAO_MappingField(); |
661 | $mappingFields->mapping_id = $params['mappingId']; | |
662 | $mappingFields->find(); | |
663 | ||
c16da28a | 664 | $mappingFieldsId = []; |
6a488035 TO |
665 | while ($mappingFields->fetch()) { |
666 | if ($mappingFields->id) { | |
667 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
668 | } | |
669 | } | |
670 | ||
671 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
672 | $updateMappingFields = new CRM_Core_DAO_MappingField(); | |
ce80b209 | 673 | $updateMappingFields->id = CRM_Utils_Array::value($i, $mappingFieldsId); |
6a488035 TO |
674 | $updateMappingFields->mapping_id = $params['mappingId']; |
675 | $updateMappingFields->column_number = $i; | |
676 | ||
677 | $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3); | |
353ffa53 TO |
678 | $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL; |
679 | $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL; | |
680 | $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL; | |
6a488035 TO |
681 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { |
682 | $updateMappingFields->relationship_type_id = $id; | |
683 | $updateMappingFields->relationship_direction = "{$first}_{$second}"; | |
684 | $updateMappingFields->name = ucwords(str_replace("_", " ", $mapperKeys[$i][1])); | |
685 | // get phoneType id and provider id separately | |
686 | // before updating mappingFields of phone and IM for related contact, CRM-3140 | |
687 | if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'url') { | |
688 | $updateMappingFields->website_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
689 | } | |
690 | else { | |
691 | if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'phone') { | |
692 | $updateMappingFields->phone_type_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; | |
693 | } | |
694 | elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') { | |
695 | $updateMappingFields->im_provider_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; | |
696 | } | |
697 | $updateMappingFields->location_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
698 | } | |
699 | } | |
700 | else { | |
701 | $updateMappingFields->name = $mapper[$i]; | |
702 | $updateMappingFields->relationship_type_id = 'NULL'; | |
703 | $updateMappingFields->relationship_type_direction = 'NULL'; | |
b44e3f84 | 704 | // to store phoneType id and provider id separately |
6a488035 TO |
705 | // before updating mappingFields for phone and IM, CRM-3140 |
706 | if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') { | |
707 | $updateMappingFields->website_type_id = isset($mapperKeys[$i][1]) ? $mapperKeys[$i][1] : NULL; | |
708 | } | |
709 | else { | |
710 | if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'phone') { | |
711 | $updateMappingFields->phone_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
712 | } | |
713 | elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') { | |
714 | $updateMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
715 | } | |
6ebecfea | 716 | $locationTypeID = $parserParameters['mapperLocType'][$i]; |
717 | // location_type_id is NULL for non-location fields, and for Primary location. | |
718 | $updateMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : 'null'; | |
6a488035 TO |
719 | } |
720 | } | |
721 | $updateMappingFields->save(); | |
722 | } | |
723 | } | |
724 | ||
725 | //Saving Mapping Details and Records | |
a7488080 | 726 | if (!empty($params['saveMapping'])) { |
c16da28a | 727 | $mappingParams = [ |
6a488035 TO |
728 | 'name' => $params['saveMappingName'], |
729 | 'description' => $params['saveMappingDesc'], | |
37353970 | 730 | 'mapping_type_id' => 'Import Contact', |
c16da28a | 731 | ]; |
6a488035 | 732 | |
37353970 | 733 | $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams); |
6a488035 | 734 | |
6a488035 TO |
735 | $contactType = $this->get('contactType'); |
736 | switch ($contactType) { | |
a05662ef | 737 | case CRM_Import_Parser::CONTACT_INDIVIDUAL: |
6a488035 TO |
738 | $cType = 'Individual'; |
739 | break; | |
740 | ||
a05662ef | 741 | case CRM_Import_Parser::CONTACT_HOUSEHOLD: |
6a488035 TO |
742 | $cType = 'Household'; |
743 | break; | |
744 | ||
a05662ef | 745 | case CRM_Import_Parser::CONTACT_ORGANIZATION: |
6a488035 TO |
746 | $cType = 'Organization'; |
747 | } | |
748 | ||
449fda1c | 749 | $mappingID = NULL; |
6a488035 | 750 | for ($i = 0; $i < $this->_columnCount; $i++) { |
449fda1c | 751 | $mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $cType, $i, $mapper, $parserParameters); |
6a488035 | 752 | } |
449fda1c | 753 | $this->set('savedMapping', $mappingID); |
6a488035 TO |
754 | } |
755 | ||
1881b7b0 | 756 | $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $parserParameters['mapperLocType'], $parserParameters['mapperPhoneType'], |
757 | $parserParameters['mapperImProvider'], $parserParameters['mapperRelated'], $parserParameters['relatedContactType'], | |
758 | $parserParameters['relatedContactDetails'], $parserParameters['relatedContactLocType'], | |
759 | $parserParameters['relatedContactPhoneType'], $parserParameters['relatedContactImProvider'], | |
760 | $parserParameters['mapperWebsiteType'], $parserParameters['relatedContactWebsiteType'] | |
6a488035 TO |
761 | ); |
762 | ||
763 | $primaryKeyName = $this->get('primaryKeyName'); | |
764 | $statusFieldName = $this->get('statusFieldName'); | |
765 | $parser->run($this->_importTableName, | |
766 | $mapper, | |
a05662ef | 767 | CRM_Import_Parser::MODE_PREVIEW, |
6a488035 TO |
768 | $this->get('contactType'), |
769 | $primaryKeyName, | |
770 | $statusFieldName, | |
771 | $this->_onDuplicate, | |
772 | NULL, NULL, FALSE, | |
719a6fec | 773 | CRM_Contact_Import_Parser::DEFAULT_TIMEOUT, |
6a488035 TO |
774 | $this->get('contactSubType'), |
775 | $this->get('dedupe') | |
776 | ); | |
52356383 | 777 | return $parser; |
6a488035 | 778 | } |
96025800 | 779 | |
449fda1c | 780 | /** |
781 | * @param $mapperKeys | |
782 | * @param array $saveMapping | |
783 | * @param string $cType | |
784 | * @param int $i | |
785 | * @param array $mapper | |
786 | * @param array $parserParameters | |
787 | * | |
788 | * @return int | |
789 | */ | |
790 | protected function saveMappingField($mapperKeys, array $saveMapping, string $cType, int $i, array $mapper, array $parserParameters): int { | |
791 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
792 | $saveMappingFields->mapping_id = $saveMapping['id']; | |
793 | $saveMappingFields->contact_type = $cType; | |
794 | $saveMappingFields->column_number = $i; | |
795 | ||
796 | $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3); | |
797 | $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL; | |
798 | $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL; | |
799 | $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL; | |
800 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
801 | $saveMappingFields->name = ucwords(str_replace("_", " ", $mapperKeys[$i][1])); | |
802 | $saveMappingFields->relationship_type_id = $id; | |
803 | $saveMappingFields->relationship_direction = "{$first}_{$second}"; | |
804 | // to get phoneType id and provider id separately | |
805 | // before saving mappingFields of phone and IM for related contact, CRM-3140 | |
806 | if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'url') { | |
807 | $saveMappingFields->website_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
808 | } | |
809 | else { | |
810 | if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'phone') { | |
811 | $saveMappingFields->phone_type_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; | |
812 | } | |
813 | elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') { | |
814 | $saveMappingFields->im_provider_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; | |
815 | } | |
816 | $saveMappingFields->location_type_id = (isset($mapperKeys[$i][2]) && $mapperKeys[$i][2] !== 'Primary') ? $mapperKeys[$i][2] : NULL; | |
817 | } | |
818 | } | |
819 | else { | |
820 | $saveMappingFields->name = $mapper[$i]; | |
821 | $locationTypeID = $parserParameters['mapperLocType'][$i]; | |
822 | // to get phoneType id and provider id separately | |
823 | // before saving mappingFields of phone and IM, CRM-3140 | |
824 | if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') { | |
825 | $saveMappingFields->website_type_id = isset($mapperKeys[$i][1]) ? $mapperKeys[$i][1] : NULL; | |
826 | } | |
827 | else { | |
828 | if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'phone') { | |
829 | $saveMappingFields->phone_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
830 | } | |
831 | elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') { | |
832 | $saveMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; | |
833 | } | |
834 | $saveMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : NULL; | |
835 | } | |
836 | $saveMappingFields->relationship_type_id = NULL; | |
837 | } | |
838 | $saveMappingFields->save(); | |
839 | return $saveMappingFields->mapping_id; | |
840 | } | |
841 | ||
0e3fc022 | 842 | /** |
843 | * @param $mappingName | |
844 | * @param int $i | |
845 | * @param $mappingRelation | |
846 | * @param $mappingWebsiteType | |
847 | * @param $mappingLocation | |
848 | * @param $mappingPhoneType | |
849 | * @param $mappingImProvider | |
850 | * @param array $defaults | |
851 | * @param string $formName | |
852 | * @param string $js | |
853 | * @param bool $hasColumnNames | |
011d9c1e | 854 | * @param array $dataPatterns |
855 | * @param array $columnPatterns | |
0e3fc022 | 856 | * |
857 | * @return array | |
858 | */ | |
011d9c1e | 859 | protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns) { |
0e3fc022 | 860 | $jsSet = FALSE; |
861 | if (isset($mappingName[$i])) { | |
862 | if ($mappingName[$i] != ts('- do not import -')) { | |
863 | ||
864 | if (isset($mappingRelation[$i])) { | |
865 | // relationship mapping | |
866 | switch ($this->get('contactType')) { | |
867 | case CRM_Import_Parser::CONTACT_INDIVIDUAL: | |
868 | $contactType = 'Individual'; | |
869 | break; | |
870 | ||
871 | case CRM_Import_Parser::CONTACT_HOUSEHOLD: | |
872 | $contactType = 'Household'; | |
873 | break; | |
874 | ||
875 | case CRM_Import_Parser::CONTACT_ORGANIZATION: | |
876 | $contactType = 'Organization'; | |
877 | } | |
878 | //CRM-5125 | |
879 | $contactSubType = NULL; | |
880 | if ($this->get('contactSubType')) { | |
881 | $contactSubType = $this->get('contactSubType'); | |
882 | } | |
883 | ||
884 | $relations = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $contactType, | |
885 | FALSE, 'label', TRUE, $contactSubType | |
886 | ); | |
887 | ||
888 | foreach ($relations as $key => $var) { | |
889 | if ($key == $mappingRelation[$i]) { | |
890 | $relation = $key; | |
891 | break; | |
892 | } | |
893 | } | |
894 | ||
895 | $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i])); | |
896 | $websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL; | |
897 | $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; | |
898 | $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL; | |
899 | //get provider id from saved mappings | |
900 | $imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL; | |
901 | ||
902 | if ($websiteTypeId) { | |
903 | $defaults["mapper[$i]"] = [$relation, $contactDetails, $websiteTypeId]; | |
904 | if (!$websiteTypeId) { | |
905 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
906 | } | |
907 | } | |
908 | else { | |
909 | // default for IM/phone when mapping with relation is true | |
910 | $typeId = NULL; | |
911 | if (isset($phoneType)) { | |
912 | $typeId = $phoneType; | |
913 | } | |
914 | elseif (isset($imProvider)) { | |
915 | $typeId = $imProvider; | |
916 | } | |
917 | $defaults["mapper[$i]"] = [$relation, $contactDetails, $locationId, $typeId]; | |
918 | if (!$locationId) { | |
919 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
920 | } | |
921 | } | |
922 | // fix for edge cases, CRM-4954 | |
923 | if ($contactDetails == 'image_url') { | |
924 | $contactDetails = str_replace('url', 'URL', $contactDetails); | |
925 | } | |
926 | ||
927 | if (!$contactDetails) { | |
928 | $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; | |
929 | } | |
930 | ||
931 | if ((!$phoneType) && (!$imProvider)) { | |
932 | $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
933 | } | |
934 | //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
935 | $jsSet = TRUE; | |
936 | } | |
937 | else { | |
938 | $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); | |
939 | $websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL; | |
940 | $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; | |
941 | $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL; | |
942 | // get IM service provider id | |
943 | $imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL; | |
944 | ||
945 | if ($websiteTypeId) { | |
0e3fc022 | 946 | $defaults["mapper[$i]"] = [$mappingHeader[0], $websiteTypeId]; |
947 | } | |
948 | else { | |
949 | if (!$locationId) { | |
950 | $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; | |
951 | } | |
952 | //default for IM/phone without related contact | |
953 | $typeId = NULL; | |
954 | if (isset($phoneType)) { | |
955 | $typeId = $phoneType; | |
956 | } | |
957 | elseif (isset($imProvider)) { | |
958 | $typeId = $imProvider; | |
959 | } | |
aa4efdff | 960 | $defaults["mapper[$i]"] = [$mappingHeader[0] ?? '', $locationId, $typeId]; |
0e3fc022 | 961 | } |
962 | ||
963 | if ((!$phoneType) && (!$imProvider)) { | |
964 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
965 | } | |
966 | ||
967 | $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
968 | ||
969 | $jsSet = TRUE; | |
970 | } | |
971 | } | |
972 | else { | |
973 | $defaults["mapper[$i]"] = []; | |
974 | } | |
975 | if (!$jsSet) { | |
976 | for ($k = 1; $k < 4; $k++) { | |
977 | $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; | |
978 | } | |
979 | } | |
980 | } | |
981 | else { | |
982 | // this load section to help mapping if we ran out of saved columns when doing Load Mapping | |
983 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; | |
984 | ||
985 | if ($hasColumnNames) { | |
986 | $defaults["mapper[$i]"] = [$this->defaultFromColumnName($this->_columnNames[$i], $columnPatterns)]; | |
987 | } | |
988 | else { | |
989 | $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)]; | |
990 | } | |
991 | } | |
47a8acbb | 992 | return [$mappingName, $defaults, $js]; |
0e3fc022 | 993 | } |
994 | ||
6a488035 | 995 | } |