Merge pull request #23615 from totten/master-mysql-bump
[civicrm-core.git] / CRM / Event / 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_Event_Import_Form_MapField extends CRM_Import_Form_MapField {
22
23 /**
24 * Set variables up before form is built.
25 *
26 * @return void
27 */
28 public function preProcess() {
29 parent::preProcess();
30 unset($this->_mapperFields['participant_is_test']);
31
32 if ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_UPDATE) {
33 $remove = [
34 'participant_contact_id',
35 'email',
36 'first_name',
37 'last_name',
38 'external_identifier',
39 ];
40 foreach ($remove as $value) {
41 unset($this->_mapperFields[$value]);
42 }
43 }
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 }
49 }
50
51 /**
52 * Build the form object.
53 *
54 * @return void
55 */
56 public function buildQuickForm() {
57 $savedMappingID = (int) $this->getSubmittedValue('savedMapping');
58 $this->buildSavedMappingFields($savedMappingID);
59 $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this);
60 $this->addMapper();
61 $this->addFormButtons();
62 }
63
64 /**
65 * Global validation rules for the form.
66 *
67 * @param array $fields
68 * Posted values of the form.
69 *
70 * @param $files
71 * @param self $self
72 *
73 * @return array
74 * list of errors to be posted back to the form
75 */
76 public static function formRule($fields, $files, $self) {
77 $errors = [];
78 // define so we avoid notices below
79 $errors['_qf_default'] = '';
80
81 if (!array_key_exists('savedMapping', $fields)) {
82 $importKeys = [];
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 'contact_id' => ts('Contact ID'),
89 'event_id' => ts('Event ID'),
90 );
91
92 $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields($self->getContactType(), $importKeys);
93
94 foreach ($requiredFields as $field => $title) {
95 if (!in_array($field, $importKeys)) {
96 if ($field === 'contact_id') {
97 if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys) ||
98 in_array('participant_id', $importKeys)
99 ) {
100 continue;
101 }
102 if ($self->isUpdateExisting()) {
103 $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />';
104 }
105 else {
106 $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $contactFieldsBelowWeightMessage " . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />';
107 }
108 }
109 elseif (!in_array('event_title', $importKeys)) {
110 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
111 array(1 => $title, 2 => 'Event Title')
112 ) . '<br />';
113 }
114 }
115 }
116 }
117
118 if (!empty($fields['saveMapping'])) {
119 $nameField = $fields['saveMappingName'] ?? NULL;
120 if (empty($nameField)) {
121 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
122 }
123 else {
124 if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'))) {
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)) {
132 $getMapName = $fields['savedMapping'] ?? NULL;
133 if (empty($getMapName)) {
134 $errors['savedMapping'] = ts('Select saved mapping');
135 }
136 }
137
138 if (empty($errors['_qf_default'])) {
139 unset($errors['_qf_default']);
140 }
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 /**
154 * @return CRM_Event_Import_Parser_Participant
155 */
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();
161 }
162 return $this->parser;
163 }
164
165 /**
166 * Get the fields to highlight.
167 *
168 * @return array
169 */
170 protected function getHighlightedFields(): array {
171 $highlightedFields = [];
172 if ($this->isUpdateExisting()) {
173 $highlightedFieldsArray = [
174 'id',
175 'event_id',
176 'event_title',
177 'status_id',
178 ];
179 foreach ($highlightedFieldsArray as $name) {
180 $highlightedFields[] = $name;
181 }
182 }
183 elseif ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_SKIP ||
184 $this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_NOCHECK
185 ) {
186 // this should be retrieved from the parser.
187 $highlightedFieldsArray = [
188 'contact_id',
189 'event_id',
190 'email',
191 'first_name',
192 'last_name',
193 'organization_name',
194 'household_name',
195 'external_identifier',
196 'status_id',
197 ];
198 foreach ($highlightedFieldsArray as $name) {
199 $highlightedFields[] = $name;
200 }
201 }
202 return $highlightedFields;
203 }
204
205 /**
206 * Get the mapping name per the civicrm_mapping_field.type_id option group.
207 *
208 * @return string
209 */
210 public function getMappingTypeName(): string {
211 return 'Import Participant';
212 }
213
214 }