Merge pull request #11336 from eileenmcnaughton/live
[civicrm-core.git] / CRM / Contribute / Import / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
347e061b 35 * This class previews the uploaded file and returns summary statistics.
6a488035 36 */
f532671f 37class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
6a488035
TO
38
39 /**
fe482240 40 * Set variables up before form is built.
6a488035
TO
41 */
42 public function preProcess() {
bf3adfdb 43 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
6a488035
TO
44
45 //get the data from the session
353ffa53
TO
46 $dataValues = $this->get('dataValues');
47 $mapper = $this->get('mapper');
48 $softCreditFields = $this->get('softCreditFields');
1221efe9 49 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
353ffa53
TO
50 $invalidRowCount = $this->get('invalidRowCount');
51 $conflictRowCount = $this->get('conflictRowCount');
52 $mismatchCount = $this->get('unMatchCount');
6a488035
TO
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) {
a05662ef 73 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser';
6a488035
TO
74 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
75 }
76
77 if ($conflictRowCount) {
a05662ef 78 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser';
6a488035
TO
79 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
80 }
81
82 if ($mismatchCount) {
a05662ef 83 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser';
6a488035
TO
84 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
85 }
86
87 $properties = array(
353ffa53
TO
88 'mapper',
89 'softCreditFields',
1221efe9 90 'mapperSoftCreditType',
353ffa53
TO
91 'dataValues',
92 'columnCount',
93 'totalRowCount',
94 'validRowCount',
95 'invalidRowCount',
96 'conflictRowCount',
6a488035
TO
97 'downloadErrorRecordsUrl',
98 'downloadConflictRecordsUrl',
99 'downloadMismatchRecordsUrl',
100 );
8cebffb2 101 $this->setStatusUrl();
6a488035
TO
102
103 foreach ($properties as $property) {
104 $this->assign($property, $this->get($property));
105 }
106 }
107
6a488035 108 /**
347e061b 109 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
6a488035
TO
110 */
111 public function postProcess() {
353ffa53 112 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
063338e5 113 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
bf3adfdb 114 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
353ffa53 115 $invalidRowCount = $this->get('invalidRowCount');
6a488035 116 $conflictRowCount = $this->get('conflictRowCount');
353ffa53 117 $onDuplicate = $this->get('onDuplicate');
1221efe9 118 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
6a488035 119
6a488035
TO
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];
1221efe9 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'];
6a488035
TO
130 }
131 else {
874c9be7 132 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
6a488035
TO
133 }
134 }
135
1221efe9 136 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
6a488035
TO
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,
a05662ef 150 CRM_Import_Parser::MODE_IMPORT,
6a488035 151 $this->get('contactType'),
8cebffb2
JP
152 $onDuplicate,
153 $this->get('statusID'),
154 $this->get('totalRowCount')
6a488035
TO
155 );
156
68ef454c 157 // Add all the necessary variables to the form.
a05662ef 158 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
6a488035 159
68ef454c 160 // Check if there is any error occurred.
6a488035
TO
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);
a05662ef 179 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser';
6a488035 180 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 181 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser';
6a488035 182 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 183 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser';
6a488035
TO
184 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
185 }
186 }
96025800 187
6a488035 188}