3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class gets the name of the file to upload.
21 class CRM_Activity_Import_Form_MapField
extends CRM_Import_Form_MapField
{
26 public $submitOnce = TRUE;
29 * Build the form object.
31 * @throws \CiviCRM_API3_Exception
33 public function buildQuickForm() {
34 $savedMappingID = (int) $this->getSubmittedValue('savedMapping');
35 $this->buildSavedMappingFields($savedMappingID);
36 $this->addFormRule(['CRM_Activity_Import_Form_MapField', 'formRule']);
38 //-------- end of saved mapping stuff ---------
41 $headerPatterns = $this->getHeaderPatterns();
42 $dataPatterns = $this->getDataPatterns();
43 $fieldMappings = $this->getFieldMappings();
44 $columnHeaders = $this->getColumnHeaders();
45 $hasHeaders = $this->getSubmittedValue('skipColumnHeader');
47 $sel1 = $this->_mapperFields
;
49 $js = "<script type='text/javascript'>\n";
50 $formName = 'document.forms.' . $this->_name
;
52 foreach ($columnHeaders as $i => $columnHeader) {
53 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
55 if ($this->getSubmittedValue('savedMapping')) {
56 $fieldMapping = $fieldMappings[$i] ??
NULL;
57 if (isset($fieldMappings[$i])) {
58 if ($fieldMapping['name'] !== ts('do_not_import')) {
59 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
60 $defaults["mapper[$i]"] = [$fieldMapping['name']];
64 $defaults["mapper[$i]"] = [];
67 for ($k = 1; $k < 4; $k++
) {
68 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
73 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
74 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
77 $defaults["mapper[$i]"] = [$this->defaultFromHeader($columnHeader, $headerPatterns)];
80 $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
83 // End of load mapping.
86 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
88 // Infer the default from the skipped headers if we have them
89 $defaults["mapper[$i]"] = [
90 $this->defaultFromHeader($columnHeader, $headerPatterns),
95 // Otherwise guess the default from the form of the data
96 $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i), 0];
105 $js .= "</script>\n";
106 $this->assign('initHideBoxes', $js);
108 $this->setDefaults($defaults);
113 'name' => ts('Previous'),
117 'name' => ts('Continue'),
118 'spacing' => ' ',
123 'name' => ts('Cancel'),
130 * Global validation rules for the form.
132 * @param array $fields
133 * Posted values of the form.
136 * list of errors to be posted back to the form
138 public static function formRule($fields) {
140 // define so we avoid notices below
141 $errors['_qf_default'] = '';
143 $fieldMessage = NULL;
144 if (!array_key_exists('savedMapping', $fields)) {
146 foreach ($fields['mapper'] as $mapperPart) {
147 $importKeys[] = $mapperPart[0];
149 // FIXME: should use the schema titles, not redeclare them
151 'target_contact_id' => ts('Contact ID'),
152 'activity_date_time' => ts('Activity Date'),
153 'activity_subject' => ts('Activity Subject'),
154 'activity_type_id' => ts('Activity Type ID'),
157 $contactFieldsBelowWeightMessage = self
::validateRequiredContactMatchFields('Individual', $importKeys);
158 foreach ($requiredFields as $field => $title) {
159 if (!in_array($field, $importKeys)) {
160 if ($field === 'target_contact_id') {
161 if (!$contactFieldsBelowWeightMessage ||
in_array('external_identifier', $importKeys)) {
165 $errors['_qf_default'] .= ts('Missing required contact matching fields.')
166 . $contactFieldsBelowWeightMessage
170 elseif ($field === 'activity_type_id') {
171 if (in_array('activity_label', $importKeys)) {
175 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
178 2 => 'Activity Type Label',
183 $errors['_qf_default'] .= ts('Missing required field: %1', [1 => $title]) . '<br />';
189 if (!empty($fields['saveMapping'])) {
190 $nameField = $fields['saveMappingName'] ??
NULL;
191 if (empty($nameField)) {
192 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
195 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Activity'))) {
196 $errors['saveMappingName'] = ts('Duplicate Import Mapping Name');
201 if (empty($errors['_qf_default'])) {
202 unset($errors['_qf_default']);
204 if (!empty($errors)) {
205 if (!empty($errors['saveMappingName'])) {
207 $assignError = new CRM_Core_Page();
208 $assignError->assign('mappingDetailsError', $_flag);
217 * @return CRM_Activity_Import_Parser_Activity
219 protected function getParser(): CRM_Activity_Import_Parser_Activity
{
220 if (!$this->parser
) {
221 $this->parser
= new CRM_Activity_Import_Parser_Activity();
222 $this->parser
->setUserJobID($this->getUserJobID());
223 $this->parser
->init();
225 return $this->parser
;
228 protected function getHighlightedFields(): array {
229 $highlightedFields = [];
231 'activity_date_time',
236 foreach ($requiredFields as $val) {
237 $highlightedFields[] = $val;
239 return $highlightedFields;
242 public function getImportType(): string {
243 return 'Import Activity';
247 * Get the mapping name per the civicrm_mapping_field.type_id option group.
251 public function getMappingTypeName(): string {
252 return 'Import Participant';