Commit | Line | Data |
---|---|---|
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 previews the uploaded file and returns summary | |
20 | * statistics | |
21 | */ | |
f532671f | 22 | class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview { |
6a488035 TO |
23 | |
24 | /** | |
fe482240 | 25 | * Set variables up before form is built. |
6a488035 TO |
26 | * |
27 | * @return void | |
6a488035 TO |
28 | */ |
29 | public function preProcess() { | |
9d7974eb | 30 | parent::preProcess(); |
6a488035 TO |
31 | |
32 | //get the data from the session | |
353ffa53 TO |
33 | $dataValues = $this->get('dataValues'); |
34 | $mapper = $this->get('mapper'); | |
35 | $invalidRowCount = $this->get('invalidRowCount'); | |
6a488035 TO |
36 | |
37 | //get the mapping name displayed if the mappingId is set | |
38 | $mappingId = $this->get('loadMappingId'); | |
39 | if ($mappingId) { | |
40 | $mapDAO = new CRM_Core_DAO_Mapping(); | |
41 | $mapDAO->id = $mappingId; | |
42 | $mapDAO->find(TRUE); | |
6a488035 | 43 | } |
262b7f26 | 44 | $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL); |
6a488035 | 45 | |
6a488035 | 46 | if ($invalidRowCount) { |
d60cd664 | 47 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser_Participant'; |
6a488035 TO |
48 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
49 | } | |
50 | ||
be2fb01f | 51 | $properties = [ |
6a488035 | 52 | 'mapper', |
353ffa53 TO |
53 | 'dataValues', |
54 | 'columnCount', | |
55 | 'totalRowCount', | |
56 | 'validRowCount', | |
57 | 'invalidRowCount', | |
6a488035 | 58 | 'downloadErrorRecordsUrl', |
be2fb01f | 59 | ]; |
6a488035 TO |
60 | |
61 | foreach ($properties as $property) { | |
62 | $this->assign($property, $this->get($property)); | |
63 | } | |
64 | } | |
65 | ||
6a488035 TO |
66 | /** |
67 | * Process the mapped fields and map it into the uploaded file | |
68 | * preview the file and extract some summary statistics | |
69 | * | |
70 | * @return void | |
6a488035 TO |
71 | */ |
72 | public function postProcess() { | |
353ffa53 | 73 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
f0fed404 | 74 | $separator = $this->controller->exportValue('DataSource', 'fieldSeparator'); |
353ffa53 | 75 | $invalidRowCount = $this->get('invalidRowCount'); |
353ffa53 | 76 | $onDuplicate = $this->get('onDuplicate'); |
6a488035 | 77 | |
063338e5 | 78 | $mapper = $this->controller->exportValue('MapField', 'mapper'); |
be2fb01f | 79 | $mapperKeys = []; |
6a488035 TO |
80 | |
81 | foreach ($mapper as $key => $value) { | |
82 | $mapperKeys[$key] = $mapper[$key][0]; | |
83 | } | |
84 | ||
85 | $parser = new CRM_Event_Import_Parser_Participant($mapperKeys); | |
86 | ||
87 | $mapFields = $this->get('fields'); | |
88 | ||
89 | foreach ($mapper as $key => $value) { | |
be2fb01f | 90 | $header = []; |
6a488035 TO |
91 | if (isset($mapFields[$mapper[$key][0]])) { |
92 | $header[] = $mapFields[$mapper[$key][0]]; | |
93 | } | |
94 | $mapperFields[] = implode(' - ', $header); | |
95 | } | |
f0fed404 | 96 | $parser->run($fileName, $separator, |
6a488035 | 97 | $mapperFields, |
9d7974eb | 98 | $this->getSubmittedValue('skipColumnHeader'), |
a05662ef | 99 | CRM_Import_Parser::MODE_IMPORT, |
6a488035 TO |
100 | $this->get('contactType'), |
101 | $onDuplicate | |
102 | ); | |
103 | ||
104 | // add all the necessary variables to the form | |
a05662ef | 105 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
6a488035 | 106 | |
b44e3f84 | 107 | // check if there is any error occurred |
6a488035 TO |
108 | |
109 | $errorStack = CRM_Core_Error::singleton(); | |
110 | $errors = $errorStack->getErrors(); | |
be2fb01f | 111 | $errorMessage = []; |
6a488035 TO |
112 | |
113 | if (is_array($errors)) { | |
114 | foreach ($errors as $key => $value) { | |
115 | $errorMessage[] = $value['message']; | |
116 | } | |
117 | ||
118 | $errorFile = $fileName['name'] . '.error.log'; | |
119 | ||
120 | if ($fd = fopen($errorFile, 'w')) { | |
121 | fwrite($fd, implode('\n', $errorMessage)); | |
122 | } | |
123 | fclose($fd); | |
124 | ||
125 | $this->set('errorFile', $errorFile); | |
d60cd664 | 126 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser_Participant'; |
6a488035 | 127 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
6a488035 TO |
128 | } |
129 | } | |
96025800 | 130 | |
83a1c234 EM |
131 | /** |
132 | * @return CRM_Event_Import_Parser_Participant | |
133 | */ | |
134 | protected function getParser(): CRM_Event_Import_Parser_Participant { | |
135 | if (!$this->parser) { | |
136 | $this->parser = new CRM_Event_Import_Parser_Participant(); | |
137 | $this->parser->setUserJobID($this->getUserJobID()); | |
138 | $this->parser->init(); | |
139 | } | |
140 | return $this->parser; | |
141 | } | |
142 | ||
6a488035 | 143 | } |