Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | /** | |
43 | * Function to set variables up before form is built | |
44 | * | |
45 | * @return void | |
46 | * @access public | |
47 | */ | |
48 | public function preProcess() { | |
bf3adfdb | 49 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 TO |
50 | |
51 | //get the data from the session | |
1221efe9 | 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'); | |
6a488035 TO |
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) { | |
a05662ef | 79 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
80 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
81 | } | |
82 | ||
83 | if ($conflictRowCount) { | |
a05662ef | 84 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
85 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
86 | } | |
87 | ||
88 | if ($mismatchCount) { | |
a05662ef | 89 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
90 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
91 | } | |
92 | ||
93 | $properties = array( | |
94 | 'mapper', 'softCreditFields', | |
1221efe9 | 95 | 'mapperSoftCreditType', |
6a488035 TO |
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 | ||
6a488035 TO |
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() { | |
bf3adfdb RN |
117 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
118 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); | |
6a488035 TO |
119 | $invalidRowCount = $this->get('invalidRowCount'); |
120 | $conflictRowCount = $this->get('conflictRowCount'); | |
121 | $onDuplicate = $this->get('onDuplicate'); | |
1221efe9 | 122 | $mapperSoftCreditType = $this->get('mapperSoftCreditType'); |
6a488035 TO |
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]; | |
1221efe9 | 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']; | |
6a488035 TO |
137 | } |
138 | else { | |
1221efe9 | 139 | $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL; |
6a488035 TO |
140 | } |
141 | } | |
142 | ||
1221efe9 | 143 | $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType); |
6a488035 TO |
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, | |
a05662ef | 157 | CRM_Import_Parser::MODE_IMPORT, |
6a488035 TO |
158 | $this->get('contactType'), |
159 | $onDuplicate | |
160 | ); | |
161 | ||
162 | // add all the necessary variables to the form | |
a05662ef | 163 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
6a488035 TO |
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); | |
a05662ef | 184 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 | 185 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 186 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 | 187 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 188 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser'; |
6a488035 TO |
189 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
190 | } | |
191 | } | |
192 | } | |
193 |