Merge pull request #23797 from eileenmcnaughton/remove_field_types
[civicrm-core.git] / CRM / Contribute / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class gets the name of the file to upload.
20 */
21 class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
22
23 /**
24 * Check if required fields are present.
25 *
26 * @param CRM_Contribute_Import_Form_MapField $self
27 * @param string $contactORContributionId
28 * @param array $importKeys
29 * @param array $errors
30 * @param int $weightSum
31 * @param int $threshold
32 * @param string $fieldMessage
33 *
34 * @return array
35 */
36 protected static function checkRequiredFields($self, string $contactORContributionId, array $importKeys, array $errors, int $weightSum, $threshold, string $fieldMessage): array {
37 // FIXME: should use the schema titles, not redeclare them
38 $requiredFields = [
39 $contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'),
40 'total_amount' => ts('Total Amount'),
41 'financial_type_id' => ts('Financial Type'),
42 ];
43
44 foreach ($requiredFields as $field => $title) {
45 if (!in_array($field, $importKeys)) {
46 if (empty($errors['_qf_default'])) {
47 $errors['_qf_default'] = '';
48 }
49 if ($field == $contactORContributionId) {
50 if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
51 !$self->isUpdateExisting()
52 ) {
53 $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', [1 => $threshold]) . '<br />';
54 }
55 elseif ($self->isUpdateExisting() &&
56 !(in_array('invoice_id', $importKeys) || in_array('trxn_id', $importKeys) ||
57 in_array('contribution_id', $importKeys)
58 )
59 ) {
60 $errors['_qf_default'] .= ts('Invoice ID or Transaction ID or Contribution ID are required to match to the existing contribution records in Update mode.') . '<br />';
61 }
62 }
63 else {
64 $errors['_qf_default'] .= ts('Missing required field: %1', [1 => $title]) . '<br />';
65 }
66 }
67 }
68 return $errors;
69 }
70
71 /**
72 * Set variables up before form is built.
73 */
74 public function preProcess() {
75 parent::preProcess();
76
77 $highlightedFields = ['financial_type_id', 'total_amount'];
78 //CRM-2219 removing other required fields since for updation only
79 //invoice id or trxn id or contribution id is required.
80 if ($this->isUpdateExisting()) {
81 $remove = [
82 'contribution_contact_id',
83 'email',
84 'first_name',
85 'last_name',
86 'external_identifier',
87 ];
88 foreach ($remove as $value) {
89 unset($this->_mapperFields[$value]);
90 }
91
92 //modify field title only for update mode. CRM-3245
93 foreach ([
94 'contribution_id',
95 'invoice_id',
96 'trxn_id',
97 ] as $key) {
98 $this->_mapperFields[$key] .= ' (match to contribution record)';
99 $highlightedFields[] = $key;
100 }
101 }
102 elseif ($this->isSkipExisting()) {
103 unset($this->_mapperFields['contribution_id']);
104 $highlightedFieldsArray = [
105 'contribution_contact_id',
106 'email',
107 'first_name',
108 'last_name',
109 'external_identifier',
110 ];
111 foreach ($highlightedFieldsArray as $name) {
112 $highlightedFields[] = $name;
113 }
114 }
115
116 // modify field title for contribution status
117 $this->_mapperFields['contribution_status_id'] = ts('Contribution Status');
118
119 $this->assign('highlightedFields', $highlightedFields);
120 }
121
122 /**
123 * Build the form object.
124 *
125 * @throws \CiviCRM_API3_Exception
126 */
127 public function buildQuickForm() {
128 $savedMappingID = $this->getSubmittedValue('savedMapping');
129
130 $this->buildSavedMappingFields($savedMappingID);
131
132 $this->addFormRule([
133 'CRM_Contribute_Import_Form_MapField',
134 'formRule',
135 ], $this);
136
137 //-------- end of saved mapping stuff ---------
138
139 $defaults = [];
140 $mapperKeys = array_keys($this->_mapperFields);
141 $hasHeaders = $this->getSubmittedValue('skipColumnHeader');
142 $headerPatterns = $this->getHeaderPatterns();
143 $dataPatterns = $this->getDataPatterns();
144 $mapperKeysValues = $this->getSubmittedValue('mapper');
145 $columnHeaders = $this->getColumnHeaders();
146 $fieldMappings = $this->getFieldMappings();
147
148 /* Initialize all field usages to false */
149 foreach ($mapperKeys as $key) {
150 $this->_fieldUsed[$key] = FALSE;
151 }
152 $sel1 = $this->_mapperFields;
153
154 if (!$this->isUpdateExisting()) {
155 unset($sel1['id']);
156 unset($sel1['contribution_id']);
157 }
158
159 $softCreditFields['contact_id'] = ts('Contact ID');
160 $softCreditFields['external_identifier'] = ts('External ID');
161 $softCreditFields['email'] = ts('Email');
162
163 $sel2['soft_credit'] = $softCreditFields;
164 $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = $sel3['soft_credit']['email'] = CRM_Core_OptionGroup::values('soft_credit_type');
165 $sel4 = NULL;
166
167 // end of soft credit section
168 $js = "<script type='text/javascript'>\n";
169 $formName = 'document.forms.' . $this->_name;
170
171 //used to warn for mismatch column count or mismatch mapping
172 $warning = 0;
173
174 foreach ($columnHeaders as $i => $columnHeader) {
175 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
176 $jsSet = FALSE;
177 if ($this->getSubmittedValue('savedMapping')) {
178 // $mappingContactType is not really a contact type - the data has been mangled
179 // into that field - see https://lab.civicrm.org/dev/core/-/issues/654
180 [$mappingName, $mappingContactType] = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID);
181 $fieldMapping = $fieldMappings[$i] ?? NULL;
182 $mappingContactType = $mappingContactType[1];
183 if (isset($fieldMappings[$i])) {
184 if ($fieldMapping['name'] !== ts('do_not_import')) {
185 $softField = $mappingContactType[$i] ?? '';
186
187 if (!$softField) {
188 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
189 }
190
191 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
192 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
193 $defaults["mapper[$i]"] = [
194 $fieldMapping['name'],
195 $softField,
196 // Since the soft credit type id is not stored we can't load it here.
197 '',
198 ];
199 $jsSet = TRUE;
200 }
201 else {
202 $defaults["mapper[$i]"] = [];
203 }
204 if (!$jsSet) {
205 for ($k = 1; $k < 4; $k++) {
206 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
207 }
208 }
209 }
210 else {
211 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
212 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
213
214 if ($hasHeaders) {
215 $defaults["mapper[$i]"] = [$this->defaultFromHeader($columnHeader, $headerPatterns)];
216 }
217 else {
218 $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
219 }
220 }
221 //end of load mapping
222 }
223 else {
224 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
225 if ($hasHeaders) {
226 // do array search first to see if has mapped key
227 $columnKey = array_search($columnHeader, $this->_mapperFields);
228 if (isset($this->_fieldUsed[$columnKey])) {
229 $defaults["mapper[$i]"] = $columnKey;
230 $this->_fieldUsed[$key] = TRUE;
231 }
232 else {
233 // Infer the default from the column names if we have them
234 $defaults["mapper[$i]"] = [
235 $this->defaultFromHeader($columnHeader, $headerPatterns),
236 0,
237 ];
238 }
239 }
240 else {
241 // Otherwise guess the default from the form of the data
242 $defaults["mapper[$i]"] = [
243 $this->defaultFromData($dataPatterns, $i),
244 0,
245 ];
246 }
247 if (!empty($mapperKeysValues) && ($mapperKeysValues[$i][0] ?? NULL) === 'soft_credit') {
248 $softCreditField = $mapperKeysValues[$i][1];
249 $softCreditTypeID = $mapperKeysValues[$i][2];
250 $js .= "cj('#mapper_" . $i . "_1').val($softCreditField);\n";
251 $js .= "cj('#mapper_" . $i . "_2').val($softCreditTypeID);\n";
252 }
253 }
254 $sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
255 }
256 $js .= "</script>\n";
257 $this->assign('initHideBoxes', $js);
258 $this->setDefaults($defaults);
259
260 $this->addFormButtons();
261 }
262
263 /**
264 * Global validation rules for the form.
265 *
266 * @param array $fields
267 * Posted values of the form.
268 *
269 * @param $files
270 * @param self $self
271 *
272 * @return array
273 * list of errors to be posted back to the form
274 */
275 public static function formRule($fields, $files, $self) {
276 $errors = [];
277 $fieldMessage = NULL;
278 $contactORContributionId = $self->isUpdateExisting() ? 'contribution_id' : 'contribution_contact_id';
279 if (!array_key_exists('savedMapping', $fields)) {
280 $importKeys = [];
281 foreach ($fields['mapper'] as $mapperPart) {
282 $importKeys[] = $mapperPart[0];
283 }
284
285 $params = [
286 'used' => 'Unsupervised',
287 'contact_type' => $self->getContactType(),
288 ];
289 [$ruleFields, $threshold] = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params);
290 $weightSum = 0;
291 foreach ($importKeys as $key => $val) {
292 if (array_key_exists($val, $ruleFields)) {
293 $weightSum += $ruleFields[$val];
294 }
295 if ($val == "soft_credit") {
296 $mapperKey = CRM_Utils_Array::key('soft_credit', $importKeys);
297 if (empty($fields['mapper'][$mapperKey][1])) {
298 if (empty($errors['_qf_default'])) {
299 $errors['_qf_default'] = '';
300 }
301 $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
302 }
303 }
304 }
305 foreach ($ruleFields as $field => $weight) {
306 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
307 }
308 $errors = self::checkRequiredFields($self, $contactORContributionId, $importKeys, $errors, $weightSum, $threshold, $fieldMessage);
309
310 //at least one field should be mapped during update.
311 if ($self->isUpdateExisting()) {
312 $atleastOne = FALSE;
313 foreach ($self->_mapperFields as $key => $field) {
314 if (in_array($key, $importKeys) &&
315 !in_array($key, [
316 'doNotImport',
317 'contribution_id',
318 'invoice_id',
319 'trxn_id',
320 ])
321 ) {
322 $atleastOne = TRUE;
323 break;
324 }
325 }
326 if (!$atleastOne) {
327 $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />';
328 }
329 }
330 }
331
332 if (!empty($fields['saveMapping'])) {
333 $nameField = $fields['saveMappingName'] ?? NULL;
334 if (empty($nameField)) {
335 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
336 }
337 else {
338 if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'))) {
339 $errors['saveMappingName'] = ts('Duplicate Import Contribution Mapping Name');
340 }
341 }
342 }
343
344 if (!empty($errors)) {
345 if (!empty($errors['saveMappingName'])) {
346 $_flag = 1;
347 $assignError = new CRM_Core_Page();
348 $assignError->assign('mappingDetailsError', $_flag);
349 }
350 if (!empty($errors['_qf_default'])) {
351 CRM_Core_Session::setStatus($errors['_qf_default'], ts("Error"), "error");
352 return $errors;
353 }
354 }
355
356 return TRUE;
357 }
358
359 /**
360 * Get the mapping name per the civicrm_mapping_field.type_id option group.
361 *
362 * @return string
363 */
364 public function getMappingTypeName(): string {
365 return 'Import Contribution';
366 }
367
368 /**
369 * @return \CRM_Contribute_Import_Parser_Contribution
370 */
371 protected function getParser(): CRM_Contribute_Import_Parser_Contribution {
372 if (!$this->parser) {
373 $this->parser = new CRM_Contribute_Import_Parser_Contribution();
374 $this->parser->setUserJobID($this->getUserJobID());
375 $this->parser->init();
376 }
377 return $this->parser;
378 }
379
380 }