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