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