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_Form_DataSource
{
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 $this->processDatasource();
176 // @todo - this params are being set here because they were / possibly still
177 // are in some places being accessed by forms later in the flow
178 // ie CRM_Contact_Import_Form_MapField, CRM_Contact_Import_Form_Preview
179 // or CRM_Contact_Import_Form_Summary using `$this->get()
180 // which was the old way of saving values submitted on this form such that
181 // the other forms could access them. Now they should use
182 // `getSubmittedValue` or simply not get them if the only
183 // reason is to pass to the Parser which can itself
184 // call 'getSubmittedValue'
185 // Once the mentioned forms no longer call $this->get() all this 'setting'
188 'dateFormats' => $this->getSubmittedValue('dateFormats'),
189 'savedMapping' => $this->getSubmittedValue('savedMapping'),
192 foreach ($storeParams as $storeName => $value) {
193 $this->set($storeName, $value);
195 CRM_Core_Session
::singleton()->set('dateTypes', $storeParams['dateFormats']);
200 * General function for handling invalid configuration.
202 * I was going to statusBounce them all but when I tested I was 'bouncing' to weird places
203 * whereas throwing an exception gave no behaviour change. So, I decided to centralise
204 * and we can 'flip the switch' later.
208 * @throws \CRM_Core_Exception
210 protected function invalidConfig($message) {
211 throw new CRM_Core_Exception($message);
215 * Return a descriptive name for the page, used in wizard header
219 public function getTitle(): string {
220 return ts('Choose Data Source');