Fix Participant import, add tests
[civicrm-core.git] / CRM / Event / Import / Form / Preview.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 previews the uploaded file and returns summary
20 * statistics
21 */
22 class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
23
24 /**
25 * Process the mapped fields and map it into the uploaded file
26 * preview the file and extract some summary statistics
27 *
28 * @return void
29 */
30 public function postProcess() {
31 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
32 $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
33 $invalidRowCount = $this->get('invalidRowCount');
34 $onDuplicate = $this->get('onDuplicate');
35
36 $mapper = $this->controller->exportValue('MapField', 'mapper');
37 $mapperKeys = [];
38
39 foreach ($mapper as $key => $value) {
40 $mapperKeys[$key] = $mapper[$key][0];
41 }
42
43 $parser = new CRM_Event_Import_Parser_Participant($mapperKeys);
44 $parser->setUserJobID($this->getUserJobID());
45 $mapFields = $this->get('fields');
46
47 foreach ($mapper as $key => $value) {
48 $header = [];
49 if (isset($mapFields[$mapper[$key][0]])) {
50 $header[] = $mapFields[$mapper[$key][0]];
51 }
52 $mapperFields[] = implode(' - ', $header);
53 }
54 $parser->run($fileName, $separator,
55 $mapperFields,
56 $this->getSubmittedValue('skipColumnHeader'),
57 CRM_Import_Parser::MODE_IMPORT
58 );
59
60 // add all the necessary variables to the form
61 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
62 }
63
64 /**
65 * @return CRM_Event_Import_Parser_Participant
66 */
67 protected function getParser(): CRM_Event_Import_Parser_Participant {
68 if (!$this->parser) {
69 $this->parser = new CRM_Event_Import_Parser_Participant();
70 $this->parser->setUserJobID($this->getUserJobID());
71 $this->parser->init();
72 }
73 return $this->parser;
74 }
75
76 }