Merge pull request #11840 from mfb/smtp-reconnect
[civicrm-core.git] / CRM / Contribute / Import / Form / Preview.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class previews the uploaded file and returns summary statistics.
36 */
37 class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
38
39 /**
40 * Set variables up before form is built.
41 */
42 public function preProcess() {
43 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
44
45 //get the data from the session
46 $dataValues = $this->get('dataValues');
47 $mapper = $this->get('mapper');
48 $softCreditFields = $this->get('softCreditFields');
49 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
50 $invalidRowCount = $this->get('invalidRowCount');
51 $conflictRowCount = $this->get('conflictRowCount');
52 $mismatchCount = $this->get('unMatchCount');
53
54 //get the mapping name displayed if the mappingId is set
55 $mappingId = $this->get('loadMappingId');
56 if ($mappingId) {
57 $mapDAO = new CRM_Core_DAO_Mapping();
58 $mapDAO->id = $mappingId;
59 $mapDAO->find(TRUE);
60 $this->assign('loadedMapping', $mappingId);
61 $this->assign('savedName', $mapDAO->name);
62 }
63
64 if ($skipColumnHeader) {
65 $this->assign('skipColumnHeader', $skipColumnHeader);
66 $this->assign('rowDisplayCount', 3);
67 }
68 else {
69 $this->assign('rowDisplayCount', 2);
70 }
71
72 if ($invalidRowCount) {
73 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser';
74 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
75 }
76
77 if ($conflictRowCount) {
78 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser';
79 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
80 }
81
82 if ($mismatchCount) {
83 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser';
84 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
85 }
86
87 $properties = array(
88 'mapper',
89 'softCreditFields',
90 'mapperSoftCreditType',
91 'dataValues',
92 'columnCount',
93 'totalRowCount',
94 'validRowCount',
95 'invalidRowCount',
96 'conflictRowCount',
97 'downloadErrorRecordsUrl',
98 'downloadConflictRecordsUrl',
99 'downloadMismatchRecordsUrl',
100 );
101 $this->setStatusUrl();
102
103 foreach ($properties as $property) {
104 $this->assign($property, $this->get($property));
105 }
106 }
107
108 /**
109 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
110 */
111 public function postProcess() {
112 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
113 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
114 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
115 $invalidRowCount = $this->get('invalidRowCount');
116 $conflictRowCount = $this->get('conflictRowCount');
117 $onDuplicate = $this->get('onDuplicate');
118 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
119
120 $mapper = $this->controller->exportValue('MapField', 'mapper');
121 $mapperKeys = array();
122 $mapperSoftCredit = array();
123 $mapperPhoneType = array();
124
125 foreach ($mapper as $key => $value) {
126 $mapperKeys[$key] = $mapper[$key][0];
127 if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
128 $mapperSoftCredit[$key] = isset($mapper[$key][1]) ? $mapper[$key][1] : '';
129 $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
130 }
131 else {
132 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
133 }
134 }
135
136 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
137
138 $mapFields = $this->get('fields');
139
140 foreach ($mapper as $key => $value) {
141 $header = array();
142 if (isset($mapFields[$mapper[$key][0]])) {
143 $header[] = $mapFields[$mapper[$key][0]];
144 }
145 $mapperFields[] = implode(' - ', $header);
146 }
147 $parser->run($fileName, $seperator,
148 $mapperFields,
149 $skipColumnHeader,
150 CRM_Import_Parser::MODE_IMPORT,
151 $this->get('contactType'),
152 $onDuplicate,
153 $this->get('statusID'),
154 $this->get('totalRowCount')
155 );
156
157 // Add all the necessary variables to the form.
158 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
159
160 // Check if there is any error occurred.
161
162 $errorStack = CRM_Core_Error::singleton();
163 $errors = $errorStack->getErrors();
164 $errorMessage = array();
165
166 if (is_array($errors)) {
167 foreach ($errors as $key => $value) {
168 $errorMessage[] = $value['message'];
169 }
170
171 $errorFile = $fileName['name'] . '.error.log';
172
173 if ($fd = fopen($errorFile, 'w')) {
174 fwrite($fd, implode('\n', $errorMessage));
175 }
176 fclose($fd);
177
178 $this->set('errorFile', $errorFile);
179 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser';
180 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
181 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser';
182 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
183 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser';
184 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
185 }
186 }
187
188 }