Merge pull request #23588 from eileenmcnaughton/import_soft
[civicrm-core.git] / CRM / Contribute / 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 statistics.
20 */
21 class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
22
23 /**
24 * Set variables up before form is built.
25 */
26 public function preProcess() {
27 parent::preProcess();
28 //get the data from the session
29 $dataValues = $this->get('dataValues');
30 $invalidRowCount = $this->get('invalidRowCount');
31
32 //get the mapping name displayed if the mappingId is set
33 $mappingId = $this->get('loadMappingId');
34 if ($mappingId) {
35 $mapDAO = new CRM_Core_DAO_Mapping();
36 $mapDAO->id = $mappingId;
37 $mapDAO->find(TRUE);
38 }
39 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
40
41 if ($invalidRowCount) {
42 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
43 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
44 }
45
46 $properties = [
47 'dataValues',
48 'columnCount',
49 'totalRowCount',
50 'validRowCount',
51 'invalidRowCount',
52 'downloadErrorRecordsUrl',
53 ];
54 $this->setStatusUrl();
55 $this->assign('mapper', $this->getMappedFieldLabels());
56
57 foreach ($properties as $property) {
58 $this->assign($property, $this->get($property));
59 }
60 }
61
62 /**
63 * Get the mapped fields as an array of labels.
64 *
65 * e.g
66 * ['First Name', 'Employee Of - First Name', 'Home - Street Address']
67 *
68 * @return array
69 * @throws \API_Exception
70 * @throws \CRM_Core_Exception
71 */
72 protected function getMappedFieldLabels(): array {
73 $mapper = [];
74 $parser = $this->getParser();
75 foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
76 $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
77 }
78 return $mapper;
79 }
80
81 /**
82 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
83 */
84 public function postProcess() {
85 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
86 $onDuplicate = $this->get('onDuplicate');
87 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
88 $mapper = $this->controller->exportValue('MapField', 'mapper');
89
90 $parser = new CRM_Contribute_Import_Parser_Contribution();
91 $parser->setUserJobID($this->getUserJobID());
92
93 $mapFields = $this->get('fields');
94
95 foreach ($mapper as $key => $value) {
96 $header = [];
97 if (isset($mapFields[$mapper[$key][0]])) {
98 $header[] = $mapFields[$mapper[$key][0]];
99 }
100 $mapperFields[] = implode(' - ', $header);
101 }
102 $parser->run(
103 $this->getSubmittedValue('uploadFile'),
104 $this->getSubmittedValue('fieldSeparator'),
105 $mapperFields,
106 $this->getSubmittedValue('skipColumnHeader'),
107 CRM_Import_Parser::MODE_IMPORT,
108 $this->get('contactType'),
109 $onDuplicate,
110 $this->get('statusID'),
111 $this->get('totalRowCount')
112 );
113
114 // Add all the necessary variables to the form.
115 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
116
117 // Check if there is any error occurred.
118
119 $errorStack = CRM_Core_Error::singleton();
120 $errors = $errorStack->getErrors();
121 $errorMessage = [];
122
123 if (is_array($errors)) {
124 foreach ($errors as $key => $value) {
125 $errorMessage[] = $value['message'];
126 }
127
128 $errorFile = $fileName['name'] . '.error.log';
129
130 if ($fd = fopen($errorFile, 'w')) {
131 fwrite($fd, implode('\n', $errorMessage));
132 }
133 fclose($fd);
134
135 $this->set('errorFile', $errorFile);
136 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
137 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
138 }
139 }
140
141 /**
142 * @return \CRM_Contribute_Import_Parser_Contribution
143 */
144 protected function getParser(): CRM_Contribute_Import_Parser_Contribution {
145 if (!$this->parser) {
146 $this->parser = new CRM_Contribute_Import_Parser_Contribution();
147 $this->parser->setUserJobID($this->getUserJobID());
148 $this->parser->init();
149 }
150 return $this->parser;
151 }
152
153 }