Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class previews the uploaded file and returns summary | |
38 | * statistics | |
39 | */ | |
40 | class CRM_Member_Import_Form_Preview extends CRM_Core_Form { | |
41 | ||
42 | /** | |
43 | * Function to set variables up before form is built | |
44 | * | |
45 | * @return void | |
46 | * @access public | |
47 | */ | |
48 | public function preProcess() { | |
49 | $skipColumnHeader = $this->controller->exportValue('UploadFile', 'skipColumnHeader'); | |
50 | ||
51 | //get the data from the session | |
52 | $dataValues = $this->get('dataValues'); | |
53 | $mapper = $this->get('mapper'); | |
54 | $invalidRowCount = $this->get('invalidRowCount'); | |
55 | $conflictRowCount = $this->get('conflictRowCount'); | |
56 | $mismatchCount = $this->get('unMatchCount'); | |
57 | ||
58 | //get the mapping name displayed if the mappingId is set | |
59 | $mappingId = $this->get('loadMappingId'); | |
60 | if ($mappingId) { | |
61 | $mapDAO = new CRM_Core_DAO_Mapping(); | |
62 | $mapDAO->id = $mappingId; | |
63 | $mapDAO->find(TRUE); | |
64 | $this->assign('loadedMapping', $mappingId); | |
65 | $this->assign('savedName', $mapDAO->name); | |
66 | } | |
67 | ||
68 | if ($skipColumnHeader) { | |
69 | $this->assign('skipColumnHeader', $skipColumnHeader); | |
70 | $this->assign('rowDisplayCount', 3); | |
71 | } | |
72 | else { | |
73 | $this->assign('rowDisplayCount', 2); | |
74 | } | |
75 | ||
76 | if ($invalidRowCount) { | |
77 | $urlParams = 'type=' . CRM_Member_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser'; | |
78 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); | |
79 | } | |
80 | ||
81 | if ($conflictRowCount) { | |
82 | $urlParams = 'type=' . CRM_Member_Import_Parser::CONFLICT . '&parser=CRM_Member_Import_Parser'; | |
83 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); | |
84 | } | |
85 | ||
86 | if ($mismatchCount) { | |
87 | $urlParams = 'type=' . CRM_Member_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser'; | |
88 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); | |
89 | } | |
90 | ||
91 | $properties = array( | |
92 | 'mapper', | |
93 | 'dataValues', 'columnCount', | |
94 | 'totalRowCount', 'validRowCount', | |
95 | 'invalidRowCount', 'conflictRowCount', | |
96 | 'downloadErrorRecordsUrl', | |
97 | 'downloadConflictRecordsUrl', | |
98 | 'downloadMismatchRecordsUrl', | |
99 | ); | |
100 | ||
101 | foreach ($properties as $property) { | |
102 | $this->assign($property, $this->get($property)); | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * Function to actually build the form | |
108 | * | |
109 | * @return None | |
110 | * @access public | |
111 | */ | |
112 | public function buildQuickForm() { | |
113 | ||
114 | $this->addButtons(array( | |
115 | array( | |
116 | 'type' => 'back', | |
117 | 'name' => ts('<< Previous'), | |
118 | ), | |
119 | array( | |
120 | 'type' => 'next', | |
121 | 'name' => ts('Import Now >>'), | |
122 | 'spacing' => ' ', | |
123 | 'isDefault' => TRUE, | |
124 | ), | |
125 | array( | |
126 | 'type' => 'cancel', | |
127 | 'name' => ts('Cancel'), | |
128 | ), | |
129 | ) | |
130 | ); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Return a descriptive name for the page, used in wizard header | |
135 | * | |
136 | * @return string | |
137 | * @access public | |
138 | */ | |
139 | public function getTitle() { | |
140 | return ts('Preview'); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Process the mapped fields and map it into the uploaded file | |
145 | * preview the file and extract some summary statistics | |
146 | * | |
147 | * @return void | |
148 | * @access public | |
149 | */ | |
150 | public function postProcess() { | |
151 | $fileName = $this->controller->exportValue('UploadFile', 'uploadFile'); | |
152 | $skipColumnHeader = $this->controller->exportValue('UploadFile', 'skipColumnHeader'); | |
153 | $invalidRowCount = $this->get('invalidRowCount'); | |
154 | $conflictRowCount = $this->get('conflictRowCount'); | |
155 | $onDuplicate = $this->get('onDuplicate'); | |
156 | ||
157 | $config = CRM_Core_Config::singleton(); | |
158 | $seperator = $config->fieldSeparator; | |
159 | ||
160 | $mapper = $this->controller->exportValue('MapField', 'mapper'); | |
161 | $mapperKeys = array(); | |
162 | $mapperLocType = array(); | |
163 | $mapperPhoneType = array(); | |
164 | // Note: we keep the multi-dimension array (even thought it's not | |
165 | // needed in the case of memberships import) so that we can merge | |
166 | // the common code with contacts import later and subclass contact | |
167 | // and membership imports from there | |
168 | foreach ($mapper as $key => $value) { | |
169 | $mapperKeys[$key] = $mapper[$key][0]; | |
170 | ||
171 | if (CRM_Utils_Array::value(1, $mapper[$key]) && is_numeric($mapper[$key][1])) { | |
172 | $mapperLocType[$key] = $mapper[$key][1]; | |
173 | } | |
174 | else { | |
175 | $mapperLocType[$key] = NULL; | |
176 | } | |
177 | ||
178 | if (CRM_Utils_Array::value(2, $mapper[$key]) && (!is_numeric($mapper[$key][2]))) { | |
179 | $mapperPhoneType[$key] = $mapper[$key][2]; | |
180 | } | |
181 | else { | |
182 | $mapperPhoneType[$key] = NULL; | |
183 | } | |
184 | } | |
185 | ||
186 | $parser = new CRM_Member_Import_Parser_Membership($mapperKeys, $mapperLocType, $mapperPhoneType); | |
187 | ||
188 | $mapFields = $this->get('fields'); | |
189 | ||
190 | foreach ($mapper as $key => $value) { | |
191 | $header = array(); | |
192 | if (isset($mapFields[$mapper[$key][0]])) { | |
193 | $header[] = $mapFields[$mapper[$key][0]]; | |
194 | } | |
195 | $mapperFields[] = implode(' - ', $header); | |
196 | } | |
197 | $parser->run($fileName, $seperator, | |
198 | $mapperFields, | |
199 | $skipColumnHeader, | |
200 | CRM_Member_Import_Parser::MODE_IMPORT, | |
201 | $this->get('contactType'), | |
202 | $onDuplicate | |
203 | ); | |
204 | ||
205 | // add all the necessary variables to the form | |
206 | $parser->set($this, CRM_Member_Import_Parser::MODE_IMPORT); | |
207 | ||
208 | // check if there is any error occured | |
209 | ||
210 | $errorStack = CRM_Core_Error::singleton(); | |
211 | $errors = $errorStack->getErrors(); | |
212 | $errorMessage = array(); | |
213 | ||
214 | if (is_array($errors)) { | |
215 | foreach ($errors as $key => $value) { | |
216 | $errorMessage[] = $value['message']; | |
217 | } | |
218 | ||
219 | $errorFile = $fileName['name'] . '.error.log'; | |
220 | ||
221 | if ($fd = fopen($errorFile, 'w')) { | |
222 | fwrite($fd, implode('\n', $errorMessage)); | |
223 | } | |
224 | fclose($fd); | |
225 | ||
226 | $this->set('errorFile', $errorFile); | |
227 | $urlParams = 'type=' . CRM_Member_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser'; | |
228 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); | |
229 | $urlParams = 'type=' . CRM_Member_Import_Parser::CONFLICT . '&parser=CRM_Member_Import_Parser'; | |
230 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); | |
231 | $urlParams = 'type=' . CRM_Member_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser'; | |
232 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); | |
233 | } | |
234 | } | |
235 | } | |
236 |