phpcs - Fix error, "Expected 1 newline at end of file; XXX found".
[civicrm-core.git] / CRM / Member / Import / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
100fef9d 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
51 $dataValues = $this->get('dataValues');
52 $mapper = $this->get('mapper');
53 $invalidRowCount = $this->get('invalidRowCount');
54 $conflictRowCount = $this->get('conflictRowCount');
55 $mismatchCount = $this->get('unMatchCount');
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',
92 'dataValues', 'columnCount',
93 'totalRowCount', 'validRowCount',
94 'invalidRowCount', 'conflictRowCount',
95 'downloadErrorRecordsUrl',
96 'downloadConflictRecordsUrl',
97 'downloadMismatchRecordsUrl',
98 );
99
100 foreach ($properties as $property) {
101 $this->assign($property, $this->get($property));
102 }
103 }
104
6a488035
TO
105 /**
106 * Process the mapped fields and map it into the uploaded file
107 * preview the file and extract some summary statistics
108 *
109 * @return void
6a488035
TO
110 */
111 public function postProcess() {
de7b9b56
CW
112 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
113 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
6a488035
TO
114 $invalidRowCount = $this->get('invalidRowCount');
115 $conflictRowCount = $this->get('conflictRowCount');
116 $onDuplicate = $this->get('onDuplicate');
117
118 $config = CRM_Core_Config::singleton();
119 $seperator = $config->fieldSeparator;
120
121 $mapper = $this->controller->exportValue('MapField', 'mapper');
122 $mapperKeys = array();
123 $mapperLocType = array();
124 $mapperPhoneType = array();
125 // Note: we keep the multi-dimension array (even thought it's not
126 // needed in the case of memberships import) so that we can merge
127 // the common code with contacts import later and subclass contact
128 // and membership imports from there
129 foreach ($mapper as $key => $value) {
130 $mapperKeys[$key] = $mapper[$key][0];
131
a7488080 132 if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) {
6a488035
TO
133 $mapperLocType[$key] = $mapper[$key][1];
134 }
135 else {
136 $mapperLocType[$key] = NULL;
137 }
138
a7488080 139 if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) {
6a488035
TO
140 $mapperPhoneType[$key] = $mapper[$key][2];
141 }
142 else {
143 $mapperPhoneType[$key] = NULL;
144 }
145 }
146
147 $parser = new CRM_Member_Import_Parser_Membership($mapperKeys, $mapperLocType, $mapperPhoneType);
148
149 $mapFields = $this->get('fields');
150
151 foreach ($mapper as $key => $value) {
152 $header = array();
153 if (isset($mapFields[$mapper[$key][0]])) {
154 $header[] = $mapFields[$mapper[$key][0]];
155 }
156 $mapperFields[] = implode(' - ', $header);
157 }
158 $parser->run($fileName, $seperator,
159 $mapperFields,
160 $skipColumnHeader,
a05662ef 161 CRM_Import_Parser::MODE_IMPORT,
6a488035
TO
162 $this->get('contactType'),
163 $onDuplicate
164 );
165
166 // add all the necessary variables to the form
a05662ef 167 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
6a488035
TO
168
169 // check if there is any error occured
170
171 $errorStack = CRM_Core_Error::singleton();
172 $errors = $errorStack->getErrors();
173 $errorMessage = array();
174
175 if (is_array($errors)) {
176 foreach ($errors as $key => $value) {
177 $errorMessage[] = $value['message'];
178 }
179
180 $errorFile = $fileName['name'] . '.error.log';
181
182 if ($fd = fopen($errorFile, 'w')) {
183 fwrite($fd, implode('\n', $errorMessage));
184 }
185 fclose($fd);
186
187 $this->set('errorFile', $errorFile);
a05662ef 188 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser';
6a488035 189 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 190 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Member_Import_Parser';
6a488035 191 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 192 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser';
6a488035
TO
193 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
194 }
195 }
196}