Fix fatal when going back from MapField due to no dataSource
[civicrm-core.git] / CRM / Import / Form / DataSource.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * Base class for upload-only import forms (all but Contact import).
19 */
20 abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
21
22 /**
23 * Set variables up before form is built.
24 */
25 public function preProcess() {
26 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
27 $params = "reset=1";
28 if ($this->_id) {
29 $params .= "&id={$this->_id}";
30 }
31 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params));
32
33 // check for post max size
34 CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
35 $this->assign('importEntity', $this->getTranslatedEntity());
36 $this->assign('importEntities', $this->getTranslatedEntities());
37 }
38
39 /**
40 * Get the import entity (translated).
41 *
42 * Used for template layer text.
43 *
44 * @return string
45 */
46 protected function getTranslatedEntity(): string {
47 return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title');
48 }
49
50 /**
51 * Get the import entity plural (translated).
52 *
53 * Used for template layer text.
54 *
55 * @return string
56 */
57 protected function getTranslatedEntities(): string {
58 return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title_plural');
59 }
60
61 /**
62 * Common form elements.
63 */
64 public function buildQuickForm() {
65 $config = CRM_Core_Config::singleton();
66 // When we switch to using the DataSource.tpl used by Contact we can remove this in
67 // favour of the one used by Contact - I was trying to consolidate
68 // first & got stuck on https://github.com/civicrm/civicrm-core/pull/23458
69 $this->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV');
70 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
71
72 //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
73 if (empty($uploadFileSize)) {
74 $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
75 }
76 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
77
78 $this->assign('uploadSize', $uploadSize);
79
80 $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
81 $this->setMaxFileSize($uploadFileSize);
82 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
83 1 => $uploadSize,
84 2 => $uploadFileSize,
85 ]), 'maxfilesize', $uploadFileSize);
86 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
87 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
88
89 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
90
91 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
92 $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]);
93 $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
94
95 $this->assign('savedMapping', $mappingArray);
96 $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] + $mappingArray);
97
98 if ($loadedMapping = $this->get('loadedMapping')) {
99 $this->setDefaults(['savedMapping' => $loadedMapping]);
100 }
101
102 //build date formats
103 CRM_Core_Form_Date::buildAllowedDateFormats($this);
104
105 $this->addButtons([
106 [
107 'type' => 'upload',
108 'name' => ts('Continue'),
109 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
110 'isDefault' => TRUE,
111 ],
112 [
113 'type' => 'cancel',
114 'name' => ts('Cancel'),
115 ],
116 ]);
117 }
118
119 /**
120 * A long-winded way to add one radio element to the form.
121 */
122 protected function addContactTypeSelector() {
123 //contact types option
124 $contactTypeOptions = [];
125 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
126 $contactTypeOptions[CRM_Import_Parser::CONTACT_INDIVIDUAL] = ts('Individual');
127 }
128 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
129 $contactTypeOptions[CRM_Import_Parser::CONTACT_HOUSEHOLD] = ts('Household');
130 }
131 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
132 $contactTypeOptions[CRM_Import_Parser::CONTACT_ORGANIZATION] = ts('Organization');
133 }
134 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
135
136 $this->setDefaults([
137 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
138 ]);
139 }
140
141 /**
142 * Store form values.
143 *
144 * @param array $names
145 */
146 protected function storeFormValues($names) {
147 foreach ($names as $name) {
148 $this->set($name, $this->controller->exportValue($this->_name, $name));
149 }
150 }
151
152 /**
153 * Common form postProcess.
154 *
155 * @param string $parserClassName
156 *
157 * @param string|null $entity
158 * Entity to set for paraser currently only for custom import
159 */
160 protected function submitFileForMapping($parserClassName, $entity = NULL) {
161 $this->controller->resetPage('MapField');
162 CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
163 $this->processDatasource();
164
165 $mapper = [];
166
167 $parser = new $parserClassName($mapper);
168 if ($entity) {
169 $parser->setEntity($this->get($entity));
170 }
171 $parser->setMaxLinesToProcess(100);
172 $parser->setUserJobID($this->getUserJobID());
173 $parser->run(
174 $this->getSubmittedValue('uploadFile'),
175 $this->getSubmittedValue('fieldSeparator'),
176 [],
177 $this->getSubmittedValue('skipColumnHeader'),
178 CRM_Import_Parser::MODE_MAPFIELD,
179 $this->getSubmittedValue('contactType')
180 );
181
182 // add all the necessary variables to the form
183 $parser->set($this);
184 }
185
186 /**
187 * Return a descriptive name for the page, used in wizard header.
188 *
189 * @return string
190 */
191 public function getTitle() {
192 return ts('Upload Data');
193 }
194
195 /**
196 * Process the datasource submission - setting up the job and data source.
197 *
198 * @throws \API_Exception
199 * @throws \CRM_Core_Exception
200 */
201 protected function processDatasource(): void {
202 if (!$this->getUserJobID()) {
203 $this->createUserJob();
204 }
205 else {
206 $this->flushDataSource();
207 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
208 }
209 $this->instantiateDataSource();
210 }
211
212 /**
213 * Instantiate the datasource.
214 *
215 * This gives the datasource a chance to do any table creation etc.
216 *
217 * @throws \API_Exception
218 * @throws \CRM_Core_Exception
219 */
220 private function instantiateDataSource(): void {
221 $this->getDataSourceObject()->initialize();
222 }
223
224 }