Merge pull request #13374 from cividesk/dev-627
[civicrm-core.git] / CRM / Activity / Import / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
b6c94f42 35 * This class previews the uploaded file and returns summary statistics.
6a488035 36 */
f532671f 37class CRM_Activity_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() {
de7b9b56 43 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
6a488035 44
7808aae6 45 // Get the data from the session.
353ffa53
TO
46 $dataValues = $this->get('dataValues');
47 $mapper = $this->get('mapper');
48 $invalidRowCount = $this->get('invalidRowCount');
6a488035 49 $conflictRowCount = $this->get('conflictRowCount');
353ffa53 50 $mismatchCount = $this->get('unMatchCount');
6a488035 51
7808aae6 52 // Get the mapping name displayed if the mappingId is set.
6a488035
TO
53 $mappingId = $this->get('loadMappingId');
54 if ($mappingId) {
55 $mapDAO = new CRM_Core_DAO_Mapping();
56 $mapDAO->id = $mappingId;
57 $mapDAO->find(TRUE);
58 $this->assign('loadedMapping', $mappingId);
59 $this->assign('savedName', $mapDAO->name);
60 }
61
62 if ($skipColumnHeader) {
63 $this->assign('skipColumnHeader', $skipColumnHeader);
64 $this->assign('rowDisplayCount', 3);
65 }
66 else {
67 $this->assign('rowDisplayCount', 2);
68 }
69
70 if ($invalidRowCount) {
a05662ef 71 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
72 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
73 }
74
75 if ($conflictRowCount) {
a05662ef 76 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
77 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
78 }
79
80 if ($mismatchCount) {
a05662ef 81 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
82 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
83 }
84
be2fb01f 85 $properties = [
6a488035 86 'mapper',
353ffa53
TO
87 'dataValues',
88 'columnCount',
89 'totalRowCount',
90 'validRowCount',
91 'invalidRowCount',
92 'conflictRowCount',
6a488035
TO
93 'downloadErrorRecordsUrl',
94 'downloadConflictRecordsUrl',
95 'downloadMismatchRecordsUrl',
be2fb01f 96 ];
8cebffb2 97 $this->setStatusUrl();
6a488035
TO
98
99 foreach ($properties as $property) {
100 $this->assign($property, $this->get($property));
101 }
102 }
103
6a488035 104 /**
fe482240 105 * Process the mapped fields and map it into the uploaded file.
6a488035 106 *
b6c94f42 107 * Preview the file and extract some summary statistics
6a488035
TO
108 */
109 public function postProcess() {
353ffa53 110 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
063338e5 111 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
de7b9b56 112 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
353ffa53 113 $invalidRowCount = $this->get('invalidRowCount');
6a488035 114 $conflictRowCount = $this->get('conflictRowCount');
353ffa53 115 $onDuplicate = $this->get('onDuplicate');
6a488035 116
6a488035 117 $mapper = $this->controller->exportValue('MapField', 'mapper');
be2fb01f
CW
118 $mapperKeys = [];
119 $mapperLocType = [];
120 $mapperPhoneType = [];
6a488035
TO
121
122 foreach ($mapper as $key => $value) {
123 $mapperKeys[$key] = $mapper[$key][0];
124
a7488080 125 if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) {
6a488035
TO
126 $mapperLocType[$key] = $mapper[$key][1];
127 }
128 else {
129 $mapperLocType[$key] = NULL;
130 }
131
a7488080 132 if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) {
6a488035
TO
133 $mapperPhoneType[$key] = $mapper[$key][2];
134 }
135 else {
136 $mapperPhoneType[$key] = NULL;
137 }
138 }
139
140 $parser = new CRM_Activity_Import_Parser_Activity($mapperKeys, $mapperLocType, $mapperPhoneType);
141
142 $mapFields = $this->get('fields');
143
144 foreach ($mapper as $key => $value) {
be2fb01f 145 $header = [];
6a488035
TO
146 if (isset($mapFields[$mapper[$key][0]])) {
147 $header[] = $mapFields[$mapper[$key][0]];
148 }
149 $mapperFields[] = implode(' - ', $header);
150 }
151 $parser->run($fileName, $seperator,
152 $mapperFields,
153 $skipColumnHeader,
a05662ef 154 CRM_Import_Parser::MODE_IMPORT,
8cebffb2
JP
155 $onDuplicate,
156 $this->get('statusID'),
157 $this->get('totalRowCount')
6a488035
TO
158 );
159
160 // add all the necessary variables to the form
a05662ef 161 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
6a488035 162
b44e3f84 163 // check if there is any error occurred
6a488035
TO
164
165 $errorStack = CRM_Core_Error::singleton();
166 $errors = $errorStack->getErrors();
be2fb01f 167 $errorMessage = [];
6a488035
TO
168
169 if (is_array($errors)) {
170 foreach ($errors as $key => $value) {
171 $errorMessage[] = $value['message'];
172 }
173
174 $errorFile = $fileName['name'] . '.error.log';
175
176 if ($fd = fopen($errorFile, 'w')) {
177 fwrite($fd, implode('\n', $errorMessage));
178 }
179 fclose($fd);
180
181 $this->set('errorFile', $errorFile);
a05662ef 182 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
6a488035 183 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 184 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser';
6a488035 185 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 186 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
187 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
188 }
189 }
96025800 190
6a488035 191}