Commit | Line | Data |
---|---|---|
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 | 40 | class CRM_Contribute_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() { | |
bf3adfdb | 48 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 TO |
49 | |
50 | //get the data from the session | |
1221efe9 | 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'); | |
6a488035 TO |
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) { | |
a05662ef | 78 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
79 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
80 | } | |
81 | ||
82 | if ($conflictRowCount) { | |
a05662ef | 83 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
84 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
85 | } | |
86 | ||
87 | if ($mismatchCount) { | |
a05662ef | 88 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
89 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
90 | } | |
91 | ||
92 | $properties = array( | |
93 | 'mapper', 'softCreditFields', | |
1221efe9 | 94 | 'mapperSoftCreditType', |
6a488035 TO |
95 | 'dataValues', 'columnCount', |
96 | 'totalRowCount', 'validRowCount', | |
97 | 'invalidRowCount', 'conflictRowCount', | |
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() { | |
bf3adfdb RN |
115 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
116 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); | |
6a488035 TO |
117 | $invalidRowCount = $this->get('invalidRowCount'); |
118 | $conflictRowCount = $this->get('conflictRowCount'); | |
119 | $onDuplicate = $this->get('onDuplicate'); | |
1221efe9 | 120 | $mapperSoftCreditType = $this->get('mapperSoftCreditType'); |
6a488035 TO |
121 | |
122 | $config = CRM_Core_Config::singleton(); | |
123 | $seperator = $config->fieldSeparator; | |
124 | ||
125 | $mapper = $this->controller->exportValue('MapField', 'mapper'); | |
126 | $mapperKeys = array(); | |
127 | $mapperSoftCredit = array(); | |
128 | $mapperPhoneType = array(); | |
129 | ||
130 | foreach ($mapper as $key => $value) { | |
131 | $mapperKeys[$key] = $mapper[$key][0]; | |
1221efe9 | 132 | if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) { |
133 | $mapperSoftCredit[$key] = isset($mapper[$key][1]) ? $mapper[$key][1] : ''; | |
134 | $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value']; | |
6a488035 TO |
135 | } |
136 | else { | |
874c9be7 | 137 | $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL; |
6a488035 TO |
138 | } |
139 | } | |
140 | ||
1221efe9 | 141 | $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType); |
6a488035 TO |
142 | |
143 | $mapFields = $this->get('fields'); | |
144 | ||
145 | foreach ($mapper as $key => $value) { | |
146 | $header = array(); | |
147 | if (isset($mapFields[$mapper[$key][0]])) { | |
148 | $header[] = $mapFields[$mapper[$key][0]]; | |
149 | } | |
150 | $mapperFields[] = implode(' - ', $header); | |
151 | } | |
152 | $parser->run($fileName, $seperator, | |
153 | $mapperFields, | |
154 | $skipColumnHeader, | |
a05662ef | 155 | CRM_Import_Parser::MODE_IMPORT, |
6a488035 TO |
156 | $this->get('contactType'), |
157 | $onDuplicate | |
158 | ); | |
159 | ||
160 | // add all the necessary variables to the form | |
a05662ef | 161 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
6a488035 TO |
162 | |
163 | // check if there is any error occured | |
164 | ||
165 | $errorStack = CRM_Core_Error::singleton(); | |
166 | $errors = $errorStack->getErrors(); | |
167 | $errorMessage = array(); | |
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_Contribute_Import_Parser'; |
6a488035 | 183 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 184 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_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_Contribute_Import_Parser'; |
6a488035 TO |
187 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
188 | } | |
189 | } | |
190 | } |