Fix custom import to work again (broke in master)
[civicrm-core.git] / CRM / Event / Import / Form / MapField.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class gets the name of the file to upload
20 */
b26295b8 21class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField {
6a488035 22
6a488035 23 /**
fe482240 24 * Set variables up before form is built.
6a488035
TO
25 *
26 * @return void
6a488035
TO
27 */
28 public function preProcess() {
99308da4 29 parent::preProcess();
6a488035 30 unset($this->_mapperFields['participant_is_test']);
6a488035 31
99308da4
EM
32 if ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_UPDATE) {
33 $remove = [
353ffa53 34 'participant_contact_id',
353ffa53
TO
35 'email',
36 'first_name',
37 'last_name',
38 'external_identifier',
99308da4
EM
39 ];
40 foreach ($remove as $value) {
41 unset($this->_mapperFields[$value]);
6a488035
TO
42 }
43 }
99308da4
EM
44 elseif (
45 $this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_SKIP
46 || $this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_NOCHECK) {
47 unset($this->_mapperFields['participant_id']);
48 }
6a488035
TO
49 }
50
51 /**
fe482240 52 * Build the form object.
6a488035
TO
53 *
54 * @return void
6a488035
TO
55 */
56 public function buildQuickForm() {
fa5ea589
EM
57 $savedMappingID = (int) $this->getSubmittedValue('savedMapping');
58 $this->buildSavedMappingFields($savedMappingID);
6a488035 59 $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this);
739acba7 60 $this->addMapper();
9b324cef 61 $this->addFormButtons();
6a488035
TO
62 }
63
64 /**
fe482240 65 * Global validation rules for the form.
6a488035 66 *
d4dd1e85
TO
67 * @param array $fields
68 * Posted values of the form.
6a488035 69 *
da6b46f4 70 * @param $files
e8cf95b4 71 * @param self $self
da6b46f4 72 *
a6c01b45
CW
73 * @return array
74 * list of errors to be posted back to the form
6a488035 75 */
00be9182 76 public static function formRule($fields, $files, $self) {
affcc9d2 77 $errors = [];
e1a33ae4 78 // define so we avoid notices below
79 $errors['_qf_default'] = '';
739acba7 80
6a488035 81 if (!array_key_exists('savedMapping', $fields)) {
affcc9d2 82 $importKeys = [];
6a488035
TO
83 foreach ($fields['mapper'] as $mapperPart) {
84 $importKeys[] = $mapperPart[0];
85 }
86 // FIXME: should use the schema titles, not redeclare them
87 $requiredFields = array(
88 'participant_contact_id' => ts('Contact ID'),
89 'event_id' => ts('Event ID'),
90 );
91
99308da4 92 $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields($self->getContactType(), $importKeys);
6a488035
TO
93
94 foreach ($requiredFields as $field => $title) {
95 if (!in_array($field, $importKeys)) {
96 if ($field == 'participant_contact_id') {
02a237ce 97 if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys) ||
6a488035
TO
98 in_array('participant_id', $importKeys)
99 ) {
100 continue;
101 }
a05662ef 102 if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
ef30e8dc 103 $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />';
6a488035
TO
104 }
105 else {
02a237ce 106 $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $contactFieldsBelowWeightMessage " . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />';
6a488035
TO
107 }
108 }
109 elseif (!in_array('event_title', $importKeys)) {
110 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
353ffa53
TO
111 array(1 => $title, 2 => 'Event Title')
112 ) . '<br />';
6a488035
TO
113 }
114 }
115 }
116 }
117
a7488080 118 if (!empty($fields['saveMapping'])) {
9c1bc317 119 $nameField = $fields['saveMappingName'] ?? NULL;
6a488035
TO
120 if (empty($nameField)) {
121 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
122 }
123 else {
95f52e3b 124 if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'))) {
6a488035
TO
125 $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name');
126 }
127 }
128 }
129
130 //display Error if loaded mapping is not selected
131 if (array_key_exists('loadMapping', $fields)) {
9c1bc317 132 $getMapName = $fields['savedMapping'] ?? NULL;
6a488035
TO
133 if (empty($getMapName)) {
134 $errors['savedMapping'] = ts('Select saved mapping');
135 }
136 }
137
e1a33ae4 138 if (empty($errors['_qf_default'])) {
139 unset($errors['_qf_default']);
140 }
6a488035
TO
141 if (!empty($errors)) {
142 if (!empty($errors['saveMappingName'])) {
143 $_flag = 1;
144 $assignError = new CRM_Core_Page();
145 $assignError->assign('mappingDetailsError', $_flag);
146 }
147 return $errors;
148 }
149
150 return TRUE;
151 }
152
153 /**
99308da4 154 * @return CRM_Event_Import_Parser_Participant
6a488035 155 */
99308da4
EM
156 protected function getParser(): CRM_Event_Import_Parser_Participant {
157 if (!$this->parser) {
158 $this->parser = new CRM_Event_Import_Parser_Participant();
159 $this->parser->setUserJobID($this->getUserJobID());
160 $this->parser->init();
6a488035 161 }
99308da4
EM
162 return $this->parser;
163 }
6a488035 164
99308da4
EM
165 /**
166 * Get the fields to highlight.
167 *
168 * @return array
169 * @throws \CRM_Core_Exception
170 */
171 protected function getHighlightedFields(): array {
172 $highlightedFields = [];
173 if ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_UPDATE) {
174 $highlightedFieldsArray = [
175 'participant_id',
176 'event_id',
177 'event_title',
178 'participant_status_id',
179 ];
180 foreach ($highlightedFieldsArray as $name) {
181 $highlightedFields[] = $name;
6a488035
TO
182 }
183 }
99308da4
EM
184 elseif ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_SKIP ||
185 $this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_NOCHECK
186 ) {
187 $highlightedFieldsArray = [
188 'participant_contact_id',
189 'event_id',
190 'email',
191 'first_name',
192 'last_name',
193 'external_identifier',
194 'participant_status_id',
195 ];
196 foreach ($highlightedFieldsArray as $name) {
197 $highlightedFields[] = $name;
6a488035 198 }
6a488035 199 }
99308da4 200 return $highlightedFields;
6a488035 201 }
96025800 202
83a1c234 203 /**
99308da4
EM
204 * Get the mapping name per the civicrm_mapping_field.type_id option group.
205 *
206 * @return string
83a1c234 207 */
99308da4 208 public function getMappingTypeName(): string {
fa5ea589 209 return 'Import Participants';
83a1c234
EM
210 }
211
6a488035 212}