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 2009. | | |
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 delegates to the chosen DataSource to grab the data to be | |
38 | * imported. | |
39 | */ | |
40 | class CRM_Import_Form_DataSource extends CRM_Core_Form { | |
41 | ||
42 | private $_dataSource; | |
43 | ||
44 | private $_dataSourceIsValid = FALSE; | |
45 | ||
46 | private $_dataSourceClassFile; | |
47 | ||
b0b2638a DL |
48 | private $_dataSourceClass; |
49 | ||
6a488035 TO |
50 | /** |
51 | * Function to set variables up before form is built | |
52 | * | |
53 | * @return void | |
54 | * @access public | |
55 | */ | |
56 | public function preProcess() { | |
57 | ||
58 | //Test database user privilege to create table(Temporary) CRM-4725 | |
59 | CRM_Core_Error::ignoreException(); | |
60 | $daoTestPrivilege = new CRM_Core_DAO; | |
61 | $daoTestPrivilege->query("CREATE TEMPORARY TABLE import_job_permission_one(test int) ENGINE=InnoDB"); | |
62 | $daoTestPrivilege->query("CREATE TEMPORARY TABLE import_job_permission_two(test int) ENGINE=InnoDB"); | |
63 | $daoTestPrivilege->query("DROP TABLE IF EXISTS import_job_permission_one, import_job_permission_two"); | |
64 | CRM_Core_Error::setCallback(); | |
65 | ||
66 | if ($daoTestPrivilege->_lastError) { | |
67 | CRM_Core_Error::fatal(ts('Database Configuration Error: Insufficient permissions. Import requires that the CiviCRM database user has permission to create temporary tables. Contact your site administrator for assistance.')); | |
68 | } | |
69 | ||
70 | $results = array(); | |
71 | $config = CRM_Core_Config::singleton(); | |
72 | $handler = opendir($config->uploadDir); | |
73 | $errorFiles = array('sqlImport.errors', 'sqlImport.conflicts', 'sqlImport.duplicates', 'sqlImport.mismatch'); | |
74 | ||
75 | while ($file = readdir($handler)) { | |
76 | if ($file != '.' && $file != '..' && | |
77 | in_array($file, $errorFiles) && !is_writable($config->uploadDir . $file) | |
78 | ) { | |
79 | $results[] = $file; | |
80 | } | |
81 | } | |
82 | closedir($handler); | |
83 | if (!empty($results)) { | |
84 | CRM_Core_Error::fatal(ts('<b>%1</b> file(s) in %2 directory are not writable. Listed file(s) might be used during the import to log the errors occurred during Import process. Contact your site administrator for assistance.', array(1 => implode(', ', $results), 2 => $config->uploadDir))); | |
85 | } | |
86 | ||
87 | $this->_dataSourceIsValid = FALSE; | |
0a11d4d8 DL |
88 | $this->_dataSource = CRM_Utils_Request::retrieve( |
89 | 'dataSource', | |
90 | 'String', | |
91 | CRM_Core_DAO::$_nullObject, | |
92 | FALSE, | |
93 | NULL, | |
94 | 'GET' | |
6a488035 TO |
95 | ); |
96 | ||
97 | $this->_params = $this->controller->exportValues($this->_name); | |
98 | if (!$this->_dataSource) { | |
99 | //considering dataSource as base criteria instead of hidden_dataSource. | |
100 | $this->_dataSource = CRM_Utils_Array::value('dataSource', | |
101 | $_POST, | |
102 | CRM_Utils_Array::value('dataSource', | |
103 | $this->_params | |
104 | ) | |
105 | ); | |
106 | $this->assign('showOnlyDataSourceFormPane', FALSE); | |
107 | } | |
108 | else { | |
109 | $this->assign('showOnlyDataSourceFormPane', TRUE); | |
110 | } | |
111 | ||
112 | if (strpos($this->_dataSource, 'CRM_Import_DataSource_') === 0) { | |
113 | $this->_dataSourceIsValid = TRUE; | |
114 | $this->assign('showDataSourceFormPane', TRUE); | |
115 | $dataSourcePath = explode('_', $this->_dataSource); | |
116 | $templateFile = "CRM/Import/Form/" . $dataSourcePath[3] . ".tpl"; | |
117 | $this->assign('dataSourceFormTemplateFile', $templateFile); | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Function to actually build the form | |
123 | * | |
124 | * @return None | |
125 | * @access public | |
126 | */ | |
127 | ||
128 | public function buildQuickForm() { | |
129 | ||
130 | // If there's a dataSource in the query string, we need to load | |
131 | // the form from the chosen DataSource class | |
132 | if ($this->_dataSourceIsValid) { | |
133 | $this->_dataSourceClassFile = str_replace('_', '/', $this->_dataSource) . ".php"; | |
134 | require_once $this->_dataSourceClassFile; | |
b0b2638a DL |
135 | $this->_dataSourceClass = new $this->_dataSource; |
136 | $this->_dataSourceClass->buildQuickForm( $this ); | |
6a488035 TO |
137 | } |
138 | ||
139 | // Get list of data sources and display them as options | |
140 | $dataSources = $this->_getDataSources(); | |
141 | ||
142 | $this->assign('urlPath', "civicrm/import"); | |
143 | $this->assign('urlPathVar', 'snippet=4'); | |
144 | ||
145 | $this->add('select', 'dataSource', ts('Data Source'), $dataSources, TRUE, | |
146 | array('onchange' => 'buildDataSourceFormBlock(this.value);') | |
147 | ); | |
148 | ||
149 | // duplicate handling options | |
150 | $duplicateOptions = array(); | |
151 | $duplicateOptions[] = $this->createElement('radio', | |
152 | NULL, NULL, ts('Skip'), CRM_Import_Parser::DUPLICATE_SKIP | |
153 | ); | |
154 | $duplicateOptions[] = $this->createElement('radio', | |
155 | NULL, NULL, ts('Update'), CRM_Import_Parser::DUPLICATE_UPDATE | |
156 | ); | |
157 | $duplicateOptions[] = $this->createElement('radio', | |
158 | NULL, NULL, ts('Fill'), CRM_Import_Parser::DUPLICATE_FILL | |
159 | ); | |
160 | $duplicateOptions[] = $this->createElement('radio', | |
161 | NULL, NULL, ts('No Duplicate Checking'), CRM_Import_Parser::DUPLICATE_NOCHECK | |
162 | ); | |
163 | ||
164 | $this->addGroup($duplicateOptions, 'onDuplicate', | |
165 | ts('For Duplicate Contacts') | |
166 | ); | |
167 | ||
168 | $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type', | |
169 | 'Import Contact', | |
170 | 'name' | |
171 | )); | |
172 | ||
173 | $this->assign('savedMapping', $mappingArray); | |
174 | $this->addElement('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray); | |
175 | ||
176 | ||
177 | $js = array('onClick' => "buildSubTypes();buildDedupeRules();"); | |
178 | // contact types option | |
179 | $contactOptions = array(); | |
180 | if (CRM_Contact_BAO_ContactType::isActive('Individual')) { | |
181 | $contactOptions[] = $this->createElement('radio', | |
182 | NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL, $js | |
183 | ); | |
184 | } | |
185 | if (CRM_Contact_BAO_ContactType::isActive('Household')) { | |
186 | $contactOptions[] = $this->createElement('radio', | |
187 | NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD, $js | |
188 | ); | |
189 | } | |
190 | if (CRM_Contact_BAO_ContactType::isActive('Organization')) { | |
191 | $contactOptions[] = $this->createElement('radio', | |
192 | NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION, $js | |
193 | ); | |
194 | } | |
195 | ||
196 | $this->addGroup($contactOptions, 'contactType', | |
197 | ts('Contact Type') | |
198 | ); | |
199 | ||
200 | $this->addElement('select', 'subType', ts('Subtype')); | |
201 | $this->addElement('select', 'dedupe', ts('Dedupe Rule')); | |
202 | ||
203 | CRM_Core_Form_Date::buildAllowedDateFormats($this); | |
204 | ||
205 | $config = CRM_Core_Config::singleton(); | |
206 | $geoCode = FALSE; | |
207 | if (!empty($config->geocodeMethod)) { | |
208 | $geoCode = TRUE; | |
209 | $this->addElement('checkbox', 'doGeocodeAddress', ts('Lookup mapping info during import?')); | |
210 | } | |
211 | $this->assign('geoCode', $geoCode); | |
212 | ||
213 | $this->addElement('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2)); | |
214 | ||
215 | $this->addButtons(array( | |
216 | array( | |
217 | 'type' => 'upload', | |
218 | 'name' => ts('Continue >>'), | |
219 | 'spacing' => ' ', | |
220 | 'isDefault' => TRUE, | |
221 | ), | |
222 | array( | |
223 | 'type' => 'cancel', | |
224 | 'name' => ts('Cancel'), | |
225 | ), | |
226 | ) | |
227 | ); | |
228 | } | |
229 | ||
230 | function setDefaultValues() { | |
231 | $config = CRM_Core_Config::singleton(); | |
232 | $defaults = array( | |
233 | 'dataSource' => 'CRM_Import_DataSource_CSV', | |
234 | 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP, | |
235 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, | |
236 | 'fieldSeparator' => $config->fieldSeparator, | |
237 | ); | |
238 | ||
239 | if ($loadeMapping = $this->get('loadedMapping')) { | |
240 | $this->assign('loadedMapping', $loadeMapping); | |
241 | $defaults['savedMapping'] = $loadeMapping; | |
242 | } | |
243 | ||
244 | return $defaults; | |
245 | } | |
246 | ||
247 | private function _getDataSources() { | |
248 | // Open the data source dir and scan it for class files | |
249 | $config = CRM_Core_Config::singleton(); | |
250 | $dataSourceDir = $config->importDataSourceDir; | |
251 | $dataSources = array(); | |
252 | if (!is_dir($dataSourceDir)) { | |
253 | CRM_Core_Error::fatal("Import DataSource directory $dataSourceDir does not exist"); | |
254 | } | |
255 | if (!$dataSourceHandle = opendir($dataSourceDir)) { | |
256 | CRM_Core_Error::fatal("Unable to access DataSource directory $dataSourceDir"); | |
257 | } | |
258 | ||
259 | while (($dataSourceFile = readdir($dataSourceHandle)) !== FALSE) { | |
260 | $fileType = filetype($dataSourceDir . $dataSourceFile); | |
261 | $matches = array(); | |
262 | if (($fileType == 'file' || $fileType == 'link') && | |
263 | preg_match('/^(.+)\.php$/', $dataSourceFile, $matches) | |
264 | ) { | |
265 | $dataSourceClass = "CRM_Import_DataSource_" . $matches[1]; | |
266 | require_once $dataSourceDir . DIRECTORY_SEPARATOR . $dataSourceFile; | |
b0b2638a DL |
267 | $object = new $dataSourceClass; |
268 | $info = $object->getInfo(); | |
6a488035 TO |
269 | $dataSources[$dataSourceClass] = $info['title']; |
270 | } | |
271 | } | |
272 | closedir($dataSourceHandle); | |
273 | return $dataSources; | |
274 | } | |
275 | ||
276 | /** | |
277 | * Call the DataSource's postProcess method to take over | |
278 | * and then setup some common data structures for the next step | |
279 | * | |
280 | * @return void | |
281 | * @access public | |
282 | */ | |
283 | public function postProcess() { | |
284 | $this->controller->resetPage('MapField'); | |
285 | ||
286 | if ($this->_dataSourceIsValid) { | |
287 | // Setup the params array | |
288 | $this->_params = $this->controller->exportValues($this->_name); | |
289 | ||
290 | $storeParams = array( | |
291 | 'onDuplicate' => 'onDuplicate', | |
292 | 'dedupe' => 'dedupe', | |
293 | 'contactType' => 'contactType', | |
294 | 'contactSubType' => 'subType', | |
295 | 'dateFormats' => 'dateFormats', | |
296 | 'savedMapping' => 'savedMapping', | |
297 | ); | |
298 | ||
299 | foreach ($storeParams as $storeName => $storeValueName) { | |
300 | $$storeName = $this->exportValue($storeValueName); | |
301 | $this->set($storeName, $$storeName); | |
302 | } | |
303 | ||
304 | $this->set('dataSource', $this->_params['dataSource']); | |
305 | $this->set('skipColumnHeader', CRM_Utils_Array::value('skipColumnHeader', $this->_params)); | |
306 | ||
307 | $session = CRM_Core_Session::singleton(); | |
308 | $session->set('dateTypes', $dateFormats); | |
309 | ||
310 | // Get the PEAR::DB object | |
311 | $dao = new CRM_Core_DAO(); | |
312 | $db = $dao->getDatabaseConnection(); | |
313 | ||
314 | //hack to prevent multiple tables. | |
315 | $this->_params['import_table_name'] = $this->get('importTableName'); | |
316 | if (!$this->_params['import_table_name']) { | |
317 | $this->_params['import_table_name'] = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE)); | |
318 | } | |
319 | ||
b0b2638a | 320 | $this->_dataSourceClass->postProcess( $this->_params, $db, $this ); |
6a488035 TO |
321 | |
322 | // We should have the data in the DB now, parse it | |
323 | $importTableName = $this->get('importTableName'); | |
324 | $fieldNames = $this->_prepareImportTable($db, $importTableName); | |
325 | $mapper = array(); | |
326 | ||
327 | $parser = new CRM_Import_Parser_Contact($mapper); | |
328 | $parser->setMaxLinesToProcess(100); | |
329 | $parser->run($importTableName, | |
330 | $mapper, | |
331 | CRM_Import_Parser::MODE_MAPFIELD, | |
332 | $contactType, | |
333 | $fieldNames['pk'], | |
334 | $fieldNames['status'], | |
335 | CRM_Import_Parser::DUPLICATE_SKIP, | |
336 | NULL, NULL, FALSE, | |
337 | CRM_Import_Parser::DEFAULT_TIMEOUT, | |
338 | $contactSubType, | |
339 | $dedupe | |
340 | ); | |
341 | ||
342 | // add all the necessary variables to the form | |
343 | $parser->set($this); | |
344 | } | |
345 | else { | |
346 | CRM_Core_Error::fatal("Invalid DataSource on form post. This shouldn't happen!"); | |
347 | } | |
348 | } | |
349 | ||
350 | /** | |
351 | * Add a PK and status column to the import table so we can track our progress | |
352 | * Returns the name of the primary key and status columns | |
353 | * | |
354 | * @return array | |
355 | * @access private | |
356 | */ | |
357 | private function _prepareImportTable($db, $importTableName) { | |
358 | /* TODO: Add a check for an existing _status field; | |
359 | * if it exists, create __status instead and return that | |
360 | */ | |
361 | ||
362 | $statusFieldName = '_status'; | |
363 | $primaryKeyName = '_id'; | |
364 | ||
365 | $this->set('primaryKeyName', $primaryKeyName); | |
366 | $this->set('statusFieldName', $statusFieldName); | |
367 | ||
368 | /* Make sure the PK is always last! We rely on this later. | |
369 | * Should probably stop doing that at some point, but it | |
370 | * would require moving to associative arrays rather than | |
371 | * relying on numerical order of the fields. This could in | |
372 | * turn complicate matters for some DataSources, which | |
373 | * would also not be good. Decisions, decisions... | |
374 | */ | |
375 | ||
376 | $alterQuery = "ALTER TABLE $importTableName | |
377 | ADD COLUMN $statusFieldName VARCHAR(32) | |
378 | DEFAULT 'NEW' NOT NULL, | |
379 | ADD COLUMN ${statusFieldName}Msg TEXT, | |
380 | ADD COLUMN $primaryKeyName INT PRIMARY KEY NOT NULL | |
381 | AUTO_INCREMENT"; | |
382 | $db->query($alterQuery); | |
383 | ||
384 | return array('status' => $statusFieldName, 'pk' => $primaryKeyName); | |
385 | } | |
386 | ||
387 | /** | |
388 | * Return a descriptive name for the page, used in wizard header | |
389 | * | |
390 | * | |
391 | * @return string | |
392 | * @access public | |
393 | */ | |
394 | public function getTitle() { | |
395 | return ts('Choose Data Source'); | |
396 | } | |
397 | } | |
398 |