commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Contribute / Import / Form / Preview.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class previews the uploaded file and returns summary
38 * statistics
39 */
40 class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
41
42 /**
43 * Set variables up before form is built.
44 *
45 * @return void
46 */
47 public function preProcess() {
48 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
49
50 //get the data from the session
51 $dataValues = $this->get('dataValues');
52 $mapper = $this->get('mapper');
53 $softCreditFields = $this->get('softCreditFields');
54 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
55 $invalidRowCount = $this->get('invalidRowCount');
56 $conflictRowCount = $this->get('conflictRowCount');
57 $mismatchCount = $this->get('unMatchCount');
58
59 //get the mapping name displayed if the mappingId is set
60 $mappingId = $this->get('loadMappingId');
61 if ($mappingId) {
62 $mapDAO = new CRM_Core_DAO_Mapping();
63 $mapDAO->id = $mappingId;
64 $mapDAO->find(TRUE);
65 $this->assign('loadedMapping', $mappingId);
66 $this->assign('savedName', $mapDAO->name);
67 }
68
69 if ($skipColumnHeader) {
70 $this->assign('skipColumnHeader', $skipColumnHeader);
71 $this->assign('rowDisplayCount', 3);
72 }
73 else {
74 $this->assign('rowDisplayCount', 2);
75 }
76
77 if ($invalidRowCount) {
78 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser';
79 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
80 }
81
82 if ($conflictRowCount) {
83 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser';
84 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
85 }
86
87 if ($mismatchCount) {
88 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser';
89 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
90 }
91
92 $properties = array(
93 'mapper',
94 'softCreditFields',
95 'mapperSoftCreditType',
96 'dataValues',
97 'columnCount',
98 'totalRowCount',
99 'validRowCount',
100 'invalidRowCount',
101 'conflictRowCount',
102 'downloadErrorRecordsUrl',
103 'downloadConflictRecordsUrl',
104 'downloadMismatchRecordsUrl',
105 );
106
107 foreach ($properties as $property) {
108 $this->assign($property, $this->get($property));
109 }
110 }
111
112 /**
113 * Process the mapped fields and map it into the uploaded file
114 * preview the file and extract some summary statistics
115 *
116 * @return void
117 */
118 public function postProcess() {
119 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
120 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
121 $invalidRowCount = $this->get('invalidRowCount');
122 $conflictRowCount = $this->get('conflictRowCount');
123 $onDuplicate = $this->get('onDuplicate');
124 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
125
126 $config = CRM_Core_Config::singleton();
127 $seperator = $config->fieldSeparator;
128
129 $mapper = $this->controller->exportValue('MapField', 'mapper');
130 $mapperKeys = array();
131 $mapperSoftCredit = array();
132 $mapperPhoneType = array();
133
134 foreach ($mapper as $key => $value) {
135 $mapperKeys[$key] = $mapper[$key][0];
136 if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
137 $mapperSoftCredit[$key] = isset($mapper[$key][1]) ? $mapper[$key][1] : '';
138 $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
139 }
140 else {
141 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
142 }
143 }
144
145 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
146
147 $mapFields = $this->get('fields');
148
149 foreach ($mapper as $key => $value) {
150 $header = array();
151 if (isset($mapFields[$mapper[$key][0]])) {
152 $header[] = $mapFields[$mapper[$key][0]];
153 }
154 $mapperFields[] = implode(' - ', $header);
155 }
156 $parser->run($fileName, $seperator,
157 $mapperFields,
158 $skipColumnHeader,
159 CRM_Import_Parser::MODE_IMPORT,
160 $this->get('contactType'),
161 $onDuplicate
162 );
163
164 // Add all the necessary variables to the form.
165 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
166
167 // Check if there is any error occurred.
168
169 $errorStack = CRM_Core_Error::singleton();
170 $errors = $errorStack->getErrors();
171 $errorMessage = array();
172
173 if (is_array($errors)) {
174 foreach ($errors as $key => $value) {
175 $errorMessage[] = $value['message'];
176 }
177
178 $errorFile = $fileName['name'] . '.error.log';
179
180 if ($fd = fopen($errorFile, 'w')) {
181 fwrite($fd, implode('\n', $errorMessage));
182 }
183 fclose($fd);
184
185 $this->set('errorFile', $errorFile);
186 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser';
187 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
188 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser';
189 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
190 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser';
191 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
192 }
193 }
194
195 }