3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class delegates to the chosen DataSource to grab the data to be imported.
21 class CRM_Contact_Import_Form_DataSource
extends CRM_Import_Forms
{
24 * Get any smarty elements that may not be present in the form.
26 * To make life simpler for smarty we ensure they are set to null
27 * rather than unset. This is done at the last minute when $this
28 * is converted to an array to be assigned to the form.
32 public function getOptionalQuickFormElements(): array {
33 return ['disableUSPS'];
37 * Set variables up before form is built.
39 * @throws \CRM_Core_Exception
41 public function preProcess() {
43 $config = CRM_Core_Config
::singleton();
44 $handler = opendir($config->uploadDir
);
45 $errorFiles = ['sqlImport.errors', 'sqlImport.conflicts', 'sqlImport.duplicates', 'sqlImport.mismatch'];
47 // check for post max size avoid when called twice
48 $snippet = $_GET['snippet'] ??
0;
49 if (empty($snippet)) {
50 CRM_Utils_Number
::formatUnitSize(ini_get('post_max_size'), TRUE);
53 while ($file = readdir($handler)) {
54 if ($file !== '.' && $file !== '..' &&
55 in_array($file, $errorFiles) && !is_writable($config->uploadDir
. $file)
61 if (!empty($results)) {
62 $this->invalidConfig(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.', [
63 1 => implode(', ', $results),
64 2 => $config->uploadDir
,
70 * Build the form object.
72 * @throws \CRM_Core_Exception
74 public function buildQuickForm() {
76 $this->assign('urlPath', 'civicrm/import/datasource');
77 $this->assign('urlPathVar', 'snippet=4&user_job_id=' . $this->get('user_job_id'));
79 $this->add('select', 'dataSource', ts('Data Source'), $this->getDataSources(), TRUE,
80 ['onchange' => 'buildDataSourceFormBlock(this.value);']
83 // duplicate handling options
84 $this->addRadio('onDuplicate', ts('For Duplicate Contacts'), [
85 CRM_Import_Parser
::DUPLICATE_SKIP
=> ts('Skip'),
86 CRM_Import_Parser
::DUPLICATE_UPDATE
=> ts('Update'),
87 CRM_Import_Parser
::DUPLICATE_FILL
=> ts('Fill'),
88 CRM_Import_Parser
::DUPLICATE_NOCHECK
=> ts('No Duplicate Checking'),
91 $mappingArray = CRM_Core_BAO_Mapping
::getMappings('Import Contact');
93 $this->assign('savedMapping', $mappingArray);
94 $this->addElement('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] +
$mappingArray);
96 $js = ['onClick' => "buildSubTypes();buildDedupeRules();"];
97 // contact types option
98 $contactTypeOptions = $contactTypeAttributes = [];
99 if (CRM_Contact_BAO_ContactType
::isActive('Individual')) {
100 $contactTypeOptions[CRM_Import_Parser
::CONTACT_INDIVIDUAL
] = ts('Individual');
101 $contactTypeAttributes[CRM_Import_Parser
::CONTACT_INDIVIDUAL
] = $js;
103 if (CRM_Contact_BAO_ContactType
::isActive('Household')) {
104 $contactTypeOptions[CRM_Import_Parser
::CONTACT_HOUSEHOLD
] = ts('Household');
105 $contactTypeAttributes[CRM_Import_Parser
::CONTACT_HOUSEHOLD
] = $js;
107 if (CRM_Contact_BAO_ContactType
::isActive('Organization')) {
108 $contactTypeOptions[CRM_Import_Parser
::CONTACT_ORGANIZATION
] = ts('Organization');
109 $contactTypeAttributes[CRM_Import_Parser
::CONTACT_ORGANIZATION
] = $js;
111 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions, [], NULL, FALSE, $contactTypeAttributes);
113 $this->addElement('select', 'contactSubType', ts('Subtype'));
114 $this->addElement('select', 'dedupe_rule_id', ts('Dedupe Rule'));
116 CRM_Core_Form_Date
::buildAllowedDateFormats($this);
119 if (CRM_Utils_GeocodeProvider
::getUsableClassName()) {
121 $this->addElement('checkbox', 'doGeocodeAddress', ts('Geocode addresses during import?'));
123 $this->assign('geoCode', $geoCode);
125 $this->addElement('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2]);
127 if (Civi
::settings()->get('address_standardization_provider') === 'USPS') {
128 $this->addElement('checkbox', 'disableUSPS', ts('Disable USPS address validation during import?'));
130 $this->buildDataSourceFields();
135 'name' => ts('Continue'),
136 'spacing' => ' ',
141 'name' => ts('Cancel'),
147 * Set the default values of various form elements.
150 * reference to the array of default values
152 public function setDefaultValues() {
154 'dataSource' => $this->getDefaultDataSource(),
155 'onDuplicate' => CRM_Import_Parser
::DUPLICATE_SKIP
,
156 'contactType' => CRM_Import_Parser
::CONTACT_INDIVIDUAL
,
157 'fieldSeparator' => CRM_Core_Config
::singleton()->fieldSeparator
,
160 if ($this->get('loadedMapping')) {
161 $defaults['savedMapping'] = $this->get('loadedMapping');
168 * Call the DataSource's postProcess method.
170 * @throws \CRM_Core_Exception
171 * @throws \API_Exception
173 public function postProcess() {
174 $this->controller
->resetPage('MapField');
175 if (!$this->getUserJobID()) {
176 $this->createUserJob();
179 $this->flushDataSource();
180 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
182 // Setup the params array
183 $this->_params
= $this->controller
->exportValues($this->_name
);
185 // @todo - this params are being set here because they were / possibly still
186 // are in some places being accessed by forms later in the flow
187 // ie CRM_Contact_Import_Form_MapField, CRM_Contact_Import_Form_Preview
188 // or CRM_Contact_Import_Form_Summary using `$this->get()
189 // which was the old way of saving values submitted on this form such that
190 // the other forms could access them. Now they should use
191 // `getSubmittedValue` or simply not get them if the only
192 // reason is to pass to the Parser which can itself
193 // call 'getSubmittedValue'
194 // Once the mentioned forms no longer call $this->get() all this 'setting'
197 'onDuplicate' => $this->getSubmittedValue('onDuplicate'),
198 'dedupe' => $this->getSubmittedValue('dedupe_rule_id'),
199 'contactType' => $this->getSubmittedValue('contactType'),
200 'dateFormats' => $this->getSubmittedValue('dateFormats'),
201 'savedMapping' => $this->getSubmittedValue('savedMapping'),
204 foreach ($storeParams as $storeName => $value) {
205 $this->set($storeName, $value);
207 $this->set('disableUSPS', $this->getSubmittedValue('disableUSPS'));
208 $this->set('dataSource', $this->getSubmittedValue('dataSource'));
209 $this->set('skipColumnHeader', CRM_Utils_Array
::value('skipColumnHeader', $this->_params
));
211 CRM_Core_Session
::singleton()->set('dateTypes', $storeParams['dateFormats']);
213 $this->instantiateDataSource();
217 $parser = new CRM_Contact_Import_Parser_Contact($mapper);
218 $parser->setMaxLinesToProcess(100);
219 $parser->setUserJobID($this->getUserJobID());
222 CRM_Import_Parser
::MODE_MAPFIELD
228 * Instantiate the datasource.
230 * This gives the datasource a chance to do any table creation etc.
232 * @throws \API_Exception
233 * @throws \CRM_Core_Exception
235 private function instantiateDataSource(): void
{
236 $this->getDataSourceObject()->initialize();
240 * General function for handling invalid configuration.
242 * I was going to statusBounce them all but when I tested I was 'bouncing' to weird places
243 * whereas throwing an exception gave no behaviour change. So, I decided to centralise
244 * and we can 'flip the switch' later.
248 * @throws \CRM_Core_Exception
250 protected function invalidConfig($message) {
251 throw new CRM_Core_Exception($message);
255 * Return a descriptive name for the page, used in wizard header
259 public function getTitle(): string {
260 return ts('Choose Data Source');