Merge pull request #23184 from totten/master-bd-drush
[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 $mapper = $this->get('mapper');
31 $softCreditFields = $this->get('softCreditFields');
32 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
33 $invalidRowCount = $this->get('invalidRowCount');
34 $conflictRowCount = $this->get('conflictRowCount');
35 $mismatchCount = $this->get('unMatchCount');
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);
43 }
44 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
45
46 if ($invalidRowCount) {
47 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
48 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
49 }
50
51 if ($conflictRowCount) {
52 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser_Contribution';
53 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
54 }
55
56 if ($mismatchCount) {
57 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser_Contribution';
58 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
59 }
60
61 $properties = [
62 'mapper',
63 'softCreditFields',
64 'mapperSoftCreditType',
65 'dataValues',
66 'columnCount',
67 'totalRowCount',
68 'validRowCount',
69 'invalidRowCount',
70 'conflictRowCount',
71 'downloadErrorRecordsUrl',
72 'downloadConflictRecordsUrl',
73 'downloadMismatchRecordsUrl',
74 ];
75 $this->setStatusUrl();
76
77 foreach ($properties as $property) {
78 $this->assign($property, $this->get($property));
79 }
80 }
81
82 /**
83 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
84 */
85 public function postProcess() {
86 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
87 $invalidRowCount = $this->get('invalidRowCount');
88 $conflictRowCount = $this->get('conflictRowCount');
89 $onDuplicate = $this->get('onDuplicate');
90 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
91
92 $mapper = $this->controller->exportValue('MapField', 'mapper');
93 $mapperKeys = [];
94 $mapperSoftCredit = [];
95 $mapperPhoneType = [];
96
97 foreach ($mapper as $key => $value) {
98 $mapperKeys[$key] = $mapper[$key][0];
99 if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
100 $mapperSoftCredit[$key] = $mapper[$key][1] ?? '';
101 $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
102 }
103 else {
104 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
105 }
106 }
107
108 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
109
110 $mapFields = $this->get('fields');
111
112 foreach ($mapper as $key => $value) {
113 $header = [];
114 if (isset($mapFields[$mapper[$key][0]])) {
115 $header[] = $mapFields[$mapper[$key][0]];
116 }
117 $mapperFields[] = implode(' - ', $header);
118 }
119 $parser->run(
120 $this->getSubmittedValue('uploadFile'),
121 $this->getSubmittedValue('fieldSeparator'),
122 $mapperFields,
123 $this->getSubmittedValue('skipColumnHeader'),
124 CRM_Import_Parser::MODE_IMPORT,
125 $this->get('contactType'),
126 $onDuplicate,
127 $this->get('statusID'),
128 $this->get('totalRowCount')
129 );
130
131 // Add all the necessary variables to the form.
132 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
133
134 // Check if there is any error occurred.
135
136 $errorStack = CRM_Core_Error::singleton();
137 $errors = $errorStack->getErrors();
138 $errorMessage = [];
139
140 if (is_array($errors)) {
141 foreach ($errors as $key => $value) {
142 $errorMessage[] = $value['message'];
143 }
144
145 $errorFile = $fileName['name'] . '.error.log';
146
147 if ($fd = fopen($errorFile, 'w')) {
148 fwrite($fd, implode('\n', $errorMessage));
149 }
150 fclose($fd);
151
152 $this->set('errorFile', $errorFile);
153 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
154 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
155 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser_Contribution';
156 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
157 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser_Contribution';
158 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
159 }
160 }
161
162 }