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